[RFC PATCH 2/2] spi: add spi_sync_atomic() for panic and oops
From: ChunYuZhiQiang
Date: Mon Nov 16 2026 - 04:54:40 EST
Add an explicit atomic transfer entry point for crash logging. The
shutdown path added by Heiko Stuebner is gated on system_state being past
SYSTEM_RUNNING, but panic and oops run while the system is still running,
so they cannot use it.
spi_sync_atomic() reuses the controller's transfer_atomic() callback. It
only accepts callers that are in panic or oops context with interrupts or
preemption disabled, takes the bus locks with trylock so a normal transfer
in progress on another CPU returns -EBUSY instead of deadlocking, and
validates the message without allocating or optimizing it.
The first user is pstore over an SPI FRAM, which needs to persist the
crash log before the next reset. The controller must already be powered
and clocked; this path does not resume it. fsl_lpspi implements the
callback in the previous patch.
This patch was prepared with AI assistance; the submitter must review it
before submission.
Signed-off-by: ChunYuZhiQiang <chunyuzhiqiang@xxxxxxxx>
---
drivers/spi/spi.c | 72 ++++++++++++++++++++++++++++++++++++++---
include/linux/spi/spi.h | 1 +
2 files changed, 68 insertions(+), 5 deletions(-)
diff --git a/drivers/spi/spi.c b/drivers/spi/spi.c
index 0b1e07d87..e24ad60e1 100644
--- a/drivers/spi/spi.c
+++ b/drivers/spi/spi.c
@@ -23,6 +23,7 @@
#include <linux/mutex.h>
#include <linux/of_device.h>
#include <linux/of_irq.h>
+#include <linux/panic.h>
#include <linux/percpu.h>
#include <linux/platform_data/x86/apple.h>
#include <linux/pm_domain.h>
@@ -1712,11 +1713,25 @@ static inline bool spi_in_atomic_mode(void)
(IS_ENABLED(CONFIG_PREEMPT_COUNT) ? !preemptible() : irqs_disabled());
}
+/*
+ * Panic and oops happen while the system is still running, so the shutdown
+ * check above does not catch them. This is only for crash logging and other
+ * very late communication; it must not be used for general atomic transfers.
+ */
+static inline bool spi_in_panic_mode(void)
+{
+ if (!panic_in_progress() && !oops_in_progress)
+ return false;
+
+ return IS_ENABLED(CONFIG_PREEMPT_COUNT) ? !preemptible() : irqs_disabled();
+}
+
static void _spi_transfer_delay_ns(u32 ns)
{
if (!ns)
return;
- if (ns <= NSEC_PER_USEC || spi_in_atomic_mode()) {
+ if (ns <= NSEC_PER_USEC || spi_in_atomic_mode() ||
+ spi_in_panic_mode()) {
ndelay(ns);
} else {
u32 us = DIV_ROUND_UP(ns, NSEC_PER_USEC);
@@ -5027,7 +5042,8 @@ static int __spi_sync(struct spi_device *spi, struct spi_message *message)
return status;
}
-static int __spi_sync_atomic(struct spi_device *spi, struct spi_message *message)
+static int __spi_sync_atomic(struct spi_device *spi, struct spi_message *message,
+ bool trylock)
{
struct spi_controller *ctlr = spi->controller;
unsigned long flags;
@@ -5048,7 +5064,12 @@ static int __spi_sync_atomic(struct spi_device *spi, struct spi_message *message
if (status)
return status;
- spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
+ if (trylock) {
+ if (!spin_trylock_irqsave(&ctlr->bus_lock_spinlock, flags))
+ return -EBUSY;
+ } else {
+ spin_lock_irqsave(&ctlr->bus_lock_spinlock, flags);
+ }
status = ctlr->transfer_atomic(spi, message);
spin_unlock_irqrestore(&ctlr->bus_lock_spinlock, flags);
@@ -5090,7 +5111,7 @@ int spi_sync(struct spi_device *spi, struct spi_message *message)
/* Keep old behaviour for clients that tolerate the warnings */
if (spi_in_atomic_mode() && ctlr->transfer_atomic) {
- ret = __spi_sync_atomic(spi, message);
+ ret = __spi_sync_atomic(spi, message, false);
} else {
mutex_lock(&spi->controller->bus_lock_mutex);
ret = __spi_sync(spi, message);
@@ -5126,12 +5147,53 @@ int spi_sync_locked(struct spi_device *spi, struct spi_message *message)
struct spi_controller *ctlr = spi->controller;
if (spi_in_atomic_mode() && ctlr->transfer_atomic)
- return __spi_sync_atomic(spi, message);
+ return __spi_sync_atomic(spi, message, false);
else
return __spi_sync(spi, message);
}
EXPORT_SYMBOL_GPL(spi_sync_locked);
+/**
+ * spi_sync_atomic - transfer a message during panic or oops
+ * @spi: device with which data will be exchanged
+ * @message: describes the data transfers
+ *
+ * Context: panic or oops, with interrupts or preemption disabled.
+ *
+ * This is the panic-time counterpart of spi_sync(). It is only for crash
+ * logging and other very late communication; it must not be used from
+ * ordinary interrupt context. The controller's transfer_atomic() callback
+ * must perform the whole message without sleeping, allocating, using DMA or
+ * waiting for interrupts or completions.
+ *
+ * The bus locks are taken with trylock(): if a normal transfer is still in
+ * progress on another CPU, this function returns -EBUSY instead of
+ * deadlocking. The controller must already be powered and clocked; this
+ * function does not resume it.
+ *
+ * Return: zero on success, -EOPNOTSUPP if the controller does not support
+ * atomic transfers, -EPERM if not called from panic/oops context, -EBUSY if
+ * the bus is busy, -ESHUTDOWN if the controller is suspended, or another
+ * negative error from the controller.
+ */
+int spi_sync_atomic(struct spi_device *spi, struct spi_message *message)
+{
+ struct spi_controller *ctlr = spi->controller;
+ int ret;
+
+ if (!spi_in_panic_mode())
+ return -EPERM;
+
+ if (!mutex_trylock(&ctlr->bus_lock_mutex))
+ return -EBUSY;
+
+ ret = __spi_sync_atomic(spi, message, true);
+ mutex_unlock(&ctlr->bus_lock_mutex);
+
+ return ret;
+}
+EXPORT_SYMBOL_GPL(spi_sync_atomic);
+
/**
* spi_bus_lock - obtain a lock for exclusive SPI bus usage
* @ctlr: SPI bus controller that should be locked for exclusive bus access
diff --git a/include/linux/spi/spi.h b/include/linux/spi/spi.h
index f62b21af5..b3fad85ec 100644
--- a/include/linux/spi/spi.h
+++ b/include/linux/spi/spi.h
@@ -1512,6 +1512,7 @@ extern int spi_split_transfers_maxwords(struct spi_controller *ctlr,
extern int spi_sync(struct spi_device *spi, struct spi_message *message);
extern int spi_sync_locked(struct spi_device *spi, struct spi_message *message);
+int spi_sync_atomic(struct spi_device *spi, struct spi_message *message);
extern int spi_bus_lock(struct spi_controller *ctlr);
extern int spi_bus_unlock(struct spi_controller *ctlr);
--
2.47.3