PCI/sysfs: Use sysfs_emit() and sysfs_emit_at() in "show" functions

The sysfs_emit() and sysfs_emit_at() functions were introduced to make it
less ambiguous which function is preferred when writing to the output
buffer in a device attribute's "show" callback [1].

Convert the PCI sysfs object "show" functions from sprintf(), snprintf()
and scnprintf() to sysfs_emit() and sysfs_emit_at() accordingly, as the
latter is aware of the PAGE_SIZE buffer and correctly returns the number of
bytes written into the buffer.

No functional change intended.

[1] Documentation/filesystems/sysfs.rst

[bhelgaas: drop dsm_label_utf16s_to_utf8s(), link speed/width changes]
Link: https://lore.kernel.org/r/20210416205856.3234481-10-kw@linux.com
Signed-off-by: Krzysztof Wilczyński <kw@linux.com>
Signed-off-by: Bjorn Helgaas <bhelgaas@google.com>
This commit is contained in:
Krzysztof Wilczyński 2021-04-16 20:58:45 +00:00 committed by Bjorn Helgaas
parent df1af7cbe7
commit ad025f8e46
2 changed files with 40 additions and 42 deletions

View File

@ -73,12 +73,10 @@ static size_t find_smbios_instance_string(struct pci_dev *pdev, char *buf,
donboard->devfn == devfn) { donboard->devfn == devfn) {
if (buf) { if (buf) {
if (attribute == SMBIOS_ATTR_INSTANCE_SHOW) if (attribute == SMBIOS_ATTR_INSTANCE_SHOW)
return scnprintf(buf, PAGE_SIZE, return sysfs_emit(buf, "%d\n",
"%d\n",
donboard->instance); donboard->instance);
else if (attribute == SMBIOS_ATTR_LABEL_SHOW) else if (attribute == SMBIOS_ATTR_LABEL_SHOW)
return scnprintf(buf, PAGE_SIZE, return sysfs_emit(buf, "%s\n",
"%s\n",
dmi->name); dmi->name);
} }
return strlen(dmi->name); return strlen(dmi->name);

View File

@ -39,7 +39,7 @@ field##_show(struct device *dev, struct device_attribute *attr, char *buf) \
struct pci_dev *pdev; \ struct pci_dev *pdev; \
\ \
pdev = to_pci_dev(dev); \ pdev = to_pci_dev(dev); \
return sprintf(buf, format_string, pdev->field); \ return sysfs_emit(buf, format_string, pdev->field); \
} \ } \
static DEVICE_ATTR_RO(field) static DEVICE_ATTR_RO(field)
@ -56,7 +56,7 @@ static ssize_t broken_parity_status_show(struct device *dev,
char *buf) char *buf)
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
return sprintf(buf, "%u\n", pdev->broken_parity_status); return sysfs_emit(buf, "%u\n", pdev->broken_parity_status);
} }
static ssize_t broken_parity_status_store(struct device *dev, static ssize_t broken_parity_status_store(struct device *dev,
@ -129,7 +129,7 @@ static ssize_t power_state_show(struct device *dev,
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
return sprintf(buf, "%s\n", pci_power_name(pdev->current_state)); return sysfs_emit(buf, "%s\n", pci_power_name(pdev->current_state));
} }
static DEVICE_ATTR_RO(power_state); static DEVICE_ATTR_RO(power_state);
@ -138,10 +138,10 @@ static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
struct pci_dev *pci_dev = to_pci_dev(dev); struct pci_dev *pci_dev = to_pci_dev(dev);
char *str = buf;
int i; int i;
int max; int max;
resource_size_t start, end; resource_size_t start, end;
size_t len = 0;
if (pci_dev->subordinate) if (pci_dev->subordinate)
max = DEVICE_COUNT_RESOURCE; max = DEVICE_COUNT_RESOURCE;
@ -151,12 +151,12 @@ static ssize_t resource_show(struct device *dev, struct device_attribute *attr,
for (i = 0; i < max; i++) { for (i = 0; i < max; i++) {
struct resource *res = &pci_dev->resource[i]; struct resource *res = &pci_dev->resource[i];
pci_resource_to_user(pci_dev, i, res, &start, &end); pci_resource_to_user(pci_dev, i, res, &start, &end);
str += sprintf(str, "0x%016llx 0x%016llx 0x%016llx\n", len += sysfs_emit_at(buf, len, "0x%016llx 0x%016llx 0x%016llx\n",
(unsigned long long)start, (unsigned long long)start,
(unsigned long long)end, (unsigned long long)end,
(unsigned long long)res->flags); (unsigned long long)res->flags);
} }
return (str - buf); return len;
} }
static DEVICE_ATTR_RO(resource); static DEVICE_ATTR_RO(resource);
@ -165,7 +165,7 @@ static ssize_t max_link_speed_show(struct device *dev,
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
return sprintf(buf, "%s\n", return sysfs_emit(buf, "%s\n",
pci_speed_string(pcie_get_speed_cap(pdev))); pci_speed_string(pcie_get_speed_cap(pdev)));
} }
static DEVICE_ATTR_RO(max_link_speed); static DEVICE_ATTR_RO(max_link_speed);
@ -175,7 +175,7 @@ static ssize_t max_link_width_show(struct device *dev,
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
return sprintf(buf, "%u\n", pcie_get_width_cap(pdev)); return sysfs_emit(buf, "%u\n", pcie_get_width_cap(pdev));
} }
static DEVICE_ATTR_RO(max_link_width); static DEVICE_ATTR_RO(max_link_width);
@ -193,7 +193,7 @@ static ssize_t current_link_speed_show(struct device *dev,
speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS]; speed = pcie_link_speed[linkstat & PCI_EXP_LNKSTA_CLS];
return sprintf(buf, "%s\n", pci_speed_string(speed)); return sysfs_emit(buf, "%s\n", pci_speed_string(speed));
} }
static DEVICE_ATTR_RO(current_link_speed); static DEVICE_ATTR_RO(current_link_speed);
@ -208,7 +208,7 @@ static ssize_t current_link_width_show(struct device *dev,
if (err) if (err)
return -EINVAL; return -EINVAL;
return sprintf(buf, "%u\n", return sysfs_emit(buf, "%u\n",
(linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT); (linkstat & PCI_EXP_LNKSTA_NLW) >> PCI_EXP_LNKSTA_NLW_SHIFT);
} }
static DEVICE_ATTR_RO(current_link_width); static DEVICE_ATTR_RO(current_link_width);
@ -225,7 +225,7 @@ static ssize_t secondary_bus_number_show(struct device *dev,
if (err) if (err)
return -EINVAL; return -EINVAL;
return sprintf(buf, "%u\n", sec_bus); return sysfs_emit(buf, "%u\n", sec_bus);
} }
static DEVICE_ATTR_RO(secondary_bus_number); static DEVICE_ATTR_RO(secondary_bus_number);
@ -241,7 +241,7 @@ static ssize_t subordinate_bus_number_show(struct device *dev,
if (err) if (err)
return -EINVAL; return -EINVAL;
return sprintf(buf, "%u\n", sub_bus); return sysfs_emit(buf, "%u\n", sub_bus);
} }
static DEVICE_ATTR_RO(subordinate_bus_number); static DEVICE_ATTR_RO(subordinate_bus_number);
@ -251,7 +251,7 @@ static ssize_t ari_enabled_show(struct device *dev,
{ {
struct pci_dev *pci_dev = to_pci_dev(dev); struct pci_dev *pci_dev = to_pci_dev(dev);
return sprintf(buf, "%u\n", pci_ari_enabled(pci_dev->bus)); return sysfs_emit(buf, "%u\n", pci_ari_enabled(pci_dev->bus));
} }
static DEVICE_ATTR_RO(ari_enabled); static DEVICE_ATTR_RO(ari_enabled);
@ -260,7 +260,7 @@ static ssize_t modalias_show(struct device *dev, struct device_attribute *attr,
{ {
struct pci_dev *pci_dev = to_pci_dev(dev); struct pci_dev *pci_dev = to_pci_dev(dev);
return sprintf(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n", return sysfs_emit(buf, "pci:v%08Xd%08Xsv%08Xsd%08Xbc%02Xsc%02Xi%02X\n",
pci_dev->vendor, pci_dev->device, pci_dev->vendor, pci_dev->device,
pci_dev->subsystem_vendor, pci_dev->subsystem_device, pci_dev->subsystem_vendor, pci_dev->subsystem_device,
(u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8), (u8)(pci_dev->class >> 16), (u8)(pci_dev->class >> 8),
@ -302,7 +302,7 @@ static ssize_t enable_show(struct device *dev, struct device_attribute *attr,
struct pci_dev *pdev; struct pci_dev *pdev;
pdev = to_pci_dev(dev); pdev = to_pci_dev(dev);
return sprintf(buf, "%u\n", atomic_read(&pdev->enable_cnt)); return sysfs_emit(buf, "%u\n", atomic_read(&pdev->enable_cnt));
} }
static DEVICE_ATTR_RW(enable); static DEVICE_ATTR_RW(enable);
@ -338,7 +338,7 @@ static ssize_t numa_node_store(struct device *dev,
static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr, static ssize_t numa_node_show(struct device *dev, struct device_attribute *attr,
char *buf) char *buf)
{ {
return sprintf(buf, "%d\n", dev->numa_node); return sysfs_emit(buf, "%d\n", dev->numa_node);
} }
static DEVICE_ATTR_RW(numa_node); static DEVICE_ATTR_RW(numa_node);
#endif #endif
@ -348,7 +348,7 @@ static ssize_t dma_mask_bits_show(struct device *dev,
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
return sprintf(buf, "%d\n", fls64(pdev->dma_mask)); return sysfs_emit(buf, "%d\n", fls64(pdev->dma_mask));
} }
static DEVICE_ATTR_RO(dma_mask_bits); static DEVICE_ATTR_RO(dma_mask_bits);
@ -356,7 +356,7 @@ static ssize_t consistent_dma_mask_bits_show(struct device *dev,
struct device_attribute *attr, struct device_attribute *attr,
char *buf) char *buf)
{ {
return sprintf(buf, "%d\n", fls64(dev->coherent_dma_mask)); return sysfs_emit(buf, "%d\n", fls64(dev->coherent_dma_mask));
} }
static DEVICE_ATTR_RO(consistent_dma_mask_bits); static DEVICE_ATTR_RO(consistent_dma_mask_bits);
@ -366,7 +366,7 @@ static ssize_t msi_bus_show(struct device *dev, struct device_attribute *attr,
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
struct pci_bus *subordinate = pdev->subordinate; struct pci_bus *subordinate = pdev->subordinate;
return sprintf(buf, "%u\n", subordinate ? return sysfs_emit(buf, "%u\n", subordinate ?
!(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI) !(subordinate->bus_flags & PCI_BUS_FLAGS_NO_MSI)
: !pdev->no_msi); : !pdev->no_msi);
} }
@ -523,7 +523,7 @@ static ssize_t d3cold_allowed_show(struct device *dev,
struct device_attribute *attr, char *buf) struct device_attribute *attr, char *buf)
{ {
struct pci_dev *pdev = to_pci_dev(dev); struct pci_dev *pdev = to_pci_dev(dev);
return sprintf(buf, "%u\n", pdev->d3cold_allowed); return sysfs_emit(buf, "%u\n", pdev->d3cold_allowed);
} }
static DEVICE_ATTR_RW(d3cold_allowed); static DEVICE_ATTR_RW(d3cold_allowed);
#endif #endif
@ -537,7 +537,7 @@ static ssize_t devspec_show(struct device *dev,
if (np == NULL) if (np == NULL)
return 0; return 0;
return sprintf(buf, "%pOF", np); return sysfs_emit(buf, "%pOF", np);
} }
static DEVICE_ATTR_RO(devspec); static DEVICE_ATTR_RO(devspec);
#endif #endif
@ -583,7 +583,7 @@ static ssize_t driver_override_show(struct device *dev,
ssize_t len; ssize_t len;
device_lock(dev); device_lock(dev);
len = scnprintf(buf, PAGE_SIZE, "%s\n", pdev->driver_override); len = sysfs_emit(buf, "%s\n", pdev->driver_override);
device_unlock(dev); device_unlock(dev);
return len; return len;
} }
@ -658,9 +658,9 @@ static ssize_t boot_vga_show(struct device *dev, struct device_attribute *attr,
struct pci_dev *vga_dev = vga_default_device(); struct pci_dev *vga_dev = vga_default_device();
if (vga_dev) if (vga_dev)
return sprintf(buf, "%u\n", (pdev == vga_dev)); return sysfs_emit(buf, "%u\n", (pdev == vga_dev));
return sprintf(buf, "%u\n", return sysfs_emit(buf, "%u\n",
!!(pdev->resource[PCI_ROM_RESOURCE].flags & !!(pdev->resource[PCI_ROM_RESOURCE].flags &
IORESOURCE_ROM_SHADOW)); IORESOURCE_ROM_SHADOW));
} }