pci_crash: capture PCI config space at panic time
Subsystems (4)
PATCH v2 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.
---
v2:
- Kconfig: remove default y (opt-in, not auto-enabled)
- Default changed to capture=always (better for general consumers; capture=aer available as opt-in optimization)
- Fix build with CONFIG_PCIEAER=n: guard pdev->aer_cap access (kernel test robot)
- Warn at init when capture=aer but CONFIG_PCIEAER=n (only if ALWAYS not also set)
- Fix MAINTAINERS alphabetical order (PCI CRASH before PCI DRIVER)
- Drop redundant OOM messages (kvmalloc/kmalloc already warn)
- Add Documentation/admin-guide/kernel-parameters.txt entries
- Fix kernel-doc warnings on exported variable declarations
- Initialize status variable in AER scan
- Fix documentation: pci_crash_ready replaces slab_is_available()
- Correct pci_lock comment (raw spinlock, not lockless)
- pr_emerg → pr_info for non-emergency messages in crash path
Documentation/PCI/index.rst | 1 + Documentation/PCI/pci-crash-capture.rst | 137 ++++ .../admin-guide/kernel-parameters.txt | 16 + MAINTAINERS | 8 + include/linux/pci_crash.h | 110 +++ kernel/Kconfig.kexec | 16 + kernel/Makefile | 1 + kernel/pci_crash.c | 750 ++++++++++++++++++ kernel/vmcore_info.c | 13 + 9 files changed, 1052 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
@@ -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 +137 -0
137 lines not on this page · raw message
Documentation/admin-guide/kernel-parameters.txt +16 -0
@@ -5331,6 +5331,22 @@ Kernel parameters nomsi Do not use MSI for native PCIe PME signaling (this makes all PCIe root ports use INTx for all services). + pci_crash.capture= + [PCI] When to capture PCI config space at panic time. + always (default): capture on every panic. + aer: capture only if root port AER reports + uncorrectable errors. + Requires CONFIG_PCI_CRASH=y. + + pci_crash.devices= + [PCI] Which devices to include in crash capture. + all (default): every PCI device. + bridges: PCI bridges only. + root_ports: PCIe root ports only. + XXYY: hex class code (up to 8). + Bridges always implicitly included. + Requires CONFIG_PCI_CRASH=y. + pcmv= [HW,PCMCIA] BadgePAD 4 pd_ignore_unused
MAINTAINERS +8 -0
@@ -20304,6 +20304,14 @@ S: Maintained F: drivers/leds/leds-pca9532.c F: include/linux/leds-pca9532.h +PCI CRASH BUFFER +M: Johannes Hange <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/PCI/pci-crash-capture.rst +F: include/linux/pci_crash.h +F: kernel/pci_crash.c + PCI DRIVER FOR AARDVARK (Marvell Armada 3700) M: Thomas Petazzoni <[email protected]> M: Pali Rohár <[email protected]>
include/linux/pci_crash.h new file +110 -0
110 lines not on this page · raw message
kernel/Kconfig.kexec +16 -0
@@ -179,4 +179,20 @@ 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) + 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
@@ -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 +750 -0
750 lines not on this page · raw message
kernel/vmcore_info.c +13 -0
@@ -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 (); }
base-commit: 50897c955902c93ae71c38698abb910525ebdc89
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
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider: - [High] Calling pci_read_config_dword() in the panic path can cause a hard deadlock if another CPU was…
Thank you for your contribution! Sashiko AI review found 9 potential issue(s) to consider:
- [High] Calling pci_read_config_dword() in the panic path can cause a hard deadlock if another CPU was halted while holding the pci_lock raw spinlock.
- [High] pci_crash_build_pagemap() discards the intra-page offset for kvmalloc buffers, exposing garbage data to the crash parser.
- [High] Lack of RCU synchronization and memory barriers during rebuild allows the panic path to hit use-after-free or out-of-bounds array accesses.
- [High] Accessing the PCI configuration space of devices in the D3cold power state triggers fatal bus errors.
- [Medium] Missing mutex in capture_param_set() allows concurrent sysfs writes to corrupt the configuration string.
- [Medium] The default capture mode in code contradicts the documented default in the commit message.
- [Medium] Rebuild incorrectly retains stale state and leaks device references if zero devices match the filter.
- [Low] Ignoring the return value of strim() fails to remove leading whitespace from module parameters.
- [Low] Registering the bus notifier after the initial snapshot creates a race condition that can miss hotplug events.
--
commit 0e9239796d3fc7bf4512456a275c5174281a6334
Author: Johannes Hange <[email protected]>pci_crash: capture PCI config space at panic time
This commit adds CONFIG_PCI_CRASH to capture PCI configuration space (including AER registers) during a panic. The data is written to a pre-allocated buffer and exported via VMCOREINFO for crash analysis.
Quoted 2 lines
- capture=aer (default): quick-scan root port
ROOT_STATUS, bail if no uncorrectable errors. capture=always: every panic.
[Severity: Medium]
This isn't a bug in the code, but the commit message states the default capture mode is aer, while the code initializes it to always. Should the documentation and code match?
Quoted 3 lines
kernel/pci_crash.c new file +0
-0
[ ... ]
kernel/pci_crash.c line 123
Quoted 10 lines
kernel/pci_crash.c new file +10
-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); +if (READ_ONCE (pci_crash_ready)) +pci_crash_parse_capture (); +return 0 ; +}
[Severity: Medium]
Is it possible for concurrent sysfs writes to corrupt the configuration string since this locklessly modifies the global capture array?
[Severity: Low]
Does ignoring the return value of strim() here (and in devices_param_set) leave leading spaces in the buffer, causing exact string matches (like strcmp(token, "aer")) to fail later?
[ ... ]
Quoted 19 lines
kernel/pci_crash.c new file +19
-0+static struct pci_crash_pagemap *pci_crash_build_pagemap (void *buf, +size_t buf_size) +{ +unsigned int num_pages =DIV_ROUND_UP (buf_size,PAGE_SIZE ); +struct pci_crash_pagemap *pm; +unsigned int i; + + pm =kmalloc (struct_size (pm, addrs, num_pages),GFP_KERNEL ); +if (!pm) +return NULL ; + + pm->magic =cpu_to_le32 (PCI_CRASH_PAGEMAP_MAGIC ); + pm->num_pages =cpu_to_le32 (num_pages); + pm->buf_size =cpu_to_le64 (buf_size); + +for (i =0 ; i < num_pages; i++) { +struct page *page; +phys_addr_t pa; +
kernel/pci_crash.c line 347
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+if (is_vmalloc_addr (buf + i *PAGE_SIZE )) + 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);
[Severity: High]
Could this discard the intra-page offset for kvmalloc buffers? When kvmalloc falls back to kmalloc for small sizes, the returned buffer might not be page-aligned. Would calculating the physical address from the page boundary cause the vmcore parser to read unrelated slab data and miss the PCI_CRASH_MAGIC header?
[ ... ]
Quoted 8 lines
kernel/pci_crash.c new file +8
-0+static void pci_crash_read_config_space (struct pci_dev *pdev,u8 *ptr) +{ +struct pci_crash_device_record *record = + (struct pci_crash_device_record *)ptr; +u8 *cfg_data = ptr +PCI_CRASH_RECORD_META ; +int offset; +u32 val; +
kernel/pci_crash.c line 381
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+ record->domain =cpu_to_le16 (pci_domain_nr (pdev->bus)); + record->bus = pdev->bus->number; + record->devfn = pdev->devfn; + record->config_size =cpu_to_le32 (pdev->cfg_size); + +for (offset =0 ; offset < pdev->cfg_size; offset +=4 ) { +if (pci_read_config_dword (pdev, offset, &val)) { +put_unaligned_le32 (0xFFFFFFFF , &cfg_data[offset]); +continue ; + }
[Severity: High]
Will accessing the configuration space of devices in the D3cold power state trigger fatal bus errors? Unconditional MMIO reads to a physically powered-off device can cause unhandled Master/Target Aborts, potentially escalating to fatal exceptions (MCE/SError) that immediately double-fault the panic path.
[ ... ]
Quoted 4 lines
kernel/pci_crash.c new file +4
-0+static void pci_crash_rebuild_snapshot (void ) +{ +struct pci_dev *pdev =NULL ; +unsigned int count =0 , i;
[ ... ]
Quoted 16 lines
kernel/pci_crash.c new file +16
-0+/* +* Pass 2: populate device array (filtered) and compute exact +* buffer size from actual pdev->cfg_size per device. +* count from pass 1 is an upper bound; actual may be smaller. +*/ + total_size =PCI_CRASH_HEADER_SIZE ; + pdev =NULL ; + i =0 ; +for_each_pci_dev (pdev) { +if (i >= count) { +pci_dev_put (pdev); +break ; + } +if (!pci_crash_device_matches (pdev)) +continue ; + new_devs[i] =pci_dev_get (pdev);
kernel/pci_crash.c line 521
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+ total_size +=PCI_CRASH_RECORD_META + pdev->cfg_size; + i++; + } + count = i; + +if (count ==0 ) { +kvfree (new_devs); +pr_info ("no devices match devices=%s\n" , devices); + goto err_restore; + }
[Severity: Medium]
If a hotplug removal or filter change results in zero matching devices, this jumps to err_restore and restores the old count, keeping the old_devs array intact. Does this leak the refcounts of the removed devices and continue to expose stale device data in the crash buffer?
[ ... ]
Quoted 16 lines
kernel/pci_crash.c new file +16
-0+/* Release old device references */ +for (i =0 ; i < old_num_devs; i++) +if (old_devs && old_devs[i]) +pci_dev_put (old_devs[i]); + +WRITE_ONCE (pci_crash_devs, new_devs); +kvfree (old_devs); + +/* +* Swap pagemap first (with pre-computed phys addr), then buffer, +* then size and count. If crash fires mid-swap, num_devs is +* still 0 from above, so pci_crash_save() bails out safely. +*/ +WRITE_ONCE (pci_crash_pagemap, new_pm); +WRITE_ONCE (pci_crash_pagemap_phys,virt_to_phys (new_pm)); + WRITE_ONCE(pci_crash_pagemap_size, struct_size(new_pm, addrs,
kernel/pci_crash.c line 574
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+ le32_to_cpu(new_pm->num_pages))); +kfree (old_pm); + + old_buf = pci_crash_buffer; +WRITE_ONCE (pci_crash_buffer, new_buf); +WRITE_ONCE (pci_crash_buffer_size, total_size); +/* Ensure buffer/pagemap/size are visible before num_devs enables capture */ +smp_wmb (); +WRITE_ONCE (pci_crash_num_devs, count); +kvfree (old_buf);
[Severity: High]
Does the lack of RCU synchronization and memory barriers here allow the panic path to hit a use-after-free or out-of-bounds array access?
The old_devs array is freed immediately after updating the pointer, and new_devs can be smaller. A panicking CPU could locklessly read a stale, larger pci_crash_num_devs and then iterate over the new smaller array, or iterate over old_devs while it is being freed.
[ ... ]
Quoted 8 lines
kernel/pci_crash.c new file +8
-0+void pci_crash_save (void ) +{ +struct pci_crash_pagemap *pm; +unsigned long cflags; +unsigned int num_devs; +size_t pm_size; +size_t buf_size; +void *buffer;
[ ... ]
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+#ifdef CONFIG_PCIEAER +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++) {
kernel/pci_crash.c line 652
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+struct pci_dev *pdev = devs[i]; +u32 status =0 ; + +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);
[Severity: High]
Can calling pci_read_config_dword() in the panic path cause a hard deadlock? This function acquires the global pci_lock raw spinlock. If another CPU was forcefully halted by an NMI while holding this lock, the panicking CPU will spin indefinitely and completely hang the crash dump process.
[ ... ]
Quoted 9 lines
kernel/pci_crash.c new file +9
-0+static int __init pci_crash_init (void ) +{ +/* Nothing to do in crash kernel -- the buffer from the first kernel +* is already in RAM (flushed before kexec) and the parser finds it +* via the pagemap in VMCOREINFO. +*/ +if (is_kdump_kernel ()) +return 0 ; +
kernel/pci_crash.c line 729
Quoted 10 lines
kernel/pci_crash.c new file +10
-0+INIT_DELAYED_WORK (&pci_crash_rebuild_dwork, pci_crash_rebuild_worker); + +pci_crash_parse_capture (); +pci_crash_parse_devices (); + +mutex_lock (&pci_crash_lock); +pci_crash_rebuild_snapshot (); +mutex_unlock (&pci_crash_lock); + +bus_register_notifier (&pci_bus_type, &pci_crash_bus_nb);
[Severity: Low]
Does registering the bus notifier after the initial snapshot create a race condition? Any PCI hotplug events occurring in the window between the snapshot completion and the notifier registration might be missed, leaving the crash buffer out of sync with the hardware.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
PATCH v3 1/1 pci_crash: capture PCI config space at panic time
In reply to hangejFrom: 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; the buffer is pre-allocated and filled only at crash time so the registers reflect crash-time state.
- capture=always (default): capture all devices unconditionally. capture=aer: quick-scan root port ROOT_STATUS and bail if no uncorrectable errors are present. always is the default because a PCI link-down can cause an MCE or NMI watchdog timeout before DPC/AER fires, so the crash reason is unrelated yet the AER registers still hold the originating error.
- devices= filter applied at rebuild time (zero crash-path cost).
- 200ms debounced workqueue rebuild for VF enumeration storms.
- kvmalloc buffer + kmalloc pagemap for the physical page directory (vmalloc addresses need explicit page-to-phys translation).
- dcache flush (arm64/x86) ensures the data survives kexec.
Crash-context safety:
- Config reads use pci_bus_read_config_dword_trylock(), a new internal accessor in drivers/pci/access.c (where pci_lock lives) that takes pci_lock with a trylock and skips the device on contention. Depending on crash_kexec_post_notifiers the other CPUs may still be running or already halted (possibly holding pci_lock), and the panicking CPU may itself have been interrupted mid config access while holding it. pci_lock is a raw, non-reentrant spinlock, so a blocking acquire could deadlock the dump in either case. The trylock covers pci_lock only: arm64 ECAM and x86-64 MMCONFIG are fully covered; legacy x86 conf1/mmconfig_32 take a second blocking lock (pci_config_lock) below bus->ops->read() and remain outside its scope. This does not make the read fault-tolerant, which is why unreachable devices are skipped first. The accessor is declared in linux/pci_crash.h and is not exported, since its only caller is this built-in feature.
- A config read to a device whose PCIe link is physically down can raise a synchronous external abort on some architectures (notably arm64), which in the crash path is unrecoverable. Reachability is therefore decided before touching the endpoint: pci_dev_is_disconnected() / channel_offline / D3cold are flag-only (no MMIO), and the immediate upstream PCIe port's Data Link Layer Link Active bit is read on the on-die parent (which always responds) to detect a down link without accessing the endpoint itself. Every crash-path config read is fail-closed: if a read cannot complete (lock contention or PCIBIOS error), the device is treated as unreachable and the AER quick-scan treats it as "no error seen".
See Documentation/PCI/pci-crash-capture.rst for the narrow arm64 residual (TOCTOU and multi-level link loss), which is documented rather than silently left in place.
- Live capture state is a single RCU-published snapshot; the rebuild worker swaps it via rcu_assign_pointer() and retires the old one via call_rcu(), while pci_crash_save() reads it under rcu_read_lock(). RCU keeps the snapshot alive for the fill, but the exported buffer/pagemap addresses are consumed after pci_crash_save() returns (the VMCOREINFO export, and machine_kexec() snapshotting RAM) -- i.e. after rcu_read_unlock(). On the default panic path peer CPUs are still live and may retire snapshots. pci_crash_save() therefore pins the snapshot it captured (pci_crash_captured_snap); the RCU free callback leaks a pinned snapshot instead of freeing it, which is harmless while rebooting into the crash kernel.
- The buffer is capped at 24 MiB, per-device reads clamped to 4096 bytes, and every record bounds-checked against the buffer end.
The on-wire buffer/pagemap layout is shared with the userspace vmcore parser and is asserted with BUILD_BUG_ON(); the format and the exported VMCOREINFO keys (PCI_CRASH_PAGEMAP, PCI_CRASH_VERSION, PCI_CRASH_BUF_SZ) are documented in Documentation/PCI/pci-crash-capture.rst.
Build-tested on x86_64 (mainline). Runtime-tested on 6.6 with forced
panics: ~2ms for 31 PCIe devices (4096B config each), sub-microsecond AER bail on non-PCI panics. The arm64 read path is not yet build- or runtime-tested in this environment.
See Documentation/PCI/pci-crash-capture.rst for full details.
--- Documentation/PCI/index.rst | 1 + Documentation/PCI/pci-crash-capture.rst | 232 ++++ .../admin-guide/kernel-parameters.txt | 16 + MAINTAINERS | 8 + drivers/pci/access.c | 60 + include/linux/pci_crash.h | 122 ++ kernel/Kconfig.kexec | 16 + kernel/Makefile | 1 + kernel/pci_crash.c | 1001 +++++++++++++++++ kernel/vmcore_info.c | 13 + 10 files changed, 1470 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
@@ -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 +232 -0
232 lines not on this page · raw message
Documentation/admin-guide/kernel-parameters.txt +16 -0
@@ -5298,6 +5298,22 @@ Kernel parameters nomsi Do not use MSI for native PCIe PME signaling (this makes all PCIe root ports use INTx for all services). + pci_crash.capture= + [PCI] When to capture PCI config space at panic time. + always (default): capture on every panic. + aer: capture only if root port AER reports + uncorrectable errors. + Requires CONFIG_PCI_CRASH=y. + + pci_crash.devices= + [PCI] Which devices to include in crash capture. + all (default): every PCI device. + bridges: PCI bridges only. + root_ports: PCIe root ports only. + XXYY: hex class code (up to 8). + Bridges always implicitly included. + Requires CONFIG_PCI_CRASH=y. + pcmv= [HW,PCMCIA] BadgePAD 4 pd_ignore_unused
MAINTAINERS +8 -0
@@ -20560,6 +20560,14 @@ S: Maintained F: drivers/leds/leds-pca9532.c F: include/linux/leds-pca9532.h +PCI CRASH BUFFER +M: Johannes Hange <[email protected]> +L: [email protected] +S: Maintained +F: Documentation/PCI/pci-crash-capture.rst +F: include/linux/pci_crash.h +F: kernel/pci_crash.c + PCI DRIVER FOR AARDVARK (Marvell Armada 3700) M: Thomas Petazzoni <[email protected]> M: Pali Rohár <[email protected]>
drivers/pci/access.c +60 -0
80 lines not on this page · raw message
include/linux/pci_crash.h new file +122 -0
122 lines not on this page · raw message
kernel/Kconfig.kexec +16 -0
@@ -179,4 +179,20 @@ 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) + 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
@@ -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 +1001 -0
1,001 lines not on this page · raw message
kernel/vmcore_info.c +13 -0
@@ -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 · 1 line
2.47.3
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider: - [High] Calling `pci_dev_put()` from an RCU softirq callback can sleep in atomic context. - [High]…
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:
- [High] Calling pci_dev_put() from an RCU softirq callback can sleep in atomic context.
- [High] Accessing bus->ops->read on a removed device causes a Use-After-Free if the host controller driver was unloaded.
- [Medium] Skipped captures export un-flushed zeroed buffers, allowing the crash parser to read stale crash data from RAM. --
--- Patch [1]: [PATCH v3] pci_crash: capture PCI config space at panic time --- commit 93c2fd5e7de948b1f47b018b3d8a6d93723570c6
Author: Johannes Hange <[email protected]>
pci_crash: capture PCI config space at panic time
This commit adds CONFIG_PCI_CRASH to capture PCI configuration space during a panic before kexec. It uses a pre-allocated buffer exported via VMCOREINFO and relies on RCU snapshots and lockless accesses to safely record device state.
drivers/pci/access.c int pci_bus_read_config_dword_trylock(struct pci_bus *bus, unsigned int devfn, · line 135
Quoted 9 lines
drivers/pci/access.c +0
-0@@ -131,7 +131,7 @@ int pci_bus_read_config_dword_trylock(struct pci_bus *bus, unsigned int devfn,if (!pci_trylock_config (flags)) {PCI_SET_ERROR_RESPONSE (value);return PCIBIOS_SET_FAILED ; } res = bus->ops->read (bus, devfn, pos,4 , &data);
[Severity: High]
If a host controller driver is unloaded right before a panic, could this dereference freed module memory?
Because the bus notifier uses a 200ms debounce delay, the crash snapshot might hold a stale pci_dev reference. The reference keeps the pci_dev struct alive, but if the controller module was unloaded, bus->ops->read points to freed memory. Clean removals do not set error_state, so the pci_crash_endpoint_reachable() check might not filter the device out.
[ ... ]
kernel/pci_crash.c static void pci_crash_snapshot_free_rcu(struct rcu_head *head) · line 147
Quoted 10 lines
kernel/pci_crash.c +0
-0@@ -142,6 +142,6 @@ static void pci_crash_snapshot_free_rcu(struct rcu_head *head)if (READ_ONCE (pci_crash_captured_snap) == s)return ;for (i =0 ; i < s->num_devs; i++)if (s->devs && s->devs[i])pci_dev_put (s->devs[i]);
[Severity: High]
Can this call to pci_dev_put() sleep in atomic context?
This runs in an RCU softirq callback. If a device was hot-removed, this snapshot might hold the final reference to the device. Dropping the last reference triggers device_release() -> pci_release_dev() -> devres_release_all(), which may take sleeping locks or allocate memory with GFP_KERNEL.
[ ... ]
Quoted 15 lines
kernel/pci_crash.c +0
-0@@ -864,14 +864,14 @@ void pci_crash_save(void)WRITE_ONCE (pci_crash_captured_snap, s); pci_crash_buffer = s->buffer; pci_crash_buffer_size = s->buffer_size; pci_crash_pagemap_phys = s->pagemap_phys; cflags =READ_ONCE (capture_flags);if (!(cflags &PCI_CRASH_CAPTURE_ALWAYS )) {if (!(cflags &PCI_CRASH_CAPTURE_AER )) { goto out; }if (!pci_crash_aer_error_present (s)) {pr_info ("no PCI errors detected, skipping capture\n" ); goto out;
kernel/pci_crash.c
Quoted 2 lines
kernel/pci_crash.c +0
-0} }
[Severity: Medium]
If the capture is skipped and jumps to the out label, could the crash parser read stale data from RAM?
The export variables are set before the AER check. If no AER errors are found, the code jumps out without flushing the dcache. Since the buffer and pagemap were written in process context during rebuild, they might only reside in the CPU cache.
The crash kernel will read physical RAM, which might contain an older, valid PCIC magic signature from a previous boot or panic, leading to stale records being parsed.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1