[RFC PATCH 1/3] nvmem: add panic write support
From: ChunYuZhiQiang
Date: Mon Nov 16 2026 - 05:29:18 EST
Add a reg_write_panic callback to nvmem_config and a matching
nvmem_device_write_panic() consumer API. The callback is used after the
system has panicked, when the write must not sleep, take locks, allocate
memory or touch a GPIO that may sleep.
Providers that can write from panic context implement the callback;
others leave it NULL and the API returns -EOPNOTSUPP. The regular
nvmem_device_write() path is unchanged.
This patch was prepared with AI assistance; the submitter must review it
before submission.
Signed-off-by: ChunYuZhiQiang <chunyuzhiqiang@xxxxxxxx>
---
drivers/nvmem/core.c | 44 ++++++++++++++++++++++++++++++++++
drivers/nvmem/internals.h | 1 +
include/linux/nvmem-consumer.h | 9 +++++++
include/linux/nvmem-provider.h | 3 +++
4 files changed, 57 insertions(+)
diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c
index 7d4cce8e2..40b2f59e2 100644
--- a/drivers/nvmem/core.c
+++ b/drivers/nvmem/core.c
@@ -960,6 +960,7 @@ struct nvmem_device *nvmem_register(const struct nvmem_config *config)
ops->reg_read = config->reg_read;
ops->reg_write = config->reg_write;
+ ops->reg_write_panic = config->reg_write_panic;
nvmem->owner = config->owner;
if (!nvmem->owner && config->dev->driver)
@@ -2111,6 +2112,49 @@ int nvmem_device_write(struct nvmem_device *nvmem,
}
EXPORT_SYMBOL_GPL(nvmem_device_write);
+/**
+ * nvmem_device_write_panic() - Write to a given nvmem device during panic
+ *
+ * @nvmem: nvmem device to be written to.
+ * @offset: offset in nvmem device.
+ * @bytes: number of bytes to write.
+ * @buf: buffer to be written.
+ *
+ * This is the panic-time counterpart of nvmem_device_write(). It must not
+ * sleep, take mutexes, allocate memory or touch a GPIO that may sleep. The
+ * provider's reg_write_panic() callback is responsible for using a
+ * non-sleeping bus path.
+ *
+ * Return: length of bytes written or negative error code on failure.
+ */
+int nvmem_device_write_panic(struct nvmem_device *nvmem,
+ unsigned int offset,
+ size_t bytes, void *buf)
+{
+ struct nvmem_operations *ops;
+ int rc;
+
+ if (!nvmem)
+ return -EINVAL;
+
+ if (nvmem->read_only)
+ return -EPERM;
+
+ if (offset > nvmem->size || bytes > nvmem->size - offset)
+ return -EINVAL;
+
+ ops = rcu_dereference_raw(nvmem->ops);
+ if (!ops || !ops->reg_write_panic)
+ return -EOPNOTSUPP;
+
+ rc = ops->reg_write_panic(nvmem->priv, offset, buf, bytes);
+ if (rc)
+ return rc;
+
+ return bytes;
+}
+EXPORT_SYMBOL_GPL(nvmem_device_write_panic);
+
/**
* nvmem_add_cell_lookups() - register a list of cell lookup entries
*
diff --git a/drivers/nvmem/internals.h b/drivers/nvmem/internals.h
index bc7a99f5a..1c32947ab 100644
--- a/drivers/nvmem/internals.h
+++ b/drivers/nvmem/internals.h
@@ -12,6 +12,7 @@
struct nvmem_operations {
nvmem_reg_read_t reg_read;
nvmem_reg_write_t reg_write;
+ nvmem_reg_write_t reg_write_panic;
};
struct nvmem_device {
diff --git a/include/linux/nvmem-consumer.h b/include/linux/nvmem-consumer.h
index 34c0e58df..2a23c24fa 100644
--- a/include/linux/nvmem-consumer.h
+++ b/include/linux/nvmem-consumer.h
@@ -75,6 +75,8 @@ int nvmem_device_read(struct nvmem_device *nvmem, unsigned int offset,
size_t bytes, void *buf);
int nvmem_device_write(struct nvmem_device *nvmem, unsigned int offset,
size_t bytes, void *buf);
+int nvmem_device_write_panic(struct nvmem_device *nvmem, unsigned int offset,
+ size_t bytes, void *buf);
ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem,
struct nvmem_cell_info *info, void *buf);
int nvmem_device_cell_write(struct nvmem_device *nvmem,
@@ -215,6 +217,13 @@ static inline int nvmem_device_write(struct nvmem_device *nvmem,
return -EOPNOTSUPP;
}
+static inline int nvmem_device_write_panic(struct nvmem_device *nvmem,
+ unsigned int offset, size_t bytes,
+ void *buf)
+{
+ return -EOPNOTSUPP;
+}
+
static inline const char *nvmem_dev_name(struct nvmem_device *nvmem)
{
return NULL;
diff --git a/include/linux/nvmem-provider.h b/include/linux/nvmem-provider.h
index 6063fe5b7..382ea9183 100644
--- a/include/linux/nvmem-provider.h
+++ b/include/linux/nvmem-provider.h
@@ -94,6 +94,8 @@ struct nvmem_cell_info {
* @of_node: If given, this will be used instead of the parent's of_node.
* @reg_read: Callback to read data; return zero if successful.
* @reg_write: Callback to write data; return zero if successful.
+ * @reg_write_panic: Callback to write data during panic; must not sleep,
+ * allocate, use DMA or wait for interrupts.
* @size: Device size.
* @word_size: Minimum read/write access granularity.
* @stride: Minimum read/write access stride.
@@ -128,6 +130,7 @@ struct nvmem_config {
struct device_node *of_node;
nvmem_reg_read_t reg_read;
nvmem_reg_write_t reg_write;
+ nvmem_reg_write_t reg_write_panic;
int size;
int word_size;
int stride;
--
2.47.3