From: Matthew Wood <thepacketgeek@gmail.com>
To: Bjorn Helgaas <bhelgaas@google.com>
Cc: linux-pci@vger.kernel.org, linux-kernel@vger.kernel.org,
kbusch@kernel.org, Nikita Vetoshkin <nekto0n@meta.com>
Subject: [PATCH 5/5] PCI/sysfs: Use user config accessors for serial_number
Date: Sat, 24 Oct 2026 17:08:33 -0700 [thread overview]
Message-ID: <20261025000836.58294-6-thepacketgeek@gmail.com> (raw)
In-Reply-To: <20261025000836.58294-1-thepacketgeek@gmail.com>
serial_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.
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.
Signed-off-by: Matthew Wood <thepacketgeek@gmail.com>
Tested-by: Nikita Vetoshkin <nekto0n@meta.com>
---
drivers/pci/pci-sysfs.c | 17 +++++++++++++++--
1 file changed, 15 insertions(+), 2 deletions(-)
diff --git a/drivers/pci/pci-sysfs.c b/drivers/pci/pci-sysfs.c
index a272f8583a8f..cbdd56b92d1a 100644
--- a/drivers/pci/pci-sysfs.c
+++ b/drivers/pci/pci-sysfs.c
@@ -696,10 +696,23 @@ 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 dword;
+ u64 dsn = 0;
u8 bytes[8];
+ int pos;
+
+ 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) {
+ pos += 4;
+ pci_user_read_config_dword(pci_dev, pos, &dword);
+ dsn = (u64)dword;
+ pci_user_read_config_dword(pci_dev, pos + 4, &dword);
+ dsn |= ((u64)dword) << 32;
+ }
+ pci_config_pm_runtime_put(pci_dev);
- dsn = pci_get_dsn(pci_dev);
if (!dsn)
return -EIO;
--
2.53.0-Meta
prev parent reply other threads:[~2026-10-25 0:08 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-25 0:08 [PATCH 0/5] PCI/sysfs: Wait for config access lock in sysfs attributes Matthew Wood
2026-10-25 0:08 ` [PATCH 1/5] PCI: Add disconnected device checks to user config reads Matthew Wood
2026-10-25 0:08 ` [PATCH 2/5] PCI/sysfs: Use user config accessors for bus number attributes Matthew Wood
2026-10-25 0:08 ` [PATCH 3/5] PCI: Add pcie_capability_user_read_{word,dword}() Matthew Wood
2026-10-25 0:08 ` [PATCH 4/5] PCI/sysfs: Use user config accessors for link attributes Matthew Wood
2026-10-25 0:08 ` Matthew Wood [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261025000836.58294-6-thepacketgeek@gmail.com \
--to=thepacketgeek@gmail.com \
--cc=bhelgaas@google.com \
--cc=kbusch@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=nekto0n@meta.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®