Add shared memory type of pstore.

Signed-off-by: Eugen Hristev <[email protected]>
---
 fs/pstore/Kconfig           |  13 ++++
 fs/pstore/Makefile          |   3 +
 fs/pstore/smem.c            | 115 ++++++++++++++++++++++++++++++++++++
 include/linux/pstore_smem.h |   9 +++
 4 files changed, 140 insertions(+)
 create mode 100644 fs/pstore/smem.c
 create mode 100644 include/linux/pstore_smem.h

diff --git a/fs/pstore/Kconfig b/fs/pstore/Kconfig
index 3acc38600cd1..84f87edf9b8f 100644
--- a/fs/pstore/Kconfig
+++ b/fs/pstore/Kconfig
@@ -81,6 +81,19 @@ config PSTORE_RAM
 
          For more information, see Documentation/admin-guide/ramoops.rst.
 
+config PSTORE_SMEM
+       tristate "Log panic/oops to a shared memory buffer"
+       depends on PSTORE
+       select PSTORE_ZONE
+       help
+         This enables panic and oops messages to be logged to memory
+         that is shared between different hardware blocks in the system.
+         This shared memory can be a static ram, a part of dynamic RAM,
+         a dedicated cache or memory area specific for crash dumps,
+         or even a memory on an attached device.
+
+         if unsure, say N.
+
 config PSTORE_ZONE
        tristate
        depends on PSTORE
diff --git a/fs/pstore/Makefile b/fs/pstore/Makefile
index c270467aeece..f2a314ca03a0 100644
--- a/fs/pstore/Makefile
+++ b/fs/pstore/Makefile
@@ -18,3 +18,6 @@ obj-$(CONFIG_PSTORE_ZONE)     += pstore_zone.o
 
 pstore_blk-objs += blk.o
 obj-$(CONFIG_PSTORE_BLK)       += pstore_blk.o
+
+pstore_smem-objs += smem.o
+obj-$(CONFIG_PSTORE_SMEM)      += pstore_smem.o
diff --git a/fs/pstore/smem.c b/fs/pstore/smem.c
new file mode 100644
index 000000000000..9eedd7df5446
--- /dev/null
+++ b/fs/pstore/smem.c
@@ -0,0 +1,115 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Implements pstore backend driver for shared memory devices,
+ * using the pstore/zone API.
+ */
+
+#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/blkdev.h>
+#include <linux/string.h>
+#include <linux/of.h>
+#include <linux/of_address.h>
+#include <linux/platform_device.h>
+#include <linux/pstore_smem.h>
+#include <linux/fs.h>
+#include <linux/file.h>
+#include <linux/init_syscalls.h>
+#include <linux/mount.h>
+
+/*
+ * All globals must only be accessed under the pstore_smem_lock
+ * during the register/unregister functions.
+ */
+static DEFINE_MUTEX(pstore_smem_lock);
+static struct pstore_device_info *pstore_device_info;
+
+static int __register_pstore_device(struct pstore_device_info *dev)
+{
+       int ret;
+
+       lockdep_assert_held(&pstore_smem_lock);
+
+       if (!dev) {
+               pr_err("NULL device info\n");
+               return -EINVAL;
+       }
+       if (!dev->zone.total_size) {
+               pr_err("zero sized device\n");
+               return -EINVAL;
+       }
+       if (!dev->zone.read) {
+               pr_err("no read handler for device\n");
+               return -EINVAL;
+       }
+       if (!dev->zone.write) {
+               pr_err("no write handler for device\n");
+               return -EINVAL;
+       }
+
+       /* someone already registered before */
+       if (pstore_device_info)
+               return -EBUSY;
+
+       /* zero means not limit on which backends to attempt to store. */
+       if (!dev->flags)
+               dev->flags = UINT_MAX;
+
+       /* Initialize required zone ownership details. */
+       dev->zone.name = KBUILD_MODNAME;
+       dev->zone.owner = THIS_MODULE;
+
+       ret = register_pstore_zone(&dev->zone);
+       if (ret == 0)
+               pstore_device_info = dev;
+
+       return ret;
+}
+/**
+ * register_pstore_smem_device() - register smem device to pstore
+ *
+ * @dev: smem device information
+ *
+ * Return:
+ * * 0         - OK
+ * * Others    - some error.
+ */
+int register_pstore_smem_device(struct pstore_device_info *dev)
+{
+       int ret;
+
+       mutex_lock(&pstore_smem_lock);
+       ret = __register_pstore_device(dev);
+       mutex_unlock(&pstore_smem_lock);
+
+       return ret;
+}
+EXPORT_SYMBOL_GPL(register_pstore_smem_device);
+
+static void __unregister_pstore_device(struct pstore_device_info *dev)
+{
+       lockdep_assert_held(&pstore_smem_lock);
+       if (pstore_device_info && pstore_device_info == dev) {
+               unregister_pstore_zone(&dev->zone);
+               pstore_device_info = NULL;
+       }
+}
+
+/**
+ * unregister_pstore_smem_device() - unregister smem device from pstore
+ *
+ * @dev: smem device information
+ */
+void unregister_pstore_smem_device(struct pstore_device_info *dev)
+{
+       mutex_lock(&pstore_smem_lock);
+       __unregister_pstore_device(dev);
+       mutex_unlock(&pstore_smem_lock);
+}
+EXPORT_SYMBOL_GPL(unregister_pstore_smem_device);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Eugen Hristev <[email protected]>");
+MODULE_DESCRIPTION("pstore backend for smem devices");
diff --git a/include/linux/pstore_smem.h b/include/linux/pstore_smem.h
new file mode 100644
index 000000000000..f0ad23e117c4
--- /dev/null
+++ b/include/linux/pstore_smem.h
@@ -0,0 +1,9 @@
+#ifndef PSTORE_SMEM_H
+#define PSTORE_SMEM_H
+
+#include <linux/pstore_zone.h>
+
+int register_pstore_smem_device(struct pstore_device_info *dev);
+void unregister_pstore_smem_device(struct pstore_device_info *dev);
+
+#endif
-- 
2.43.0


Reply via email to