Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

PCI: Use driver_set_override() instead of open-coding

Use a helper to set driver_override to the reduce amount of duplicated
code. Make the driver_override field const char, because it is not
modified by the core and it matches other subsystems.

Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
Acked-by: Bjorn Helgaas <bhelgaas@google.com>
Signed-off-by: Krzysztof Kozlowski <krzysztof.kozlowski@linaro.org>
Link: https://lore.kernel.org/r/20220419113435.246203-6-krzysztof.kozlowski@linaro.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>

authored by

Krzysztof Kozlowski and committed by
Greg Kroah-Hartman
23d99baf 01ed1002

+9 -25
+4 -24
drivers/pci/pci-sysfs.c
··· 567 567 const char *buf, size_t count) 568 568 { 569 569 struct pci_dev *pdev = to_pci_dev(dev); 570 - char *driver_override, *old, *cp; 570 + int ret; 571 571 572 - /* We need to keep extra room for a newline */ 573 - if (count >= (PAGE_SIZE - 1)) 574 - return -EINVAL; 575 - 576 - driver_override = kstrndup(buf, count, GFP_KERNEL); 577 - if (!driver_override) 578 - return -ENOMEM; 579 - 580 - cp = strchr(driver_override, '\n'); 581 - if (cp) 582 - *cp = '\0'; 583 - 584 - device_lock(dev); 585 - old = pdev->driver_override; 586 - if (strlen(driver_override)) { 587 - pdev->driver_override = driver_override; 588 - } else { 589 - kfree(driver_override); 590 - pdev->driver_override = NULL; 591 - } 592 - device_unlock(dev); 593 - 594 - kfree(old); 572 + ret = driver_set_override(dev, &pdev->driver_override, buf, count); 573 + if (ret) 574 + return ret; 595 575 596 576 return count; 597 577 }
+5 -1
include/linux/pci.h
··· 516 516 u16 acs_cap; /* ACS Capability offset */ 517 517 phys_addr_t rom; /* Physical address if not from BAR */ 518 518 size_t romlen; /* Length if not from BAR */ 519 - char *driver_override; /* Driver name to force a match */ 519 + /* 520 + * Driver name to force a match. Do not set directly, because core 521 + * frees it. Use driver_set_override() to set or clear it. 522 + */ 523 + const char *driver_override; 520 524 521 525 unsigned long priv_flags; /* Private flags for the PCI driver */ 522 526