[PATCH 01/13] mmc: cavium: Remove MMC slots before destroying slot platform devices

From: Lee Jones

Date: Mon Nov 23 2026 - 14:17:31 EST


cvm_mmc_of_slot_probe() allocates each slot and its struct mmc_host via
devm_mmc_alloc_host() bound to the dummy slot platform device created in
octeon_mmc_probe() and thunder_mmc_probe(), and indexes host->slot[] by
the Device Tree reg property while host->slot_pdev[] is indexed by the
child iteration counter.

When a slot reg index is greater than its child iteration index (for
example when only slot 1 is populated or enabled), the single teardown
loop in probe error handling and driver removal calls
of_platform_device_destroy() on the slot platform device before calling
cvm_mmc_of_slot_remove() on its corresponding host->slot[] entry.
Unregistering the unbound platform device immediately releases its
managed resources, freeing slot regulators and dropping the initial
struct mmc_host reference via mmc_free_host() while the MMC host is
still registered. The subsequent cvm_mmc_of_slot_remove() call then
accesses freed regulator state and frees struct mmc_host and private
slot memory mid-way through mmc_remove_host(), resulting in a
use-after-free.

Remove all populated MMC slots in a first pass before destroying the
child slot platform devices in a second pass across both probe error and
driver removal paths.

Fixes: f32b90e6887f ("mmc: cavium: Use devm_mmc_alloc_host() helper")
Fixes: 0f4d4424fc1d ("mmc: cavium-octeon: destroy slot platform devices on remove")
Fixes: 29ee8cf18a21 ("mmc: cavium-thunderx: destroy slot platform devices on remove")
Closes: https://sashiko.dev/#/bug/linux-f5f8989d-5e08-46d1-91a1-69841c36692b
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: Lee Jones <lee@xxxxxxxxxx>
---
drivers/mmc/host/cavium-octeon.c | 4 ++++
drivers/mmc/host/cavium-thunderx.c | 4 ++++
2 files changed, 8 insertions(+)

diff --git a/drivers/mmc/host/cavium-octeon.c b/drivers/mmc/host/cavium-octeon.c
index dd62b96712c8..7b75b3dfd3e6 100644
--- a/drivers/mmc/host/cavium-octeon.c
+++ b/drivers/mmc/host/cavium-octeon.c
@@ -283,6 +283,8 @@ static int octeon_mmc_probe(struct platform_device *pdev)
for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot[i])
cvm_mmc_of_slot_remove(host->slot[i]);
+ }
+ for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot_pdev[i])
of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
}
@@ -298,6 +300,8 @@ static void octeon_mmc_remove(struct platform_device *pdev)
for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot[i])
cvm_mmc_of_slot_remove(host->slot[i]);
+ }
+ for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot_pdev[i])
of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
}
diff --git a/drivers/mmc/host/cavium-thunderx.c b/drivers/mmc/host/cavium-thunderx.c
index 526ae9019b2f..b8296b2de127 100644
--- a/drivers/mmc/host/cavium-thunderx.c
+++ b/drivers/mmc/host/cavium-thunderx.c
@@ -157,6 +157,8 @@ static int thunder_mmc_probe(struct pci_dev *pdev,
for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot[i])
cvm_mmc_of_slot_remove(host->slot[i]);
+ }
+ for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot_pdev[i])
of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
}
@@ -173,6 +175,8 @@ static void thunder_mmc_remove(struct pci_dev *pdev)
for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot[i])
cvm_mmc_of_slot_remove(host->slot[i]);
+ }
+ for (i = 0; i < CAVIUM_MAX_MMC; i++) {
if (host->slot_pdev[i])
of_platform_device_destroy(&host->slot_pdev[i]->dev, NULL);
}
--
2.56.1.750.gf70dc682cf-goog