[PATCH v4 08/24] PCI/sysfs: Convert __resource_resize_store() to use static attributes

Krzysztof Wilczyński
2026-04-11T08:02:21Z

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.

Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
---
drivers/pci/pci-sysfs.c | 25 ++++++++++++++++++-------
1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index 2922905ff070..c0925bdc9ecd 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -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,
};
--
2.53.0