Skip to contentSkip to conversation

PCI: Convert all dynamic sysfs attributes to static

PATCH Superseded Krzysztof Wilczyński 4 participants 44 messages Apr 11 – Aug 14

Subsystems (3)

v4 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

v4 cover PCI: Convert all dynamic sysfs attributes to static

Hello,

This series converts every dynamically allocated PCI sysfs attribute to a static const definition. After the full series, pci_sysfs_init() and sysfs_initialized are gone, and every sysfs file is created by the driver model at device_add() time.

Currently, the PCI resource files (resourceN, resourceN_wc) and the legacy bus files (legacy_io, legacy_mem) are created dynamically from two unsynchronised paths:

Path A: late_initcall

pci_sysfs_init (late_initcall)
sysfs_initialized = 1
for_each_pci_dev
pci_create_sysfs_dev_files
sysfs_create_bin_file (resourceN, resourceN_wc)
pci_find_next_bus
pci_create_legacy_files
sysfs_create_bin_file (legacy_io, legacy_mem)

Path B: device registration / hotplug

pci_bus_add_devices
pci_bus_add_device
pci_create_sysfs_dev_files
if (!sysfs_initialized) return <- only guard
sysfs_create_bin_file (resourceN, resourceN_wc)

On most ACPI systems this does not race because PCI enumeration completes at subsys_initcall time, before pci_sysfs_init() runs:

subsys_initcall (level 4):
acpi_pci_root_add
pci_bus_add_device
pci_create_sysfs_dev_files
if (!sysfs_initialized) <- not yet set
return -EACCES

late_initcall (level 7):
pci_sysfs_init
sysfs_initialized = 1
for_each_pci_dev
pci_create_sysfs_dev_files <- creates the files, no race

On Devicetree platforms the host controller is a platform driver that probes via the driver model, often on a workqueue, and overlaps with the late_initcall:

CPU 0 (late_initcall) CPU 1 (driver probe)
--------------------------- ----------------------------
pci_sysfs_init()
sysfs_initialized = 1
for_each_pci_dev(pdev) pci_bus_add_device(pdev)
pci_create_sysfs_dev_files() pci_create_sysfs_dev_files()
sysfs_create_bin_file() sysfs_create_bin_file()
-> "duplicate filename"

The same happens on ACPI when probing is asynchronous (hv_pci on Azure, RISC-V with ACPI).

The duplicate causes sysfs_create_bin_file() to fail with -EEXIST. pci_create_resource_files() then calls pci_remove_resource_files() in its error unwind, tearing down files the other thread created and still references through pdev->res_attr[]. This has caused kernel panics on i.MX6 and boot failures on other platforms.

Several different fixes have been proposed over the years: reordering the sysfs_initialized assignment, adding locks, checking pci_dev_is_added(), setting pdev->res_attr[] to NULL after kfree (which only prevents a double-free on the teardown path, not the error unwind removing the other thread's files). None would address the root cause.

This has been reported a few times:

- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/SY0P300MB04687548090B73E40AF97D8897B82@SY0P300MB0468.AUSP300.PROD.OUTLOOK.COM/
- https://lore.kernel.org/linux-pci/20230105174736.GA1154719@bhelgaas/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/20200716110423.xtfyb3n6tn5ixedh@pali/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://bugzilla.kernel.org/show_bug.cgi?id=215515
- https://bugzilla.kernel.org/show_bug.cgi?id=216888

With static attributes the driver model creates sysfs entries once per device at device_add() time, under the device lock, eliminating the late_initcall iteration and the race along with it.

Krzysztof

---
Changes in v4:
https://lore.kernel.org/linux-pci/[email protected]/

- Added new Reviewed-by tags.
- Added pci_resource_is_io() and pci_resource_is_mem() helpers for resource type checks, replacing the open-coded bitwise flag tests in pci_mmap_resource(), pci_resource_io(), and Alpha's pci_mmap_resource(), as per Ilpo Järvinen's suggestion.
- Split the __pci_mmap_fits() cleanup into two patches. An overflow fix for zero-length BARs, which now includes a
Fixes: tag referencing the original Alpha PCI sysfs commit, and the WARN macro removal is a separate cleanup as per Ilpo Järvinen's suggestion.
- Added a missing Fixes: tag to the Alpha lockdown check, referencing the commit that added the check to the generic path but missed Alpha's implementation.
- Added PCI_LEGACY_IO_SIZE and PCI_LEGACY_MEM_SIZE macros to replace the raw literals used for legacy address space sizes. These are used in both Alpha's pci_mmap_legacy_page_range() and the static legacy attribute definitions, as per Ilpo Järvinen's suggestion.
- Replaced sysfs_update_groups() in the BAR resize path with sysfs_remove_groups() before the resize and sysfs_create_groups() after, restoring the original teardown before BAR resize ordering. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org?part=7
- Defined pci_dev_resource_attr_groups as a NULL macro when HAVE_PCI_MMAP and ARCH_GENERIC_PCI_MMAP_RESOURCE are both absent, so the resize path compiles unconditionally without #ifdef guards in the function body. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org?part=7
- Moved the pci_legacy_has_sparse() prototype into the patch that introduces the function, alongside the existing pci_adjust_legacy_attr() declaration, to fix a bisection issue where Alpha would warn on -Wmissing-prototypes. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org?part=18

Changes in v3:
https://lore.kernel.org/linux-pci/[email protected]/

- Updated for modern kernel releases and expanded scope. The v2 only covered the generic resource files. This version also converts Alpha's sparse/dense resource files and the legacy bus attributes, removing pci_sysfs_init() entirely.
- Split the single macro definition into three distinct ones (per I/O, UC, and WC), to make sure that each carries only the callbacks its resource type needs.
- Updated to use the new .bin_size callback, as the attributes are const, to replace using a->size directly, which was not ideal. This required changes to pci_llseek_resource(), to ensure that it would work for device and bus-level attributes.
- Updated the __resource_resize_store() to include CAP_SYS_ADMIN capabilities check.
- Added the security_locked_down() check to Alpha's pci_mmap_resource(), to align with other architectures.

Changes in v2:
https://lore.kernel.org/linux-pci/[email protected]/

- Refactored code so that the macros, helpers and internal functions can be used to correctly leverage the read(), write() and mmap() callbacks rather than to use the .is_bin_visible() callback to set up sysfs objects internals as this is not supported.
- Refactored some if-statements to check for a resource flag first, and then call either arch_can_pci_mmap_io() or arch_can_pci_mmap_wc(), plus store result of testing for IORESOURCE_MEM and IORESOURCE_PREFETCH flags into a boolean variable, as per Bjorn Helgaas' suggestion.
- Renamed pci_read_resource_io() and pci_write_resource_io() callbacks so that these are not specifically tied to I/O BARs read() and write() operations also as per Bjorn Helgaas' suggestion.
- Updated style for code handling bitwise operations to match the style that is preferred as per Bjorn Helgaas' suggestion.
- Updated commit messages adding more details about the implementation as requested by Bjorn Helgaas.

Krzysztof Wilczyński (24):
PCI/sysfs: Use PCI resource accessor macros
PCI: Add pci_resource_is_io() and pci_resource_is_mem() helpers PCI/sysfs: Only allow supported resource types in I/O and MMIO helpers PCI/sysfs: Use BAR length in pci_llseek_resource() when attr->size is
zero
PCI/sysfs: Add CAP_SYS_ADMIN check to __resource_resize_store() PCI/sysfs: Add static PCI resource attribute macros PCI/sysfs: Convert PCI resource files to static attributes PCI/sysfs: Convert __resource_resize_store() to use static attributes PCI/sysfs: Add stubs for pci_{create,remove}_sysfs_dev_files() PCI/sysfs: Limit pci_sysfs_init() late_initcall compile scope alpha/PCI: Add security_locked_down() check to pci_mmap_resource() alpha/PCI: Use BAR index in sysfs attr->private instead of resource
pointer
alpha/PCI: Use PCI resource accessor macros
alpha/PCI: Clean up pci_mmap_resource()
alpha/PCI: Fix __pci_mmap_fits() overflow for zero-length BARs alpha/PCI: Remove WARN from __pci_mmap_fits()
alpha/PCI: Add static PCI resource attribute macros alpha/PCI: Convert resource files to static attributes PCI/sysfs: Remove pci_{create,remove}_sysfs_dev_files()
PCI: Add macros for legacy I/O and memory address space sizes alpha/PCI: Compute legacy size in pci_mmap_legacy_page_range() PCI/sysfs: Add __weak pci_legacy_has_sparse() helper PCI/sysfs: Convert legacy I/O and memory attributes to static
definitions
PCI/sysfs: Remove pci_create_legacy_files() and pci_sysfs_init()

 arch/alpha/include/asm/pci.h   |  13 +-
 arch/alpha/kernel/pci-sysfs.c  | 373 +++++++++++----------
 arch/powerpc/include/asm/pci.h |   2 -
 drivers/pci/bus.c              |   1 -
 drivers/pci/pci-sysfs.c        | 575 +++++++++++++++++++--------------
 drivers/pci/pci.h              |  16 +-
 drivers/pci/probe.c            |   6 -
 drivers/pci/remove.c           |   3 -
 include/linux/pci.h            |  39 ++-
 9 files changed, 578 insertions(+), 450 deletions(-)
Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/24 PCI/sysfs: Use PCI resource accessor macros

Replace direct pdev->resource[] accesses with pci_resource_n(), and pdev->resource[].flags accesses with pci_resource_flags().

No functional changes intended.

---
 drivers/pci/pci-sysfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/pci/pci-sysfs.c +4 -4
index 16eaaf749ba9..ad3c17f86c7f 100644
@@ -177,7 +177,7 @@ static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
 		max = PCI_BRIDGE_RESOURCES;
 
 	for (i = 0; i < max; i++) {
-		struct resource *res =  &pci_dev->resource[i];
+		struct resource *res = pci_resource_n(pci_dev, i);
 		struct resource zerores = {};
 
 		/* For backwards compatibility */
@@ -715,7 +715,7 @@ static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
 		return sysfs_emit(buf, "%u\n", (pdev == vga_dev));
 
 	return sysfs_emit(buf, "%u\n",
-			  !!(pdev->resource[PCI_ROM_RESOURCE].flags &
+			  !!(pci_resource_flags(pdev, PCI_ROM_RESOURCE) &
 			     IORESOURCE_ROM_SHADOW));
 }
 static DEVICE_ATTR_RO(boot_vga);
@@ -1108,7 +1108,7 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	int bar = (unsigned long)attr->private;
 	enum pci_mmap_state mmap_type;
-	struct resource *res = &pdev->resource[bar];
+	struct resource *res = pci_resource_n(pdev, bar);
 	int ret;
 
 	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
@@ -1312,7 +1312,7 @@ static int pci_create_resource_files(struct pci_dev *pdev)
 		retval = pci_create_attr(pdev, i, 0);
 		/* for prefetchable resources, create a WC mappable file */
 		if (!retval && arch_can_pci_mmap_wc() &&
-		    pdev->resource[i].flags & IORESOURCE_PREFETCH)
+		    pci_resource_flags(pdev, i) & IORESOURCE_PREFETCH)
 			retval = pci_create_attr(pdev, i, 1);
 		if (retval) {
 			pci_remove_resource_files(pdev);
Signature · 1 line
2.53.0
#2 lore raw

PATCH v4 2/24 PCI: Add pci_resource_is_io() and pci_resource_is_mem() helpers

In reply to Krzysztof Wilczyński

Add helpers to check whether a PCI resource is of I/O port or memory type. These replace the open-coded pci_resource_flags() with IORESOURCE_IO and IORESOURCE_MEM pattern used across the tree.

---
 include/linux/pci.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/pci.h +25 -0
index 1c270f1d5123..05aceb7f1f37 100644
@@ -2295,6 +2295,31 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma);
 	CONCATENATE(__pci_dev_for_each_res, COUNT_ARGS(__VA_ARGS__)) 	\
 		    (dev, res, __VA_ARGS__)
 
+/**
+ * pci_resource_is_io - check if a PCI resource is of I/O port type.
+ * @dev: PCI device to check.
+ * @resno: The resource number (BAR index) to check.
+ *
+ * Returns true if the resource type is I/O port.
+ */
+static inline bool pci_resource_is_io(const struct pci_dev *dev, int resno)
+{
+	return resource_type(pci_resource_n(dev, resno)) == IORESOURCE_IO;
+}
+
+/**
+ * pci_resource_is_mem - check if a PCI resource is of memory type.
+ * @dev: PCI device to check.
+ * @resno: The resource number (BAR index) to check.
+ *
+ * Returns true if the resource type is memory, including
+ * prefetchable memory.
+ */
+static inline bool pci_resource_is_mem(const struct pci_dev *dev, int resno)
+{
+	return resource_type(pci_resource_n(dev, resno)) == IORESOURCE_MEM;
+}
+
 /*
  * Similar to the helpers above, these manipulate per-pci_dev
  * driver-specific data.  They are really just a wrapper around
Signature · 1 line
2.53.0
Reviewed-by on v4 2/24
#3 lore raw

Re: [PATCH v4 02/24] PCI: Add pci_resource_is_io() and pci_resource_is_mem() helpers

Quoted 38 lines · Krzysztof Wilczyński

Add helpers to check whether a PCI resource is of I/O port or memory type. These replace the open-coded pci_resource_flags() with IORESOURCE_IO and IORESOURCE_MEM pattern used across the tree.

---
 include/linux/pci.h | 25 +++++++++++++++++++++++++
 1 file changed, 25 insertions(+)
include/linux/pci.h +18 -0
index 1c270f1d5123..05aceb7f1f37 100644
@@ -2295,6 +2295,31 @@ int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma);
 	CONCATENATE(__pci_dev_for_each_res, COUNT_ARGS(__VA_ARGS__)) 	\
 		    (dev, res, __VA_ARGS__)
 
+/**
+ * pci_resource_is_io - check if a PCI resource is of I/O port type.
+ * @dev: PCI device to check.
+ * @resno: The resource number (BAR index) to check.
+ *
+ * Returns true if the resource type is I/O port.
+ */
+static inline bool pci_resource_is_io(const struct pci_dev *dev, int resno)
+{
+	return resource_type(pci_resource_n(dev, resno)) == IORESOURCE_IO;
+}
+
+/**
+ * pci_resource_is_mem - check if a PCI resource is of memory type.
+ * @dev: PCI device to check.
+ * @resno: The resource number (BAR index) to check.
+ *
+ * Returns true if the resource type is memory, including

include/linux/pci.h int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma); · line 2325

Quoted 10 lines
include/linux/pci.h +7 -0
+ * prefetchable memory.
+ */
+static inline bool pci_resource_is_mem(const struct pci_dev *dev, int resno)
+{
+	return resource_type(pci_resource_n(dev, resno)) == IORESOURCE_MEM;
+}
+
 /*
  * Similar to the helpers above, these manipulate per-pci_dev
  * driver-specific data.  They are really just a wrapper around
Signature · 1 line
 i.
#4 lore raw

PATCH v4 3/24 PCI/sysfs: Only allow supported resource types in I/O and MMIO helpers

In reply to Krzysztof Wilczyński

Currently, when the sysfs attributes for PCI resources are added dynamically, the resource access callbacks are only set when the underlying BAR type matches, using .read and .write for IORESOURCE_IO, and .mmap for IORESOURCE_MEM or IORESOURCE_IO with arch_can_pci_mmap_io() support. As such, when the callback is not set, the operation inherently fails.

After the conversion to static attributes, visibility callbacks will control which resource files appear for each BAR, but the callbacks themselves will always be set.

Thus, add a type check to pci_resource_io() and pci_mmap_resource() to return -EIO for an unsupported resource type.

Use the new pci_resource_is_io() and pci_resource_is_mem() helpers for the type checks, replacing the open-coded bitwise flag tests and also drop the local struct resource pointer in pci_mmap_resource().

---
 drivers/pci/pci-sysfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/pci/pci-sysfs.c +10 -3
index ad3c17f86c7f..6783c6168445 100644
@@ -1108,20 +1108,24 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	int bar = (unsigned long)attr->private;
 	enum pci_mmap_state mmap_type;
-	struct resource *res = pci_resource_n(pdev, bar);
 	int ret;
 
 	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
 	if (ret)
 		return ret;
 
-	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
+	if (!pci_resource_is_mem(pdev, bar) &&
+	    !(pci_resource_is_io(pdev, bar) && arch_can_pci_mmap_io()))
+		return -EIO;
+
+	if (pci_resource_is_mem(pdev, bar) &&
+	    iomem_is_exclusive(pci_resource_start(pdev, bar)))
 		return -EINVAL;
 
 	if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
 		return -EINVAL;
 
-	mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
+	mmap_type = pci_resource_is_mem(pdev, bar) ? pci_mmap_mem : pci_mmap_io;
 
 	return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
 }
@@ -1149,6 +1153,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
 	int bar = (unsigned long)attr->private;
 	unsigned long port = off;
 
+	if (!pci_resource_is_io(pdev, bar))
+		return -EIO;
+
 	port += pci_resource_start(pdev, bar);
 
 	if (port > pci_resource_end(pdev, bar))
Signature · 1 line
2.53.0
Reviewed-by on v4 3/24
#5 lore raw

Re: [PATCH v4 03/24] PCI/sysfs: Only allow supported resource types in I/O and MMIO helpers

Quoted 57 lines · Krzysztof Wilczyński

Currently, when the sysfs attributes for PCI resources are added dynamically, the resource access callbacks are only set when the underlying BAR type matches, using .read and .write for IORESOURCE_IO, and .mmap for IORESOURCE_MEM or IORESOURCE_IO with arch_can_pci_mmap_io() support. As such, when the callback is not set, the operation inherently fails.

After the conversion to static attributes, visibility callbacks will control which resource files appear for each BAR, but the callbacks themselves will always be set.

Thus, add a type check to pci_resource_io() and pci_mmap_resource() to return -EIO for an unsupported resource type.

Use the new pci_resource_is_io() and pci_resource_is_mem() helpers for the type checks, replacing the open-coded bitwise flag tests and also drop the local struct resource pointer in pci_mmap_resource().

---
 drivers/pci/pci-sysfs.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)
drivers/pci/pci-sysfs.c +7 -3
index ad3c17f86c7f..6783c6168445 100644
@@ -1108,20 +1108,24 @@ static int pci_mmap_resource(struct kobject *kobj, const struct bin_attribute *a
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	int bar = (unsigned long)attr->private;
 	enum pci_mmap_state mmap_type;
-	struct resource *res = pci_resource_n(pdev, bar);
 	int ret;
 
 	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
 	if (ret)
 		return ret;
 
-	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
+	if (!pci_resource_is_mem(pdev, bar) &&
+	    !(pci_resource_is_io(pdev, bar) && arch_can_pci_mmap_io()))
+		return -EIO;
+
+	if (pci_resource_is_mem(pdev, bar) &&
+	    iomem_is_exclusive(pci_resource_start(pdev, bar)))
 		return -EINVAL;
 
 	if (!pci_mmap_fits(pdev, bar, vma, PCI_MMAP_SYSFS))
 		return -EINVAL;
 
-	mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
+	mmap_type = pci_resource_is_mem(pdev, bar) ? pci_mmap_mem : pci_mmap_io;
 
 	return pci_mmap_resource_range(pdev, bar, vma, mmap_type, write_combine);
 }

drivers/pci/pci-sysfs.c static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj, · line 1161

Quoted 9 lines
drivers/pci/pci-sysfs.c +3 -0
@@ -1149,6 +1153,9 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
 	int bar = (unsigned long)attr->private;
 	unsigned long port = off;
 
+	if (!pci_resource_is_io(pdev, bar))
+		return -EIO;
+
 	port += pci_resource_start(pdev, bar);
 
 	if (port > pci_resource_end(pdev, bar))
Signature · 1 line
 i.
#6 lore raw

PATCH v4 4/24 PCI/sysfs: Use BAR length in pci_llseek_resource() when attr->size is zero

In reply to Krzysztof Wilczyński

Both legacy and resource attributes set .f_mapping = iomem_get_mapping, so the default generic_file_llseek() would consult iomem_inode for the file size, which knows nothing about the attribute. That is why this custom llseek callback exists.

Currently, the legacy and resource attributes have .size set at creation time, as such, using the attr->size is sufficient. However, the upcoming static resource attributes will have .size == 0 set, since they are const, and the .bin_size callback will be used to provide the real size to kernfs instead.

Thus, update pci_llseek_resource() to derive the file size from the BAR using pci_resource_len() instead of reading the attr->size directly.

The custom pci_llseek_resource() helper has been added in commit 24de09c16f97 ("PCI: Implement custom llseek for sysfs resource entries").

---
 drivers/pci/pci-sysfs.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +12 -2
index 6783c6168445..73a9ae9d289b 100644
@@ -909,11 +909,21 @@ static const struct attribute_group pci_dev_config_attr_group = {
  */
 static __maybe_unused loff_t
 pci_llseek_resource(struct file *filep,
-		    struct kobject *kobj __always_unused,
+		    struct kobject *kobj,
 		    const struct bin_attribute *attr,
 		    loff_t offset, int whence)
 {
-	return fixed_size_llseek(filep, offset, whence, attr->size);
+	struct pci_dev *pdev;
+	int bar;
+
+	if (attr->size)
+		return fixed_size_llseek(filep, offset, whence, attr->size);
+
+	pdev = to_pci_dev(kobj_to_dev(kobj));
+	bar = (unsigned long)attr->private;
+
+	return fixed_size_llseek(filep, offset, whence,
+				 pci_resource_len(pdev, bar));
 }
 
 #ifdef HAVE_PCI_LEGACY
Signature · 1 line
2.53.0
#7 lore raw

Re: [PATCH v4 04/24] PCI/sysfs: Use BAR length in pci_llseek_resource() when attr->size is zero

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 11 lines

Both legacy and resource attributes set .f_mapping = iomem_get_mapping, so the default generic_file_llseek() would consult iomem_inode for the file size, which knows nothing about the attribute. That is why this custom llseek callback exists.

Currently, the legacy and resource attributes have .size set at creation time, as such, using the attr->size is sufficient. However, the upcoming static resource attributes will have .size == 0 set, since they are const, and the .bin_size callback will be used to provide the real size to kernfs instead.

Quoted 8 lines

Thus, update pci_llseek_resource() to derive the file size from the BAR using pci_resource_len() instead of reading the attr->size directly.

The custom pci_llseek_resource() helper has been added in commit 24de09c16f97 ("PCI: Implement custom llseek for sysfs resource entries").

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#8 lore raw

PATCH v4 5/24 PCI/sysfs: Add CAP_SYS_ADMIN check to __resource_resize_store()

In reply to Krzysztof Wilczyński

Currently, the __resource_resize_store() allows writing to the resourceN_resize sysfs attribute to change a BAR's size without checking for capabilities, currently relying only on the file access check.

Resizing a BAR modifies PCI device configuration and can disrupt active drivers. After the upcoming conversion to static attributes, it will also trigger resource file updates via sysfs_update_groups().

Thus, add a CAP_SYS_ADMIN check to prevent unprivileged users from performing BAR resize operations.

---
 drivers/pci/pci-sysfs.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/pci/pci-sysfs.c +3 -0
index 73a9ae9d289b..4f95b336cc34 100644
@@ -1619,6 +1619,9 @@ static ssize_t __resource_resize_store(struct device *dev, int n,
 	int ret;
 	u16 cmd;
 
+	if (!capable(CAP_SYS_ADMIN))
+		return -EPERM;
+
 	if (kstrtoul(buf, 0, &size) < 0)
 		return -EINVAL;
 
Signature · 1 line
2.53.0
#9 lore raw

PATCH v4 6/24 PCI/sysfs: Add static PCI resource attribute macros

In reply to Krzysztof Wilczyński

Add three macros for declaring static binary attributes for PCI resource files:

- pci_dev_resource_io_attr(), for I/O BAR resources (read/write)
- pci_dev_resource_uc_attr(), for memory BAR resources (mmap uncached)
- pci_dev_resource_wc_attr(), for write-combine resources (mmap WC)

Each macro only sets the callbacks its resource type needs. The I/O macro conditionally includes mmap support via __PCI_RESOURCE_IO_MMAP_ATTRS on architectures where arch_can_pci_mmap_io() is true at compile time (such as PowerPC, SPARC, and Xtensa).

---
 drivers/pci/pci-sysfs.c | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
drivers/pci/pci-sysfs.c +41 -0
index 4f95b336cc34..ca0074674acc 100644
@@ -1220,6 +1220,47 @@ static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
 	return pci_resource_io(filp, kobj, attr, buf, off, count, true);
 }
 
+/*
+ * generic_file_llseek() consults f_mapping->host to determine
+ * the file size. As iomem_inode knows nothing about the
+ * attribute, it's not going to work, so override it as well.
+ */
+#if arch_can_pci_mmap_io()
+# define __PCI_RESOURCE_IO_MMAP_ATTRS		\
+	.f_mapping = iomem_get_mapping,		\
+	.llseek = pci_llseek_resource,		\
+	.mmap = pci_mmap_resource_uc,
+#else
+# define __PCI_RESOURCE_IO_MMAP_ATTRS
+#endif
+
+#define pci_dev_resource_io_attr(_bar)					\
+static const struct bin_attribute dev_resource##_bar##_io_attr = {	\
+	.attr = { .name = "resource" __stringify(_bar), .mode = 0600 },	\
+	.private = (void *)(unsigned long)(_bar),			\
+	.read = pci_read_resource,					\
+	.write = pci_write_resource,					\
+	__PCI_RESOURCE_IO_MMAP_ATTRS					\
+}
+
+#define pci_dev_resource_uc_attr(_bar)					\
+static const struct bin_attribute dev_resource##_bar##_uc_attr = {	\
+	.attr = { .name = "resource" __stringify(_bar), .mode = 0600 },	\
+	.private = (void *)(unsigned long)(_bar),			\
+	.f_mapping = iomem_get_mapping,					\
+	.llseek = pci_llseek_resource,					\
+	.mmap = pci_mmap_resource_uc,					\
+}
+
+#define pci_dev_resource_wc_attr(_bar)						\
+static const struct bin_attribute dev_resource##_bar##_wc_attr = {		\
+	.attr = { .name = "resource" __stringify(_bar) "_wc", .mode = 0600 },	\
+	.private = (void *)(unsigned long)(_bar),				\
+	.f_mapping = iomem_get_mapping,						\
+	.llseek = pci_llseek_resource,						\
+	.mmap = pci_mmap_resource_wc,						\
+}
+
 /**
  * pci_remove_resource_files - cleanup resource files
  * @pdev: dev to cleanup
Signature · 1 line
2.53.0
#10 lore raw

PATCH v4 7/24 PCI/sysfs: Convert PCI resource files to static attributes

In reply to Krzysztof Wilczyński

Currently, the PCI resource files (resourceN, resourceN_wc) are dynamically created by pci_create_sysfs_dev_files(), called from both pci_bus_add_device() and the pci_sysfs_init() late_initcall, with only a sysfs_initialized flag for synchronisation. This has caused "duplicate filename" warnings and boot panics when both paths race on the same device.

This is especially likely on Devicetree-based platforms, where the PCI host controllers are platform drivers that probe via the driver model, which can happen during or after the late_initcall. As such, pci_bus_add_device() and pci_sysfs_init() are more likely to overlap.

Thus, convert to static const attributes with three attribute groups (I/O, UC, WC), each with an .is_bin_visible callback that checks resource flags, BAR length, and non_mappable_bars. A .bin_size callback provides pci_resource_len() to the kernfs node for correct stat and lseek behaviour.

As part of this conversion:

- Rename pci_read_resource_io() and pci_write_resource_io() to pci_read_resource() and pci_write_resource() since the callbacks are no longer I/O-specific in the static attribute context.

- Remove pci_create_resource_files(), pci_remove_resource_files(), and pci_create_attr() which are no longer needed.

- Move the __weak stubs outside the #if guard so they remain available for callers converted in subsequent commits.

Platforms that do not define the HAVE_PCI_MMAP macro or the ARCH_GENERIC_PCI_MMAP_RESOURCE macro, such as Alpha architecture, continue using their platform-specific resource file creation.

For reference, the dynamic creation dates back to the pre-Git era:

https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/drivers/pci/pci-sysfs.c?id=42298be0eeb5ae98453b3374c36161b05a46c5dc

The write-combine support was added in commit 45aec1ae72fc ("x86: PAT export resource_wc in pci sysfs").

---
 drivers/pci/pci-sysfs.c | 242 +++++++++++++++++++++-------------------
 include/linux/pci.h     |   2 -
 2 files changed, 127 insertions(+), 117 deletions(-)
drivers/pci/pci-sysfs.c +127 -115
index ca0074674acc..2922905ff070 100644
@@ -1200,14 +1200,14 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
 #endif
 }
 
-static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
+static ssize_t pci_read_resource(struct file *filp, struct kobject *kobj,
 				    const struct bin_attribute *attr, char *buf,
 				    loff_t off, size_t count)
 {
 	return pci_resource_io(filp, kobj, attr, buf, off, count, false);
 }
 
-static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
+static ssize_t pci_write_resource(struct file *filp, struct kobject *kobj,
 				     const struct bin_attribute *attr, char *buf,
 				     loff_t off, size_t count)
 {
@@ -1261,129 +1261,136 @@ static const struct bin_attribute dev_resource##_bar##_wc_attr = {		\
 	.mmap = pci_mmap_resource_wc,						\
 }
 
-/**
- * pci_remove_resource_files - cleanup resource files
- * @pdev: dev to cleanup
- *
- * If we created resource files for @pdev, remove them from sysfs and
- * free their resources.
- */
-static void pci_remove_resource_files(struct pci_dev *pdev)
+static inline umode_t
+__pci_resource_attr_is_visible(struct kobject *kobj,
+			       const struct bin_attribute *a,
+			       int bar, bool write_combine,
+			       unsigned long flags)
 {
-	int i;
+	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		struct bin_attribute *res_attr;
-
-		res_attr = pdev->res_attr[i];
-		if (res_attr) {
-			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
-			kfree(res_attr);
-		}
-
-		res_attr = pdev->res_attr_wc[i];
-		if (res_attr) {
-			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
-			kfree(res_attr);
-		}
-	}
-}
-
-static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
-{
-	/* allocate attribute structure, piggyback attribute name */
-	int name_len = write_combine ? 13 : 10;
-	struct bin_attribute *res_attr;
-	char *res_attr_name;
-	int retval;
-
-	res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
-	if (!res_attr)
-		return -ENOMEM;
-
-	res_attr_name = (char *)(res_attr + 1);
-
-	sysfs_bin_attr_init(res_attr);
-	if (write_combine) {
-		sprintf(res_attr_name, "resource%d_wc", num);
-		res_attr->mmap = pci_mmap_resource_wc;
-	} else {
-		sprintf(res_attr_name, "resource%d", num);
-		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
-			res_attr->read = pci_read_resource_io;
-			res_attr->write = pci_write_resource_io;
-			if (arch_can_pci_mmap_io())
-				res_attr->mmap = pci_mmap_resource_uc;
-		} else {
-			res_attr->mmap = pci_mmap_resource_uc;
-		}
-	}
-	if (res_attr->mmap) {
-		res_attr->f_mapping = iomem_get_mapping;
-		/*
-		 * generic_file_llseek() consults f_mapping->host to determine
-		 * the file size. As iomem_inode knows nothing about the
-		 * attribute, it's not going to work, so override it as well.
-		 */
-		res_attr->llseek = pci_llseek_resource;
-	}
-	res_attr->attr.name = res_attr_name;
-	res_attr->attr.mode = 0600;
-	res_attr->size = pci_resource_len(pdev, num);
-	res_attr->private = (void *)(unsigned long)num;
-	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
-	if (retval) {
-		kfree(res_attr);
-		return retval;
-	}
-
-	if (write_combine)
-		pdev->res_attr_wc[num] = res_attr;
-	else
-		pdev->res_attr[num] = res_attr;
-
-	return 0;
-}
-
-/**
- * pci_create_resource_files - create resource files in sysfs for @dev
- * @pdev: dev in question
- *
- * Walk the resources in @pdev creating files for each resource available.
- */
-static int pci_create_resource_files(struct pci_dev *pdev)
-{
-	int i;
-	int retval;
-
-	/* Skip devices with non-mappable BARs */
 	if (pdev->non_mappable_bars)
 		return 0;
 
-	/* Expose the PCI resources from this device as files */
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	if (!pci_resource_len(pdev, bar))
+		return 0;
 
-		/* skip empty resources */
-		if (!pci_resource_len(pdev, i))
-			continue;
+	if ((pci_resource_flags(pdev, bar) & flags) != flags)
+		return 0;
 
-		retval = pci_create_attr(pdev, i, 0);
-		/* for prefetchable resources, create a WC mappable file */
-		if (!retval && arch_can_pci_mmap_wc() &&
-		    pci_resource_flags(pdev, i) & IORESOURCE_PREFETCH)
-			retval = pci_create_attr(pdev, i, 1);
-		if (retval) {
-			pci_remove_resource_files(pdev);
-			return retval;
-		}
-	}
-	return 0;
+	if (write_combine && !arch_can_pci_mmap_wc())
+		return 0;
+
+	return a->attr.mode;
 }
-#else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */
-int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
-void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
+
+static umode_t pci_dev_resource_io_is_visible(struct kobject *kobj,
+					      const struct bin_attribute *a,
+					      int n)
+{
+	return __pci_resource_attr_is_visible(kobj, a, n, false,
+					      IORESOURCE_IO);
+}
+
+static umode_t pci_dev_resource_uc_is_visible(struct kobject *kobj,
+					      const struct bin_attribute *a,
+					      int n)
+{
+	return __pci_resource_attr_is_visible(kobj, a, n, false,
+					      IORESOURCE_MEM);
+}
+
+static umode_t pci_dev_resource_wc_is_visible(struct kobject *kobj,
+					      const struct bin_attribute *a,
+					      int n)
+{
+	return __pci_resource_attr_is_visible(kobj, a, n, true,
+					      IORESOURCE_MEM | IORESOURCE_PREFETCH);
+}
+
+static size_t pci_dev_resource_bin_size(struct kobject *kobj,
+					const struct bin_attribute *a,
+					int n)
+{
+	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
+
+	return pci_resource_len(pdev, n);
+}
+
+pci_dev_resource_io_attr(0);
+pci_dev_resource_io_attr(1);
+pci_dev_resource_io_attr(2);
+pci_dev_resource_io_attr(3);
+pci_dev_resource_io_attr(4);
+pci_dev_resource_io_attr(5);
+
+pci_dev_resource_uc_attr(0);
+pci_dev_resource_uc_attr(1);
+pci_dev_resource_uc_attr(2);
+pci_dev_resource_uc_attr(3);
+pci_dev_resource_uc_attr(4);
+pci_dev_resource_uc_attr(5);
+
+pci_dev_resource_wc_attr(0);
+pci_dev_resource_wc_attr(1);
+pci_dev_resource_wc_attr(2);
+pci_dev_resource_wc_attr(3);
+pci_dev_resource_wc_attr(4);
+pci_dev_resource_wc_attr(5);
+
+static const struct bin_attribute *const pci_dev_resource_io_attrs[] = {
+	&dev_resource0_io_attr,
+	&dev_resource1_io_attr,
+	&dev_resource2_io_attr,
+	&dev_resource3_io_attr,
+	&dev_resource4_io_attr,
+	&dev_resource5_io_attr,
+	NULL,
+};
+
+static const struct bin_attribute *const pci_dev_resource_uc_attrs[] = {
+	&dev_resource0_uc_attr,
+	&dev_resource1_uc_attr,
+	&dev_resource2_uc_attr,
+	&dev_resource3_uc_attr,
+	&dev_resource4_uc_attr,
+	&dev_resource5_uc_attr,
+	NULL,
+};
+
+static const struct bin_attribute *const pci_dev_resource_wc_attrs[] = {
+	&dev_resource0_wc_attr,
+	&dev_resource1_wc_attr,
+	&dev_resource2_wc_attr,
+	&dev_resource3_wc_attr,
+	&dev_resource4_wc_attr,
+	&dev_resource5_wc_attr,
+	NULL,
+};
+
+static const struct attribute_group pci_dev_resource_io_attr_group = {
+	.bin_attrs = pci_dev_resource_io_attrs,
+	.is_bin_visible = pci_dev_resource_io_is_visible,
+	.bin_size = pci_dev_resource_bin_size,
+};
+
+static const struct attribute_group pci_dev_resource_uc_attr_group = {
+	.bin_attrs = pci_dev_resource_uc_attrs,
+	.is_bin_visible = pci_dev_resource_uc_is_visible,
+	.bin_size = pci_dev_resource_bin_size,
+};
+
+static const struct attribute_group pci_dev_resource_wc_attr_group = {
+	.bin_attrs = pci_dev_resource_wc_attrs,
+	.is_bin_visible = pci_dev_resource_wc_is_visible,
+	.bin_size = pci_dev_resource_bin_size,
+};
+
 #endif
 
+int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
+void __weak pci_remove_resource_files(struct pci_dev *dev) { }
+
 /**
  * pci_write_rom - used to enable access to the PCI ROM display
  * @filp: sysfs file
@@ -1861,6 +1868,11 @@ static const struct attribute_group pci_dev_group = {
 
 const struct attribute_group *pci_dev_groups[] = {
 	&pci_dev_group,
+#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
+	&pci_dev_resource_io_attr_group,
+	&pci_dev_resource_uc_attr_group,
+	&pci_dev_resource_wc_attr_group,
+#endif
 	&pci_dev_config_attr_group,
 	&pci_dev_rom_attr_group,
 	&pci_dev_reset_attr_group,
include/linux/pci.h +0 -2
index 05aceb7f1f37..9c0782899ef9 100644
@@ -2526,10 +2526,8 @@ int pcibios_alloc_irq(struct pci_dev *dev);
 void pcibios_free_irq(struct pci_dev *dev);
 resource_size_t pcibios_default_alignment(void);
 
-#if !defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
 extern int pci_create_resource_files(struct pci_dev *dev);
 extern void pci_remove_resource_files(struct pci_dev *dev);
-#endif
 
 #if defined(CONFIG_PCI_MMCONFIG) || defined(CONFIG_ACPI_MCFG)
 void __init pci_mmcfg_early_init(void);
Signature · 1 line
2.53.0
#11 lore raw

Re: [PATCH v4 07/24] PCI/sysfs: Convert PCI resource files to static attributes

Quoted 171 lines · Krzysztof Wilczyński

Currently, the PCI resource files (resourceN, resourceN_wc) are dynamically created by pci_create_sysfs_dev_files(), called from both pci_bus_add_device() and the pci_sysfs_init() late_initcall, with only a sysfs_initialized flag for synchronisation. This has caused "duplicate filename" warnings and boot panics when both paths race on the same device.

This is especially likely on Devicetree-based platforms, where the PCI host controllers are platform drivers that probe via the driver model, which can happen during or after the late_initcall. As such, pci_bus_add_device() and pci_sysfs_init() are more likely to overlap.

Thus, convert to static const attributes with three attribute groups (I/O, UC, WC), each with an .is_bin_visible callback that checks resource flags, BAR length, and non_mappable_bars. A .bin_size callback provides pci_resource_len() to the kernfs node for correct stat and lseek behaviour.

As part of this conversion:

- Rename pci_read_resource_io() and pci_write_resource_io() to pci_read_resource() and pci_write_resource() since the callbacks are no longer I/O-specific in the static attribute context.

- Remove pci_create_resource_files(), pci_remove_resource_files(), and pci_create_attr() which are no longer needed.

- Move the __weak stubs outside the #if guard so they remain available for callers converted in subsequent commits.

Platforms that do not define the HAVE_PCI_MMAP macro or the ARCH_GENERIC_PCI_MMAP_RESOURCE macro, such as Alpha architecture, continue using their platform-specific resource file creation.

For reference, the dynamic creation dates back to the pre-Git era:

https://git.kernel.org/pub/scm/linux/kernel/git/tglx/history.git/commit/drivers/pci/pci-sysfs.c?id=42298be0eeb5ae98453b3374c36161b05a46c5dc

The write-combine support was added in commit 45aec1ae72fc ("x86: PAT export resource_wc in pci sysfs").

---
 drivers/pci/pci-sysfs.c | 242 +++++++++++++++++++++-------------------
 include/linux/pci.h     |   2 -
 2 files changed, 127 insertions(+), 117 deletions(-)
drivers/pci/pci-sysfs.c +8 -93
index ca0074674acc..2922905ff070 100644
@@ -1200,14 +1200,14 @@ static ssize_t pci_resource_io(struct file *filp, struct kobject *kobj,
 #endif
 }
 
-static ssize_t pci_read_resource_io(struct file *filp, struct kobject *kobj,
+static ssize_t pci_read_resource(struct file *filp, struct kobject *kobj,
 				    const struct bin_attribute *attr, char *buf,
 				    loff_t off, size_t count)
 {
 	return pci_resource_io(filp, kobj, attr, buf, off, count, false);
 }
 
-static ssize_t pci_write_resource_io(struct file *filp, struct kobject *kobj,
+static ssize_t pci_write_resource(struct file *filp, struct kobject *kobj,
 				     const struct bin_attribute *attr, char *buf,
 				     loff_t off, size_t count)
 {
@@ -1261,129 +1261,136 @@ static const struct bin_attribute dev_resource##_bar##_wc_attr = {		\
 	.mmap = pci_mmap_resource_wc,						\
 }
 
-/**
- * pci_remove_resource_files - cleanup resource files
- * @pdev: dev to cleanup
- *
- * If we created resource files for @pdev, remove them from sysfs and
- * free their resources.
- */
-static void pci_remove_resource_files(struct pci_dev *pdev)
+static inline umode_t
+__pci_resource_attr_is_visible(struct kobject *kobj,
+			       const struct bin_attribute *a,
+			       int bar, bool write_combine,
+			       unsigned long flags)
 {
-	int i;
+	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
-		struct bin_attribute *res_attr;
-
-		res_attr = pdev->res_attr[i];
-		if (res_attr) {
-			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
-			kfree(res_attr);
-		}
-
-		res_attr = pdev->res_attr_wc[i];
-		if (res_attr) {
-			sysfs_remove_bin_file(&pdev->dev.kobj, res_attr);
-			kfree(res_attr);
-		}
-	}
-}
-
-static int pci_create_attr(struct pci_dev *pdev, int num, int write_combine)
-{
-	/* allocate attribute structure, piggyback attribute name */
-	int name_len = write_combine ? 13 : 10;
-	struct bin_attribute *res_attr;
-	char *res_attr_name;
-	int retval;
-
-	res_attr = kzalloc(sizeof(*res_attr) + name_len, GFP_ATOMIC);
-	if (!res_attr)
-		return -ENOMEM;
-
-	res_attr_name = (char *)(res_attr + 1);
-
-	sysfs_bin_attr_init(res_attr);
-	if (write_combine) {
-		sprintf(res_attr_name, "resource%d_wc", num);
-		res_attr->mmap = pci_mmap_resource_wc;
-	} else {
-		sprintf(res_attr_name, "resource%d", num);
-		if (pci_resource_flags(pdev, num) & IORESOURCE_IO) {
-			res_attr->read = pci_read_resource_io;
-			res_attr->write = pci_write_resource_io;
-			if (arch_can_pci_mmap_io())
-				res_attr->mmap = pci_mmap_resource_uc;
-		} else {
-			res_attr->mmap = pci_mmap_resource_uc;
-		}
-	}
-	if (res_attr->mmap) {
-		res_attr->f_mapping = iomem_get_mapping;
-		/*
-		 * generic_file_llseek() consults f_mapping->host to determine
-		 * the file size. As iomem_inode knows nothing about the
-		 * attribute, it's not going to work, so override it as well.
-		 */
-		res_attr->llseek = pci_llseek_resource;
-	}
-	res_attr->attr.name = res_attr_name;
-	res_attr->attr.mode = 0600;
-	res_attr->size = pci_resource_len(pdev, num);
-	res_attr->private = (void *)(unsigned long)num;
-	retval = sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
-	if (retval) {
-		kfree(res_attr);
-		return retval;
-	}
-
-	if (write_combine)
-		pdev->res_attr_wc[num] = res_attr;
-	else
-		pdev->res_attr[num] = res_attr;
-
-	return 0;
-}
-
-/**
- * pci_create_resource_files - create resource files in sysfs for @dev
- * @pdev: dev in question
- *
- * Walk the resources in @pdev creating files for each resource available.
- */
-static int pci_create_resource_files(struct pci_dev *pdev)
-{
-	int i;

drivers/pci/pci-sysfs.c static const struct bin_attribute dev_resource##_bar##_wc_attr = { \ · line 1276

Quoted 10 lines
drivers/pci/pci-sysfs.c +2 -5
-	int retval;
-
-	/* Skip devices with non-mappable BARs */
 	if (pdev->non_mappable_bars)
 		return 0;
 
-	/* Expose the PCI resources from this device as files */
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	if (!pci_resource_len(pdev, bar))
+		return 0;

Hi,

Did you accidently forget to address some of the comments as I thought you were agreeing to changing this to resource_assigned() but I found no resource_assigned() from entire series?

Signature · 1 line
 i.
Quoted 165 lines
drivers/pci/pci-sysfs.c +117 -17
-		/* skip empty resources */
-		if (!pci_resource_len(pdev, i))
-			continue;
+	if ((pci_resource_flags(pdev, bar) & flags) != flags)
+		return 0;
 
-		retval = pci_create_attr(pdev, i, 0);
-		/* for prefetchable resources, create a WC mappable file */
-		if (!retval && arch_can_pci_mmap_wc() &&
-		    pci_resource_flags(pdev, i) & IORESOURCE_PREFETCH)
-			retval = pci_create_attr(pdev, i, 1);
-		if (retval) {
-			pci_remove_resource_files(pdev);
-			return retval;
-		}
-	}
-	return 0;
+	if (write_combine && !arch_can_pci_mmap_wc())
+		return 0;
+
+	return a->attr.mode;
 }
-#else /* !(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)) */
-int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
-void __weak pci_remove_resource_files(struct pci_dev *dev) { return; }
+
+static umode_t pci_dev_resource_io_is_visible(struct kobject *kobj,
+					      const struct bin_attribute *a,
+					      int n)
+{
+	return __pci_resource_attr_is_visible(kobj, a, n, false,
+					      IORESOURCE_IO);
+}
+
+static umode_t pci_dev_resource_uc_is_visible(struct kobject *kobj,
+					      const struct bin_attribute *a,
+					      int n)
+{
+	return __pci_resource_attr_is_visible(kobj, a, n, false,
+					      IORESOURCE_MEM);
+}
+
+static umode_t pci_dev_resource_wc_is_visible(struct kobject *kobj,
+					      const struct bin_attribute *a,
+					      int n)
+{
+	return __pci_resource_attr_is_visible(kobj, a, n, true,
+					      IORESOURCE_MEM | IORESOURCE_PREFETCH);
+}
+
+static size_t pci_dev_resource_bin_size(struct kobject *kobj,
+					const struct bin_attribute *a,
+					int n)
+{
+	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
+
+	return pci_resource_len(pdev, n);
+}
+
+pci_dev_resource_io_attr(0);
+pci_dev_resource_io_attr(1);
+pci_dev_resource_io_attr(2);
+pci_dev_resource_io_attr(3);
+pci_dev_resource_io_attr(4);
+pci_dev_resource_io_attr(5);
+
+pci_dev_resource_uc_attr(0);
+pci_dev_resource_uc_attr(1);
+pci_dev_resource_uc_attr(2);
+pci_dev_resource_uc_attr(3);
+pci_dev_resource_uc_attr(4);
+pci_dev_resource_uc_attr(5);
+
+pci_dev_resource_wc_attr(0);
+pci_dev_resource_wc_attr(1);
+pci_dev_resource_wc_attr(2);
+pci_dev_resource_wc_attr(3);
+pci_dev_resource_wc_attr(4);
+pci_dev_resource_wc_attr(5);
+
+static const struct bin_attribute *const pci_dev_resource_io_attrs[] = {
+	&dev_resource0_io_attr,
+	&dev_resource1_io_attr,
+	&dev_resource2_io_attr,
+	&dev_resource3_io_attr,
+	&dev_resource4_io_attr,
+	&dev_resource5_io_attr,
+	NULL,
+};
+
+static const struct bin_attribute *const pci_dev_resource_uc_attrs[] = {
+	&dev_resource0_uc_attr,
+	&dev_resource1_uc_attr,
+	&dev_resource2_uc_attr,
+	&dev_resource3_uc_attr,
+	&dev_resource4_uc_attr,
+	&dev_resource5_uc_attr,
+	NULL,
+};
+
+static const struct bin_attribute *const pci_dev_resource_wc_attrs[] = {
+	&dev_resource0_wc_attr,
+	&dev_resource1_wc_attr,
+	&dev_resource2_wc_attr,
+	&dev_resource3_wc_attr,
+	&dev_resource4_wc_attr,
+	&dev_resource5_wc_attr,
+	NULL,
+};
+
+static const struct attribute_group pci_dev_resource_io_attr_group = {
+	.bin_attrs = pci_dev_resource_io_attrs,
+	.is_bin_visible = pci_dev_resource_io_is_visible,
+	.bin_size = pci_dev_resource_bin_size,
+};
+
+static const struct attribute_group pci_dev_resource_uc_attr_group = {
+	.bin_attrs = pci_dev_resource_uc_attrs,
+	.is_bin_visible = pci_dev_resource_uc_is_visible,
+	.bin_size = pci_dev_resource_bin_size,
+};
+
+static const struct attribute_group pci_dev_resource_wc_attr_group = {
+	.bin_attrs = pci_dev_resource_wc_attrs,
+	.is_bin_visible = pci_dev_resource_wc_is_visible,
+	.bin_size = pci_dev_resource_bin_size,
+};
+
 #endif
 
+int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
+void __weak pci_remove_resource_files(struct pci_dev *dev) { }
+
 /**
  * pci_write_rom - used to enable access to the PCI ROM display
  * @filp: sysfs file
@@ -1861,6 +1868,11 @@ static const struct attribute_group pci_dev_group = {
 
 const struct attribute_group *pci_dev_groups[] = {
 	&pci_dev_group,
+#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
+	&pci_dev_resource_io_attr_group,
+	&pci_dev_resource_uc_attr_group,
+	&pci_dev_resource_wc_attr_group,
+#endif
 	&pci_dev_config_attr_group,
 	&pci_dev_rom_attr_group,
 	&pci_dev_reset_attr_group,
include/linux/pci.h +0 -2
index 05aceb7f1f37..9c0782899ef9 100644
@@ -2526,10 +2526,8 @@ int pcibios_alloc_irq(struct pci_dev *dev);
 void pcibios_free_irq(struct pci_dev *dev);
 resource_size_t pcibios_default_alignment(void);
 
-#if !defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
 extern int pci_create_resource_files(struct pci_dev *dev);
 extern void pci_remove_resource_files(struct pci_dev *dev);
-#endif
 
 #if defined(CONFIG_PCI_MMCONFIG) || defined(CONFIG_ACPI_MCFG)
 void __init pci_mmcfg_early_init(void);
#12 lore raw

Re: [PATCH v4 07/24] PCI/sysfs: Convert PCI resource files to static attributes

Hello,

Quoted 4 lines
Quoted 4 lines
drivers/pci/pci-sysfs.c +2 -2
-	/* Expose the PCI resources from this device as files */
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	if (!pci_resource_len(pdev, bar))
+		return 0;

[...]

Quoted 3 lines

Did you accidently forget to address some of the comments as I thought you were agreeing to changing this to resource_assigned() but I found no resource_assigned() from entire series?

I have not. Sorry for late reply here.

When testing resource_assigned() as replacement for pci_resource_len(), none of the resource files would be made visible.

The resource_assigned() checks res->parent, but the static .is_bin_visible callback runs at device_add() time, called from pci_device_add() during bus enumeration, so before pci_assign_unassigned_bus_resources() runs and inserts resources into the resource tree via pci_claim_resource(), etc.

The call sequence in pci_host_probe() is:

pci_scan_root_bus_bridge()

    pci_scan_child_bus()
      pci_scan_slot()
        pci_scan_single_device()
          pci_scan_device()
            pci_setup_device()
              pci_read_bases()        <- res->start/end set from BARs
          pci_device_add()
            device_add()              <- is_bin_visible() runs here
                                         res->parent still NULL

pci_assign_unassigned_root_bus_resources()

    pci_claim_resource()
      request_resource_conflict()
        __request_resource()          <- res->parent set here

At that point, the pci_resource_len() would return a non-zero value as res->start and res->end would already be set, but the res->parent is still NULL (not yet assigned to tree).

The old dynamic code ran from pci_sysfs_init() as a late_initcall (after assignment), where resource_assigned() would have worked.

As such, we can't really use resource_assigned() together with static sysfs attributes, at least not without solving the resources evaluation order here.

Thank you!

Krzysztof

#13 lore raw

Re: [PATCH v4 07/24] PCI/sysfs: Convert PCI resource files to static attributes

Quoted 43 lines · Krzysztof Wilczyński

Hello,

Quoted 4 lines
Quoted 4 lines
drivers/pci/pci-sysfs.c +2 -2
-	/* Expose the PCI resources from this device as files */
-	for (i = 0; i < PCI_STD_NUM_BARS; i++) {
+	if (!pci_resource_len(pdev, bar))
+		return 0;

[...]

Quoted 3 lines

Did you accidently forget to address some of the comments as I thought you were agreeing to changing this to resource_assigned() but I found no resource_assigned() from entire series?

I have not. Sorry for late reply here.

When testing resource_assigned() as replacement for pci_resource_len(), none of the resource files would be made visible.

The resource_assigned() checks res->parent, but the static .is_bin_visible callback runs at device_add() time, called from pci_device_add() during bus enumeration, so before pci_assign_unassigned_bus_resources() runs and inserts resources into the resource tree via pci_claim_resource(), etc.

The call sequence in pci_host_probe() is:

pci_scan_root_bus_bridge()

    pci_scan_child_bus()
      pci_scan_slot()
        pci_scan_single_device()
          pci_scan_device()
            pci_setup_device()
              pci_read_bases()        <- res->start/end set from BARs
          pci_device_add()
            device_add()              <- is_bin_visible() runs here
                                         res->parent still NULL

pci_assign_unassigned_root_bus_resources()

    pci_claim_resource()
      request_resource_conflict()
        __request_resource()          <- res->parent set here

At that point, the pci_resource_len() would return a non-zero value as res->start and res->end would already be set, but the res->parent is still NULL (not yet assigned to tree).

Quoted 8 lines

The old dynamic code ran from pci_sysfs_init() as a late_initcall (after assignment), where resource_assigned() would have worked.

As such, we can't really use resource_assigned() together with static sysfs attributes, at least not without solving the resources evaluation order here.

Thank you!

Okay, understood (I already saw you coverletter as well).

This however implies there's no guarantee the resource space range is even available for the BAR if the check is not based on ->parent. (Hit a problem like this kind of highlights how using pci_resource_len() is definitely hacky.)

I suppose it would be more proper to always recalculate the sysfs visibilities after resources have been rearranged (released or assigned). But as with the BAR resize, there's the race window when releasing BARs so solving it may be non-trivial to get the ordering right (without mass removing sysfs files prior to running the resource fitting and assignment algorithm which doesn't seem wise either).

So to conclude, IMO, this series is a major improvement to state of things as is and solving this pci_resource_len() vs resource_assigned() within the context of this series doesn't seem worth the extra delay and complexity. Thus, I'm fine with using pci_resource_len() for now.

Signature · 1 line
 i.
#14 lore raw

PATCH v4 8/24 PCI/sysfs: Convert __resource_resize_store() to use static attributes

In reply to Krzysztof Wilczyński

Currently, __resource_resize_store() calls pci_remove_resource_files() and pci_create_resource_files() to tear down and recreate resource files after a BAR resize.

Replace these with sysfs_remove_groups() before the resize and sysfs_create_groups() after, which re-evaluates visibility and runs the .bin_size callback for the static resource attribute groups. Also, add a warning if pci_resize_resource() fails to expose potential failures.

For architectures without HAVE_PCI_MMAP or ARCH_GENERIC_PCI_MMAP_RESOURCE, define pci_dev_resource_attr_groups as NULL so the resize path compiles unconditionally and sysfs_remove_groups() and sysfs_create_groups() become no-ops.

While at it, rename the resource_resize_is_visible() to resource_resize_attr_is_visible() and the corresponding group variable to align with the naming convention used by the resource attribute groups.

---
 drivers/pci/pci-sysfs.c | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)
drivers/pci/pci-sysfs.c +18 -7
index 2922905ff070..c0925bdc9ecd 100644
@@ -1386,6 +1386,14 @@ static const struct attribute_group pci_dev_resource_wc_attr_group = {
 	.bin_size = pci_dev_resource_bin_size,
 };
 
+static const struct attribute_group *pci_dev_resource_attr_groups[] = {
+	&pci_dev_resource_io_attr_group,
+	&pci_dev_resource_uc_attr_group,
+	&pci_dev_resource_wc_attr_group,
+	NULL,
+};
+#else
+#define pci_dev_resource_attr_groups NULL
 #endif
 
 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
@@ -1692,14 +1700,17 @@ static ssize_t __resource_resize_store(struct device *dev, int n,
 	pci_write_config_word(pdev, PCI_COMMAND,
 			      cmd & ~PCI_COMMAND_MEMORY);
 
-	pci_remove_resource_files(pdev);
+	sysfs_remove_groups(&pdev->dev.kobj, pci_dev_resource_attr_groups);
 
 	ret = pci_resize_resource(pdev, n, size, 0);
+	if (ret)
+		pci_warn(pdev, "Failed to resize BAR %d: %pe\n",
+			 n, ERR_PTR(ret));
 
 	pci_assign_unassigned_bus_resources(bus);
 
-	if (pci_create_resource_files(pdev))
-		pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n");
+	if (sysfs_create_groups(&pdev->dev.kobj, pci_dev_resource_attr_groups))
+		pci_warn(pdev, "Failed to recreate resource groups after BAR resizing\n");
 
 	pci_write_config_word(pdev, PCI_COMMAND, cmd);
 pm_put:
@@ -1742,7 +1753,7 @@ static struct attribute *resource_resize_attrs[] = {
 	NULL,
 };
 
-static umode_t resource_resize_is_visible(struct kobject *kobj,
+static umode_t resource_resize_attr_is_visible(struct kobject *kobj,
 					  struct attribute *a, int n)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
@@ -1750,9 +1761,9 @@ static umode_t resource_resize_is_visible(struct kobject *kobj,
 	return pci_rebar_get_current_size(pdev, n) < 0 ? 0 : a->mode;
 }
 
-static const struct attribute_group pci_dev_resource_resize_group = {
+static const struct attribute_group pci_dev_resource_resize_attr_group = {
 	.attrs = resource_resize_attrs,
-	.is_visible = resource_resize_is_visible,
+	.is_visible = resource_resize_attr_is_visible,
 };
 
 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
@@ -1873,6 +1884,7 @@ const struct attribute_group *pci_dev_groups[] = {
 	&pci_dev_resource_uc_attr_group,
 	&pci_dev_resource_wc_attr_group,
 #endif
+	&pci_dev_resource_resize_attr_group,
 	&pci_dev_config_attr_group,
 	&pci_dev_rom_attr_group,
 	&pci_dev_reset_attr_group,
@@ -1884,7 +1896,6 @@ const struct attribute_group *pci_dev_groups[] = {
 #ifdef CONFIG_ACPI
 	&pci_dev_acpi_attr_group,
 #endif
-	&pci_dev_resource_resize_group,
 	ARCH_PCI_DEV_GROUPS
 	NULL,
 };
Signature · 1 line
2.53.0
#15 lore raw

PATCH v4 9/24 PCI/sysfs: Add stubs for pci_{create,remove}_sysfs_dev_files()

In reply to Krzysztof Wilczyński

On platforms with HAVE_PCI_MMAP or ARCH_GENERIC_PCI_MMAP_RESOURCE, resource files are now handled by static attribute groups registered via pci_dev_groups[].

Thus, the pci_create_sysfs_dev_files() and pci_remove_sysfs_dev_files() can now be stubbed out, as the dynamic resource file creation is no longer needed.

Also, simplify pci_sysfs_init() on these platforms to only iterate buses for legacy attributes creation, skipping the per-device loop.

Move the __weak stubs for pci_create_resource_files() and pci_remove_resource_files() into the #else branch since only platforms without HAVE_PCI_MMAP (such as Alpha architecture) still need them. Guard the res_attr[] and res_attr_wc[] fields in struct pci_dev the same way.

---
 drivers/pci/pci-sysfs.c | 15 ++++++++++++---
 include/linux/pci.h     |  4 ++++
 2 files changed, 16 insertions(+), 3 deletions(-)
drivers/pci/pci-sysfs.c +12 -3
index c0925bdc9ecd..517efb6d71cc 100644
@@ -1394,10 +1394,9 @@ static const struct attribute_group *pci_dev_resource_attr_groups[] = {
 };
 #else
 #define pci_dev_resource_attr_groups NULL
-#endif
-
 int __weak pci_create_resource_files(struct pci_dev *dev) { return 0; }
 void __weak pci_remove_resource_files(struct pci_dev *dev) { }
+#endif
 
 /**
  * pci_write_rom - used to enable access to the PCI ROM display
@@ -1766,6 +1765,10 @@ static const struct attribute_group pci_dev_resource_resize_attr_group = {
 	.is_visible = resource_resize_attr_is_visible,
 };
 
+#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
+int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
+void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
+#else
 int __must_check pci_create_sysfs_dev_files(struct pci_dev *pdev)
 {
 	if (!sysfs_initialized)
@@ -1787,9 +1790,15 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
 
 	pci_remove_resource_files(pdev);
 }
+#endif
 
 static int __init pci_sysfs_init(void)
 {
+#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
+	struct pci_bus *pbus = NULL;
+
+	sysfs_initialized = 1;
+#else
 	struct pci_dev *pdev = NULL;
 	struct pci_bus *pbus = NULL;
 	int retval;
@@ -1802,7 +1811,7 @@ static int __init pci_sysfs_init(void)
 			return retval;
 		}
 	}
-
+#endif
 	while ((pbus = pci_find_next_bus(pbus)))
 		pci_create_legacy_files(pbus);
 
include/linux/pci.h +4 -0
index 9c0782899ef9..30aeb8e02b7b 100644
@@ -507,8 +507,10 @@ struct pci_dev {
 	spinlock_t	pcie_cap_lock;		/* Protects RMW ops in capability accessors */
 	u32		saved_config_space[16]; /* Config space saved at suspend time */
 	struct hlist_head saved_cap_space;
+#if !defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
 	struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */
 	struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */
+#endif
 
 #ifdef CONFIG_HOTPLUG_PCI_PCIE
 	unsigned int	broken_cmd_compl:1;	/* No compl for some cmds */
@@ -2526,8 +2528,10 @@ int pcibios_alloc_irq(struct pci_dev *dev);
 void pcibios_free_irq(struct pci_dev *dev);
 resource_size_t pcibios_default_alignment(void);
 
+#if !defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
 extern int pci_create_resource_files(struct pci_dev *dev);
 extern void pci_remove_resource_files(struct pci_dev *dev);
+#endif
 
 #if defined(CONFIG_PCI_MMCONFIG) || defined(CONFIG_ACPI_MCFG)
 void __init pci_mmcfg_early_init(void);
Signature · 1 line
2.53.0
#16 lore raw

PATCH v4 10/24 PCI/sysfs: Limit pci_sysfs_init() late_initcall compile scope

In reply to Krzysztof Wilczyński

Currently, pci_sysfs_init() and sysfs_initialized compile unconditionally, even on platforms where static attribute groups handle all resource file creation.

Thus, place them behind a new HAVE_PCI_SYSFS_INIT macro, especially as the late_initcall is only needed when:

- HAVE_PCI_LEGACY is set, to iterate buses and create legacy I/O and memory files.

- Neither HAVE_PCI_MMAP nor ARCH_GENERIC_PCI_MMAP_RESOURCE is set, to iterate devices and create resource files via the __weak pci_create_resource_files() stub override (this is how the Alpha architecture handles this currently).

On most systems both conditions are false and the entire late_initcall compiles away.

---
 drivers/pci/pci-sysfs.c | 9 +++++++++
 1 file changed, 9 insertions(+)
drivers/pci/pci-sysfs.c +9 -0
index 517efb6d71cc..d22bc5c48bc0 100644
@@ -37,7 +37,14 @@
 #define ARCH_PCI_DEV_GROUPS
 #endif
 
+#if defined(HAVE_PCI_LEGACY) || \
+	!defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
+#define HAVE_PCI_SYSFS_INIT
+#endif
+
+#ifdef HAVE_PCI_SYSFS_INIT
 static int sysfs_initialized;	/* = 0 */
+#endif
 
 /* show configuration fields */
 #define pci_config_attr(field, format_string)				\
@@ -1792,6 +1799,7 @@ void pci_remove_sysfs_dev_files(struct pci_dev *pdev)
 }
 #endif
 
+#ifdef HAVE_PCI_SYSFS_INIT
 static int __init pci_sysfs_init(void)
 {
 #if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
@@ -1818,6 +1826,7 @@ static int __init pci_sysfs_init(void)
 	return 0;
 }
 late_initcall(pci_sysfs_init);
+#endif
 
 static struct attribute *pci_dev_dev_attrs[] = {
 	&dev_attr_boot_vga.attr,
Signature · 1 line
2.53.0
#17 lore raw

PATCH v4 11/24 alpha/PCI: Add security_locked_down() check to pci_mmap_resource()

In reply to Krzysztof Wilczyński

Currently, Alpha's pci_mmap_resource() does not check security_locked_down(LOCKDOWN_PCI_ACCESS) before allowing userspace to mmap PCI BARs.

The generic version has had this check since commit eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down") to prevent DMA attacks when the kernel is locked down.

Add the same check to Alpha's pci_mmap_resource().

Fixes eb627e17727e ("PCI: Lock down BAR access when the kernel is locked down")
---
 arch/alpha/kernel/pci-sysfs.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
arch/alpha/kernel/pci-sysfs.c +6 -1
index 3048758304b5..2324720c3e83 100644
@@ -11,6 +11,7 @@
  */
 
 #include <linux/sched.h>
+#include <linux/security.h>
 #include <linux/stat.h>
 #include <linux/slab.h>
 #include <linux/pci.h>
@@ -71,7 +72,11 @@ static int pci_mmap_resource(struct kobject *kobj,
 	struct resource *res = attr->private;
 	enum pci_mmap_state mmap_type;
 	struct pci_bus_region bar;
-	int i;
+	int i, ret;
+
+	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
+	if (ret)
+		return ret;
 
 	for (i = 0; i < PCI_STD_NUM_BARS; i++)
 		if (res == &pdev->resource[i])
Signature · 1 line
2.53.0
#18 lore raw

PATCH v4 12/24 alpha/PCI: Use BAR index in sysfs attr->private instead of resource pointer

In reply to Krzysztof Wilczyński

Currently, Alpha's pci_create_one_attr() stores a resource pointer in attr->private, and pci_mmap_resource() loops through all BARs to find the matching index.

Thus, store the BAR index directly in attr->private and retrieve the resource via pci_resource_n(). This eliminates the loop and aligns with the convention used by the generic PCI sysfs code.

While at it, add parentheses around the bitwise flag test in pci_mmap_resource() to make the precedence explicit, and to match the preferred style.

The PCI core change was first added in the commit dca40b186b75 ("PCI:
Use BAR index in sysfs attr->private instead of resource pointer").

---
 arch/alpha/kernel/pci-sysfs.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
arch/alpha/kernel/pci-sysfs.c +6 -11
index 2324720c3e83..c84867ce31f5 100644
@@ -69,25 +69,20 @@ static int pci_mmap_resource(struct kobject *kobj,
 			     struct vm_area_struct *vma, int sparse)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
-	struct resource *res = attr->private;
+	int barno = (unsigned long)attr->private;
+	struct resource *res = pci_resource_n(pdev, barno);
 	enum pci_mmap_state mmap_type;
 	struct pci_bus_region bar;
-	int i, ret;
+	int ret;
 
 	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
 	if (ret)
 		return ret;
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++)
-		if (res == &pdev->resource[i])
-			break;
-	if (i >= PCI_STD_NUM_BARS)
-		return -ENODEV;
-
-	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
+	if ((res->flags & IORESOURCE_MEM) && iomem_is_exclusive(res->start))
 		return -EINVAL;
 
-	if (!__pci_mmap_fits(pdev, i, vma, sparse))
+	if (!__pci_mmap_fits(pdev, barno, vma, sparse))
 		return -EINVAL;
 
 	pcibios_resource_to_bus(pdev->bus, &bar, res);
@@ -170,7 +165,7 @@ static int pci_create_one_attr(struct pci_dev *pdev, int num, char *name,
 	res_attr->attr.name = name;
 	res_attr->attr.mode = S_IRUSR | S_IWUSR;
 	res_attr->size = sparse ? size << 5 : size;
-	res_attr->private = &pdev->resource[num];
+	res_attr->private = (void *)(unsigned long)num;
 	return sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
 }
 
Signature · 1 line
2.53.0
Reviewed-by on v4 12/24
#19 lore raw

Re: [PATCH v4 12/24] alpha/PCI: Use BAR index in sysfs attr->private instead of resource pointer

Quoted 11 lines · Krzysztof Wilczyński

Currently, Alpha's pci_create_one_attr() stores a resource pointer in attr->private, and pci_mmap_resource() loops through all BARs to find the matching index.

Thus, store the BAR index directly in attr->private and retrieve the resource via pci_resource_n(). This eliminates the loop and aligns with the convention used by the generic PCI sysfs code.

While at it, add parentheses around the bitwise flag test in pci_mmap_resource() to make the precedence explicit, and to match the preferred style.

Probably not necessary to touch this line now at all as you're going to replace it two patches later anyway so it just adds noise.

Quoted 53 lines

The PCI core change was first added in the commit dca40b186b75 ("PCI:
Use BAR index in sysfs attr->private instead of resource pointer").

---
 arch/alpha/kernel/pci-sysfs.c | 17 ++++++-----------
 1 file changed, 6 insertions(+), 11 deletions(-)
arch/alpha/kernel/pci-sysfs.c +6 -11
index 2324720c3e83..c84867ce31f5 100644
@@ -69,25 +69,20 @@ static int pci_mmap_resource(struct kobject *kobj,
 			     struct vm_area_struct *vma, int sparse)
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
-	struct resource *res = attr->private;
+	int barno = (unsigned long)attr->private;
+	struct resource *res = pci_resource_n(pdev, barno);
 	enum pci_mmap_state mmap_type;
 	struct pci_bus_region bar;
-	int i, ret;
+	int ret;
 
 	ret = security_locked_down(LOCKDOWN_PCI_ACCESS);
 	if (ret)
 		return ret;
 
-	for (i = 0; i < PCI_STD_NUM_BARS; i++)
-		if (res == &pdev->resource[i])
-			break;
-	if (i >= PCI_STD_NUM_BARS)
-		return -ENODEV;
-
-	if (res->flags & IORESOURCE_MEM && iomem_is_exclusive(res->start))
+	if ((res->flags & IORESOURCE_MEM) && iomem_is_exclusive(res->start))
 		return -EINVAL;
 
-	if (!__pci_mmap_fits(pdev, i, vma, sparse))
+	if (!__pci_mmap_fits(pdev, barno, vma, sparse))
 		return -EINVAL;
 
 	pcibios_resource_to_bus(pdev->bus, &bar, res);
@@ -170,7 +165,7 @@ static int pci_create_one_attr(struct pci_dev *pdev, int num, char *name,
 	res_attr->attr.name = name;
 	res_attr->attr.mode = S_IRUSR | S_IWUSR;
 	res_attr->size = sparse ? size << 5 : size;
-	res_attr->private = &pdev->resource[num];
+	res_attr->private = (void *)(unsigned long)num;
 	return sysfs_create_bin_file(&pdev->dev.kobj, res_attr);
 }
 
Signature · 1 line
 i.
#20 lore raw

Re: [PATCH v4 12/24] alpha/PCI: Use BAR index in sysfs attr->private instead of resource pointer

Hello,

Quoted 8 lines
Quoted 3 lines

While at it, add parentheses around the bitwise flag test in pci_mmap_resource() to make the precedence explicit, and to match the preferred style.

Probably not necessary to touch this line now at all as you're going to replace it two patches later anyway so it just adds noise.

Makes sense. Taken care of this in v5.

Thank you!

Krzysztof

#21 lore raw

PATCH v4 13/24 alpha/PCI: Use PCI resource accessor macros

In reply to Krzysztof Wilczyński

Replace direct pdev->resource[] accesses with pci_resource_n(), and pdev->resource[].flags accesses with pci_resource_flags().

No functional changes intended.

---
 arch/alpha/kernel/pci-sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/alpha/kernel/pci-sysfs.c +2 -2
index c84867ce31f5..7aac5e76dcd6 100644
@@ -141,7 +141,7 @@ static int sparse_mem_mmap_fits(struct pci_dev *pdev, int num)
 	long dense_offset;
 	unsigned long sparse_size;
 
-	pcibios_resource_to_bus(pdev->bus, &bar, &pdev->resource[num]);
+	pcibios_resource_to_bus(pdev->bus, &bar, pci_resource_n(pdev, num));
 
 	/* All core logic chips have 4G sparse address space, except
 	   CIA which has 16G (see xxx_SPARSE_MEM and xxx_DENSE_MEM
@@ -181,7 +181,7 @@ static int pci_create_attr(struct pci_dev *pdev, int num)
 	suffix = "";	/* Assume bwx machine, normal resourceN files. */
 	nlen1 = 10;
 
-	if (pdev->resource[num].flags & IORESOURCE_MEM) {
+	if (pci_resource_flags(pdev, num) & IORESOURCE_MEM) {
 		sparse_base = hose->sparse_mem_base;
 		dense_base = hose->dense_mem_base;
 		if (sparse_base && !sparse_mem_mmap_fits(pdev, num)) {
Signature · 1 line
2.53.0
Reviewed-by on v4 13/24
#22 lore raw

Re: [PATCH v4 13/24] alpha/PCI: Use PCI resource accessor macros

Quoted 25 lines · Krzysztof Wilczyński

Replace direct pdev->resource[] accesses with pci_resource_n(), and pdev->resource[].flags accesses with pci_resource_flags().

No functional changes intended.

---
 arch/alpha/kernel/pci-sysfs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
arch/alpha/kernel/pci-sysfs.c +1 -1
index c84867ce31f5..7aac5e76dcd6 100644
@@ -141,7 +141,7 @@ static int sparse_mem_mmap_fits(struct pci_dev *pdev, int num)
 	long dense_offset;
 	unsigned long sparse_size;
 
-	pcibios_resource_to_bus(pdev->bus, &bar, &pdev->resource[num]);
+	pcibios_resource_to_bus(pdev->bus, &bar, pci_resource_n(pdev, num));
 
 	/* All core logic chips have 4G sparse address space, except
 	   CIA which has 16G (see xxx_SPARSE_MEM and xxx_DENSE_MEM

arch/alpha/kernel/pci-sysfs.c static int pci_create_attr(struct pci_dev *pdev, int num) · line 184

Quoted 5 lines
arch/alpha/kernel/pci-sysfs.c +1 -1
@@ -181,7 +181,7 @@ static int pci_create_attr(struct pci_dev *pdev, int num)
 	suffix = "";	/* Assume bwx machine, normal resourceN files. */
 	nlen1 = 10;
 
-	if (pdev->resource[num].flags & IORESOURCE_MEM) {
+	if (pci_resource_flags(pdev, num) & IORESOURCE_MEM) {

This could have used the new helper but since this code is going away anyway in a later patch it doesn't matter,

Signature · 1 line
 i.
#23 lore raw

Re: [PATCH v4 13/24] alpha/PCI: Use PCI resource accessor macros

Hello,

Quoted 11 lines

arch/alpha/kernel/pci-sysfs.c static int pci_create_attr(struct pci_dev *pdev, int num) · line 184

Quoted 6 lines
arch/alpha/kernel/pci-sysfs.c +1 -1
@@ -181,7 +181,7 @@ static int pci_create_attr(struct pci_dev *pdev, int num)
 	suffix = "";	/* Assume bwx machine, normal resourceN files. */
 	nlen1 = 10;
 
-	if (pdev->resource[num].flags & IORESOURCE_MEM) {
+	if (pci_resource_flags(pdev, num) & IORESOURCE_MEM) {

This could have used the new helper but since this code is going away anyway in a later patch it doesn't matter,

Taken care of in v5.

Thank you!

Krzysztof

#24 lore raw

PATCH v4 14/24 alpha/PCI: Clean up pci_mmap_resource()

In reply to Krzysztof Wilczyński

Replace open-coded res->flags type checks with new pci_resource_is_mem() and pci_resource_start() helpers. Move the pci_resource_n() call into pcibios_resource_to_bus() and drop the local struct resource pointer.

---
 arch/alpha/kernel/pci-sysfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/alpha/kernel/pci-sysfs.c +4 -4
index 7aac5e76dcd6..6c011dab326d 100644
@@ -70,7 +70,6 @@ static int pci_mmap_resource(struct kobject *kobj,
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	int barno = (unsigned long)attr->private;
-	struct resource *res = pci_resource_n(pdev, barno);
 	enum pci_mmap_state mmap_type;
 	struct pci_bus_region bar;
 	int ret;
@@ -79,15 +78,16 @@ static int pci_mmap_resource(struct kobject *kobj,
 	if (ret)
 		return ret;
 
-	if ((res->flags & IORESOURCE_MEM) && iomem_is_exclusive(res->start))
+	if (pci_resource_is_mem(pdev, barno) &&
+	    iomem_is_exclusive(pci_resource_start(pdev, barno)))
 		return -EINVAL;
 
 	if (!__pci_mmap_fits(pdev, barno, vma, sparse))
 		return -EINVAL;
 
-	pcibios_resource_to_bus(pdev->bus, &bar, res);
+	pcibios_resource_to_bus(pdev->bus, &bar, pci_resource_n(pdev, barno));
 	vma->vm_pgoff += bar.start >> (PAGE_SHIFT - (sparse ? 5 : 0));
-	mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
+	mmap_type = pci_resource_is_mem(pdev, barno) ? pci_mmap_mem : pci_mmap_io;
 
 	return hose_mmap_page_range(pdev->sysdata, vma, mmap_type, sparse);
 }
Signature · 1 line
2.53.0
Reviewed-by on v4 14/24
#25 lore raw

Re: [PATCH v4 14/24] alpha/PCI: Clean up pci_mmap_resource()

Quoted 32 lines · Krzysztof Wilczyński

Replace open-coded res->flags type checks with new pci_resource_is_mem() and pci_resource_start() helpers. Move the pci_resource_n() call into pcibios_resource_to_bus() and drop the local struct resource pointer.

---
 arch/alpha/kernel/pci-sysfs.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
arch/alpha/kernel/pci-sysfs.c +2 -2
index 7aac5e76dcd6..6c011dab326d 100644
@@ -70,7 +70,6 @@ static int pci_mmap_resource(struct kobject *kobj,
 {
 	struct pci_dev *pdev = to_pci_dev(kobj_to_dev(kobj));
 	int barno = (unsigned long)attr->private;
-	struct resource *res = pci_resource_n(pdev, barno);
 	enum pci_mmap_state mmap_type;
 	struct pci_bus_region bar;
 	int ret;
@@ -79,15 +78,16 @@ static int pci_mmap_resource(struct kobject *kobj,
 	if (ret)
 		return ret;
 
-	if ((res->flags & IORESOURCE_MEM) && iomem_is_exclusive(res->start))
+	if (pci_resource_is_mem(pdev, barno) &&
+	    iomem_is_exclusive(pci_resource_start(pdev, barno)))
 		return -EINVAL;
 
 	if (!__pci_mmap_fits(pdev, barno, vma, sparse))

arch/alpha/kernel/pci-sysfs.c static int pci_mmap_resource(struct kobject *kobj, · line 93

Quoted 10 lines
arch/alpha/kernel/pci-sysfs.c +2 -2
 		return -EINVAL;
 
-	pcibios_resource_to_bus(pdev->bus, &bar, res);
+	pcibios_resource_to_bus(pdev->bus, &bar, pci_resource_n(pdev, barno));
 	vma->vm_pgoff += bar.start >> (PAGE_SHIFT - (sparse ? 5 : 0));
-	mmap_type = res->flags & IORESOURCE_MEM ? pci_mmap_mem : pci_mmap_io;
+	mmap_type = pci_resource_is_mem(pdev, barno) ? pci_mmap_mem : pci_mmap_io;
 
 	return hose_mmap_page_range(pdev->sysdata, vma, mmap_type, sparse);
 }
Signature · 1 line
 i.
#26 lore raw

PATCH v4 15/24 alpha/PCI: Fix __pci_mmap_fits() overflow for zero-length BARs

In reply to Krzysztof Wilczyński

Currently, __pci_mmap_fits() computes the BAR size using pci_resource_len() - 1, which wraps to a large value when the BAR length is zero, causing the bounds check to incorrectly succeed.

Thus, add an early return for empty resources.

Fixes 10a0ef39fbd1 ("PCI/alpha: pci sysfs resources")
---
 arch/alpha/kernel/pci-sysfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/alpha/kernel/pci-sysfs.c +5 -1
index 6c011dab326d..2748000a7486 100644
@@ -37,12 +37,16 @@ static int hose_mmap_page_range(struct pci_controller *hose,
 static int __pci_mmap_fits(struct pci_dev *pdev, int num,
 			   struct vm_area_struct *vma, int sparse)
 {
+	resource_size_t len = pci_resource_len(pdev, num);
 	unsigned long nr, start, size;
 	int shift = sparse ? 5 : 0;
 
+	if (!len)
+		return 0;
+
 	nr = vma_pages(vma);
 	start = vma->vm_pgoff;
-	size = ((pci_resource_len(pdev, num) - 1) >> (PAGE_SHIFT - shift)) + 1;
+	size = ((len - 1) >> (PAGE_SHIFT - shift)) + 1;
 
 	if (start < size && size - start >= nr)
 		return 1;
Signature · 1 line
2.53.0
Reviewed-by on v4 15/24
#27 lore raw

Re: [PATCH v4 15/24] alpha/PCI: Fix __pci_mmap_fits() overflow for zero-length BARs

Quoted 26 lines · Krzysztof Wilczyński

Currently, __pci_mmap_fits() computes the BAR size using pci_resource_len() - 1, which wraps to a large value when the BAR length is zero, causing the bounds check to incorrectly succeed.

Thus, add an early return for empty resources.

Fixes 10a0ef39fbd1 ("PCI/alpha: pci sysfs resources")
---
 arch/alpha/kernel/pci-sysfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
arch/alpha/kernel/pci-sysfs.c +1 -0
index 6c011dab326d..2748000a7486 100644
@@ -37,12 +37,16 @@ static int hose_mmap_page_range(struct pci_controller *hose,
 static int __pci_mmap_fits(struct pci_dev *pdev, int num,
 			   struct vm_area_struct *vma, int sparse)
 {
+	resource_size_t len = pci_resource_len(pdev, num);
 	unsigned long nr, start, size;
 	int shift = sparse ? 5 : 0;
 

arch/alpha/kernel/pci-sysfs.c static int hose_mmap_page_range(struct pci_controller *hose, · line 52

Quoted 10 lines
arch/alpha/kernel/pci-sysfs.c +4 -1
+	if (!len)
+		return 0;
+
 	nr = vma_pages(vma);
 	start = vma->vm_pgoff;
-	size = ((pci_resource_len(pdev, num) - 1) >> (PAGE_SHIFT - shift)) + 1;
+	size = ((len - 1) >> (PAGE_SHIFT - shift)) + 1;
 
 	if (start < size && size - start >= nr)
 		return 1;
Signature · 1 line
 i.
#28 lore raw

PATCH v4 16/24 alpha/PCI: Remove WARN from __pci_mmap_fits()

In reply to Krzysztof Wilczyński

Remove the WARN() that fires when userspace attempts to mmap beyond the BAR bounds. The check still returns 0 to reject the mapping, but the warning is excessive for normal operation.

A similar warning was removed from the PCI core in the commit 3b519e4ea618 ("PCI: fix size checks for mmap() on /proc/bus/pci files").

---
 arch/alpha/kernel/pci-sysfs.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
arch/alpha/kernel/pci-sysfs.c +1 -7
index 2748000a7486..c136603ddf2c 100644
@@ -48,13 +48,7 @@ static int __pci_mmap_fits(struct pci_dev *pdev, int num,
 	start = vma->vm_pgoff;
 	size = ((len - 1) >> (PAGE_SHIFT - shift)) + 1;
 
-	if (start < size && size - start >= nr)
-		return 1;
-	WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on %s BAR %d "
-		"(size 0x%08lx)\n",
-		current->comm, sparse ? " sparse" : "", start, start + nr,
-		pci_name(pdev), num, size);
-	return 0;
+	return start < size && size - start >= nr;
 }
 
 /**
Signature · 1 line
2.53.0
Reviewed-by on v4 16/24
#29 lore raw

Re: [PATCH v4 16/24] alpha/PCI: Remove WARN from __pci_mmap_fits()

Quoted 22 lines · Krzysztof Wilczyński

Remove the WARN() that fires when userspace attempts to mmap beyond the BAR bounds. The check still returns 0 to reject the mapping, but the warning is excessive for normal operation.

A similar warning was removed from the PCI core in the commit 3b519e4ea618 ("PCI: fix size checks for mmap() on /proc/bus/pci files").

---
 arch/alpha/kernel/pci-sysfs.c | 8 +-------
 1 file changed, 1 insertion(+), 7 deletions(-)
arch/alpha/kernel/pci-sysfs.c +0 -1
index 2748000a7486..c136603ddf2c 100644
@@ -48,13 +48,7 @@ static int __pci_mmap_fits(struct pci_dev *pdev, int num,
 	start = vma->vm_pgoff;
 	size = ((len - 1) >> (PAGE_SHIFT - shift)) + 1;
 
-	if (start < size && size - start >= nr)

arch/alpha/kernel/pci-sysfs.c static int __pci_mmap_fits(struct pci_dev *pdev, int num, · line 54

Quoted 10 lines
arch/alpha/kernel/pci-sysfs.c +1 -6
-		return 1;
-	WARN(1, "process \"%s\" tried to map%s 0x%08lx-0x%08lx on %s BAR %d "
-		"(size 0x%08lx)\n",
-		current->comm, sparse ? " sparse" : "", start, start + nr,
-		pci_name(pdev), num, size);
-	return 0;
+	return start < size && size - start >= nr;
 }
 
 /**
Signature · 1 line
 i.
#30 lore raw

Re: [PATCH v4 16/24] alpha/PCI: Remove WARN from __pci_mmap_fits()

In reply to Krzysztof Wilczyński
Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 8 lines

Remove the WARN() that fires when userspace attempts to mmap beyond the BAR bounds. The check still returns 0 to reject the mapping, but the warning is excessive for normal operation.

A similar warning was removed from the PCI core in the commit 3b519e4ea618 ("PCI: fix size checks for mmap() on /proc/bus/pci files").

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
#31 lore raw

PATCH v4 17/24 alpha/PCI: Add static PCI resource attribute macros

In reply to Krzysztof Wilczyński

Add macros for declaring static binary attributes for Alpha's PCI resource files:

- pci_dev_resource_attr(), for dense/BWX systems (mmap dense)
- pci_dev_resource_sparse_attr(), for sparse systems (mmap sparse)
- pci_dev_resource_dense_attr(), for dense companion files (mmap dense)

Each macro creates a const bin_attribute with the BAR index stored in the .private property and the appropriate .mmap callback.

---
 arch/alpha/kernel/pci-sysfs.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/alpha/kernel/pci-sysfs.c +20 -0
index c136603ddf2c..824e514c4adb 100644
@@ -104,6 +104,26 @@ static int pci_mmap_resource_dense(struct file *filp, struct kobject *kobj,
 	return pci_mmap_resource(kobj, attr, vma, 0);
 }
 
+#define __pci_dev_resource_attr(_bar, _name, _suffix, _mmap)		\
+static const struct bin_attribute					\
+pci_dev_resource##_bar##_suffix##_attr = {				\
+	.attr = { .name = __stringify(_name), .mode = 0600 },		\
+	.private = (void *)(unsigned long)(_bar),			\
+	.mmap = (_mmap),						\
+}
+
+#define pci_dev_resource_attr(_bar)					\
+	__pci_dev_resource_attr(_bar, resource##_bar,,			\
+			    pci_mmap_resource_dense)
+
+#define pci_dev_resource_sparse_attr(_bar)				\
+	__pci_dev_resource_attr(_bar, resource##_bar##_sparse, _sparse,	\
+			    pci_mmap_resource_sparse)
+
+#define pci_dev_resource_dense_attr(_bar)				\
+	__pci_dev_resource_attr(_bar, resource##_bar##_dense, _dense,	\
+			    pci_mmap_resource_dense)
+
 /**
  * pci_remove_resource_files - cleanup resource files
  * @pdev: pci_dev to cleanup
Signature · 1 line
2.53.0
#32 lore raw

PATCH v4 18/24 alpha/PCI: Convert resource files to static attributes

In reply to Krzysztof Wilczyński

Currently, Alpha's PCI resource files (resourceN, resourceN_sparse, resourceN_dense) are dynamically created by pci_create_resource_files(), which overrides the generic __weak implementation. The current code allocates bin_attributes at runtime and manages them via the res_attr[] and res_attr_wc[] fields in struct pci_dev.

Thus, convert to static const attributes with three attribute groups (plain, sparse, dense), each with an .is_bin_visible callback that checks resource length, has_sparse(), and sparse_mem_mmap_fits(). A .bin_size callback provides the resource size to the kernfs node, with the sparse variant shifting by 5 bits for byte-level addressing.

Register the groups via ARCH_PCI_DEV_GROUPS so the driver model handles creation and removal automatically.

Use the new pci_resource_is_mem() helper for the type check, replacing the open-coded bitwise flag test.

Finally, remove pci_create_resource_files(), pci_remove_resource_files(), pci_create_attr(), and pci_create_one_attr() which are no longer needed.

---
 arch/alpha/include/asm/pci.h  |   9 ++
 arch/alpha/kernel/pci-sysfs.c | 291 +++++++++++++++++++---------------
 2 files changed, 172 insertions(+), 128 deletions(-)
arch/alpha/include/asm/pci.h +9 -0
index 6c04fcbdc8ed..ef19295f2e33 100644
@@ -88,4 +88,13 @@ extern void pci_adjust_legacy_attr(struct pci_bus *bus,
 				   enum pci_mmap_state mmap_type);
 #define HAVE_PCI_LEGACY	1
 
+extern const struct attribute_group pci_dev_resource_attr_group;
+extern const struct attribute_group pci_dev_resource_sparse_attr_group;
+extern const struct attribute_group pci_dev_resource_dense_attr_group;
+
+#define ARCH_PCI_DEV_GROUPS		\
+	&pci_dev_resource_attr_group,	\
+	&pci_dev_resource_sparse_attr_group,	\
+	&pci_dev_resource_dense_attr_group,
+
 #endif /* __ALPHA_PCI_H */
arch/alpha/kernel/pci-sysfs.c +163 -128

312 lines not on this page · raw message

Signature · 1 line
2.53.0
#33 lore raw

PATCH v4 19/24 PCI/sysfs: Remove pci_{create,remove}_sysfs_dev_files()

In reply to Krzysztof Wilczyński

Currently, pci_create_sysfs_dev_files() and pci_remove_sysfs_dev_files() are no-op stubs. With both the generic and Alpha resource files now handled by static attribute groups, no platform needs dynamic per-device sysfs file creation.

Thus, remove both functions, their declarations, and the call sites in pci_bus_add_device() and pci_stop_bus_device().

Remove __weak pci_create_resource_files() and pci_remove_resource_files() stubs and their declarations in pci.h, as no architecture overrides them anymore.

Remove the res_attr[] and res_attr_wc[] fields from struct pci_dev which were used to track dynamically allocated resource attributes.

Finally, simplify pci_sysfs_init() to only handle legacy file creation under HAVE_PCI_LEGACY, removing the per-device loop and the HAVE_PCI_SYSFS_INIT helper added earlier.

---
 drivers/pci/bus.c       |  1 -
 drivers/pci/pci-sysfs.c | 52 ++---------------------------------------
 drivers/pci/pci.h       |  4 ----
 drivers/pci/remove.c    |  1 -
 include/linux/pci.h     |  9 -------
 5 files changed, 2 insertions(+), 65 deletions(-)
drivers/pci/bus.c +0 -1
index 6c1ad1f542d9..655ed53436d3 100644
@@ -354,7 +354,6 @@ void pci_bus_add_device(struct pci_dev *dev)
 	pci_fixup_device(pci_fixup_final, dev);
 	if (pci_is_bridge(dev))
 		of_pci_make_dev_node(dev);
-	pci_create_sysfs_dev_files(dev);
 	pci_proc_attach_device(dev);
 	pci_bridge_d3_update(dev);
 
drivers/pci/pci-sysfs.c +2 -50

76 lines not on this page · raw message

drivers/pci/pci.h +0 -4
index 13d998fbacce..1bf165595583 100644
@@ -391,16 +391,12 @@ static inline int pci_no_d1d2(struct pci_dev *dev)
 }
 
 #ifdef CONFIG_SYSFS
-int pci_create_sysfs_dev_files(struct pci_dev *pdev);
-void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
 extern const struct attribute_group *pci_dev_groups[];
 extern const struct attribute_group *pci_dev_attr_groups[];
 extern const struct attribute_group *pcibus_groups[];
 extern const struct attribute_group *pci_bus_groups[];
 extern const struct attribute_group pci_doe_sysfs_group;
 #else
-static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
-static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
 #define pci_dev_groups NULL
 #define pci_dev_attr_groups NULL
 #define pcibus_groups NULL
drivers/pci/remove.c +0 -1
index e9d519993853..6e796dbc5b29 100644
@@ -26,7 +26,6 @@ static void pci_stop_dev(struct pci_dev *dev)
 
 	device_release_driver(&dev->dev);
 	pci_proc_detach_device(dev);
-	pci_remove_sysfs_dev_files(dev);
 	of_pci_remove_node(dev);
 }
 
include/linux/pci.h +0 -9
index 30aeb8e02b7b..f6f55005f82d 100644
@@ -507,10 +507,6 @@ struct pci_dev {
 	spinlock_t	pcie_cap_lock;		/* Protects RMW ops in capability accessors */
 	u32		saved_config_space[16]; /* Config space saved at suspend time */
 	struct hlist_head saved_cap_space;
-#if !defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
-	struct bin_attribute *res_attr[DEVICE_COUNT_RESOURCE]; /* sysfs file for resources */
-	struct bin_attribute *res_attr_wc[DEVICE_COUNT_RESOURCE]; /* sysfs file for WC mapping of resources */
-#endif
 
 #ifdef CONFIG_HOTPLUG_PCI_PCIE
 	unsigned int	broken_cmd_compl:1;	/* No compl for some cmds */
@@ -2528,11 +2524,6 @@ int pcibios_alloc_irq(struct pci_dev *dev);
 void pcibios_free_irq(struct pci_dev *dev);
 resource_size_t pcibios_default_alignment(void);
 
-#if !defined(HAVE_PCI_MMAP) && !defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
-extern int pci_create_resource_files(struct pci_dev *dev);
-extern void pci_remove_resource_files(struct pci_dev *dev);
-#endif
-
 #if defined(CONFIG_PCI_MMCONFIG) || defined(CONFIG_ACPI_MCFG)
 void __init pci_mmcfg_early_init(void);
 void __init pci_mmcfg_late_init(void);
Signature · 1 line
2.53.0
#34 lore raw

PATCH v4 20/24 PCI: Add macros for legacy I/O and memory address space sizes

In reply to Krzysztof Wilczyński

Add defines for the standard PCI legacy address space sizes, replacing the raw literals used by the legacy sysfs attributes.

---
 include/linux/pci.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/pci.h +5 -0
index f6f55005f82d..6b630bac8c08 100644
@@ -27,6 +27,7 @@
 #include <linux/mod_devicetable.h>
 
 #include <linux/types.h>
+#include <linux/sizes.h>
 #include <linux/init.h>
 #include <linux/ioport.h>
 #include <linux/list.h>
@@ -1167,6 +1168,10 @@ enum {
 /* These external functions are only available when PCI support is enabled */
 #ifdef CONFIG_PCI
 
+/* PCI legacy I/O port and memory address space sizes. */
+#define PCI_LEGACY_IO_SIZE	(SZ_64K - 1)
+#define PCI_LEGACY_MEM_SIZE	SZ_1M
+
 extern unsigned int pci_flags;
 
 static inline void pci_set_flags(int flags) { pci_flags = flags; }
Signature · 1 line
2.53.0
#35 lore raw

Re: [PATCH v4 20/24] PCI: Add macros for legacy I/O and memory address space sizes

Quoted 23 lines · Krzysztof Wilczyński

Add defines for the standard PCI legacy address space sizes, replacing the raw literals used by the legacy sysfs attributes.

---
 include/linux/pci.h | 5 +++++
 1 file changed, 5 insertions(+)
include/linux/pci.h +1 -0
index f6f55005f82d..6b630bac8c08 100644
@@ -27,6 +27,7 @@
 #include <linux/mod_devicetable.h>
 
 #include <linux/types.h>
+#include <linux/sizes.h>
 #include <linux/init.h>
 #include <linux/ioport.h>
 #include <linux/list.h>

include/linux/pci.h enum { · line 1177

Quoted 10 lines
include/linux/pci.h +4 -0
@@ -1167,6 +1168,10 @@ enum {
 /* These external functions are only available when PCI support is enabled */
 #ifdef CONFIG_PCI
 
+/* PCI legacy I/O port and memory address space sizes. */
+#define PCI_LEGACY_IO_SIZE	(SZ_64K - 1)
+#define PCI_LEGACY_MEM_SIZE	SZ_1M
+
 extern unsigned int pci_flags;
 
 static inline void pci_set_flags(int flags) { pci_flags = flags; }

Shouldn't this also convert the use in pci-sysfs.c (which you now mixed into the static attrs conversion patch)?

Signature · 1 line
 i.
#36 lore raw

Re: [PATCH v4 20/24] PCI: Add macros for legacy I/O and memory address space sizes

Hello,

Quoted 11 lines

include/linux/pci.h enum { · line 1177

Quoted 8 lines
include/linux/pci.h +4 -0
+/* PCI legacy I/O port and memory address space sizes. */
+#define PCI_LEGACY_IO_SIZE	(SZ_64K - 1)
+#define PCI_LEGACY_MEM_SIZE	SZ_1M
+
 extern unsigned int pci_flags;
 
 static inline void pci_set_flags(int flags) { pci_flags = flags; }

Shouldn't this also convert the use in pci-sysfs.c (which you now mixed into the static attrs conversion patch)?

I took care of this in v5.

Thank you!

Krzysztof

#37 lore raw

PATCH v4 21/24 alpha/PCI: Compute legacy size in pci_mmap_legacy_page_range()

In reply to Krzysztof Wilczyński

Currently, pci_mmap_legacy_page_range() reads the legacy resource size from bus->legacy_mem->size or bus->legacy_io->size. This couples the mmap bounds check to the struct pci_bus fields that will be removed when legacy attributes are converted to static definitions.

Compute the size directly using PCI_LEGACY_MEM_SIZE (0x100000) and PCI_LEGACY_IO_SIZE (0xffff) macros, and shift by 5 bits for sparse systems.

---
 arch/alpha/kernel/pci-sysfs.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)
arch/alpha/kernel/pci-sysfs.c +5 -2
index 2e9693e5abe1..a9dea147528d 100644
@@ -180,8 +180,11 @@ int pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
 	int sparse = has_sparse(hose, mmap_type);
 	unsigned long res_size;
 
-	res_size = (mmap_type == pci_mmap_mem) ? bus->legacy_mem->size :
-						 bus->legacy_io->size;
+	res_size = (mmap_type == pci_mmap_mem) ? PCI_LEGACY_MEM_SIZE : \
+						 PCI_LEGACY_IO_SIZE;
+	if (sparse)
+		res_size <<= 5;
+
 	if (!__legacy_mmap_fits(hose, vma, res_size, sparse))
 		return -EINVAL;
 
Signature · 1 line
2.53.0
#38 lore raw

PATCH v4 22/24 PCI/sysfs: Add __weak pci_legacy_has_sparse() helper

In reply to Krzysztof Wilczyński

Currently, Alpha's sparse/dense legacy attribute handling is done via pci_adjust_legacy_attr(), which updates dynamically allocated attributes at runtime. The upcoming conversion to static attributes needs a way to determine sparse support at visibility check time.

Add a __weak pci_legacy_has_sparse() that returns false by default. Alpha overrides it to check has_sparse() on the bus host controller.

---
 arch/alpha/include/asm/pci.h  | 2 ++
 arch/alpha/kernel/pci-sysfs.c | 7 +++++++
 drivers/pci/pci-sysfs.c       | 6 ++++++
 drivers/pci/pci.h             | 4 ++++
 4 files changed, 19 insertions(+)
arch/alpha/include/asm/pci.h +2 -0
index ef19295f2e33..95de7ffd59e8 100644
@@ -86,6 +86,8 @@ extern int pci_mmap_legacy_page_range(struct pci_bus *bus,
 				      enum pci_mmap_state mmap_state);
 extern void pci_adjust_legacy_attr(struct pci_bus *bus,
 				   enum pci_mmap_state mmap_type);
+extern bool pci_legacy_has_sparse(struct pci_bus *bus,
+				  enum pci_mmap_state type);
 #define HAVE_PCI_LEGACY	1
 
 extern const struct attribute_group pci_dev_resource_attr_group;
arch/alpha/kernel/pci-sysfs.c +7 -0
index a9dea147528d..ea51f6b20458 100644
@@ -191,6 +191,13 @@ int pci_mmap_legacy_page_range(struct pci_bus *bus, struct vm_area_struct *vma,
 	return hose_mmap_page_range(hose, vma, mmap_type, sparse);
 }
 
+bool pci_legacy_has_sparse(struct pci_bus *bus, enum pci_mmap_state type)
+{
+	struct pci_controller *hose = bus->sysdata;
+
+	return has_sparse(hose, type);
+}
+
 /**
  * pci_adjust_legacy_attr - adjustment of legacy file attributes
  * @bus: bus to create files under
drivers/pci/pci-sysfs.c +6 -0
index 3a7bbb00ca60..ce004b349594 100644
@@ -1019,6 +1019,12 @@ static int pci_mmap_legacy_io(struct file *filp, struct kobject *kobj,
 	return pci_mmap_legacy_page_range(bus, vma, pci_mmap_io);
 }
 
+bool __weak pci_legacy_has_sparse(struct pci_bus *bus,
+				  enum pci_mmap_state type)
+{
+	return false;
+}
+
 /**
  * pci_adjust_legacy_attr - adjustment of legacy file attributes
  * @b: bus to create files under
drivers/pci/pci.h +4 -0
index 1bf165595583..f52caf7e4d13 100644
@@ -390,6 +390,10 @@ static inline int pci_no_d1d2(struct pci_dev *dev)
 
 }
 
+#ifdef HAVE_PCI_LEGACY
+bool pci_legacy_has_sparse(struct pci_bus *bus, enum pci_mmap_state type);
+#endif
+
 #ifdef CONFIG_SYSFS
 extern const struct attribute_group *pci_dev_groups[];
 extern const struct attribute_group *pci_dev_attr_groups[];
Signature · 1 line
2.53.0
#39 lore raw

PATCH v4 23/24 PCI/sysfs: Convert legacy I/O and memory attributes to static definitions

In reply to Krzysztof Wilczyński

Currently, legacy_io and legacy_mem are dynamically allocated and created by pci_create_legacy_files(), with pci_adjust_legacy_attr() updating the attributes at runtime on Alpha to rename them and shift the size for sparse addressing.

Convert to four static const attributes (legacy_io, legacy_io_sparse, legacy_mem, legacy_mem_sparse) with is_bin_visible() callbacks that use pci_legacy_has_sparse() to select the appropriate variant per bus. The sizes are compile-time constants and .size is set directly on each attribute.

Register the groups in pcibus_groups[] under a HAVE_PCI_LEGACY guard so the driver model handles creation and removal automatically.

Stub out pci_create_legacy_files() and pci_remove_legacy_files() as the dynamic creation is no longer needed. Remove the __weak pci_adjust_legacy_attr(), Alpha's override, and its declaration from both Alpha and PowerPC asm/pci.h headers.

---
 arch/alpha/include/asm/pci.h   |   2 -
 arch/alpha/kernel/pci-sysfs.c  |  24 ----
 arch/powerpc/include/asm/pci.h |   2 -
 drivers/pci/pci-sysfs.c        | 200 ++++++++++++++++++++-------------
 4 files changed, 120 insertions(+), 108 deletions(-)
arch/alpha/include/asm/pci.h +0 -2
index 95de7ffd59e8..ad5d1391e1fa 100644
@@ -84,8 +84,6 @@ extern int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val,
 extern int pci_mmap_legacy_page_range(struct pci_bus *bus,
 				      struct vm_area_struct *vma,
 				      enum pci_mmap_state mmap_state);
-extern void pci_adjust_legacy_attr(struct pci_bus *bus,
-				   enum pci_mmap_state mmap_type);
 extern bool pci_legacy_has_sparse(struct pci_bus *bus,
 				  enum pci_mmap_state type);
 #define HAVE_PCI_LEGACY	1
arch/alpha/kernel/pci-sysfs.c +0 -24
index ea51f6b20458..7f052e05cbae 100644
@@ -198,30 +198,6 @@ bool pci_legacy_has_sparse(struct pci_bus *bus, enum pci_mmap_state type)
 	return has_sparse(hose, type);
 }
 
-/**
- * pci_adjust_legacy_attr - adjustment of legacy file attributes
- * @bus: bus to create files under
- * @mmap_type: I/O port or memory
- *
- * Adjust file name and size for sparse mappings.
- */
-void pci_adjust_legacy_attr(struct pci_bus *bus, enum pci_mmap_state mmap_type)
-{
-	struct pci_controller *hose = bus->sysdata;
-
-	if (!has_sparse(hose, mmap_type))
-		return;
-
-	if (mmap_type == pci_mmap_mem) {
-		bus->legacy_mem->attr.name = "legacy_mem_sparse";
-		bus->legacy_mem->size <<= 5;
-	} else {
-		bus->legacy_io->attr.name = "legacy_io_sparse";
-		bus->legacy_io->size <<= 5;
-	}
-	return;
-}
-
 /* Legacy I/O bus read/write functions */
 int pci_legacy_read(struct pci_bus *bus, loff_t port, u32 *val, size_t size)
 {
arch/powerpc/include/asm/pci.h +0 -2
index 46a9c4491ed0..72f286e74786 100644
@@ -82,8 +82,6 @@ extern int pci_legacy_write(struct pci_bus *bus, loff_t port, u32 val,
 extern int pci_mmap_legacy_page_range(struct pci_bus *bus,
 				      struct vm_area_struct *vma,
 				      enum pci_mmap_state mmap_state);
-extern void pci_adjust_legacy_attr(struct pci_bus *bus,
-				   enum pci_mmap_state mmap_type);
 #define HAVE_PCI_LEGACY	1
 
 extern void pcibios_claim_one_bus(struct pci_bus *b);
drivers/pci/pci-sysfs.c +120 -80

222 lines not on this page · raw message

Signature · 1 line
2.53.0
#40 lore raw

PATCH v4 24/24 PCI/sysfs: Remove pci_create_legacy_files() and pci_sysfs_init()

In reply to Krzysztof Wilczyński

Currently, pci_create_legacy_files() and pci_remove_legacy_files() are no-op stubs. With legacy attributes now handled by static groups registered via pcibus_groups[], no call site needs them.

Remove both functions, their declarations, and the call sites in pci_register_host_bridge(), pci_alloc_child_bus(), and pci_remove_bus().

Remove the pci_sysfs_init() late_initcall and sysfs_initialized. The late_initcall originally existed to create all the dynamic PCI sysfs files, but with both resource and legacy attributes now handled by static groups, it is no longer needed.

Remove the legacy_io and legacy_mem fields from struct pci_bus which were used to track the dynamically allocated legacy attributes.

---
 drivers/pci/pci-sysfs.c | 21 ---------------------
 drivers/pci/pci.h       |  8 --------
 drivers/pci/probe.c     |  6 ------
 drivers/pci/remove.c    |  2 --
 include/linux/pci.h     |  2 --
 5 files changed, 39 deletions(-)
drivers/pci/pci-sysfs.c +0 -21
index c81d3964cfa4..b167e32d55ac 100644
@@ -37,10 +37,6 @@
 #define ARCH_PCI_DEV_GROUPS
 #endif
 
-#ifdef HAVE_PCI_LEGACY
-static int sysfs_initialized;	/* = 0 */
-#endif
-
 /* show configuration fields */
 #define pci_config_attr(field, format_string)				\
 static ssize_t								\
@@ -1135,8 +1131,6 @@ static const struct attribute_group pci_legacy_mem_sparse_group = {
 	.is_bin_visible = pci_legacy_mem_sparse_is_visible,
 };
 
-void pci_create_legacy_files(struct pci_bus *b) { }
-void pci_remove_legacy_files(struct pci_bus *b) { }
 #endif /* HAVE_PCI_LEGACY */
 
 const struct attribute_group *pcibus_groups[] = {
@@ -1811,21 +1805,6 @@ static const struct attribute_group pci_dev_resource_resize_attr_group = {
 	.is_visible = resource_resize_attr_is_visible,
 };
 
-#ifdef HAVE_PCI_LEGACY
-static int __init pci_sysfs_init(void)
-{
-	struct pci_bus *pbus = NULL;
-
-	sysfs_initialized = 1;
-
-	while ((pbus = pci_find_next_bus(pbus)))
-		pci_create_legacy_files(pbus);
-
-	return 0;
-}
-late_initcall(pci_sysfs_init);
-#endif
-
 static struct attribute *pci_dev_dev_attrs[] = {
 	&dev_attr_boot_vga.attr,
 	&dev_attr_serial_number.attr,
drivers/pci/pci.h +0 -8
index f52caf7e4d13..63f249d4833a 100644
@@ -356,14 +356,6 @@ static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
 int pci_hp_add_bridge(struct pci_dev *dev);
 bool pci_hp_spurious_link_change(struct pci_dev *pdev);
 
-#if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
-void pci_create_legacy_files(struct pci_bus *bus);
-void pci_remove_legacy_files(struct pci_bus *bus);
-#else
-static inline void pci_create_legacy_files(struct pci_bus *bus) { }
-static inline void pci_remove_legacy_files(struct pci_bus *bus) { }
-#endif
-
 /* Lock for read/write access to pci device and bus lists */
 extern struct rw_semaphore pci_bus_sem;
 extern struct mutex pci_slot_mutex;
drivers/pci/probe.c +0 -6
index bccc7a4bdd79..94c52bf3a65c 100644
@@ -1068,9 +1068,6 @@ static int pci_register_host_bridge(struct pci_host_bridge *bridge)
 			dev_err(&bus->dev, "failed to add bus: %d\n", err);
 	}
 
-	/* Create legacy_io and legacy_mem files for this bus */
-	pci_create_legacy_files(bus);
-
 	if (parent)
 		dev_info(parent, "PCI host bridge to bus %s\n", name);
 	else
@@ -1276,9 +1273,6 @@ static struct pci_bus *pci_alloc_child_bus(struct pci_bus *parent,
 			dev_err(&child->dev, "failed to add bus: %d\n", ret);
 	}
 
-	/* Create legacy_io and legacy_mem files for this bus */
-	pci_create_legacy_files(child);
-
 	return child;
 }
 
drivers/pci/remove.c +0 -2
index 6e796dbc5b29..d8bffa21498a 100644
@@ -65,8 +65,6 @@ void pci_remove_bus(struct pci_bus *bus)
 	list_del(&bus->node);
 	pci_bus_release_busn_res(bus);
 	up_write(&pci_bus_sem);
-	pci_remove_legacy_files(bus);
-
 	if (bus->ops->remove_bus)
 		bus->ops->remove_bus(bus);
 
include/linux/pci.h +0 -2
index 6b630bac8c08..1859c0bf4e35 100644
@@ -724,8 +724,6 @@ struct pci_bus {
 	pci_bus_flags_t bus_flags;	/* Inherited by child buses */
 	struct device		*bridge;
 	struct device		dev;
-	struct bin_attribute	*legacy_io;	/* Legacy I/O for this bus */
-	struct bin_attribute	*legacy_mem;	/* Legacy mem */
 	unsigned int		is_added:1;
 	unsigned int		unsafe_warn:1;	/* warned about RW1C config write */
 	unsigned int		flit_mode:1;	/* Link in Flit mode */
Signature · 1 line
2.53.0
maintainer Tested-by Acked-by on the v4 series
#41 lore raw
In reply to Krzysztof Wilczyński
Quoted 207 lines · Krzysztof Wilczyński

Hello,

This series converts every dynamically allocated PCI sysfs attribute to a static const definition. After the full series, pci_sysfs_init() and sysfs_initialized are gone, and every sysfs file is created by the driver model at device_add() time.

Currently, the PCI resource files (resourceN, resourceN_wc) and the legacy bus files (legacy_io, legacy_mem) are created dynamically from two unsynchronised paths:

Path A: late_initcall

pci_sysfs_init (late_initcall)
sysfs_initialized = 1
for_each_pci_dev
pci_create_sysfs_dev_files
sysfs_create_bin_file (resourceN, resourceN_wc)
pci_find_next_bus
pci_create_legacy_files
sysfs_create_bin_file (legacy_io, legacy_mem)

Path B: device registration / hotplug

pci_bus_add_devices
pci_bus_add_device
pci_create_sysfs_dev_files
if (!sysfs_initialized) return <- only guard
sysfs_create_bin_file (resourceN, resourceN_wc)

On most ACPI systems this does not race because PCI enumeration completes at subsys_initcall time, before pci_sysfs_init() runs:

subsys_initcall (level 4):
acpi_pci_root_add
pci_bus_add_device
pci_create_sysfs_dev_files
if (!sysfs_initialized) <- not yet set
return -EACCES

late_initcall (level 7):
pci_sysfs_init
sysfs_initialized = 1
for_each_pci_dev
pci_create_sysfs_dev_files <- creates the files, no race

On Devicetree platforms the host controller is a platform driver that probes via the driver model, often on a workqueue, and overlaps with the late_initcall:

CPU 0 (late_initcall) CPU 1 (driver probe)
--------------------------- ----------------------------
pci_sysfs_init()
sysfs_initialized = 1
for_each_pci_dev(pdev) pci_bus_add_device(pdev)
pci_create_sysfs_dev_files() pci_create_sysfs_dev_files()
sysfs_create_bin_file() sysfs_create_bin_file()
-> "duplicate filename"

The same happens on ACPI when probing is asynchronous (hv_pci on Azure, RISC-V with ACPI).

The duplicate causes sysfs_create_bin_file() to fail with -EEXIST. pci_create_resource_files() then calls pci_remove_resource_files() in its error unwind, tearing down files the other thread created and still references through pdev->res_attr[]. This has caused kernel panics on i.MX6 and boot failures on other platforms.

Several different fixes have been proposed over the years: reordering the sysfs_initialized assignment, adding locks, checking pci_dev_is_added(), setting pdev->res_attr[] to NULL after kfree (which only prevents a double-free on the teardown path, not the error unwind removing the other thread's files). None would address the root cause.

This has been reported a few times:

- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/SY0P300MB04687548090B73E40AF97D8897B82@SY0P300MB0468.AUSP300.PROD.OUTLOOK.COM/
- https://lore.kernel.org/linux-pci/20230105174736.GA1154719@bhelgaas/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://lore.kernel.org/linux-pci/20200716110423.xtfyb3n6tn5ixedh@pali/
- https://lore.kernel.org/linux-pci/[email protected]/
- https://bugzilla.kernel.org/show_bug.cgi?id=215515
- https://bugzilla.kernel.org/show_bug.cgi?id=216888

With static attributes the driver model creates sysfs entries once per device at device_add() time, under the device lock, eliminating the late_initcall iteration and the race along with it.

Krzysztof

---
Changes in v4:
https://lore.kernel.org/linux-pci/[email protected]/

- Added new Reviewed-by tags.
- Added pci_resource_is_io() and pci_resource_is_mem() helpers for resource type checks, replacing the open-coded bitwise flag tests in pci_mmap_resource(), pci_resource_io(), and Alpha's pci_mmap_resource(), as per Ilpo Järvinen's suggestion.
- Split the __pci_mmap_fits() cleanup into two patches. An overflow fix for zero-length BARs, which now includes a
Fixes: tag referencing the original Alpha PCI sysfs commit, and the WARN macro removal is a separate cleanup as per Ilpo Järvinen's suggestion.
- Added a missing Fixes: tag to the Alpha lockdown check, referencing the commit that added the check to the generic path but missed Alpha's implementation.
- Added PCI_LEGACY_IO_SIZE and PCI_LEGACY_MEM_SIZE macros to replace the raw literals used for legacy address space sizes. These are used in both Alpha's pci_mmap_legacy_page_range() and the static legacy attribute definitions, as per Ilpo Järvinen's suggestion.
- Replaced sysfs_update_groups() in the BAR resize path with sysfs_remove_groups() before the resize and sysfs_create_groups() after, restoring the original teardown before BAR resize ordering. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org?part=7
- Defined pci_dev_resource_attr_groups as a NULL macro when HAVE_PCI_MMAP and ARCH_GENERIC_PCI_MMAP_RESOURCE are both absent, so the resize path compiles unconditionally without #ifdef guards in the function body. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org?part=7
- Moved the pci_legacy_has_sparse() prototype into the patch that introduces the function, alongside the existing pci_adjust_legacy_attr() declaration, to fix a bisection issue where Alpha would warn on -Wmissing-prototypes. This was reported by Sashiko, see:
https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org?part=18

Changes in v3:
https://lore.kernel.org/linux-pci/[email protected]/

- Updated for modern kernel releases and expanded scope. The v2 only covered the generic resource files. This version also converts Alpha's sparse/dense resource files and the legacy bus attributes, removing pci_sysfs_init() entirely.
- Split the single macro definition into three distinct ones (per I/O, UC, and WC), to make sure that each carries only the callbacks its resource type needs.
- Updated to use the new .bin_size callback, as the attributes are const, to replace using a->size directly, which was not ideal. This required changes to pci_llseek_resource(), to ensure that it would work for device and bus-level attributes.
- Updated the __resource_resize_store() to include CAP_SYS_ADMIN capabilities check.
- Added the security_locked_down() check to Alpha's pci_mmap_resource(), to align with other architectures.

Changes in v2:
https://lore.kernel.org/linux-pci/[email protected]/

- Refactored code so that the macros, helpers and internal functions can be used to correctly leverage the read(), write() and mmap() callbacks rather than to use the .is_bin_visible() callback to set up sysfs objects internals as this is not supported.
- Refactored some if-statements to check for a resource flag first, and then call either arch_can_pci_mmap_io() or arch_can_pci_mmap_wc(), plus store result of testing for IORESOURCE_MEM and IORESOURCE_PREFETCH flags into a boolean variable, as per Bjorn Helgaas' suggestion.
- Renamed pci_read_resource_io() and pci_write_resource_io() callbacks so that these are not specifically tied to I/O BARs read() and write() operations also as per Bjorn Helgaas' suggestion.
- Updated style for code handling bitwise operations to match the style that is preferred as per Bjorn Helgaas' suggestion.
- Updated commit messages adding more details about the implementation as requested by Bjorn Helgaas.

Krzysztof Wilczyński (24):
PCI/sysfs: Use PCI resource accessor macros
PCI: Add pci_resource_is_io() and pci_resource_is_mem() helpers PCI/sysfs: Only allow supported resource types in I/O and MMIO helpers PCI/sysfs: Use BAR length in pci_llseek_resource() when attr->size is
zero
PCI/sysfs: Add CAP_SYS_ADMIN check to __resource_resize_store() PCI/sysfs: Add static PCI resource attribute macros PCI/sysfs: Convert PCI resource files to static attributes PCI/sysfs: Convert __resource_resize_store() to use static attributes PCI/sysfs: Add stubs for pci_{create,remove}_sysfs_dev_files() PCI/sysfs: Limit pci_sysfs_init() late_initcall compile scope alpha/PCI: Add security_locked_down() check to pci_mmap_resource() alpha/PCI: Use BAR index in sysfs attr->private instead of resource
pointer
alpha/PCI: Use PCI resource accessor macros
alpha/PCI: Clean up pci_mmap_resource()
alpha/PCI: Fix __pci_mmap_fits() overflow for zero-length BARs alpha/PCI: Remove WARN from __pci_mmap_fits()
alpha/PCI: Add static PCI resource attribute macros alpha/PCI: Convert resource files to static attributes PCI/sysfs: Remove pci_{create,remove}_sysfs_dev_files()
PCI: Add macros for legacy I/O and memory address space sizes alpha/PCI: Compute legacy size in pci_mmap_legacy_page_range() PCI/sysfs: Add __weak pci_legacy_has_sparse() helper PCI/sysfs: Convert legacy I/O and memory attributes to static
definitions
PCI/sysfs: Remove pci_create_legacy_files() and pci_sysfs_init()

Quoted 11 lines
 arch/alpha/include/asm/pci.h   |  13 +-
 arch/alpha/kernel/pci-sysfs.c  | 373 +++++++++++----------
 arch/powerpc/include/asm/pci.h |   2 -
 drivers/pci/bus.c              |   1 -
 drivers/pci/pci-sysfs.c        | 575 +++++++++++++++++++--------------
 drivers/pci/pci.h              |  16 +-
 drivers/pci/probe.c            |   6 -
 drivers/pci/remove.c           |   3 -
 include/linux/pci.h            |  39 ++-
 9 files changed, 578 insertions(+), 450 deletions(-)

Hi ,

Applied the series on an Alpha UP2000+, built and booted successfully. PCI enumeration and device initialization looked normal, I saw no relevant sysfs/PCI warnings in dmesg, and PCI sysfs resource files for tested devices looked sane.

From the Alpha side, this looks good to me.

#42 lore raw

Hello,

Quoted 4 lines
Quoted 4 lines

This series converts every dynamically allocated PCI sysfs attribute to a static const definition. After the full series, pci_sysfs_init() and sysfs_initialized are gone, and every sysfs file is created by the driver model at device_add() time.

[...]

Quoted 9 lines

Applied the series on an Alpha UP2000+, built and booted successfully. PCI enumeration and device initialization looked normal, I saw no relevant sysfs/PCI warnings in dmesg, and PCI sysfs resource files for tested devices looked sane.

Quoted 1 line

From the Alpha side, this looks good to me.

I appreciate you taking the time to test this.

Thank you!

Krzysztof

#43 lore raw
In reply to Krzysztof Wilczyński

Hello,

Quoted 4 lines

This series converts every dynamically allocated PCI sysfs attribute to a static const definition. After the full series, pci_sysfs_init() and sysfs_initialized are gone, and every sysfs file is created by the driver model at device_add() time.

A note on testing:

0-day bot (recent test runs; newer builds will arrive later):
- https://lore.kernel.org/linux-pci/[email protected]
- https://lore.kernel.org/linux-pci/[email protected]
- https://lore.kernel.org/linux-pci/[email protected]
- https://lore.kernel.org/linux-pci/[email protected]
- https://lore.kernel.org/linux-pci/[email protected]
- https://lore.kernel.org/linux-pci/[email protected]

KernelCI (for the "for-kernelci" branch):
- https://dashboard.kernelci.org/tree/linux-pci/for-kernelci/209e2cfd205a8aad4bae32e6f82b96b20902aa74
- https://dashboard.kernelci.org/tree/linux-pci/for-kernelci/70293477e2c0ae8cbc250098818e726e1d658b53
- https://dashboard.kernelci.org/tree?ts=pci

Sashiko's feedback:
- https://sashiko.dev/#/patchset/20260411080148.471335-1-kwilczynski%40kernel.org
- https://sashiko.dev/#/patchset/20260410055040.39233-1-kwilczynski%40kernel.org

I sadly do not own any Alpha or PowerPC hardware, so when I was testing these architectures while working on the series, it would be only under QEMU.

That said, Magnus Lindholm was able to test the series on the Alpha hardware he owns, see:

- https://lore.kernel.org/linux-pci/CA+=Fv5Q1tZQwnanw99NbvzT-QenfYz7vUdY02_TuPqHX32ZAiA@mail.gmail.com

Lorenzo Pieralisi did some testing reported outside the mailing list (we talked on IRC), on the platform he had issues before, and while the issues were more with procfs races, similar to the sysfs ones this series aims to fix, he didn't notice regressions when having this series applied.

Thank you!

Krzysztof