[PATCH v5 1/4] mmc: core: Add panic-context host operations for pstore backends

From: Kamal Dasu

Date: Wed Apr 22 2026 - 16:52:03 EST


Add three new optional callbacks to struct mmc_host_ops for panic-safe
MMC I/O:

- panic_prepare: drain in-flight requests and prepare for polled I/O
- panic_poll_completion: poll for request completion without interrupts
- panic_complete: restore normal host state after panic I/O

Add mmc_panic_claim_host() which uses WRITE_ONCE() to claim the host
without taking the spin lock, since during panic other CPUs are stopped
and may hold the lock.

Signed-off-by: Kamal Dasu <kamal.dasu@xxxxxxxxxxxx>
---
drivers/mmc/core/core.c | 54 ++++++++++++++++++++++++++++++++++++++++
include/linux/mmc/host.h | 12 +++++++++
2 files changed, 66 insertions(+)

diff --git a/drivers/mmc/core/core.c b/drivers/mmc/core/core.c
index 29e80e5f928e..076bd4eb2878 100644
--- a/drivers/mmc/core/core.c
+++ b/drivers/mmc/core/core.c
@@ -861,6 +861,60 @@ void mmc_release_host(struct mmc_host *host)
}
EXPORT_SYMBOL(mmc_release_host);

+/**
+ * mmc_panic_claim_host - force-claim a host in panic context
+ * @host: mmc host to claim
+ *
+ * Force-claims the MMC host without locking during kernel panic. Other CPUs
+ * are stopped and may be holding mmc_host->lock (e.g. inside __mmc_claim_host
+ * or mmc_release_host). Unlike sdhci_host->lock which is freed by the hardware
+ * drain+reset, mmc_host->lock has no hardware counterpart, so we must bypass
+ * it with WRITE_ONCE.
+ *
+ * This function first checks the current host state (claimed/suspended/ongoing),
+ * then calls panic_prepare() to let the controller driver safely drain any
+ * in-flight hardware requests and handle runtime PM suspend state. After
+ * panic_prepare() returns, the host is forcefully claimed for panic I/O.
+ */
+void mmc_panic_claim_host(struct mmc_host *host)
+{
+ bool claimed;
+ bool suspended;
+ struct mmc_request *ongoing;
+
+ if (!host)
+ return;
+
+ /* Check current state for visibility into what we're dealing with */
+ claimed = READ_ONCE(host->claimed);
+ suspended = pm_runtime_suspended(&host->class_dev);
+ ongoing = READ_ONCE(host->ongoing_mrq);
+
+ if (claimed || suspended || ongoing) {
+ pr_info("mmc_panic_claim: host state: claimed=%d suspended=%d ongoing_mrq=%p\n",
+ claimed, suspended, ongoing);
+ }
+
+ /* Always call panic_prepare to drain/reset hardware, regardless of claimed state.
+ * This ensures the controller is in a safe state for panic I/O even if the
+ * host was already claimed (which shouldn't happen, but we handle it safely).
+ */
+ if (host->ops && host->ops->panic_prepare) {
+ int ret = host->ops->panic_prepare(host);
+
+ if (ret)
+ pr_warn("mmc_panic_claim: panic_prepare failed: %d\n", ret);
+ }
+
+ /* Force-claim the host. Safe because all other CPUs are stopped. */
+ WRITE_ONCE(host->claimed, 1);
+ host->claimer = &host->default_ctx;
+ host->claimer->task = current;
+ WRITE_ONCE(host->claim_cnt, 1);
+ WRITE_ONCE(host->ongoing_mrq, NULL);
+}
+EXPORT_SYMBOL(mmc_panic_claim_host);
+
/*
* This is a helper function, which fetches a runtime pm reference for the
* card device and also claims the host.
diff --git a/include/linux/mmc/host.h b/include/linux/mmc/host.h
index ba84f02c2a10..cd44b62d29cb 100644
--- a/include/linux/mmc/host.h
+++ b/include/linux/mmc/host.h
@@ -272,6 +272,16 @@ struct mmc_host_ops {
* negative errno in case of a failure or zero for success.
*/
int (*uhs2_control)(struct mmc_host *host, enum sd_uhs2_operation op);
+
+ /*
+ * Optional panic-context ops for pstore backends that write to MMC
+ * during kernel panic with interrupts disabled.
+ */
+ int (*panic_prepare)(struct mmc_host *host);
+ bool (*panic_poll_completion)(struct mmc_host *host,
+ struct mmc_request *mrq);
+ void (*panic_complete)(struct mmc_host *host,
+ struct mmc_request *mrq);
};

struct mmc_cqe_ops {
@@ -758,4 +768,6 @@ int mmc_send_abort_tuning(struct mmc_host *host, u32 opcode);
int mmc_get_ext_csd(struct mmc_card *card, u8 **new_ext_csd);
int mmc_read_tuning(struct mmc_host *host, unsigned int blksz, unsigned int blocks);

+void mmc_panic_claim_host(struct mmc_host *host);
+
#endif /* LINUX_MMC_HOST_H */
--
2.34.1