[RFC v2 07/43] mm: PKRAM: link nodes by pfn before reboot

From: Anthony Yznaga
Date: Tue Mar 30 2021 - 17:28:22 EST


Since page structs are used for linking PKRAM nodes and cleared
on boot, organize all PKRAM nodes into a list singly-linked by pfns
before reboot to facilitate restoring the node list in the new kernel.

Originally-by: Vladimir Davydov <vdavydov.dev@xxxxxxxxx>
Signed-off-by: Anthony Yznaga <anthony.yznaga@xxxxxxxxxx>
---
mm/pkram.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 50 insertions(+)

diff --git a/mm/pkram.c b/mm/pkram.c
index d81af26c9a66..975f200aef38 100644
--- a/mm/pkram.c
+++ b/mm/pkram.c
@@ -2,12 +2,16 @@
#include <linux/err.h>
#include <linux/gfp.h>
#include <linux/highmem.h>
+#include <linux/init.h>
#include <linux/io.h>
#include <linux/kernel.h>
#include <linux/list.h>
#include <linux/mm.h>
+#include <linux/module.h>
#include <linux/mutex.h>
+#include <linux/notifier.h>
#include <linux/pkram.h>
+#include <linux/reboot.h>
#include <linux/sched.h>
#include <linux/string.h>
#include <linux/types.h>
@@ -62,11 +66,15 @@ struct pkram_obj {
* singly-linked list of PKRAM link structures (see above), the node has a
* pointer to the head of.
*
+ * To facilitate data restore in the new kernel, before reboot all PKRAM nodes
+ * are organized into a list singly-linked by pfn's (see pkram_reboot()).
+ *
* The structure occupies a memory page.
*/
struct pkram_node {
__u32 flags;
__u64 obj_pfn; /* points to the first obj of the node */
+ __u64 node_pfn; /* points to the next node in the node list */

__u8 name[PKRAM_NAME_MAX];
};
@@ -75,6 +83,10 @@ struct pkram_node {
#define PKRAM_LOAD 2
#define PKRAM_ACCMODE_MASK 3

+/*
+ * For convenience sake PKRAM nodes are kept in an auxiliary doubly-linked list
+ * connected through the lru field of the page struct.
+ */
static LIST_HEAD(pkram_nodes); /* linked through page::lru */
static DEFINE_MUTEX(pkram_mutex); /* serializes open/close */

@@ -780,3 +792,41 @@ size_t pkram_read(struct pkram_access *pa, void *buf, size_t count)
}
return read_count;
}
+
+/*
+ * Build the list of PKRAM nodes.
+ */
+static void __pkram_reboot(void)
+{
+ struct page *page;
+ struct pkram_node *node;
+ unsigned long node_pfn = 0;
+
+ list_for_each_entry_reverse(page, &pkram_nodes, lru) {
+ node = page_address(page);
+ if (WARN_ON(node->flags & PKRAM_ACCMODE_MASK))
+ continue;
+ node->node_pfn = node_pfn;
+ node_pfn = page_to_pfn(page);
+ }
+}
+
+static int pkram_reboot(struct notifier_block *notifier,
+ unsigned long val, void *v)
+{
+ if (val != SYS_RESTART)
+ return NOTIFY_DONE;
+ __pkram_reboot();
+ return NOTIFY_OK;
+}
+
+static struct notifier_block pkram_reboot_notifier = {
+ .notifier_call = pkram_reboot,
+};
+
+static int __init pkram_init(void)
+{
+ register_reboot_notifier(&pkram_reboot_notifier);
+ return 0;
+}
+module_init(pkram_init);
--
1.8.3.1