Skip to contentSkip to conversation

PCI/AER: Fix memory leak in aer_recover_work_func() when pci_dev is missing

PATCH New Priyank Rathod 2 participants 2 messages Aug 3

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PCI/AER: Fix memory leak in aer_recover_work_func() when pci_dev is missing

When ACPI APEI/GHES processes PCIe AER error records, it allocates memory for aer_capability_regs (entry.regs) from ghes_estatus_pool and queues the entry into aer_recover_ring.

In aer_recover_work_func(), items are popped from aer_recover_ring via kfifo_get(). If pci_get_domain_bus_and_slot() fails to find a matching pci_dev, the code previously executed 'continue', bypassing the call to ghes_estatus_pool_region_free(). As a result, the memory allocated for entry.regs from ghes_estatus_pool was leaked.

Refactor aer_recover_work_func() to ensure ghes_estatus_pool_region_free() is called unconditionally for every dequeued entry, releasing the pool memory even when pci_dev is missing.

---
 drivers/pci/pcie/aer.c | 23 ++++++++++++-----------
 1 file changed, 12 insertions(+), 11 deletions(-)
drivers/pci/pcie/aer.c +12 -11
index c4fd9c0b2a54..555f39b60b14 100644
@@ -1223,14 +1223,13 @@ static void aer_recover_work_func(struct work_struct *work)
 	while (kfifo_get(&aer_recover_ring, &entry)) {
 		pdev = pci_get_domain_bus_and_slot(entry.domain, entry.bus,
 						   entry.devfn);
-		if (!pdev) {
+		if (!pdev)
 			pr_err_ratelimited("%04x:%02x:%02x.%x: no pci_dev found\n",
 					   entry.domain, entry.bus,
 					   PCI_SLOT(entry.devfn),
 					   PCI_FUNC(entry.devfn));
-			continue;
-		}
-		pci_print_aer(pdev, entry.severity, entry.regs);
+		else
+			pci_print_aer(pdev, entry.severity, entry.regs);
 
 		/*
 		 * Memory for aer_capability_regs(entry.regs) is being
@@ -1242,13 +1241,15 @@ static void aer_recover_work_func(struct work_struct *work)
 		ghes_estatus_pool_region_free((unsigned long)entry.regs,
 					    sizeof(struct aer_capability_regs));
 
-		if (entry.severity == AER_NONFATAL)
-			pcie_do_recovery(pdev, pci_channel_io_normal,
-					 aer_root_reset);
-		else if (entry.severity == AER_FATAL)
-			pcie_do_recovery(pdev, pci_channel_io_frozen,
-					 aer_root_reset);
-		pci_dev_put(pdev);
+		if (pdev) {
+			if (entry.severity == AER_NONFATAL)
+				pcie_do_recovery(pdev, pci_channel_io_normal,
+						 aer_root_reset);
+			else if (entry.severity == AER_FATAL)
+				pcie_do_recovery(pdev, pci_channel_io_frozen,
+						 aer_root_reset);
+			pci_dev_put(pdev);
+		}
 	}
 }
 
Signature · 1 line
2.55.0.571.g244d577d93-goog
#1 lore raw
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Memory allocated from ghes_estatus_pool for aer_regs is leaked in aer_recover_queue() when aer_recover_ring kfifo is full. --

--- Patch [1]: [PATCH] PCI/AER: Fix memory leak in aer_recover_work_func() when pci_dev is missing --- commit 6bb7331494c5c24e65c258479be47a6a7424c240
Author: Priyank Rathod <[email protected]>

PCI/AER: Fix memory leak in aer_recover_work_func() when pci_dev is missing

This commit fixes a memory leak in aer_recover_work_func() by ensuring that memory allocated for entry.regs is freed via ghes_estatus_pool_region_free() unconditionally, even when pci_get_domain_bus_and_slot() fails to find a matching PCI device.

Quoted 4 lines
drivers/pci/pcie/aer.c +0 -0
index c4fd9c0b2a548..555f39b60b14d 100644

[ ... ]

Quoted 12 lines
drivers/pci/pcie/aer.c +1 -7
@@ -1242,13 +1241,15 @@ static void aer_recover_work_func(struct work_struct *work)
 		ghes_estatus_pool_region_free((unsigned long)entry.regs,
 					    sizeof(struct aer_capability_regs));
 
-		if (entry.severity == AER_NONFATAL)
-			pcie_do_recovery(pdev, pci_channel_io_normal,
-					 aer_root_reset);
-		else if (entry.severity == AER_FATAL)
-			pcie_do_recovery(pdev, pci_channel_io_frozen,
-					 aer_root_reset);
-		pci_dev_put(pdev);
+		if (pdev) {

drivers/pci/pcie/aer.c static void aer_recover_work_func(struct work_struct *work) · line 1254

Quoted 10 lines
drivers/pci/pcie/aer.c +8 -0
+			if (entry.severity == AER_NONFATAL)
+				pcie_do_recovery(pdev, pci_channel_io_normal,
+						 aer_root_reset);
+			else if (entry.severity == AER_FATAL)
+				pcie_do_recovery(pdev, pci_channel_io_frozen,
+						 aer_root_reset);
+			pci_dev_put(pdev);
+		}
 	}
 }

[Severity: High]
This is a pre-existing issue, but while reviewing this fix for the memory leak in the consumer (aer_recover_work_func), I noticed a similar leak could occur in the producer (aer_recover_queue).

In drivers/acpi/apei/ghes.c:ghes_handle_aer(), aer_info is allocated from the ghes_estatus_pool:

	aer_info = (void *)gen_pool_alloc(ghes_estatus_pool,
					  sizeof(struct aer_capability_regs));
	/* ... */
	aer_recover_queue(pcie_err->device_id.segment,
			  pcie_err->device_id.bus,
			  devfn, aer_severity,
			  (struct aer_capability_regs *)
			  aer_info);

If the aer_recover_ring kfifo is full (e.g., during an AER storm with more than 16 errors), kfifo_in_spinlocked() fails in aer_recover_queue():

drivers/pci/pcie/aer.c:aer_recover_queue() {
    ...
	if (kfifo_in_spinlocked(&aer_recover_ring, &entry, 1,
				 &aer_recover_ring_lock))
		schedule_work(&aer_recover_work);
	else
		pr_err("buffer overflow in recovery for %04x:%02x:%02x.%x\n",
		       domain, bus, PCI_SLOT(devfn), PCI_FUNC(devfn));
}

Since aer_recover_queue() returns void, the caller ghes_handle_aer() does not know if the queueing failed. The else branch logs a buffer overflow but does not call ghes_estatus_pool_region_free() to release the memory.

Could this exhaust the finite ghes_estatus_pool during an AER storm, preventing the system from handling future hardware errors like fatal MCEs?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1