diff mbox series

pmem: emulating usable memory as persistent memory

Message ID 1569468653-3489-1-git-send-email-taeho1224@gmail.com (mailing list archive)
State New, archived
Headers show
Series pmem: emulating usable memory as persistent memory | expand

Commit Message

Taeho Hwang Sept. 26, 2019, 3:30 a.m. UTC
This patch checks whether the specified memory (memmap) is
userable or not. This patch prevents non-existing memory
from being emulated as persistent memory.
If non-existing memory is specified by memmap without
this patch, struct nd_namespace_io and struct pmem_device
will have invalid values.

Signed-off-by: Taeho Hwang <taeho1224@gmail.com>
---
 arch/x86/kernel/e820.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 55 insertions(+), 1 deletion(-)

Comments

Dan Williams Sept. 26, 2019, 3:58 a.m. UTC | #1
On Wed, Sep 25, 2019 at 8:31 PM Taeho Hwang <taeho1224@gmail.com> wrote:
>
> This patch checks whether the specified memory (memmap) is
> userable or not. This patch prevents non-existing memory
> from being emulated as persistent memory.
> If non-existing memory is specified by memmap without
> this patch, struct nd_namespace_io and struct pmem_device
> will have invalid values.

The whole point of the memmap= option is that the person specifying it
knows more about the memory map than either the kernel or the platform
BIOS. Validating the memmap= parameter against the very same memory
map that is trying to be overridden violates that assumption. So
memmap= is a dangerous interface on purpose. Now, if you want to
define a new option that requires the base type to be E820_RAM that
might be defensible, but at this point given the age of the
memmap=ss!nn option and the wikis available to picking a valid value,
I'd advocate leaving it as is.
diff mbox series

Patch

diff --git a/arch/x86/kernel/e820.c b/arch/x86/kernel/e820.c
index 76dd605..ba9115d 100644
--- a/arch/x86/kernel/e820.c
+++ b/arch/x86/kernel/e820.c
@@ -178,9 +178,63 @@  static void __init __e820__range_add(struct e820_table *table, u64 start, u64 si
 	table->nr_entries++;
 }
 
+static void __init __e820__range_add_pram(struct e820_table *table, u64 start,
+					  u64 size)
+{
+	int nr_entries = table->nr_entries;
+	int new_nr_entries = nr_entries;
+	u64 range_start, range_end;
+	u64 end = start + size;
+	bool flag;
+	int i;
+
+	if (nr_entries >= ARRAY_SIZE(table->entries)) {
+		pr_err("too many entries; ignoring [mem %#010llx-%#010llx]\n",
+		       start, start + size - 1);
+		return;
+	}
+
+	for (i = 0; i < nr_entries; i++) {
+		if (table->entries[i].type != E820_TYPE_RESERVED_KERN &&
+				table->entries[i].type != E820_TYPE_RAM)
+			continue;
+
+		range_start = table->entries[i].addr;
+		range_end = table->entries[i].addr + table->entries[i].size;
+		flag = false;
+		if (range_start <= start && start < range_end) {
+			table->entries[new_nr_entries].addr = start;
+			if (end <= range_end)
+				table->entries[new_nr_entries].size = size;
+			else
+				table->entries[new_nr_entries].size =
+					range_end - start;
+			flag = true;
+		} else if (range_start < end && end <= range_end) {
+			table->entries[new_nr_entries].addr = range_start;
+			table->entries[new_nr_entries].size = end - range_end;
+			flag = true;
+		} else if (start <= range_start && range_end <= end) {
+			table->entries[new_nr_entries].addr = range_start;
+			table->entries[new_nr_entries].size =
+				range_end - range_start;
+			flag = true;
+		}
+		if (flag) {
+			table->entries[new_nr_entries].type = E820_TYPE_PRAM;
+			if (++new_nr_entries >= ARRAY_SIZE(table->entries))
+				break;
+		}
+	}
+	table->nr_entries = new_nr_entries;
+}
+
 void __init e820__range_add(u64 start, u64 size, enum e820_type type)
 {
-	__e820__range_add(e820_table, start, size, type);
+	if (type == E820_TYPE_PRAM || type == E820_TYPE_PMEM)
+		__e820__range_add_pram(e820_table, start, size);
+	else
+		__e820__range_add(e820_table, start, size, type);
 }
 
 static void __init e820_print_type(enum e820_type type)