Skip to content

Commit f6c7399

Browse files
ij-intelbjorn-helgaas
authored andcommitted
PCI/sysfs: Demacrofy pci_dev_resource_resize_attr(n) functions
pci_dev_resource_resize_attr(n) macro is invoked for six resources, creating a large footprint function for each resource. Rework the macro to only create a function that calls a helper function so the compiler can decide if it warrants to inline the function or not. With x86_64 defconfig, this saves roughly 2.5kB: $ scripts/bloat-o-meter drivers/pci/pci-sysfs.o{.old,.new} add/remove: 1/0 grow/shrink: 0/6 up/down: 512/-2934 (-2422) Function old new delta __resource_resize_store - 512 +512 resource5_resize_store 503 14 -489 resource4_resize_store 503 14 -489 resource3_resize_store 503 14 -489 resource2_resize_store 503 14 -489 resource1_resize_store 503 14 -489 resource0_resize_store 500 11 -489 Total: Before=13399, After=10977, chg -18.08% (The compiler seemingly chose to still inline __resource_resize_show() which is fine, those functions are not very complex/large.) Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Ilpo Järvinen <[email protected]> Signed-off-by: Bjorn Helgaas <[email protected]>
1 parent 2ea548a commit f6c7399

File tree

1 file changed

+74
-64
lines changed

1 file changed

+74
-64
lines changed

drivers/pci/pci-sysfs.c

Lines changed: 74 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -1387,79 +1387,89 @@ static const struct attribute_group pci_dev_reset_attr_group = {
13871387
.is_visible = pci_dev_reset_attr_is_visible,
13881388
};
13891389

1390+
static ssize_t __resource_resize_show(struct device *dev, int n, char *buf)
1391+
{
1392+
struct pci_dev *pdev = to_pci_dev(dev);
1393+
ssize_t ret;
1394+
1395+
pci_config_pm_runtime_get(pdev);
1396+
1397+
ret = sysfs_emit(buf, "%016llx\n",
1398+
(u64)pci_rebar_get_possible_sizes(pdev, n));
1399+
1400+
pci_config_pm_runtime_put(pdev);
1401+
1402+
return ret;
1403+
}
1404+
1405+
static ssize_t __resource_resize_store(struct device *dev, int n,
1406+
const char *buf, size_t count)
1407+
{
1408+
struct pci_dev *pdev = to_pci_dev(dev);
1409+
unsigned long size, flags;
1410+
int ret, i;
1411+
u16 cmd;
1412+
1413+
if (kstrtoul(buf, 0, &size) < 0)
1414+
return -EINVAL;
1415+
1416+
device_lock(dev);
1417+
if (dev->driver) {
1418+
ret = -EBUSY;
1419+
goto unlock;
1420+
}
1421+
1422+
pci_config_pm_runtime_get(pdev);
1423+
1424+
if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) {
1425+
ret = aperture_remove_conflicting_pci_devices(pdev,
1426+
"resourceN_resize");
1427+
if (ret)
1428+
goto pm_put;
1429+
}
1430+
1431+
pci_read_config_word(pdev, PCI_COMMAND, &cmd);
1432+
pci_write_config_word(pdev, PCI_COMMAND,
1433+
cmd & ~PCI_COMMAND_MEMORY);
1434+
1435+
flags = pci_resource_flags(pdev, n);
1436+
1437+
pci_remove_resource_files(pdev);
1438+
1439+
for (i = 0; i < PCI_STD_NUM_BARS; i++) {
1440+
if (pci_resource_len(pdev, i) &&
1441+
pci_resource_flags(pdev, i) == flags)
1442+
pci_release_resource(pdev, i);
1443+
}
1444+
1445+
ret = pci_resize_resource(pdev, n, size);
1446+
1447+
pci_assign_unassigned_bus_resources(pdev->bus);
1448+
1449+
if (pci_create_resource_files(pdev))
1450+
pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n");
1451+
1452+
pci_write_config_word(pdev, PCI_COMMAND, cmd);
1453+
pm_put:
1454+
pci_config_pm_runtime_put(pdev);
1455+
unlock:
1456+
device_unlock(dev);
1457+
1458+
return ret ? ret : count;
1459+
}
1460+
13901461
#define pci_dev_resource_resize_attr(n) \
13911462
static ssize_t resource##n##_resize_show(struct device *dev, \
13921463
struct device_attribute *attr, \
1393-
char * buf) \
1464+
char *buf) \
13941465
{ \
1395-
struct pci_dev *pdev = to_pci_dev(dev); \
1396-
ssize_t ret; \
1397-
\
1398-
pci_config_pm_runtime_get(pdev); \
1399-
\
1400-
ret = sysfs_emit(buf, "%016llx\n", \
1401-
(u64)pci_rebar_get_possible_sizes(pdev, n)); \
1402-
\
1403-
pci_config_pm_runtime_put(pdev); \
1404-
\
1405-
return ret; \
1466+
return __resource_resize_show(dev, n, buf); \
14061467
} \
1407-
\
14081468
static ssize_t resource##n##_resize_store(struct device *dev, \
14091469
struct device_attribute *attr,\
14101470
const char *buf, size_t count)\
14111471
{ \
1412-
struct pci_dev *pdev = to_pci_dev(dev); \
1413-
unsigned long size, flags; \
1414-
int ret, i; \
1415-
u16 cmd; \
1416-
\
1417-
if (kstrtoul(buf, 0, &size) < 0) \
1418-
return -EINVAL; \
1419-
\
1420-
device_lock(dev); \
1421-
if (dev->driver) { \
1422-
ret = -EBUSY; \
1423-
goto unlock; \
1424-
} \
1425-
\
1426-
pci_config_pm_runtime_get(pdev); \
1427-
\
1428-
if ((pdev->class >> 8) == PCI_CLASS_DISPLAY_VGA) { \
1429-
ret = aperture_remove_conflicting_pci_devices(pdev, \
1430-
"resourceN_resize"); \
1431-
if (ret) \
1432-
goto pm_put; \
1433-
} \
1434-
\
1435-
pci_read_config_word(pdev, PCI_COMMAND, &cmd); \
1436-
pci_write_config_word(pdev, PCI_COMMAND, \
1437-
cmd & ~PCI_COMMAND_MEMORY); \
1438-
\
1439-
flags = pci_resource_flags(pdev, n); \
1440-
\
1441-
pci_remove_resource_files(pdev); \
1442-
\
1443-
for (i = 0; i < PCI_STD_NUM_BARS; i++) { \
1444-
if (pci_resource_len(pdev, i) && \
1445-
pci_resource_flags(pdev, i) == flags) \
1446-
pci_release_resource(pdev, i); \
1447-
} \
1448-
\
1449-
ret = pci_resize_resource(pdev, n, size); \
1450-
\
1451-
pci_assign_unassigned_bus_resources(pdev->bus); \
1452-
\
1453-
if (pci_create_resource_files(pdev)) \
1454-
pci_warn(pdev, "Failed to recreate resource files after BAR resizing\n");\
1455-
\
1456-
pci_write_config_word(pdev, PCI_COMMAND, cmd); \
1457-
pm_put: \
1458-
pci_config_pm_runtime_put(pdev); \
1459-
unlock: \
1460-
device_unlock(dev); \
1461-
\
1462-
return ret ? ret : count; \
1472+
return __resource_resize_store(dev, n, buf, count); \
14631473
} \
14641474
static DEVICE_ATTR_RW(resource##n##_resize)
14651475

0 commit comments

Comments
 (0)