Skip to contentSkip to conversation

kdump: Allow removal of page tables for crashkernel memory

PATCH Superseded Michael Holzheu 3 participants 8 messages Sep 13, 2011 – Sep 15, 2011

Subsystems (4)

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover kdump: Allow removal of page tables for crashkernel memory

Hello Vivek, Andrew, Martin

Here the updated patch series for the removal of page tables for the crashkernel memory:

[1] kdump: Add infrastructure for unmapping crashkernel memory
[2] s390: Add architecture code for unmapping crashkernel memory

The patches apply on top of the last kdump patch series that I sent.

I would suggest that Martin sends the patches together with the other s390 kdump patches in the next merge window.

Andrew is that ok for you?

Michael

#1 lore raw

PATCH v2 1/2 kdump: Add infrastructure for unmapping crashkernel memory

From: Michael Holzheu <[email protected]>

This patch introduces a mechanism that allows architecture backends to remove page tables for the crashkernel memory. This can protect the loaded kdump kernel from being overwritten by broken kernel code. Two new functions crash_map_reserved_pages() and crash_unmap_reserved_pages() are added that can be implemented by architecture code. The crash_map_reserved_pages() function is called before and crash_unmap_reserved_pages() after the crashkernel segments are loaded. The functions are also called in crash_shrink_memory() to create/remove page tables when the crashkernel memory size is reduced.

To support architectures that have large pages this patch also introduces a new define KEXEC_CRASH_MEM_ALIGN. The crashkernel start and size must always be aligned with KEXEC_CRASH_MEM_ALIGN.

---
 include/linux/kexec.h |    6 ++++++
 kernel/kexec.c        |   21 +++++++++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
include/linux/kexec.h +6 -0
@@ -37,6 +37,10 @@
 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
 #endif
 
+#ifndef KEXEC_CRASH_MEM_ALIGN
+#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
+#endif
+
 #define KEXEC_NOTE_HEAD_BYTES ALIGN(sizeof(struct elf_note), 4)
 #define KEXEC_CORE_NOTE_NAME "CORE"
 #define KEXEC_CORE_NOTE_NAME_BYTES ALIGN(sizeof(KEXEC_CORE_NOTE_NAME), 4)
@@ -133,6 +137,8 @@ extern void crash_kexec(struct pt_regs *
 int kexec_should_crash(struct task_struct *);
 void crash_save_cpu(struct pt_regs *regs, int cpu);
 void crash_save_vmcoreinfo(void);
+void crash_map_reserved_pages(void);
+void crash_unmap_reserved_pages(void);
 void arch_crash_save_vmcoreinfo(void);
 void vmcoreinfo_append_str(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
kernel/kexec.c +19 -2
@@ -999,6 +999,7 @@ SYSCALL_DEFINE4(kexec_load, unsigned lon
 			kimage_free(xchg(&kexec_crash_image, NULL));
 			result = kimage_crash_alloc(&image, entry,
 						     nr_segments, segments);
+			crash_map_reserved_pages();
 		}
 		if (result)
 			goto out;
@@ -1015,6 +1016,8 @@ SYSCALL_DEFINE4(kexec_load, unsigned lon
 				goto out;
 		}
 		kimage_terminate(image);
+		if (flags & KEXEC_ON_CRASH)
+			crash_unmap_reserved_pages();
 	}
 	/* Install the new kernel, and  Uninstall the old */
 	image = xchg(dest_image, image);
@@ -1026,6 +1029,18 @@ out:
 	return result;
 }
 
+/*
+ * Add and remove page tables for crashkernel memory
+ *
+ * Provide an empty default implementation here -- architecture
+ * code may override this
+ */
+void __weak crash_map_reserved_pages(void)
+{}
+
+void __weak crash_unmap_reserved_pages(void)
+{}
+
 #ifdef CONFIG_COMPAT
 asmlinkage long compat_sys_kexec_load(unsigned long entry,
 				unsigned long nr_segments,
@@ -1134,14 +1149,16 @@ int crash_shrink_memory(unsigned long ne
 		goto unlock;
 	}
 
-	start = roundup(start, PAGE_SIZE);
-	end = roundup(start + new_size, PAGE_SIZE);
+	start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
+	end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
 
+	crash_map_reserved_pages();
 	crash_free_reserved_phys_range(end, crashk_res.end);
 
 	if ((start == end) && (crashk_res.parent != NULL))
 		release_resource(&crashk_res);
 	crashk_res.end = end - 1;
+	crash_unmap_reserved_pages();
 
 unlock:
 	mutex_unlock(&kexec_mutex);
Acked-by on v2 1/2
#2 lore raw

Re: [patch v2 1/2] kdump: Add infrastructure for unmapping crashkernel memory

Quoted 12 lines · Michael Holzheu

From: Michael Holzheu <[email protected]>

This patch introduces a mechanism that allows architecture backends to remove page tables for the crashkernel memory. This can protect the loaded kdump kernel from being overwritten by broken kernel code. Two new functions crash_map_reserved_pages() and crash_unmap_reserved_pages() are added that can be implemented by architecture code. The crash_map_reserved_pages() function is called before and crash_unmap_reserved_pages() after the crashkernel segments are loaded. The functions are also called in crash_shrink_memory() to create/remove page tables when the crashkernel memory size is reduced.

Quoted 5 lines

To support architectures that have large pages this patch also introduces a new define KEXEC_CRASH_MEM_ALIGN. The crashkernel start and size must always be aligned with KEXEC_CRASH_MEM_ALIGN.

Looks reasonable conceptually. Hence...

Thanks
Vivek

Quoted 84 lines
---
 include/linux/kexec.h |    6 ++++++
 kernel/kexec.c        |   21 +++++++++++++++++++--
 2 files changed, 25 insertions(+), 2 deletions(-)
include/linux/kexec.h +6 -0
@@ -37,6 +37,10 @@
 #define KEXEC_CRASH_CONTROL_MEMORY_LIMIT KEXEC_CONTROL_MEMORY_LIMIT
 #endif
 
+#ifndef KEXEC_CRASH_MEM_ALIGN
+#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
+#endif
+
 #define KEXEC_NOTE_HEAD_BYTES ALIGN(sizeof(struct elf_note), 4)
 #define KEXEC_CORE_NOTE_NAME "CORE"
 #define KEXEC_CORE_NOTE_NAME_BYTES ALIGN(sizeof(KEXEC_CORE_NOTE_NAME), 4)
@@ -133,6 +137,8 @@ extern void crash_kexec(struct pt_regs *
 int kexec_should_crash(struct task_struct *);
 void crash_save_cpu(struct pt_regs *regs, int cpu);
 void crash_save_vmcoreinfo(void);
+void crash_map_reserved_pages(void);
+void crash_unmap_reserved_pages(void);
 void arch_crash_save_vmcoreinfo(void);
 void vmcoreinfo_append_str(const char *fmt, ...)
 	__attribute__ ((format (printf, 1, 2)));
kernel/kexec.c +19 -2
@@ -999,6 +999,7 @@ SYSCALL_DEFINE4(kexec_load, unsigned lon
 			kimage_free(xchg(&kexec_crash_image, NULL));
 			result = kimage_crash_alloc(&image, entry,
 						     nr_segments, segments);
+			crash_map_reserved_pages();
 		}
 		if (result)
 			goto out;
@@ -1015,6 +1016,8 @@ SYSCALL_DEFINE4(kexec_load, unsigned lon
 				goto out;
 		}
 		kimage_terminate(image);
+		if (flags & KEXEC_ON_CRASH)
+			crash_unmap_reserved_pages();
 	}
 	/* Install the new kernel, and  Uninstall the old */
 	image = xchg(dest_image, image);
@@ -1026,6 +1029,18 @@ out:
 	return result;
 }
 
+/*
+ * Add and remove page tables for crashkernel memory
+ *
+ * Provide an empty default implementation here -- architecture
+ * code may override this
+ */
+void __weak crash_map_reserved_pages(void)
+{}
+
+void __weak crash_unmap_reserved_pages(void)
+{}
+
 #ifdef CONFIG_COMPAT
 asmlinkage long compat_sys_kexec_load(unsigned long entry,
 				unsigned long nr_segments,
@@ -1134,14 +1149,16 @@ int crash_shrink_memory(unsigned long ne
 		goto unlock;
 	}
 
-	start = roundup(start, PAGE_SIZE);
-	end = roundup(start + new_size, PAGE_SIZE);
+	start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
+	end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
 
+	crash_map_reserved_pages();
 	crash_free_reserved_phys_range(end, crashk_res.end);
 
 	if ((start == end) && (crashk_res.parent != NULL))
 		release_resource(&crashk_res);
 	crashk_res.end = end - 1;
+	crash_unmap_reserved_pages();
 
 unlock:
 	mutex_unlock(&kexec_mutex);
#3 lore raw

PATCH v2 2/2 s390: Add architecture code for unmapping crashkernel memory

In reply to Michael Holzheu

From: Michael Holzheu <[email protected]>

This patch implements the crash_map_pages() function for s390. KEXEC_CRASH_MEM_ALIGN is set to HPAGE_SIZE, in order to support kernel mappings that use large pages.

---
 arch/s390/include/asm/kexec.h    |    3 +++
 arch/s390/kernel/machine_kexec.c |   31 +++++++++++++++++++++++++++++++
 arch/s390/kernel/setup.c         |   10 ++++++----
 3 files changed, 40 insertions(+), 4 deletions(-)
arch/s390/include/asm/kexec.h +3 -0
@@ -36,6 +36,9 @@
 /* Allocate one page for the pdp and the second for the code */
 #define KEXEC_CONTROL_PAGE_SIZE 4096
 
+/* Alignment of crashkernel memory */
+#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE
+
 /* The native architecture */
 #define KEXEC_ARCH KEXEC_ARCH_S390
 
arch/s390/kernel/machine_kexec.c +31 -0
@@ -243,6 +243,37 @@ static void __machine_kdump(void *image)
 #endif
 
 /*
+ * Map or unmap crashkernel memory
+ */
+static void crash_map_pages(int enable)
+{
+	unsigned long size = crashk_res.end - crashk_res.start + 1;
+
+	BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
+	       size % KEXEC_CRASH_MEM_ALIGN);
+	if (enable)
+		vmem_add_mapping(crashk_res.start, size);
+	else
+		vmem_remove_mapping(crashk_res.start, size);
+}
+
+/*
+ * Map crashkernel memory
+ */
+void crash_map_reserved_pages(void)
+{
+	crash_map_pages(1);
+}
+
+/*
+ * Unmap crashkernel memory
+ */
+void crash_unmap_reserved_pages(void)
+{
+	crash_map_pages(0);
+}
+
+/*
  * Give back memory to hypervisor before new kdump is loaded
  */
 static int machine_kexec_prepare_kdump(void)
arch/s390/kernel/setup.c +6 -4
@@ -446,6 +446,7 @@ static void __init setup_resources(void)
 		res->flags = IORESOURCE_BUSY | IORESOURCE_MEM;
 		switch (memory_chunk[i].type) {
 		case CHUNK_READ_WRITE:
+		case CHUNK_CRASHK:
 			res->name = "System RAM";
 			break;
 		case CHUNK_READ_ONLY:
@@ -706,8 +707,8 @@ static void __init reserve_crashkernel(v
 			       &crash_base);
 	if (rc || crash_size == 0)
 		return;
-	crash_base = PAGE_ALIGN(crash_base);
-	crash_size = PAGE_ALIGN(crash_size);
+	crash_base = ALIGN(crash_base, KEXEC_CRASH_MEM_ALIGN);
+	crash_size = ALIGN(crash_size, KEXEC_CRASH_MEM_ALIGN);
 	if (register_memory_notifier(&kdump_mem_nb))
 		return;
 	if (!crash_base)
@@ -727,7 +728,7 @@ static void __init reserve_crashkernel(v
 	crashk_res.start = crash_base;
 	crashk_res.end = crash_base + crash_size - 1;
 	insert_resource(&iomem_resource, &crashk_res);
-	reserve_kdump_bootmem(crash_base, crash_size, CHUNK_READ_WRITE);
+	reserve_kdump_bootmem(crash_base, crash_size, CHUNK_CRASHK);
 	pr_info("Reserving %lluMB of memory at %lluMB "
 		"for crashkernel (System RAM: %luMB)\n",
 		crash_size >> 20, crash_base >> 20, memory_end >> 20);
@@ -802,7 +803,8 @@ setup_memory(void)
 	for (i = 0; i < MEMORY_CHUNKS && memory_chunk[i].size > 0; i++) {
 		unsigned long start_chunk, end_chunk, pfn;
 
-		if (memory_chunk[i].type != CHUNK_READ_WRITE)
+		if (memory_chunk[i].type != CHUNK_READ_WRITE &&
+		    memory_chunk[i].type != CHUNK_CRASHK)
 			continue;
 		start_chunk = PFN_DOWN(memory_chunk[i].addr);
 		end_chunk = start_chunk + PFN_DOWN(memory_chunk[i].size);
#4 lore raw

Re: [patch v2 2/2] s390: Add architecture code for unmapping crashkernel memory

Quoted 16 lines · On Tue, 13 Sep 2011 15:26:37 +0200 Michael Holzheu…

From: Michael Holzheu <[email protected]>

This patch implements the crash_map_pages() function for s390. KEXEC_CRASH_MEM_ALIGN is set to HPAGE_SIZE, in order to support kernel mappings that use large pages.

---
 arch/s390/include/asm/kexec.h    |    3 +++
 arch/s390/kernel/machine_kexec.c |   31 +++++++++++++++++++++++++++++++
 arch/s390/kernel/setup.c         |   10 ++++++----
 3 files changed, 40 insertions(+), 4 deletions(-)

arch/s390/include/asm/kexec.h line 40

Quoted 5 lines
arch/s390/include/asm/kexec.h +2 -0
@@ -36,6 +36,9 @@
 /* Allocate one page for the pdp and the second for the code */
 #define KEXEC_CONTROL_PAGE_SIZE 4096
 
+/* Alignment of crashkernel memory */
+#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE

Why not make this unconditional, for all architectures which support hugepages? ie:

#ifdef HPAGE_SIZE
#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE
#else
#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
#endif

in include/linux/kexec.h?

IOW, what are the compromises here?

Also, does s390 support CONFIG_HUGETLB_PAGE=n? If so, does the use of HPAGE_SIZE still make sense? Does it compile?

Quoted 6 lines
arch/s390/include/asm/kexec.h +0 -0
 /* The native architecture */
 #define KEXEC_ARCH KEXEC_ARCH_S390
 

arch/s390/kernel/machine_kexec.c static void __machine_kdump(void *image) · line 250

Quoted 8 lines
arch/s390/kernel/machine_kexec.c +5 -0
@@ -243,6 +243,37 @@ static void __machine_kdump(void *image)
 #endif
 
 /*
+ * Map or unmap crashkernel memory
+ */
+static void crash_map_pages(int enable)
+{
+	unsigned long size = crashk_res.end - crashk_res.start + 1;

resource_size().

Quoted 10 lines
arch/s390/kernel/machine_kexec.c +7 -0
+	BUG_ON(crashk_res.start % KEXEC_CRASH_MEM_ALIGN ||
+	       size % KEXEC_CRASH_MEM_ALIGN);
+	if (enable)
+		vmem_add_mapping(crashk_res.start, size);
+	else
+		vmem_remove_mapping(crashk_res.start, size);
+}
 
...
#5 lore raw

Re: [patch v2 2/2] s390: Add architecture code for unmapping crashkernel memory

Hello Andrew,

Quoted 2 lines · Andrew Morton

On Tue, 13 Sep 2011 15:26:37 +0200
Michael Holzheu <[email protected]> wrote:

[snip]

Quoted 18 lines

arch/s390/include/asm/kexec.h line 40

Quoted 8 lines
arch/s390/include/asm/kexec.h +2 -0
@@ -36,6 +36,9 @@
 /* Allocate one page for the pdp and the second for the code */
 #define KEXEC_CONTROL_PAGE_SIZE 4096
 
+/* Alignment of crashkernel memory */
+#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE

Why not make this unconditional, for all architectures which support hugepages? ie:

#ifdef HPAGE_SIZE
#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE
#else
#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
#endif

Quoted 3 lines

in include/linux/kexec.h?

IOW, what are the compromises here?

If we would do it that way, crashkernel memory on architectures that support large pages but do not support unmapping of crashkernel memory would always be aligned to HPAGE_SIZE. But only PAGE_SIZE alignment would be necessary in that case.

If that is acceptable I have no problem to define that unconditional for all architectures. Vivek what do you think?

Quoted 3 lines

Also, does s390 support CONFIG_HUGETLB_PAGE=n? If so, does the use of HPAGE_SIZE still make sense? Does it compile?

Yes, s390 supports CONFIG_HUGETLB_PAGE=n and it still compiles in that case. For us it is ok to use HPAGE_SIZE crashkernel alignment also for kernels with CONFIG_HUGETLB_PAGE=n.

Quoted 10 lines
Quoted 10 lines
arch/s390/include/asm/kexec.h +0 -0
 /* The native architecture */
 #define KEXEC_ARCH KEXEC_ARCH_S390
 
arch/s390/kernel/machine_kexec.c +0 -0
@@ -243,6 +243,37 @@ static void __machine_kdump(void *image)
 #endif
 
Quoted 8 lines

arch/s390/kernel/machine_kexec.c static void __machine_kdump(void *image) · line 250

Quoted 6 lines
arch/s390/kernel/machine_kexec.c +5 -0
 /*
+ * Map or unmap crashkernel memory
+ */
+static void crash_map_pages(int enable)
+{
+	unsigned long size = crashk_res.end - crashk_res.start + 1;

resource_size().

Ok I will use that. Thanks for the hint.

Michael

#6 lore raw

Re: [patch v2 2/2] s390: Add architecture code for unmapping crashkernel memory

Quoted 30 lines · Michael Holzheu

Hello Andrew,

Quoted 2 lines · Andrew Morton

On Tue, 13 Sep 2011 15:26:37 +0200
Michael Holzheu <[email protected]> wrote:

[snip]

Quoted 18 lines

arch/s390/include/asm/kexec.h line 40

Quoted 8 lines
arch/s390/include/asm/kexec.h +2 -0
@@ -36,6 +36,9 @@
 /* Allocate one page for the pdp and the second for the code */
 #define KEXEC_CONTROL_PAGE_SIZE 4096
 
+/* Alignment of crashkernel memory */
+#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE

Why not make this unconditional, for all architectures which support hugepages? ie:

#ifdef HPAGE_SIZE
#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE
#else
#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
#endif

Quoted 3 lines

in include/linux/kexec.h?

IOW, what are the compromises here?

Quoted 7 lines

If we would do it that way, crashkernel memory on architectures that support large pages but do not support unmapping of crashkernel memory would always be aligned to HPAGE_SIZE. But only PAGE_SIZE alignment would be necessary in that case.

If that is acceptable I have no problem to define that unconditional for all architectures. Vivek what do you think?

As PAGE_SIZE alignment is sufficient for rest of the architecture, I am fine with keeping it in arch dependent files.

Thanks
Vivek

#7 lore raw

Re: [patch v2 2/2] s390: Add architecture code for unmapping crashkernel memory

Hello Vivek, Andrew,

Quoted 19 lines · Vivek Goyal
Quoted 18 lines · Michael Holzheu
Quoted 9 lines

Why not make this unconditional, for all architectures which support hugepages? ie:

#ifdef HPAGE_SIZE
#define KEXEC_CRASH_MEM_ALIGN HPAGE_SIZE
#else
#define KEXEC_CRASH_MEM_ALIGN PAGE_SIZE
#endif

Quoted 3 lines

in include/linux/kexec.h?

IOW, what are the compromises here?

If we would do it that way, crashkernel memory on architectures that support large pages but do not support unmapping of crashkernel memory would always be aligned to HPAGE_SIZE. But only PAGE_SIZE alignment would be necessary in that case.

Quoted 5 lines
Quoted 2 lines

If that is acceptable I have no problem to define that unconditional for all architectures. Vivek what do you think?

As PAGE_SIZE alignment is sufficient for rest of the architecture, I am fine with keeping it in arch dependent files.

Ok fine. So I will resend the two patches including Andrew's resource_size() fix and keep KEXEC_CRASH_MEM_ALIGN definition architecture dependent.

Michael