PCI/sysfs: Wait for config access lock in sysfs attributes
v2 cover PCI/sysfs: Wait for config access lock in sysfs attributes
Many PCI sysfs attributes that read from config space are using kernel accessors that don't wait for pci_cfg_access_lock() holders before reading:
- serial_number
- max_link_width
- current_link_*
- *_bus_number
If userspace attempts to read from these attributes while config space is not available (e.g. during a reset) there's no telling what values will be
returned: All ones, zeros, or garbage.
Looking at attribute addition history it seems like this was an oversight instead of a specific decision so this series tries to resolve the attributes above to match the sysfs attributes that do use user accessors: config & vpd.
Patch 1 is a prerequisite for the rest of the series. The kernel accessors these attributes use today skip devices marked disconnected but the user accessors don't, so switching over alone would make the attributes start sending config reads to disconnected devices. Patch 1 adds the same pci_dev_is_disconnected() check to the user read accessors.
Note: That check also affects their existing callers. On a disconnected device, VPD reads and writes, VFIO config reads, and VFIO writes to PCI_COMMAND now fail with -ENODEV, rather than returning ~0 data or, for VPD writes, timing out with a "firmware bug" warning. pciconfig_read() fails with -EIO, and the sysfs "config" file and /proc/bus/pci still return ~0 data.
Changes were successfully tested by continuously reading sysfs attributes while a reset was issued.
Changes in v2:
- Patch 5: check DSN read errors in serial_number_show() and return -EIO instead of an all-ones serial number
- Collected Reviewed-by from Keith Busch on patches 2-4
v1: https://lore.kernel.org/linux-pci/[email protected]/
Matthew Wood (5):
PCI: Add disconnected device checks to user config reads PCI/sysfs: Use user config accessors for bus number attributes
PCI: Add pcie_capability_user_read_{word,dword}() PCI/sysfs: Use user config accessors for link attributes PCI/sysfs: Use user config accessors for serial_number
drivers/pci/access.c | 57 +++++++++++++++++++++++++++++++++++++++-- drivers/pci/pci-sysfs.c | 37 ++++++++++++++++++-------- drivers/pci/pci.h | 2 ++ 3 files changed, 83 insertions(+), 13 deletions(-)
base-commit: f4319f548f7d8cb595ac1c91bfd459876399eeb3 --
2.53.0-Meta
PATCH v2 1/5 PCI: Add disconnected device checks to user config reads
Since commit 4b1038834739 ("PCI: Don't attempt config access to disconnected devices") the kernel config read accessors skip devices marked disconnected, returning PCIBIOS_DEVICE_NOT_FOUND and ~0 data. The user config read accessors still issue the read, and if the host bridge completes it with ~0 data and no error, they report success.
Check pci_dev_is_disconnected() in pci_user_read_config_*() as well, failing with -ENODEV and ~0 data. Do the check after waiting for any pci_cfg_access_lock() holder so a reader blocked behind a reset also sees a device that was disconnected while it waited.
This prepares for converting sysfs attributes from the kernel accessors to the user accessors so they keep skipping disconnected devices.
On a disconnected device, existing callers of the user read accessors now behave as follows:
- The sysfs "config" file and /proc/bus/pci ignore the return value and return the same ~0 data as before
- pciconfig_read() fails with -EIO
- VPD reads and VFIO config reads fail with -ENODEV instead of returning ~0 data
- VPD writes fail with -ENODEV instead of timing out with a "firmware bug" warning
- VFIO writes to PCI_COMMAND fail with -ENODEV, since VFIO reads the current value first
--- drivers/pci/access.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-)
drivers/pci/access.c +5 -2
@@ -237,8 +237,11 @@ int pci_user_read_config_##size \raw_spin_lock_irq (&pci_lock); \if (unlikely (dev->block_cfg_access)) \pci_wait_cfg (dev); \ - ret = dev->bus->ops->read (dev->bus, dev->devfn, \ - pos,sizeof (type), &data); \ +if (pci_dev_is_disconnected (dev)) \ + ret =PCIBIOS_DEVICE_NOT_FOUND ; \ +else \ + ret = dev->bus->ops->read (dev->bus, dev->devfn, \ + pos,sizeof (type), &data); \raw_spin_unlock_irq (&pci_lock); \if (ret) \PCI_SET_ERROR_RESPONSE (val); \
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 1/5] PCI: Add disconnected device checks to user config reads
Sashiko has reviewed this patch and found no issues.
Quoted 28 lines
Since commit
4b1038834739("PCI: Don't attempt config access to disconnected devices") the kernel config read accessors skip devices marked disconnected, returningPCIBIOS_DEVICE_NOT_FOUNDand ~0 data. The user config read accessors still issue the read, and if the host bridge completes it with ~0 data and no error, they report success.Check
pci_dev_is_disconnected()in pci_user_read_config_*() as well, failing with-ENODEVand ~0 data. Do the check after waiting for anypci_cfg_access_lock()holder so a reader blocked behind a reset also sees a device that was disconnected while it waited.This prepares for converting sysfs attributes from the kernel accessors to the user accessors so they keep skipping disconnected devices.
On a disconnected device, existing callers of the user read accessors now behave as follows:
- The sysfs "config" file and
/proc/bus/pciignore the return value and return the same ~0 data as before
-pciconfig_read()fails with-EIO
- VPD reads and VFIO config reads fail with-ENODEVinstead of returning ~0 data
- VPD writes fail with-ENODEVinstead of timing out with a "firmware bug" warning
- VFIO writes toPCI_COMMANDfail with-ENODEV, since VFIO reads the current value first
Quoted 1 line
Signed-off-by Matthew Wood <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Re: [PATCH v2 1/5] PCI: Add disconnected device checks to user config reads
In reply to Matthew WoodQuoted 10 lines · Matthew Wood
Since commit
4b1038834739("PCI: Don't attempt config access to disconnected devices") the kernel config read accessors skip devices marked disconnected, returningPCIBIOS_DEVICE_NOT_FOUNDand ~0 data. The user config read accessors still issue the read, and if the host bridge completes it with ~0 data and no error, they report success.Check
pci_dev_is_disconnected()in pci_user_read_config_*() as well, failing with-ENODEVand ~0 data. Do the check after waiting for anypci_cfg_access_lock()holder so a reader blocked behind a reset also sees a device that was disconnected while it waited.
Looks good.
PATCH v2 2/5 PCI/sysfs: Use user config accessors for bus number attributes
In reply to Matthew Woodsecondary_bus_number and subordinate_bus_number read config space with pci_read_config_byte(). The kernel config accessors don't check block_cfg_access, so a read that races with a reset goes to the device mid-reset and can fail or return a bogus bus number.
Use pci_user_read_config_byte() instead, as the "config" attribute does, so the read waits for any pci_cfg_access_lock() holder (e.g. pci_reset_function()) to finish.
--- drivers/pci/pci-sysfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/pci/pci-sysfs.c +2 -2
@@ -267,7 +267,7 @@ static ssize_t secondary_bus_number_show(struct device *dev,int err;pci_config_pm_runtime_get (pci_dev); - err =pci_read_config_byte (pci_dev,PCI_SECONDARY_BUS , &sec_bus); + err =pci_user_read_config_byte (pci_dev,PCI_SECONDARY_BUS , &sec_bus);pci_config_pm_runtime_put (pci_dev);if (err)
@@ -286,7 +286,7 @@ static ssize_t subordinate_bus_number_show(struct device *dev,int err;pci_config_pm_runtime_get (pci_dev); - err =pci_read_config_byte (pci_dev,PCI_SUBORDINATE_BUS , &sub_bus); + err =pci_user_read_config_byte (pci_dev,PCI_SUBORDINATE_BUS , &sub_bus);pci_config_pm_runtime_put (pci_dev);if (err)
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 2/5] PCI/sysfs: Use user config accessors for bus number attributes
Sashiko has reviewed this patch and found no issues.
Quoted 10 lines
secondary_bus_numberandsubordinate_bus_numberread config space withpci_read_config_byte(). The kernel config accessors don't checkblock_cfg_access, so a read that races with a reset goes to the device mid-reset and can fail or return a bogus bus number.Use
pci_user_read_config_byte()instead, as the "config" attribute does, so the read waits for anypci_cfg_access_lock()holder (e.g.pci_reset_function()) to finish.Signed-off-by Matthew Wood <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
PATCH v2 3/5 PCI: Add pcie_capability_user_read_{word,dword}()
In reply to Matthew Woodpcie_capability_read_word() and pcie_capability_read_dword() use the kernel config accessors which don't check block_cfg_access, so they can't be used for reads on behalf of userspace that should wait for a reset (or other pci_cfg_access_lock() holder) to finish.
Add pcie_capability_user_read_word() and pcie_capability_user_read_dword() helpers, which provide the same function using the user config accessors.
These are for use within the PCI core, so they are declared in drivers/pci/pci.h and not exported.
--- drivers/pci/access.c | 50 ++++++++++++++++++++++++++++++++++++++++++++ drivers/pci/pci.h | 2 ++ 2 files changed, 52 insertions(+)
drivers/pci/access.c +50 -0
@@ -490,6 +490,56 @@ int pcie_capability_read_dword(struct pci_dev *dev, int pos, u32 *val) }EXPORT_SYMBOL (pcie_capability_read_dword); +/* +* Same as pcie_capability_read_{word,dword}(), but using the user config +* accessors. Returns 0 on success, negative values indicate error. +*/ +int pcie_capability_user_read_word (struct pci_dev *dev,int pos,u16 *val) +{ +int ret; + + *val =0 ; +if (pos &1 ) +return -EINVAL ; + +if (pcie_capability_reg_implemented (dev, pos)) { + ret =pci_user_read_config_word (dev,pci_pcie_cap (dev) + pos, + val); +if (ret) + *val =0 ; +return ret; + } + +if (pci_is_pcie (dev) &&pcie_downstream_port (dev) && + pos ==PCI_EXP_SLTSTA ) + *val =PCI_EXP_SLTSTA_PDS ; + +return 0 ; +} + +int pcie_capability_user_read_dword (struct pci_dev *dev,int pos,u32 *val) +{ +int ret; + + *val =0 ; +if (pos &3 ) +return -EINVAL ; + +if (pcie_capability_reg_implemented (dev, pos)) { + ret =pci_user_read_config_dword (dev,pci_pcie_cap (dev) + pos, + val); +if (ret) + *val =0 ; +return ret; + } + +if (pci_is_pcie (dev) &&pcie_downstream_port (dev) && + pos ==PCI_EXP_SLTSTA ) + *val =PCI_EXP_SLTSTA_PDS ; + +return 0 ; +} +int pcie_capability_write_word (struct pci_dev *dev,int pos,u16 val) {if (pos &1 )
drivers/pci/pci.h +2 -0
@@ -128,6 +128,8 @@ extern struct mutex pci_rescan_remove_lock;bool pcie_cap_has_lnkctl (const struct pci_dev *dev);bool pcie_cap_has_lnkctl2 (const struct pci_dev *dev);bool pcie_cap_has_rtctl (const struct pci_dev *dev); +int pcie_capability_user_read_word (struct pci_dev *dev,int pos,u16 *val); +int pcie_capability_user_read_dword (struct pci_dev *dev,int pos,u32 *val);/* Standard Capability finder */ /**
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 3/5] PCI: Add pcie_capability_user_read_{word,dword}()
Sashiko has reviewed this patch and found no issues.
Quoted 5 lines
pcie_capability_read_word()andpcie_capability_read_dword()use the kernel config accessors which don't checkblock_cfg_access, so they can't be used for reads on behalf of userspace that should wait for a reset (or otherpci_cfg_access_lock()holder) to finish.
Quoted 8 lines
Add
pcie_capability_user_read_word()andpcie_capability_user_read_dword()helpers, which provide the same function using the user config accessors.These are for use within the PCI core, so they are declared in
drivers/pci/pci.hand not exported.Signed-off-by Matthew Wood <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
PATCH v2 4/5 PCI/sysfs: Use user config accessors for link attributes
In reply to Matthew Woodmax_link_width, current_link_speed and current_link_width read the PCIe Link Capabilities and Link Status registers with the kernel config accessors. Those don't check block_cfg_access so a read that races with a reset goes to the device mid-reset and can fail or return a bogus value.
Use pcie_capability_user_read_{word,dword}() instead so the read waits for any pci_cfg_access_lock() holder (e.g. pci_reset_function()) to finish. For max_link_width, this means inlining the pcie_get_width_cap() logic instead of creating an additional helper with this single calling location.
--- drivers/pci/pci-sysfs.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-)
drivers/pci/pci-sysfs.c +9 -5
@@ -208,14 +208,16 @@ static ssize_t max_link_width_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pdev = to_pci_dev(dev); -ssize_t ret; +u32 lnkcap;/* We read PCI_EXP_LNKCAP, so we need the device to be accessible. */ pci_config_pm_runtime_get (pdev); - ret =sysfs_emit (buf,"%u\n" ,pcie_get_width_cap (pdev)); +pcie_capability_user_read_dword (pdev,PCI_EXP_LNKCAP , &lnkcap);pci_config_pm_runtime_put (pdev); -return ret; +return sysfs_emit (buf,"%u\n" , lnkcap ? +FIELD_GET (PCI_EXP_LNKCAP_MLW , lnkcap) : +PCIE_LNK_WIDTH_UNKNOWN ); }static DEVICE_ATTR_RO (max_link_width);
@@ -228,7 +230,8 @@ static ssize_t current_link_speed_show(struct device *dev,enum pci_bus_speed speed;pci_config_pm_runtime_get (pci_dev); - err =pcie_capability_read_word (pci_dev,PCI_EXP_LNKSTA , &linkstat); + err =pcie_capability_user_read_word (pci_dev,PCI_EXP_LNKSTA , + &linkstat);pci_config_pm_runtime_put (pci_dev);if (err)
@@ -248,7 +251,8 @@ static ssize_t current_link_width_show(struct device *dev,int err;pci_config_pm_runtime_get (pci_dev); - err =pcie_capability_read_word (pci_dev,PCI_EXP_LNKSTA , &linkstat); + err =pcie_capability_user_read_word (pci_dev,PCI_EXP_LNKSTA , + &linkstat);pci_config_pm_runtime_put (pci_dev);if (err)
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 4/5] PCI/sysfs: Use user config accessors for link attributes
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
max_link_width,current_link_speedandcurrent_link_widthread the PCIe Link Capabilities and Link Status registers with the kernel config accessors. Those don't checkblock_cfg_accessso a read that races with a reset goes to the device mid-reset and can fail or return a bogus value.
Quoted 6 lines
Use pcie_capability_user_read_{word,dword}() instead so the read waits for any
pci_cfg_access_lock()holder (e.g.pci_reset_function()) to finish. Formax_link_width, this means inlining thepcie_get_width_cap()logic instead of creating an additional helper with this single calling location.Signed-off-by Matthew Wood <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
PATCH v2 5/5 PCI/sysfs: Use user config accessors for serial_number
In reply to Matthew Woodserial_number_show() reads the device serial number with pci_get_dsn(), which uses the kernel config accessors which don't check block_cfg_access, so a read that races with a reset goes to the device mid-reset and can fail with -EIO or report an all-ones serial number.
Do the capability walk and DSN reads with the user config accessors instead, as the "config" attribute does, so each access waits for any pci_cfg_access_lock() holder (e.g. pci_reset_function()) to finish. Also wrap the reads in pci_config_pm_runtime_{get,put}() like the other attributes that read config space.
A reset can still start between two accesses, but the next access just waits for it to finish, and the DSN capability is unchanged by a reset.
Add a check that both DSN reads are successful, unlike pci_get_dsn(), and fail with -EIO instead of reporting an all-ones serial number if the device is disconnected between the capability walk and the reads.
Unlike pci_find_ext_capability(), the walk doesn't check dev->cfg_size first. serial_number is only visible if pci_get_dsn() found the DSN capability when the device was added, and cfg_size is final by then.
--- drivers/pci/pci-sysfs.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-)
drivers/pci/pci-sysfs.c +15 -4
@@ -696,14 +696,25 @@ static ssize_t serial_number_show(struct device *dev, struct device_attribute *attr, char *buf) { struct pci_dev *pci_dev = to_pci_dev(dev); -u64 dsn; +u32 lo =0 , hi =0 ;u8 bytes[8 ]; +int pos, ret = -EIO ; - dsn =pci_get_dsn (pci_dev); -if (!dsn) +pci_config_pm_runtime_get (pci_dev); + pos =PCI_FIND_NEXT_EXT_CAP (pci_user_read_config,0 , +PCI_EXT_CAP_ID_DSN ,NULL , pci_dev); +if (pos) { + ret =pci_user_read_config_dword (pci_dev, pos +4 , &lo); +if (!ret) + ret =pci_user_read_config_dword (pci_dev, pos +8 , + &hi); + } +pci_config_pm_runtime_put (pci_dev); + +if (ret || !(lo | hi))return -EIO ; -put_unaligned_be64 (dsn, bytes); +put_unaligned_be64 (((u64 )hi <<32 ) | lo, bytes);return sysfs_emit (buf,"%8phD\n" , bytes); }static DEVICE_ATTR_ADMIN_RO (serial_number);
Signature · 1 line
2.53.0-Meta
[email protected]
AI reviewRe: [PATCH v2 5/5] PCI/sysfs: Use user config accessors for serial_number
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines
serial_number_show()reads the device serial number withpci_get_dsn(), which uses the kernel config accessors which don't checkblock_cfg_access, so a read that races with a reset goes to the device mid-reset and can fail with-EIOor report an all-ones serial number.Do the capability walk and DSN reads with the user config accessors instead, as the "config" attribute does, so each access waits for any
pci_cfg_access_lock()holder (e.g.pci_reset_function()) to finish. Also wrap the reads in pci_config_pm_runtime_{get,put}() like the other attributes that read config space.A reset can still start between two accesses, but the next access just waits for it to finish, and the DSN capability is unchanged by a reset.
Add a check that both DSN reads are successful, unlike
pci_get_dsn(), and fail with-EIOinstead of reporting an all-ones serial number if the device is disconnected between the capability walk and the reads.
Quoted 5 lines
Unlike
pci_find_ext_capability(), the walk doesn't checkdev->cfg_sizefirst.serial_numberis only visible ifpci_get_dsn()found the DSN capability when the device was added, andcfg_sizeis final by then.Signed-off-by Matthew Wood <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Re: [PATCH v2 5/5] PCI/sysfs: Use user config accessors for serial_number
In reply to Matthew WoodQuoted 14 lines · Matthew Wood
serial_number_show()reads the device serial number withpci_get_dsn(), which uses the kernel config accessors which don't checkblock_cfg_access, so a read that races with a reset goes to the device mid-reset and can fail with-EIOor report an all-ones serial number.Do the capability walk and DSN reads with the user config accessors instead, as the "config" attribute does, so each access waits for any
pci_cfg_access_lock()holder (e.g.pci_reset_function()) to finish. Also wrap the reads in pci_config_pm_runtime_{get,put}() like the other attributes that read config space.A reset can still start between two accesses, but the next access just waits for it to finish, and the DSN capability is unchanged by a reset.
Quoted 7 lines
Add a check that both DSN reads are successful, unlike
pci_get_dsn(), and fail with-EIOinstead of reporting an all-ones serial number if the device is disconnected between the capability walk and the reads.Unlike
pci_find_ext_capability(), the walk doesn't checkdev->cfg_sizefirst.serial_numberis only visible ifpci_get_dsn()found the DSN capability when the device was added, andcfg_sizeis final by then.
Looks good.
Quoted 34 lines · Matthew Wood
Many PCI sysfs attributes that read from config space are using kernel accessors that don't wait for
pci_cfg_access_lock()holders before reading:-
serial_number
-max_link_width
- current_link_*
-*_bus_numberIf userspace attempts to read from these attributes while config space is not available (e.g. during a reset) there's no telling what values will be
returned: All ones, zeros, or garbage.Looking at attribute addition history it seems like this was an oversight instead of a specific decision so this series tries to resolve the attributes above to match the sysfs attributes that do use user accessors: config & vpd.
Patch 1 is a prerequisite for the rest of the series. The kernel accessors these attributes use today skip devices marked disconnected but the user accessors don't, so switching over alone would make the attributes start sending config reads to disconnected devices. Patch 1 adds the same
pci_dev_is_disconnected()check to the user read accessors.Note: That check also affects their existing callers. On a disconnected device, VPD reads and writes, VFIO config reads, and VFIO writes to
PCI_COMMANDnow fail with-ENODEV, rather than returning ~0 data or, for VPD writes, timing out with a "firmware bug" warning.pciconfig_read()fails with-EIO, and the sysfs "config" file and/proc/bus/pcistill return ~0 data.Changes were successfully tested by continuously reading sysfs attributes while a reset was issued.
Quoted 7 lines
Changes in v2:
- Patch 5: check DSN read errors inserial_number_show()and return-EIOinstead of an all-ones serial number
- Collected Reviewed-by from Keith Busch on patches 2-4
Hi Bjorn,
Checking in to see if there are any concerns with this change or if the premise makes sense?
Regards,
Matthew