Skip to contentSkip to conversation

mmc: Add pstore backend for crash dump storage on eMMC

PATCH Superseded Kamal Dasu 2 participants 6 messages Mar 20 – Apr 15

Subsystems (2)

v4 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Kamal Dasu

author
#0 lore raw

v4 cover mmc: Add pstore backend for crash dump storage on eMMC

This series adds mmcpstore, a pstore backend driver that enables persistent storage of kernel crash logs on eMMC devices. When the kernel panics, pstore captures the kmsg dump and writes it directly to a dedicated MMC partition using polled I/O with interrupts disabled.

Changes since v3:
- Fixed kernel-doc warnings reported by kernel test robot:
- Added missing @param descriptions for sdhci_panic_prepare(), sdhci_panic_poll_completion(), sdhci_panic_complete() (patch 2)
- Added missing @sect_offset param doc for mmcpstore_do_request_internal() (patch 4)
- Fixed kernel-doc function name mismatch: mmcpstore_read() -> mmcpstore_read_zone() (patch 4)
- Added missing @disk param doc for mmcpstore_card_add() (patch 4)
- Removed unused 'offset_bytes' variable in mmcpstore_register_for_card() (patch 4)

Changes since v2 (RFC):
- Rebased onto v7.0-rc — no longer reverts any existing MMC core or SDHCI changes (v1/v2 accidentally reverted recent upstream commits due to being based on an older tree)
- Removed all erase/bitmap tracking logic — MMC/eMMC is managed flash and does not need erase-before-write; the pstore_zone framework handles zone management internally
- Uses standard MMC core request path (mmc_start_request) instead of hand-building mmc_request structs; panic-context I/O is handled through proper mmc_host_ops callbacks rather than ad-hoc code in the driver
- Added panic-context ops to mmc_host_ops and sdhci_ops for clean separation of panic and normal I/O paths
- Fixed deadlocks caused by spinlock contention during panic (lockless mmc_panic_claim_host using WRITE_ONCE)
- Fixed data corruption in pstore recovery (zlib_inflate failures) by letting the normal sdhci_request() path run instead of a custom panic request handler
- Added PM suspend/resume support with eMMC re-initialization
- Supports module loading or builtin loading of the driver
- Added MAINTAINERS entry
- Split into 4-patch series for reviewability

The series is structured as follows:

Patch 1 adds panic-context operations to struct mmc_host_ops and a lockless mmc_panic_claim_host() for use during kernel panic when other CPUs are stopped and may hold locks.

Patch 2 implements these panic-context operations in the SDHCI host controller driver: draining in-flight requests, polling for completion, and making the command retry path panic-safe.

Patch 3 adds mmc_blk_get_card_by_name() helper to look up an mmc_card from a block device name, used by the mmcpstore module path for card discovery.

Patch 4 adds the mmcpstore driver itself, which registers with the pstore_blk framework and handles panic writes, PM suspend/resume, and dual-path registration (direct probe hook for builtin, mmc_blk_get_card_by_name() for module). Also adds probe/remove hooks in block.c and declarations in block.h for the builtin path.

Tested on Broadcom STB platforms (ARM64) with SDHCI controllers, verified panic dump recovery across multiple panic/reboot cycles with kmsg, pmsg, and console pstore.

Previous submissions and related work:
RFC v1: https://lore.kernel.org/linux-mmc/[email protected]/
RFC v2: https://lore.kernel.org/linux-mmc/[email protected]/
v3: https://lore.kernel.org/linux-mmc/[email protected]/
Marvell MMC pstore attempt (2020):

    https://lore.kernel.org/linux-mmc/[email protected]/

pstore/blk documentation:
https://www.kernel.org/doc/html/latest/admin-guide/pstore-blk.html

Kamal Dasu (4):
mmc: core: Add panic-context host operations for pstore backends
mmc: sdhci: Implement panic-context write support
mmc: block: Add helper to look up mmc_card by device name
mmc: core: Add MMC pstore backend driver

 MAINTAINERS                  |    6 +
 drivers/mmc/core/Kconfig     |   12 +
 drivers/mmc/core/Makefile    |    1 +
 drivers/mmc/core/block.c     |   56 ++
 drivers/mmc/core/block.h     |   18 +
 drivers/mmc/core/core.c      |   23 +
 drivers/mmc/core/mmcpstore.c | 1519 ++++++++++++++++++++++++++++++++++
 drivers/mmc/host/sdhci.c     |  169 +++-
 drivers/mmc/host/sdhci.h     |    6 +
 include/linux/mmc/host.h     |   12 +
 10 files changed, 1818 insertions(+), 4 deletions(-)
 create mode 100644 drivers/mmc/core/mmcpstore.c
Signature · 1 line
2.34.1
author
#1 lore raw

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

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.

---
 drivers/mmc/core/core.c  | 23 +++++++++++++++++++++++
 include/linux/mmc/host.h | 12 ++++++++++++
 2 files changed, 35 insertions(+)
drivers/mmc/core/core.c +23 -0
index 860378bea557..5326b246b4f2 100644
@@ -860,6 +860,29 @@ 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.
+ */
+void mmc_panic_claim_host(struct mmc_host *host)
+{
+	if (!host)
+		return;
+
+	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.
include/linux/mmc/host.h +12 -0
index ba84f02c2a10..cd44b62d29cb 100644
@@ -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 */
Signature · 1 line
2.34.1
#2 lore raw

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

Quoted 38 lines · Kamal Dasu

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.

---
 drivers/mmc/core/core.c  | 23 +++++++++++++++++++++++
 include/linux/mmc/host.h | 12 ++++++++++++
 2 files changed, 35 insertions(+)
drivers/mmc/core/core.c +12 -0
index 860378bea557..5326b246b4f2 100644
@@ -860,6 +860,29 @@ 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.
+ */
+void mmc_panic_claim_host(struct mmc_host *host)
+{

drivers/mmc/core/core.c void mmc_release_host(struct mmc_host *host) · line 884

Quoted 10 lines
drivers/mmc/core/core.c +10 -0
+       if (!host)
+               return;
+
+       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);

The above doesn't account for whether someone else has already claimed the host. I believe we should check that to better understand our current state.

If the host is claimed, it's probably due to ongoing read/write I/O. Then, how do we gracefully terminate those requests?

If the host isn't claimed, the mmc host (and mmc card) may have been runtime PM suspended. We should take that into account, because runtime resuming the host in atomic context is most likely not going to work.

Quoted 33 lines
drivers/mmc/core/core.c +1 -0
+
 /*
  * This is a helper function, which fetches a runtime pm reference for the
  * card device and also claims the host.
include/linux/mmc/host.h +12 -1
index ba84f02c2a10..cd44b62d29cb 100644
@@ -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 */
--
Quoted 2 lines

2.34.1

Kind regards
Uffe

author
#3 lore raw

PATCH v4 2/4 mmc: sdhci: Implement panic-context write support

In reply to Kamal Dasu

Implement the panic-context host operations for SDHCI controllers:

sdhci_panic_prepare(): Reset the controller, drain any pending requests by polling Present State, and clear interrupt status to start from a known-good state.

sdhci_panic_poll_completion(): Poll for command and data completion using register reads instead of waiting for interrupts.

sdhci_panic_complete(): Clear interrupt status and restore the host to normal operation after panic I/O.

Make sdhci_send_command_retry() panic-safe by using mdelay() instead of usleep_range() when oops_in_progress is set, and suppress WARN output during panic.

Add oops_in_progress guards to sdhci_timeout_timer() and sdhci_timeout_data_timer() to prevent spurious timeout handling during panic writes.

---
 drivers/mmc/host/sdhci.c | 169 ++++++++++++++++++++++++++++++++++++++-
 drivers/mmc/host/sdhci.h |   6 ++
 2 files changed, 171 insertions(+), 4 deletions(-)
drivers/mmc/host/sdhci.c +165 -4
index fec9329e1edb..c40084e07eca 100644
@@ -15,6 +15,7 @@
 #include <linux/ktime.h>
 #include <linux/highmem.h>
 #include <linux/io.h>
+#include <linux/iopoll.h>
 #include <linux/module.h>
 #include <linux/dma-mapping.h>
 #include <linux/slab.h>
@@ -1765,17 +1766,22 @@ static bool sdhci_send_command_retry(struct sdhci_host *host,
 
 	while (!sdhci_send_command(host, cmd)) {
 		if (!timeout--) {
-			pr_err("%s: Controller never released inhibit bit(s).\n",
-			       mmc_hostname(host->mmc));
+			if (!oops_in_progress) {
+				pr_err("%s: Controller never released inhibit bit(s).\n",
+				       mmc_hostname(host->mmc));
+				sdhci_dumpregs(host);
+			}
 			sdhci_err_stats_inc(host, CTRL_TIMEOUT);
-			sdhci_dumpregs(host);
 			cmd->error = -EIO;
 			return false;
 		}
 
 		spin_unlock_irqrestore(&host->lock, flags);
 
-		usleep_range(1000, 1250);
+		if (unlikely(oops_in_progress))
+			mdelay(1);
+		else
+			usleep_range(1000, 1250);
 
 		present = host->mmc->ops->get_cd(host->mmc);
 
@@ -3076,6 +3082,152 @@ static void sdhci_card_event(struct mmc_host *mmc)
 	spin_unlock_irqrestore(&host->lock, flags);
 }
 
+/*
+ * Panic-context operations for pstore backends.
+ * These run with interrupts disabled and other CPUs stopped.
+ */
+
+#define SDHCI_PANIC_POLL_ITERATIONS	2000
+#define SDHCI_PANIC_POLL_DELAY_US	500
+#define SDHCI_PANIC_MIN_POLL_COUNT	300
+#define SDHCI_PANIC_RESET_TIMEOUT_US	100000
+#define SDHCI_PANIC_DRAIN_TIMEOUT_US	100000
+
+/**
+ * sdhci_panic_prepare - Prepare SDHCI controller for panic-context I/O
+ * @mmc: MMC host structure
+ *
+ * Called during kernel panic. Drains any in-flight request, resets the
+ * CMD and DATA lines, then clears software state under spinlock.
+ * The drain + reset ensures no stopped CPU is still inside sdhci_irq
+ * holding host->lock by the time we take it.
+ */
+int sdhci_panic_prepare(struct mmc_host *mmc)
+{
+	struct sdhci_host *host = mmc_priv(mmc);
+	unsigned long flags;
+	u32 present;
+	u8 val;
+	int ret;
+
+	/*
+	 * If the controller has a request in flight, give it a short
+	 * bounded time to finish. The CMD/DATA reset below will
+	 * force-abort anything that doesn't complete in time.
+	 */
+	present = sdhci_readl(host, SDHCI_PRESENT_STATE);
+	if (present & (SDHCI_CMD_INHIBIT | SDHCI_DATA_INHIBIT)) {
+		readl_poll_timeout_atomic(host->ioaddr + SDHCI_PRESENT_STATE,
+					  present,
+					  !(present & (SDHCI_CMD_INHIBIT |
+						       SDHCI_DATA_INHIBIT)),
+					  10, SDHCI_PANIC_DRAIN_TIMEOUT_US);
+	}
+
+	sdhci_writeb(host, SDHCI_RESET_CMD | SDHCI_RESET_DATA,
+		     SDHCI_SOFTWARE_RESET);
+
+	ret = readb_poll_timeout_atomic(host->ioaddr + SDHCI_SOFTWARE_RESET,
+					val, !(val & (SDHCI_RESET_CMD | SDHCI_RESET_DATA)),
+					10, SDHCI_PANIC_RESET_TIMEOUT_US);
+
+	spin_lock_irqsave(&host->lock, flags);
+	host->cmd = NULL;
+	host->data = NULL;
+	host->data_cmd = NULL;
+	host->mrqs_done[0] = NULL;
+	host->mrqs_done[1] = NULL;
+	spin_unlock_irqrestore(&host->lock, flags);
+
+	if (host->ops && host->ops->panic_prepare)
+		host->ops->panic_prepare(host);
+
+	return ret;
+}
+EXPORT_SYMBOL_GPL(sdhci_panic_prepare);
+
+/**
+ * sdhci_panic_poll_completion - Poll SDHCI registers for request completion
+ * @mmc: MMC host structure
+ * @mrq: MMC request being polled for completion
+ *
+ * Checks interrupt status and present state registers to determine if a
+ * request has completed. Used during panic when interrupts are disabled.
+ */
+bool sdhci_panic_poll_completion(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+	struct sdhci_host *host = mmc_priv(mmc);
+	unsigned int poll_count;
+	u32 int_status, present;
+
+	for (poll_count = 0; poll_count < SDHCI_PANIC_POLL_ITERATIONS;
+	     poll_count++) {
+		cpu_relax();
+		udelay(SDHCI_PANIC_POLL_DELAY_US);
+
+		int_status = sdhci_readl(host, SDHCI_INT_STATUS);
+
+		if (int_status & SDHCI_INT_ERROR) {
+			if (mrq->cmd)
+				mrq->cmd->error = -EIO;
+			if (mrq->data)
+				mrq->data->error = -EIO;
+			sdhci_writel(host, int_status, SDHCI_INT_STATUS);
+			return true;
+		}
+
+		if (int_status & SDHCI_INT_RESPONSE)
+			sdhci_writel(host, SDHCI_INT_RESPONSE,
+				     SDHCI_INT_STATUS);
+
+		if (int_status & SDHCI_INT_DATA_END)
+			sdhci_writel(host, SDHCI_INT_DATA_END,
+				     SDHCI_INT_STATUS);
+
+		/*
+		 * Use the same completion heuristic as the working v5
+		 * implementation: after a minimum number of poll
+		 * iterations, treat the request as complete when the
+		 * DATA_INHIBIT bit clears (controller is idle).
+		 */
+		if (poll_count >= SDHCI_PANIC_MIN_POLL_COUNT) {
+			present = sdhci_readl(host, SDHCI_PRESENT_STATE);
+			if (!(present & SDHCI_DATA_INHIBIT))
+				return true;
+		}
+	}
+
+	if (mrq->cmd)
+		mrq->cmd->error = -ETIMEDOUT;
+	if (mrq->data)
+		mrq->data->error = -ETIMEDOUT;
+	return false;
+}
+EXPORT_SYMBOL_GPL(sdhci_panic_poll_completion);
+
+/**
+ * sdhci_panic_complete - Clean up SDHCI state after a panic-context request
+ * @mmc: MMC host structure
+ * @mrq: MMC request that has completed
+ *
+ * Clears host software state under spinlock so the next panic request
+ * starts clean.
+ */
+void sdhci_panic_complete(struct mmc_host *mmc, struct mmc_request *mrq)
+{
+	struct sdhci_host *host = mmc_priv(mmc);
+	unsigned long flags;
+
+	spin_lock_irqsave(&host->lock, flags);
+	host->cmd = NULL;
+	host->data = NULL;
+	host->data_cmd = NULL;
+	host->mrqs_done[0] = NULL;
+	host->mrqs_done[1] = NULL;
+	spin_unlock_irqrestore(&host->lock, flags);
+}
+EXPORT_SYMBOL_GPL(sdhci_panic_complete);
+
 static const struct mmc_host_ops sdhci_ops = {
 	.request	= sdhci_request,
 	.post_req	= sdhci_post_req,
@@ -3091,6 +3243,9 @@ static const struct mmc_host_ops sdhci_ops = {
 	.execute_tuning			= sdhci_execute_tuning,
 	.card_event			= sdhci_card_event,
 	.card_busy	= sdhci_card_busy,
+	.panic_prepare			= sdhci_panic_prepare,
+	.panic_poll_completion		= sdhci_panic_poll_completion,
+	.panic_complete			= sdhci_panic_complete,
 };
 
 /*****************************************************************************\
@@ -3242,6 +3397,9 @@ static void sdhci_timeout_timer(struct timer_list *t)
 
 	host = timer_container_of(host, t, timer);
 
+	if (oops_in_progress)
+		return;
+
 	spin_lock_irqsave(&host->lock, flags);
 
 	if (host->cmd && !sdhci_data_line_cmd(host->cmd)) {
@@ -3264,6 +3422,9 @@ static void sdhci_timeout_data_timer(struct timer_list *t)
 
 	host = timer_container_of(host, t, data_timer);
 
+	if (oops_in_progress)
+		return;
+
 	spin_lock_irqsave(&host->lock, flags);
 
 	if (host->data || host->data_cmd ||
drivers/mmc/host/sdhci.h +6 -0
index b6a571d866fa..396eca56439f 100644
@@ -724,6 +724,7 @@ struct sdhci_ops {
 	void    (*dump_vendor_regs)(struct sdhci_host *host);
 	void	(*dump_uhs2_regs)(struct sdhci_host *host);
 	void    (*uhs2_pre_detect_init)(struct sdhci_host *host);
+	void	(*panic_prepare)(struct sdhci_host *host);
 };
 
 #ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
@@ -906,6 +907,11 @@ void sdhci_switch_external_dma(struct sdhci_host *host, bool en);
 void sdhci_set_data_timeout_irq(struct sdhci_host *host, bool enable);
 void __sdhci_set_timeout(struct sdhci_host *host, struct mmc_command *cmd);
 
+int sdhci_panic_prepare(struct mmc_host *mmc);
+bool sdhci_panic_poll_completion(struct mmc_host *mmc,
+				 struct mmc_request *mrq);
+void sdhci_panic_complete(struct mmc_host *mmc, struct mmc_request *mrq);
+
 #if defined(CONFIG_DYNAMIC_DEBUG) || \
 	(defined(CONFIG_DYNAMIC_DEBUG_CORE) && defined(DYNAMIC_DEBUG_MODULE))
 #define SDHCI_DBG_ANYWAY 0
Signature · 1 line
2.34.1
author
#4 lore raw

PATCH v4 3/4 mmc: block: Add helper to look up mmc_card by device name

In reply to Kamal Dasu

Add mmc_blk_get_card_by_name() which resolves an MMC block device name (e.g., "mmcblk1") to its associated struct mmc_card by opening the block device and traversing gendisk to the mmc_blk_data private data.

This is needed by the mmcpstore backend driver to find the card associated with a given pstore_blk block device path. When mmcpstore is built as a module, the eMMC card is already probed by the time the module loads, so the helper can look it up directly.

---
 drivers/mmc/core/block.c | 54 ++++++++++++++++++++++++++++++++++++++++
 drivers/mmc/core/block.h |  4 +++
 2 files changed, 58 insertions(+)
drivers/mmc/core/block.c +54 -0
index 05ee76cb0a08..91e3a778d5b2 100644
@@ -3441,6 +3441,60 @@ static void __exit mmc_blk_exit(void)
 	bus_unregister(&mmc_rpmb_bus_type);
 }
 
+/**
+ * mmc_blk_get_card_by_name - Get mmc_card from device name
+ * @device_name: Name of the MMC device (e.g., "mmcblk1")
+ *
+ * Resolves an MMC block device name to its associated struct mmc_card.
+ * Used by the mmcpstore backend driver (when built as a module) to find
+ * the card associated with a given pstore_blk block device path.
+ *
+ * Returns: mmc_card pointer on success, NULL on failure
+ */
+struct mmc_card *mmc_blk_get_card_by_name(const char *device_name)
+{
+	struct file *bdev_file;
+	struct block_device *bdev;
+	struct gendisk *disk;
+	struct mmc_blk_data *md;
+	struct mmc_card *card = NULL;
+	char dev_path[32];
+
+	if (!device_name)
+		return NULL;
+
+	snprintf(dev_path, sizeof(dev_path), "/dev/%s", device_name);
+
+	bdev_file = bdev_file_open_by_path(dev_path, BLK_OPEN_READ, NULL, NULL);
+	if (IS_ERR(bdev_file)) {
+		pr_debug("mmc_blk: Failed to open block device %s: %ld\n",
+			 dev_path, PTR_ERR(bdev_file));
+		return NULL;
+	}
+	bdev = file_bdev(bdev_file);
+
+	disk = bdev->bd_disk;
+	if (!disk) {
+		pr_err("mmc_blk: No gendisk found for %s\n", dev_path);
+		goto out_put_bdev;
+	}
+
+	md = disk->private_data;
+	if (!md) {
+		pr_debug("mmc_blk: No mmc_blk_data found for %s\n", dev_path);
+		goto out_put_bdev;
+	}
+
+	card = md->queue.card;
+	if (!card)
+		pr_err("mmc_blk: No mmc_card found for %s\n", dev_path);
+
+out_put_bdev:
+	fput(bdev_file);
+	return card;
+}
+EXPORT_SYMBOL_GPL(mmc_blk_get_card_by_name);
+
 module_init(mmc_blk_init);
 module_exit(mmc_blk_exit);
 
drivers/mmc/core/block.h +4 -0
index 31153f656f41..08798c56dcce 100644
@@ -17,4 +17,8 @@ struct work_struct;
 
 void mmc_blk_mq_complete_work(struct work_struct *work);
 
+/* MMC block device helper for mmcpstore */
+struct mmc_card;
+struct mmc_card *mmc_blk_get_card_by_name(const char *device_name);
+
 #endif
Signature · 1 line
2.34.1
author
#5 lore raw

PATCH v4 4/4 mmc: core: Add MMC pstore backend driver

In reply to Kamal Dasu

Add mmcpstore, a pstore backend driver that provides persistent storage for kernel crash logs and other pstore data on eMMC devices. The driver registers with the pstore_blk framework and performs direct MMC writes during kernel panic using the panic-context host operations.

Key features:
- Panic-safe writes using polled I/O with interrupts disabled
- Sector offset and count parameters for flexible partition usage
- Support for both module and builtin configurations
- PM suspend/resume support with eMMC hardware re-initialization
- Dual-path registration: direct probe callback when builtin, mmc_blk_get_card_by_name() lookup when loaded as module
- Console pstore support via atomic context detection: when called from printk's atomic console path, returns -EBUSY so pstore_zone defers the write to its workqueue. Console logging works but is not recommended when kmsg is enabled, as it generates constant flash writes for every kernel log message.

When CONFIG_MMC_PSTORE=y, mmc_blk_probe() calls mmcpstore_card_add() directly. When CONFIG_MMC_PSTORE=m, the module init uses mmc_blk_get_card_by_name() to look up the already-probed eMMC card. The IS_BUILTIN() guard in block.h ensures no cross-module symbol dependency.

Required kernel config:
CONFIG_PSTORE=y
CONFIG_PSTORE_BLK=m (or =y)
CONFIG_MMC_PSTORE=m (or =y)
CONFIG_PSTORE_PMSG=y (optional, for pmsg support) CONFIG_PSTORE_COMPRESS=y (optional, for compressed kmsg dumps) CONFIG_PSTORE_CONSOLE=y (optional, console log capture)

Module loading examples:

Basic usage with a dedicated eMMC partition:
modprobe pstore_blk blkdev=/dev/mmcblk1p5 kmsg_size=64 pmsg_size=32 modprobe mmcpstore

With console log capture:

    modprobe pstore_blk blkdev=/dev/mmcblk1p5 kmsg_size=64 pmsg_size=32 \
      console_size=32
    modprobe mmcpstore

Using a subset of sectors within a partition:
modprobe pstore_blk blkdev=/dev/mmcblk1p5 kmsg_size=64 pmsg_size=32 modprobe mmcpstore sector_offset=512 sector_count=512

Builtin configuration (kernel command line):

    pstore_blk.blkdev=/dev/mmcblk1p5 pstore_blk.kmsg_size=64 \
      pstore_blk.pmsg_size=32

Verify stored pstore data after a panic reboot:
mount -t pstore pstore /sys/fs/pstore
ls /sys/fs/pstore/
cat /sys/fs/pstore/dmesg-pstore_blk-0
cat /sys/fs/pstore/pmsg-pstore_blk-0

Module parameters:
sector_offset Starting sector within partition (default: 0)
sector_count Number of sectors to use (default: entire partition)

Tested with ARM and ARM64 builds on Broadcom STB platforms with SDHCI controllers.

---
 MAINTAINERS                  |    6 +
 drivers/mmc/core/Kconfig     |   12 +
 drivers/mmc/core/Makefile    |    1 +
 drivers/mmc/core/block.c     |    2 +
 drivers/mmc/core/block.h     |   14 +
 drivers/mmc/core/mmcpstore.c | 1519 ++++++++++++++++++++++++++++++++++
 6 files changed, 1554 insertions(+)
 create mode 100644 drivers/mmc/core/mmcpstore.c
MAINTAINERS +6 -0
index 7d10988cbc62..29c334c5f9d5 100644
@@ -18089,6 +18089,12 @@ F:	drivers/mmc/
 F:	include/linux/mmc/
 F:	include/uapi/linux/mmc/
 
+MULTIMEDIA CARD (MMC) PSTORE BACKEND
+M:	Kamal Dasu <[email protected]>
+L:	[email protected]
+S:	Maintained
+F:	drivers/mmc/core/mmcpstore.c
+
 MULTIPLEXER SUBSYSTEM
 M:	Peter Rosin <[email protected]>
 S:	Odd Fixes
drivers/mmc/core/Kconfig +12 -0
index 14d2ecbb04d3..961c76f4b6c7 100644
@@ -23,6 +23,18 @@ config PWRSEQ_SD8787
 	  This driver can also be built as a module. If so, the module
 	  will be called pwrseq_sd8787.
 
+config MMC_PSTORE
+	tristate "MMC pstore backend support"
+	depends on MMC_BLOCK
+	select PSTORE_BLK
+	help
+	  This option enables MMC devices to be used as pstore backends.
+	  This allows kernel crash logs and other pstore data to be stored
+	  on MMC/SD storage devices for post-mortem analysis.
+
+	  If you want to store kernel crash logs on MMC devices, say Y.
+
+
 config PWRSEQ_SIMPLE
 	tristate "Simple HW reset support for MMC"
 	default y
drivers/mmc/core/Makefile +1 -0
index 15b067e8b0d1..6b7bbfd2adbe 100644
@@ -19,3 +19,4 @@ mmc_block-objs			:= block.o queue.o
 obj-$(CONFIG_MMC_TEST)		+= mmc_test.o
 obj-$(CONFIG_SDIO_UART)		+= sdio_uart.o
 mmc_core-$(CONFIG_MMC_CRYPTO)	+= crypto.o
+obj-$(CONFIG_MMC_PSTORE)	+= mmcpstore.o
drivers/mmc/core/block.c +2 -0
index 91e3a778d5b2..c4db0d96139b 100644
@@ -3305,6 +3305,7 @@ static int mmc_blk_probe(struct mmc_card *card)
 	}
 
 	mmc_blk_rpmb_add(card);
+	mmcpstore_card_add(card, md->disk);
 
 	return 0;
 
@@ -3320,6 +3321,7 @@ static void mmc_blk_remove(struct mmc_card *card)
 {
 	struct mmc_blk_data *md = dev_get_drvdata(&card->dev);
 
+	mmcpstore_card_remove(card);
 	mmc_blk_remove_debugfs(card, md);
 	mmc_blk_remove_parts(card, md);
 	pm_runtime_get_sync(&card->dev);
drivers/mmc/core/block.h +14 -0
index 08798c56dcce..cb58c1366c1d 100644
@@ -19,6 +19,20 @@ void mmc_blk_mq_complete_work(struct work_struct *work);
 
 /* MMC block device helper for mmcpstore */
 struct mmc_card;
+struct gendisk;
 struct mmc_card *mmc_blk_get_card_by_name(const char *device_name);
 
+/*
+ * Builtin mmcpstore: called directly from mmc_blk_probe/remove.
+ * Module mmcpstore: stubs — module uses mmc_blk_get_card_by_name() instead.
+ */
+#if IS_BUILTIN(CONFIG_MMC_PSTORE)
+void mmcpstore_card_add(struct mmc_card *card, struct gendisk *disk);
+void mmcpstore_card_remove(struct mmc_card *card);
+#else
+static inline void mmcpstore_card_add(struct mmc_card *card,
+				      struct gendisk *disk) {}
+static inline void mmcpstore_card_remove(struct mmc_card *card) {}
+#endif
+
 #endif
drivers/mmc/core/mmcpstore.c new file +1519 -0

1,519 lines not on this page · raw message

Signature · 1 line
2.34.1