Skip to content

Commit be9c3a4

Browse files
l1kbjorn-helgaas
authored andcommitted
PCI/sysfs: Compile pci-sysfs.c only if CONFIG_SYSFS=y
It is possible to enable CONFIG_PCI but disable CONFIG_SYSFS and for space-constrained devices such as routers, such a configuration may actually make sense. However pci-sysfs.c is compiled even if CONFIG_SYSFS is disabled, unnecessarily increasing the kernel's size. To rectify that: * Move pci_mmap_fits() to mmap.c. It is not only needed by pci-sysfs.c, but also proc.c. * Move pci_dev_type to probe.c and make it private. It references pci_dev_attr_groups in pci-sysfs.c. Make that public instead for consistency with pci_dev_groups, pcibus_groups and pci_bus_groups, which are likewise public and referenced by struct definitions in pci-driver.c and probe.c. * Define pci_dev_groups, pci_dev_attr_groups, pcibus_groups and pci_bus_groups to NULL if CONFIG_SYSFS is disabled. Provide empty static inlines for pci_{create,remove}_legacy_files() and pci_{create,remove}_sysfs_dev_files(). Result: vmlinux size is reduced by 122996 bytes in my arm 32-bit test build. Link: https://lore.kernel.org/r/85ca95ae8e4d57ccf082c5c069b8b21eb141846e.1698668982.git.lukas@wunner.de Signed-off-by: Lukas Wunner <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]> Reviewed-by: Alistair Francis <[email protected]>
1 parent 6613476 commit be9c3a4

File tree

5 files changed

+50
-34
lines changed

5 files changed

+50
-34
lines changed

drivers/pci/Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
obj-$(CONFIG_PCI) += access.o bus.o probe.o host-bridge.o \
66
remove.o pci.o pci-driver.o search.o \
7-
pci-sysfs.o rom.o setup-res.o irq.o vpd.o \
7+
rom.o setup-res.o irq.o vpd.o \
88
setup-bus.o vc.o mmap.o setup-irq.o
99

1010
obj-$(CONFIG_PCI) += msi/
1111
obj-$(CONFIG_PCI) += pcie/
1212

1313
ifdef CONFIG_PCI
1414
obj-$(CONFIG_PROC_FS) += proc.o
15-
obj-$(CONFIG_SYSFS) += slot.o
15+
obj-$(CONFIG_SYSFS) += pci-sysfs.o slot.o
1616
obj-$(CONFIG_ACPI) += pci-acpi.o
1717
endif
1818

drivers/pci/mmap.c

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
#include <linux/mm.h>
1212
#include <linux/pci.h>
1313

14+
#include "pci.h"
15+
1416
#ifdef ARCH_GENERIC_PCI_MMAP_RESOURCE
1517

1618
static const struct vm_operations_struct pci_phys_vm_ops = {
@@ -50,3 +52,30 @@ int pci_mmap_resource_range(struct pci_dev *pdev, int bar,
5052
}
5153

5254
#endif
55+
56+
#if (defined(CONFIG_SYSFS) || defined(CONFIG_PROC_FS)) && \
57+
(defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE))
58+
59+
int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
60+
enum pci_mmap_api mmap_api)
61+
{
62+
resource_size_t pci_start = 0, pci_end;
63+
unsigned long nr, start, size;
64+
65+
if (pci_resource_len(pdev, resno) == 0)
66+
return 0;
67+
nr = vma_pages(vma);
68+
start = vma->vm_pgoff;
69+
size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
70+
if (mmap_api == PCI_MMAP_PROCFS) {
71+
pci_resource_to_user(pdev, resno, &pdev->resource[resno],
72+
&pci_start, &pci_end);
73+
pci_start >>= PAGE_SHIFT;
74+
}
75+
if (start >= pci_start && start < pci_start + size &&
76+
start + nr <= pci_start + size)
77+
return 1;
78+
return 0;
79+
}
80+
81+
#endif

drivers/pci/pci-sysfs.c

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1022,29 +1022,6 @@ void pci_remove_legacy_files(struct pci_bus *b)
10221022
#endif /* HAVE_PCI_LEGACY */
10231023

10241024
#if defined(HAVE_PCI_MMAP) || defined(ARCH_GENERIC_PCI_MMAP_RESOURCE)
1025-
1026-
int pci_mmap_fits(struct pci_dev *pdev, int resno, struct vm_area_struct *vma,
1027-
enum pci_mmap_api mmap_api)
1028-
{
1029-
unsigned long nr, start, size;
1030-
resource_size_t pci_start = 0, pci_end;
1031-
1032-
if (pci_resource_len(pdev, resno) == 0)
1033-
return 0;
1034-
nr = vma_pages(vma);
1035-
start = vma->vm_pgoff;
1036-
size = ((pci_resource_len(pdev, resno) - 1) >> PAGE_SHIFT) + 1;
1037-
if (mmap_api == PCI_MMAP_PROCFS) {
1038-
pci_resource_to_user(pdev, resno, &pdev->resource[resno],
1039-
&pci_start, &pci_end);
1040-
pci_start >>= PAGE_SHIFT;
1041-
}
1042-
if (start >= pci_start && start < pci_start + size &&
1043-
start + nr <= pci_start + size)
1044-
return 1;
1045-
return 0;
1046-
}
1047-
10481025
/**
10491026
* pci_mmap_resource - map a PCI resource into user memory space
10501027
* @kobj: kobject for mapping
@@ -1660,7 +1637,7 @@ static const struct attribute_group pcie_dev_attr_group = {
16601637
.is_visible = pcie_dev_attrs_are_visible,
16611638
};
16621639

1663-
static const struct attribute_group *pci_dev_attr_groups[] = {
1640+
const struct attribute_group *pci_dev_attr_groups[] = {
16641641
&pci_dev_attr_group,
16651642
&pci_dev_hp_attr_group,
16661643
#ifdef CONFIG_PCI_IOV
@@ -1677,7 +1654,3 @@ static const struct attribute_group *pci_dev_attr_groups[] = {
16771654
#endif
16781655
NULL,
16791656
};
1680-
1681-
const struct device_type pci_dev_type = {
1682-
.groups = pci_dev_attr_groups,
1683-
};

drivers/pci/pci.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,6 @@ bool pcie_cap_has_rtctl(const struct pci_dev *dev);
3131

3232
/* Functions internal to the PCI core code */
3333

34-
int pci_create_sysfs_dev_files(struct pci_dev *pdev);
35-
void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
3634
void pci_cleanup_rom(struct pci_dev *dev);
3735
#ifdef CONFIG_DMI
3836
extern const struct attribute_group pci_dev_smbios_attr_group;
@@ -152,7 +150,7 @@ static inline int pci_proc_detach_bus(struct pci_bus *bus) { return 0; }
152150
/* Functions for PCI Hotplug drivers to use */
153151
int pci_hp_add_bridge(struct pci_dev *dev);
154152

155-
#ifdef HAVE_PCI_LEGACY
153+
#if defined(CONFIG_SYSFS) && defined(HAVE_PCI_LEGACY)
156154
void pci_create_legacy_files(struct pci_bus *bus);
157155
void pci_remove_legacy_files(struct pci_bus *bus);
158156
#else
@@ -185,10 +183,22 @@ static inline int pci_no_d1d2(struct pci_dev *dev)
185183
return (dev->no_d1d2 || parent_dstates);
186184

187185
}
186+
187+
#ifdef CONFIG_SYSFS
188+
int pci_create_sysfs_dev_files(struct pci_dev *pdev);
189+
void pci_remove_sysfs_dev_files(struct pci_dev *pdev);
188190
extern const struct attribute_group *pci_dev_groups[];
191+
extern const struct attribute_group *pci_dev_attr_groups[];
189192
extern const struct attribute_group *pcibus_groups[];
190-
extern const struct device_type pci_dev_type;
191193
extern const struct attribute_group *pci_bus_groups[];
194+
#else
195+
static inline int pci_create_sysfs_dev_files(struct pci_dev *pdev) { return 0; }
196+
static inline void pci_remove_sysfs_dev_files(struct pci_dev *pdev) { }
197+
#define pci_dev_groups NULL
198+
#define pci_dev_attr_groups NULL
199+
#define pcibus_groups NULL
200+
#define pci_bus_groups NULL
201+
#endif
192202

193203
extern unsigned long pci_hotplug_io_size;
194204
extern unsigned long pci_hotplug_mmio_size;

drivers/pci/probe.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2357,6 +2357,10 @@ static void pci_release_dev(struct device *dev)
23572357
kfree(pci_dev);
23582358
}
23592359

2360+
static const struct device_type pci_dev_type = {
2361+
.groups = pci_dev_attr_groups,
2362+
};
2363+
23602364
struct pci_dev *pci_alloc_dev(struct pci_bus *bus)
23612365
{
23622366
struct pci_dev *dev;

0 commit comments

Comments
 (0)