Skip to contentSkip to conversation

pci_crash: capture PCI config space at panic time

PATCH Superseded hangej 3 participants 5 messages May 12 – May 13

Subsystems (4)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore

hangej

author
#0 lore raw

PATCH 1/1 pci_crash: capture PCI config space at panic time

From: Johannes Hange <[email protected]>

When a system crashes and kexec boots into the crash kernel, PCI device state is lost. AER registers are volatile and cleared by device reset, making post-mortem root cause analysis difficult.

Add CONFIG_PCI_CRASH to capture PCI configuration space (including AER extended capability registers) during panic, before kexec. The data is exported via VMCOREINFO so crash analysis tools can extract it from the vmcore.

Hook point: crash_save_vmcoreinfo() inside __crash_kexec(), which runs before machine_kexec() -- the only reliable capture point since panic notifiers run after kexec by default.

Key design choices:
- No config reads at init or hotplug; buffer pre-allocated, filled only at crash time for fresh register state.
- capture=aer (default): quick-scan root port ROOT_STATUS, bail if no uncorrectable errors. capture=always: every panic.
- devices= filter applied at rebuild time (zero crash-path cost).
- 200ms debounced workqueue rebuild for VF enumeration storms.
- kvmalloc buffer + kmalloc pagemap for physical page directory (vmalloc addresses need explicit page-to-phys translation).
- dcache flush (ARM64/x86) ensures data survives kexec.

Build-tested on v7.1-rc2 (x86_64). Runtime-tested on 6.6 with forced panics: ~2ms for 31 PCIe devices (4096B config each), sub-microsecond AER bail on non-PCI panics.

See Documentation/PCI/pci-crash-capture.rst for full details.

---
 Documentation/PCI/index.rst             |   1 +
 Documentation/PCI/pci-crash-capture.rst | 134 +++++
 MAINTAINERS                             |   8 +
 include/linux/pci_crash.h               | 110 ++++
 kernel/Kconfig.kexec                    |  17 +
 kernel/Makefile                         |   1 +
 kernel/pci_crash.c                      | 755 ++++++++++++++++++++++++
 kernel/vmcore_info.c                    |  13 +
 8 files changed, 1039 insertions(+)
 create mode 100644 Documentation/PCI/pci-crash-capture.rst
 create mode 100644 include/linux/pci_crash.h
 create mode 100644 kernel/pci_crash.c
Documentation/PCI/index.rst +1 -0
index 5d720d2a415e..7f499a43ddb4 100644
@@ -19,4 +19,5 @@ PCI Bus Subsystem
    endpoint/index
    controller/index
    boot-interrupts
+   pci-crash-capture
    tph
Documentation/PCI/pci-crash-capture.rst new file +134 -0

134 lines not on this page · raw message

MAINTAINERS +8 -0
index b2040011a386..3e4fbb3011ce 100644
@@ -20622,6 +20622,14 @@ T:	git git://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git
 F:	drivers/pci/pwrctrl/*
 F:	include/linux/pci-pwrctrl.h
 
+
+PCI CRASH BUFFER
+M:	Johannes Hange <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	include/linux/pci_crash.h
+F:	kernel/pci_crash.c
+
 PCI SUBSYSTEM
 M:	Bjorn Helgaas <[email protected]>
 L:	[email protected]
include/linux/pci_crash.h new file +110 -0

110 lines not on this page · raw message

kernel/Kconfig.kexec +17 -0
index 15632358bcf7..6eb49a934e50 100644
@@ -179,4 +179,21 @@ config CRASH_MAX_MEMORY_RANGES
 	  the computation behind the value provided through the
 	  /sys/kernel/crash_elfcorehdr_size attribute.
 
+
+config PCI_CRASH
+	bool "Capture PCI config space at panic time"
+	depends on VMCORE_INFO && PCI && (ARM64 || X86)
+	default y
+	help
+	  Capture PCI configuration space (including AER extended capability
+	  registers) for all PCI devices at panic time.  The data is stored
+	  in a buffer whose pages are recorded in VMCOREINFO for off-site
+	  crash analysis.
+
+	  This is useful for diagnosing PCI errors that caused or contributed
+	  to the crash, especially when AER registers are volatile and cleared
+	  by device reset during kexec.
+
+	  If unsure, say Y.
+
 endmenu
kernel/Makefile +1 -0
index 6785982013dc..6629755bc5a3 100644
@@ -82,6 +82,7 @@ obj-$(CONFIG_KEXEC_CORE) += kexec_core.o
 obj-$(CONFIG_CRASH_DUMP) += crash_core.o
 obj-$(CONFIG_CRASH_DM_CRYPT) += crash_dump_dm_crypt.o
 obj-$(CONFIG_CRASH_DUMP_KUNIT_TEST) += crash_core_test.o
+obj-$(CONFIG_PCI_CRASH) += pci_crash.o
 obj-$(CONFIG_KEXEC) += kexec.o
 obj-$(CONFIG_KEXEC_FILE) += kexec_file.o
 obj-$(CONFIG_KEXEC_ELF) += kexec_elf.o
kernel/pci_crash.c new file +755 -0

755 lines not on this page · raw message

kernel/vmcore_info.c +13 -0
index 8614430ca212..8813f7e2e516 100644
@@ -14,6 +14,7 @@
 #include <linux/cpuhotplug.h>
 #include <linux/memblock.h>
 #include <linux/kmemleak.h>
+#include <linux/pci_crash.h>
 
 #include <asm/page.h>
 #include <asm/sections.h>
@@ -91,6 +92,18 @@ void crash_save_vmcoreinfo(void)
 		vmcoreinfo_data = vmcoreinfo_data_safecopy;
 
 	vmcoreinfo_append_str("CRASHTIME=%lld\n", ktime_get_real_seconds());
+
+	/* Capture PCI config space before kexec into crash kernel */
+	pci_crash_save();
+	if (pci_crash_pagemap_phys && pci_crash_buffer_size > 0) {
+		vmcoreinfo_append_str("PCI_CRASH_PAGEMAP=0x%llx\n",
+				      (unsigned long long)pci_crash_pagemap_phys);
+		vmcoreinfo_append_str("PCI_CRASH_VERSION=%d\n",
+				      PCI_CRASH_VERSION);
+		vmcoreinfo_append_str("PCI_CRASH_BUF_SZ=%zu\n",
+				      pci_crash_buffer_size);
+	}
+
 	update_vmcoreinfo_note();
 }
 
Signature · 12 lines
2.47.3




Amazon Web Services Development Center Germany GmbH
Tamara-Danz-Str. 13
10243 Berlin
Geschaeftsfuehrung: Christof Hellmis, Andreas Stieger
Eingetragen am Amtsgericht Charlottenburg unter HRB 257764 B
Sitz: Berlin
Ust-ID: DE 365 538 597
#1 lore raw
kernel test robot noticed the following build errors:

Hi hangej,

kernel test robot noticed the following build errors:

[auto build test ERROR on pci/next]
[also build test ERROR on pci/for-linus linus/master v7.1-rc3 next-20260508] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/hangej/pci_crash-capture-PCI-config-space-at-panic-time/20260514-000653
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:    https://lore.kernel.org/r/20260512162402.1929687-1-hangej%40amazon.com
patch subject: [PATCH] pci_crash: capture PCI config space at panic time
config: i386-randconfig-r071-20260514 (https://download.01.org/0day-ci/archive/20260514/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
smatch: v0.5.0-9185-gbcc58b9c
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260514/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

kernel/pci_crash.c: In function 'pci_crash_save':

>> kernel/pci_crash.c:661:53: error: 'struct pci_dev' has no member named 'aer_cap'; did you mean 'acs_cap'?
     661 |                                 if (!pdev || !pdev->aer_cap)
         |                                                     ^~~~~~~
         |                                                     acs_cap
   kernel/pci_crash.c:666:68: error: 'struct pci_dev' has no member named 'aer_cap'; did you mean 'acs_cap'?
     666 |                                                              pdev->aer_cap + PCI_ERR_ROOT_STATUS,
         |                                                                    ^~~~~~~
         |                                                                    acs_cap

vim +661 kernel/pci_crash.c

599
600 /**
601 * pci_crash_save() - Capture PCI config space at crash time
602 *
603 * Called from crash_save_vmcoreinfo() inside __crash_kexec(), which
604 * runs before machine_kexec() boots the crash kernel. This is the
605 * only reliable capture point -- panic notifiers run AFTER kexec by
606 * default (crash_kexec_post_notifiers=0).
607 *
608 * Capture check (capture param):

   609	 *   always  -- capture unconditionally
   610	 *   aer     -- quick-scan root port AER ROOT_STATUS for uncorrectable
   611	 *             errors; skip if none found
   612	 *
   613	 * When capture=always, captures on every panic.
   614	 * This is useful for cascading failures: a PCI link-down can cause
   615	 * an MCE or NMI watchdog timeout before DPC/AER fires, so the crash
   616	 * reason is UNKNOWN but AER registers may still hold error state.
   617	 *
   618	 * Reads config space fresh -- successful reads get current register
   619	 * state, failed reads (offline devices) write 0xFFFFFFFF.
   620	 *
   621	 * Flushes both buffer and pagemap from CPU cache to RAM so data
   622	 * survives kexec into crash kernel.
   623	 */
   624	void pci_crash_save(void)
   625	{
   626		struct pci_crash_pagemap *pm;
   627		unsigned long cflags;
   628		unsigned int num_devs;
   629		size_t pm_size;
   630		size_t buf_size;
   631		void *buffer;
   632	
   633		num_devs = READ_ONCE(pci_crash_num_devs);
   634		if (num_devs == 0)
   635			return;
   636	
   637		/* Pairs with smp_wmb() in rebuild -- ensures buffer/pagemap visible */
   638		smp_rmb();
   639	
   640		buffer = READ_ONCE(pci_crash_buffer);
   641		buf_size = READ_ONCE(pci_crash_buffer_size);
   642		pm = READ_ONCE(pci_crash_pagemap);
   643		pm_size = READ_ONCE(pci_crash_pagemap_size);
   644		if (!buffer || buf_size == 0)
   645			return;
   646	
   647		cflags = READ_ONCE(capture_flags);
   648		if (!(cflags & PCI_CRASH_CAPTURE_ALWAYS)) {
   649			if (cflags & PCI_CRASH_CAPTURE_AER) {
   650				struct pci_dev **devs = READ_ONCE(pci_crash_devs);
   651				unsigned int i;
   652				bool pci_error_found = false;
   653	
   654				if (!devs)
   655					return;
   656	
   657				for (i = 0; i < num_devs; i++) {
   658					struct pci_dev *pdev = devs[i];
   659					u32 status;
   660	
 > 661					if (!pdev || !pdev->aer_cap)

662 continue;
663
664 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) {
665 pci_read_config_dword(pdev,
666 pdev->aer_cap + PCI_ERR_ROOT_STATUS,
667 &status);
668 if (status & PCI_ERR_ROOT_UNCOR_RCV) {
669 pci_error_found = true;
670 break;
671 }
672 }
673 }
674
675 if (!pci_error_found) {
676 pr_emerg("no PCI errors detected, skipping capture\n");
677 return;
678 }
679 } else {
680 return;
681 }
682 }
683
684 pci_crash_fill_buffer(buffer, num_devs);
685
686 /*
687 * Flush buffer and pagemap from CPU cache to RAM so the
688 * crash kernel sees our writes after kexec.
689 */
690 pci_crash_flush_dcache(buffer, buf_size);
691
692 if (pm && pm_size > 0)
693 pci_crash_flush_dcache(pm, pm_size);
694
695 pr_emerg("CAPTURE: %u devices, %zu bytes\n",
696 num_devs, buf_size);
697 }
698 EXPORT_SYMBOL_GPL(pci_crash_save);
699

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#2 lore raw
In reply to hangej
kernel test robot noticed the following build errors:

Hi hangej,

kernel test robot noticed the following build errors:

[auto build test ERROR on pci/next]
[also build test ERROR on pci/for-linus linus/master v7.1-rc3 next-20260508] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/hangej/pci_crash-capture-PCI-config-space-at-panic-time/20260514-000653
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:    https://lore.kernel.org/r/20260512162402.1929687-1-hangej%40amazon.com
patch subject: [PATCH] pci_crash: capture PCI config space at panic time
config: x86_64-randconfig-161-20260514 (https://download.01.org/0day-ci/archive/20260514/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
smatch: v0.5.0-9185-gbcc58b9c
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260514/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All errors (new ones prefixed by >>):

>> kernel/pci_crash.c:661:25: error: no member named 'aer_cap' in 'struct pci_dev'
     661 |                                 if (!pdev || !pdev->aer_cap)
         |                                               ~~~~  ^
   kernel/pci_crash.c:666:19: error: no member named 'aer_cap' in 'struct pci_dev'
     666 |                                                              pdev->aer_cap + PCI_ERR_ROOT_STATUS,
         |                                                              ~~~~  ^

2 errors generated.

vim +661 kernel/pci_crash.c

599
600 /**
601 * pci_crash_save() - Capture PCI config space at crash time
602 *
603 * Called from crash_save_vmcoreinfo() inside __crash_kexec(), which
604 * runs before machine_kexec() boots the crash kernel. This is the
605 * only reliable capture point -- panic notifiers run AFTER kexec by
606 * default (crash_kexec_post_notifiers=0).
607 *
608 * Capture check (capture param):

   609	 *   always  -- capture unconditionally
   610	 *   aer     -- quick-scan root port AER ROOT_STATUS for uncorrectable
   611	 *             errors; skip if none found
   612	 *
   613	 * When capture=always, captures on every panic.
   614	 * This is useful for cascading failures: a PCI link-down can cause
   615	 * an MCE or NMI watchdog timeout before DPC/AER fires, so the crash
   616	 * reason is UNKNOWN but AER registers may still hold error state.
   617	 *
   618	 * Reads config space fresh -- successful reads get current register
   619	 * state, failed reads (offline devices) write 0xFFFFFFFF.
   620	 *
   621	 * Flushes both buffer and pagemap from CPU cache to RAM so data
   622	 * survives kexec into crash kernel.
   623	 */
   624	void pci_crash_save(void)
   625	{
   626		struct pci_crash_pagemap *pm;
   627		unsigned long cflags;
   628		unsigned int num_devs;
   629		size_t pm_size;
   630		size_t buf_size;
   631		void *buffer;
   632	
   633		num_devs = READ_ONCE(pci_crash_num_devs);
   634		if (num_devs == 0)
   635			return;
   636	
   637		/* Pairs with smp_wmb() in rebuild -- ensures buffer/pagemap visible */
   638		smp_rmb();
   639	
   640		buffer = READ_ONCE(pci_crash_buffer);
   641		buf_size = READ_ONCE(pci_crash_buffer_size);
   642		pm = READ_ONCE(pci_crash_pagemap);
   643		pm_size = READ_ONCE(pci_crash_pagemap_size);
   644		if (!buffer || buf_size == 0)
   645			return;
   646	
   647		cflags = READ_ONCE(capture_flags);
   648		if (!(cflags & PCI_CRASH_CAPTURE_ALWAYS)) {
   649			if (cflags & PCI_CRASH_CAPTURE_AER) {
   650				struct pci_dev **devs = READ_ONCE(pci_crash_devs);
   651				unsigned int i;
   652				bool pci_error_found = false;
   653	
   654				if (!devs)
   655					return;
   656	
   657				for (i = 0; i < num_devs; i++) {
   658					struct pci_dev *pdev = devs[i];
   659					u32 status;
   660	
 > 661					if (!pdev || !pdev->aer_cap)

662 continue;
663
664 if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) {
665 pci_read_config_dword(pdev,
666 pdev->aer_cap + PCI_ERR_ROOT_STATUS,
667 &status);
668 if (status & PCI_ERR_ROOT_UNCOR_RCV) {
669 pci_error_found = true;
670 break;
671 }
672 }
673 }
674
675 if (!pci_error_found) {
676 pr_emerg("no PCI errors detected, skipping capture\n");
677 return;
678 }
679 } else {
680 return;
681 }
682 }
683
684 pci_crash_fill_buffer(buffer, num_devs);
685
686 /*
687 * Flush buffer and pagemap from CPU cache to RAM so the
688 * crash kernel sees our writes after kexec.
689 */
690 pci_crash_flush_dcache(buffer, buf_size);
691
692 if (pm && pm_size > 0)
693 pci_crash_flush_dcache(pm, pm_size);
694
695 pr_emerg("CAPTURE: %u devices, %zu bytes\n",
696 num_devs, buf_size);
697 }
698 EXPORT_SYMBOL_GPL(pci_crash_save);
699

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#3 lore raw
In reply to hangej
kernel test robot noticed the following build warnings:

Hi hangej,

kernel test robot noticed the following build warnings:

[auto build test WARNING on pci/next]
[also build test WARNING on pci/for-linus linus/master v7.1-rc3 next-20260508] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/hangej/pci_crash-capture-PCI-config-space-at-panic-time/20260514-000653
base:   https://git.kernel.org/pub/scm/linux/kernel/git/pci/pci.git next
patch link:    https://lore.kernel.org/r/20260512162402.1929687-1-hangej%40amazon.com
patch subject: [PATCH] pci_crash: capture PCI config space at panic time
config: x86_64-kexec (https://download.01.org/0day-ci/archive/20260514/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260514/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> Warning: kernel/pci_crash.c:74 cannot understand function prototype: 'void *pci_crash_buffer;'
>> Warning: kernel/pci_crash.c:80 cannot understand function prototype: 'size_t pci_crash_buffer_size;'
>> Warning: kernel/pci_crash.c:90 cannot understand function prototype: 'phys_addr_t pci_crash_pagemap_phys;'

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#4 lore raw
In reply to hangej
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [Critical] Deadlock in panic path due to global pci_lock acquisition. - [High] Hardware error and…

Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [Critical] Deadlock in panic path due to global pci_lock acquisition.
- [High] Hardware error and recursive panic from accessing offline or D3cold devices.
- [High] Lockless array teardown and missing memory barriers cause Use-After-Free during panic.
- [High] Loss of intra-page offset in the physical pagemap for kmalloc-backed buffers.
- [Medium] Resource leak when all matched devices are hot-removed.
- [Medium] Uninitialized variable used in AER capture check.
- [Medium] Missing locking in sysfs capture parameter setter leads to data race.
- [Low] Ignoring strim() return value prevents stripping of leading whitespace.
- [Low] Commit message incorrectly claims the use of slab_is_available().
--

commit f80de92901e525b2a8355b2b6d9ab3d784f115db
Author: Johannes Hange <[email protected]>
Subject: pci_crash: capture PCI config space at panic time

This commit introduces CONFIG_PCI_CRASH to capture PCI configuration space data during a kernel panic. The data is stored in a buffer that can be extracted from the vmcore for post-mortem analysis.

Quoted 4 lines
Documentation/PCI/pci-crash-capture.rst new file +0 -0
index 0000000000000..35f676c2724b7

[ ... ]

Quoted 2 lines
+- slab_is_available() guard in param setters prevents use-before-init
+  when parameters are set via kernel command line.

This isn't a bug, but the commit message and documentation mention using a slab_is_available() guard. It looks like the code actually uses READ_ONCE(pci_crash_ready). Could this documentation be updated to match the implementation?

Quoted 5 lines
kernel/pci_crash.c new file +0 -0
new file mode 100644
index 0000000000000..8bbc9008fdc85

[ ... ]

kernel/pci_crash.c line 130

Quoted 6 lines
kernel/pci_crash.c new file +6 -0
+static int capture_param_set(const char *val, const struct kernel_param *kp)
+{
+	if (strlen(val) >= sizeof(capture))
+		return -EINVAL;
+	strscpy(capture, val, sizeof(capture));
+	strim(capture);

Could concurrent sysfs writes from userspace cause data races here? The devices_param_set() function protects the string modification with pci_crash_lock, but it appears to be missing in this setter.

Also, does this code correctly handle leading whitespace? The strim() function modifies the string in-place to remove trailing whitespace, but it returns a pointer to the first non-whitespace character to skip leading whitespace. Since the return value is ignored here and in devices_param_set(), any leading whitespace would be preserved and might break the exact token string comparisons later.

[ ... ]

Quoted 3 lines
kernel/pci_crash.c new file +3 -0
+static struct pci_crash_pagemap *pci_crash_build_pagemap(void *buf,
+							 size_t buf_size)
+{

[ ... ]

Quoted 5 lines
kernel/pci_crash.c new file +5 -0
+	for (i = 0; i < num_pages; i++) {
+		struct page *page;
+		phys_addr_t pa;
+
+		if (is_vmalloc_addr(buf + i * PAGE_SIZE))

kernel/pci_crash.c line 359

Quoted 10 lines
kernel/pci_crash.c new file +10 -0
+			page = vmalloc_to_page(buf + i * PAGE_SIZE);
+		else
+			page = virt_to_page(buf + i * PAGE_SIZE);
+
+		if (!page) {
+			kfree(pm);
+			return NULL;
+		}
+		pa = page_to_phys(page);
+		pm->addrs[i] = cpu_to_le64(pa);

Can this lose the intra-page offset for the buffer? The kvmalloc() function falls back to kmalloc() for smaller allocations, which are aligned to slab boundaries rather than page boundaries. The page_to_phys() macro returns the physical address of the start of the page, so won't the crash parser read from the page boundary instead of the actual buffer start?

[ ... ]

Quoted 2 lines
kernel/pci_crash.c new file +2 -0
+static void pci_crash_read_config_space(struct pci_dev *pdev, u8 *ptr)
+{

[ ... ]

kernel/pci_crash.c line 389

Quoted 2 lines
kernel/pci_crash.c new file +2 -0
+	for (offset = 0; offset < pdev->cfg_size; offset += 4) {
+		if (pci_read_config_dword(pdev, offset, &val)) {

Could this trigger a recursive panic when accessing offline or D3cold devices? The code iterates over all captured devices and reads their config space without checking their power state. If a device is in D3cold or otherwise unreachable, accessing it will cause a PCIe Master Abort.

On some architectures like ARM64, unhandled Master Aborts trigger an SError, which would cause an immediate hard hang or reset when raised from within the panic handler.

Also, the commit message claims that pci_read_config_dword() is lockless. Is this description accurate? Looking at pci_read_config_dword(), the generic implementation unconditionally calls pci_bus_read_config_dword(), which acquires the global raw spinlock pci_lock. If the kernel panics while this lock is held by another CPU, won't the panicking CPU spin indefinitely attempting to acquire it and deadlock the panic handler?

Quoted 6 lines
kernel/pci_crash.c new file +6 -0
+			put_unaligned_le32(0xFFFFFFFF, &cfg_data[offset]);
+			continue;
+		}
+		put_unaligned_le32(val, &cfg_data[offset]);
+	}
+}

[ ... ]

Quoted 2 lines
kernel/pci_crash.c new file +2 -0
+static void pci_crash_rebuild_snapshot(void)
+{

[ ... ]

kernel/pci_crash.c line 486

Quoted 12 lines
kernel/pci_crash.c new file +12 -0
+	if (count == 0) {
+		pr_info("no PCI devices found\n");
+		WRITE_ONCE(pci_crash_num_devs, 0);
+		if (old_devs) {
+			for (i = 0; i < old_num_devs; i++)
+				pci_dev_put(old_devs[i]);
+			kvfree(old_devs);
+			WRITE_ONCE(pci_crash_devs, NULL);
+		}
+		kfree(old_pm);
+		WRITE_ONCE(pci_crash_pagemap, NULL);
+		WRITE_ONCE(pci_crash_pagemap_phys, 0);

Could this result in a use-after-free during a panic?

On weakly ordered architectures, the CPU store buffer might delay making pci_crash_num_devs = 0 visible globally while kvfree(old_devs) or kfree(old_pm) modify allocator metadata in memory.

If an NMI stops this CPU at this exact point, could the panicking CPU observe a non-zero device count and traverse the freed old_devs array or export a freed physical address from old_pm? Should there be an smp_wmb() after writing 0 to pci_crash_num_devs?

Quoted 6 lines
kernel/pci_crash.c new file +6 -0
+		old_buf = pci_crash_buffer;
+		WRITE_ONCE(pci_crash_buffer, NULL);
+		WRITE_ONCE(pci_crash_buffer_size, 0);
+		kvfree(old_buf);
+		return;
+	}

[ ... ]

Quoted 5 lines
kernel/pci_crash.c new file +5 -0
+	if (count == 0) {
+		kvfree(new_devs);
+		pr_info("no devices match devices=%s\n", devices);
+		goto err_restore;
+	}

[ ... ]

kernel/pci_crash.c line 598

Quoted 3 lines
kernel/pci_crash.c new file +3 -0
+err_restore:
+	WRITE_ONCE(pci_crash_num_devs, old_num_devs);
+}

Does this code leak the struct pci_dev references for the removed devices? When jumping to err_restore, it restores pci_crash_num_devs to old_num_devs but doesn't appear to drop the references on the old_devs array, pinning their memory indefinitely.

[ ... ]

Quoted 2 lines
kernel/pci_crash.c new file +2 -0
+void pci_crash_save(void)
+{

[ ... ]

Quoted 11 lines
kernel/pci_crash.c new file +11 -0
+	if (!(cflags & PCI_CRASH_CAPTURE_ALWAYS)) {
+		if (cflags & PCI_CRASH_CAPTURE_AER) {
+			struct pci_dev **devs = READ_ONCE(pci_crash_devs);
+			unsigned int i;
+			bool pci_error_found = false;
+
+			if (!devs)
+				return;
+
+			for (i = 0; i < num_devs; i++) {
+				struct pci_dev *pdev = devs[i];

kernel/pci_crash.c line 668

Quoted 10 lines
kernel/pci_crash.c new file +10 -0
+				u32 status;
+
+				if (!pdev || !pdev->aer_cap)
+					continue;
+
+				if (pci_pcie_type(pdev) == PCI_EXP_TYPE_ROOT_PORT) {
+					pci_read_config_dword(pdev,
+							     pdev->aer_cap + PCI_ERR_ROOT_STATUS,
+							     &status);
+					if (status & PCI_ERR_ROOT_UNCOR_RCV) {

Can this evaluate uninitialized stack memory? If pci_read_config_dword() fails early, it returns an error without populating the pointer, leaving the status variable with uninitialized stack garbage. The return value isn't checked before evaluating status here.

Quoted 5 lines
kernel/pci_crash.c new file +5 -0
+						pci_error_found = true;
+						break;
+					}
+				}
+			}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1