Linux MultiMedia Card development
 help / color / mirror / Atom feed
From: Kamal Dasu <kamal.dasu@broadcom.com>
To: Ulf Hansson <ulf.hansson@linaro.org>
Cc: Adrian Hunter <adrian.hunter@intel.com>,
	Kees Cook <kees@kernel.org>, Tony Luck <tony.luck@intel.com>,
	"Guilherme G . Piccoli" <gpiccoli@igalia.com>,
	Florian Fainelli <florian.fainelli@broadcom.com>,
	Arend van Spriel <arend.vanspriel@broadcom.com>,
	William Zhang <william.zhang@broadcom.com>,
	bcm-kernel-feedback-list@broadcom.com, linux-mmc@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Kamal Dasu <kamal.dasu@broadcom.com>
Subject: [PATCH v5 1/4] mmc: core: Add panic-context host operations for pstore backends
Date: Wed, 22 Apr 2026 16:50:50 -0400	[thread overview]
Message-ID: <20260422205053.3392395-2-kamal.dasu@broadcom.com> (raw)
In-Reply-To: <20260422205053.3392395-1-kamal.dasu@broadcom.com>

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@broadcom.com>
---
 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


  reply	other threads:[~2026-04-22 20:51 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-22 20:50 [PATCH v5 0/4] mmc: Add pstore backend for crash dump storage on eMMC Kamal Dasu
2026-04-22 20:50 ` Kamal Dasu [this message]
2026-04-22 20:50 ` [PATCH v5 2/4] mmc: sdhci: Implement panic-context write support Kamal Dasu
2026-04-22 20:50 ` [PATCH v5 3/4] mmc: block: Add helper to look up mmc_card by device name Kamal Dasu
2026-04-22 20:50 ` [PATCH v5 4/4] mmc: core: Add MMC pstore backend driver Kamal Dasu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260422205053.3392395-2-kamal.dasu@broadcom.com \
    --to=kamal.dasu@broadcom.com \
    --cc=adrian.hunter@intel.com \
    --cc=arend.vanspriel@broadcom.com \
    --cc=bcm-kernel-feedback-list@broadcom.com \
    --cc=florian.fainelli@broadcom.com \
    --cc=gpiccoli@igalia.com \
    --cc=kees@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mmc@vger.kernel.org \
    --cc=tony.luck@intel.com \
    --cc=ulf.hansson@linaro.org \
    --cc=william.zhang@broadcom.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox