From: Wei Huang <wei.huang2@amd.com>
To: <bhelgaas@google.com>
Cc: <linux-pci@vger.kernel.org>, <linux-doc@vger.kernel.org>,
<linux-kernel@vger.kernel.org>, <skhan@linuxfoundation.org>,
<corbet@lwn.net>, <rdunlap@infradead.org>,
<fengchengwen@huawei.com>, <wei.huang2@amd.com>
Subject: [PATCH RFC 2/3] PCI: Add per-device debugfs directories
Date: Thu, 1 Oct 2026 10:55:50 -0500 [thread overview]
Message-ID: <20261001155551.4182899-3-wei.huang2@amd.com> (raw)
In-Reply-To: <20261001155551.4182899-1-wei.huang2@amd.com>
PCI features that export debug information have no common place to put it.
The only existing user, PTM, creates a "pcie_ptm_<device>" directory in the
debugfs root, which does not scale as more features follow and leaves each
user to invent its own lifetime rules and leaf names.
Add /sys/kernel/debug/pci/<device>/ directories owned by the PCI core.
pci_dev_debugfs_dir() returns the directory of a device, creating it and
the PCI root directory on first use, so devices without debug content do
not get an empty directory. pci_destroy_dev() removes the directory
recursively, so a feature that pins data in a debugfs file needs no removal
hook of its own: debugfs_remove_recursive() returns only once no operation
on those files is still running.
No functional change for existing users; the next patch is the first
consumer.
Signed-off-by: Wei Huang <wei.huang2@amd.com>
---
drivers/pci/Makefile | 1 +
drivers/pci/debugfs.c | 60 +++++++++++++++++++++++++++++++++++++++++++
drivers/pci/pci.h | 12 +++++++++
drivers/pci/remove.c | 1 +
include/linux/pci.h | 3 +++
5 files changed, 77 insertions(+)
create mode 100644 drivers/pci/debugfs.c
diff --git a/drivers/pci/Makefile b/drivers/pci/Makefile
index 41ebc3b9a518..dbc841c3e990 100644
--- a/drivers/pci/Makefile
+++ b/drivers/pci/Makefile
@@ -15,6 +15,7 @@ ifdef CONFIG_PCI
obj-$(CONFIG_PROC_FS) += proc.o
obj-$(CONFIG_SYSFS) += pci-sysfs.o slot.o
obj-$(CONFIG_ACPI) += pci-acpi.o
+obj-$(CONFIG_DEBUG_FS) += debugfs.o
obj-$(CONFIG_GENERIC_PCI_IOMAP) += iomap.o
endif
diff --git a/drivers/pci/debugfs.c b/drivers/pci/debugfs.c
new file mode 100644
index 000000000000..682fb614d7cf
--- /dev/null
+++ b/drivers/pci/debugfs.c
@@ -0,0 +1,60 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Per-device debugfs directories for the PCI core
+ *
+ * PCI features that want to export debug information create their files in
+ * the directory returned by pci_dev_debugfs_dir(). The directory itself is
+ * created on first use and is torn down, together with everything below it,
+ * when the device is destroyed.
+ */
+#include <linux/debugfs.h>
+#include <linux/mutex.h>
+#include <linux/pci.h>
+
+#include "pci.h"
+
+/* Serializes creation of the root and of the per-device directories */
+static DEFINE_MUTEX(pci_debugfs_lock);
+static struct dentry *pci_debugfs_root;
+
+/**
+ * pci_dev_debugfs_dir - Return the debugfs directory of a PCI device
+ * @dev: PCI device
+ *
+ * Return the /sys/kernel/debug/pci/<device> directory of @dev, creating it
+ * and the PCI root directory if they do not exist yet. Devices that have no
+ * debugfs content never get a directory.
+ *
+ * Return: the dentry of the directory, or an ERR_PTR() on failure. The
+ * return value only needs to be checked with IS_ERR() if the caller cares;
+ * it can be passed to debugfs_create_*() as is.
+ */
+struct dentry *pci_dev_debugfs_dir(struct pci_dev *dev)
+{
+ guard(mutex)(&pci_debugfs_lock);
+
+ if (!pci_debugfs_root)
+ pci_debugfs_root = debugfs_create_dir("pci", NULL);
+
+ if (!dev->debugfs)
+ dev->debugfs = debugfs_create_dir(pci_name(dev),
+ pci_debugfs_root);
+
+ return dev->debugfs;
+}
+
+/**
+ * pci_dev_debugfs_remove - Remove the debugfs directory of a PCI device
+ * @dev: PCI device
+ *
+ * Remove the device's debugfs directory and everything below it. Returns
+ * once no debugfs operation on those files is running any more, so the
+ * owners of the files can drop the data the files refer to afterwards.
+ */
+void pci_dev_debugfs_remove(struct pci_dev *dev)
+{
+ guard(mutex)(&pci_debugfs_lock);
+
+ debugfs_remove_recursive(dev->debugfs);
+ dev->debugfs = NULL;
+}
diff --git a/drivers/pci/pci.h b/drivers/pci/pci.h
index ba3c3fddddc2..aead99a4ea34 100644
--- a/drivers/pci/pci.h
+++ b/drivers/pci/pci.h
@@ -1059,6 +1059,18 @@ static inline int pci_resource_num_to_vf_bar(int resno)
}
#endif /* CONFIG_PCI_IOV */
+#ifdef CONFIG_DEBUG_FS
+struct dentry *pci_dev_debugfs_dir(struct pci_dev *dev);
+void pci_dev_debugfs_remove(struct pci_dev *dev);
+#else
+static inline struct dentry *pci_dev_debugfs_dir(struct pci_dev *dev)
+{
+ return ERR_PTR(-ENODEV);
+}
+
+static inline void pci_dev_debugfs_remove(struct pci_dev *dev) { }
+#endif
+
#ifdef CONFIG_PCIE_TPH
void pci_restore_tph_state(struct pci_dev *dev);
void pci_save_tph_state(struct pci_dev *dev);
diff --git a/drivers/pci/remove.c b/drivers/pci/remove.c
index e711ac1d4e38..e10139117d57 100644
--- a/drivers/pci/remove.c
+++ b/drivers/pci/remove.c
@@ -37,6 +37,7 @@ static void pci_destroy_dev(struct pci_dev *dev)
platform_pci_remove_wake(dev);
pci_doe_sysfs_teardown(dev);
pci_npem_remove(dev);
+ pci_dev_debugfs_remove(dev);
/*
* While device is in D0 drop the device from TSM link operations
diff --git a/include/linux/pci.h b/include/linux/pci.h
index d31a8d107b1e..251d6eeef087 100644
--- a/include/linux/pci.h
+++ b/include/linux/pci.h
@@ -580,6 +580,9 @@ struct pci_dev {
#endif
#ifdef CONFIG_PCI_TSM
struct pci_tsm *tsm; /* TSM operation state */
+#endif
+#ifdef CONFIG_DEBUG_FS
+ struct dentry *debugfs; /* debugfs directory, if any */
#endif
u16 acs_cap; /* ACS Capability offset */
u16 acs_capabilities; /* ACS Capabilities */
--
2.55.0
next prev parent reply other threads:[~2026-10-01 15:56 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-01 15:55 [PATCH RFC 0/3] PCI/TPH: Expose CPU-to-Steering Tag mappings via debugfs Wei Huang
2026-10-01 15:55 ` [PATCH RFC 1/3] PCI/TPH: Factor out the ST _DSM evaluation Wei Huang
2026-10-01 15:55 ` Wei Huang [this message]
2026-10-01 15:55 ` [PATCH RFC 3/3] Expose the CPU/Steering Tag mapping via debugfs Wei Huang
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=20261001155551.4182899-3-wei.huang2@amd.com \
--to=wei.huang2@amd.com \
--cc=bhelgaas@google.com \
--cc=corbet@lwn.net \
--cc=fengchengwen@huawei.com \
--cc=linux-doc@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-pci@vger.kernel.org \
--cc=rdunlap@infradead.org \
--cc=skhan@linuxfoundation.org \
/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®