Skip to contentSkip to conversation

Kexec: Enable run time memory resrvation of crash kernel

RFC In review Ronit Halder 3 participants 7 messages Aug 12, 2016 – Aug 23, 2016

Subsystems (7)
lore
Share & tools
lore
#0 lore raw

cover Kexec: Enable run time memory resrvation of crash kernel

Currenty linux kernel reserves memory at the boot time for crash kernel. It will be very useful if we can reserve memory in run time. The user can reserve the memory whenerver needed instead of reserving at the boot time.

It is possible to reserve memory for crash kernel at the run time using CMA (Contiguous Memory Allocator). CMA is capable of allocating big chunk of memory. At the boot time we will create one (if only low memory is used) or two (if we use both high memory in case of x86_64) CMA areas of size given in "crashkernel" boot time command line parameter. This memory in CMA areas can be used as movable pages (used for disk caches, process pages etc) if not allocated. Then the user can reserve or free memory from those CMA areas using "/sys/kernel/kexec_crash_size" sysfs entry. If the user uses high memory it will automatically at least 256MB low memory (needed for swiotlb and DMA buffers) when the user allocates memory using mentioned sysfs enrty. In case of high memory reservation the user controls the size of reserved region in high memory with "/sys/kernel/kexec_crash_size" entry. If the size set is zero then the memory allocated in low memory will automatically be freed.

As the pages under CMA area (when not allocated by CMA) can only be used by movable pages. The pages won't be used for DMA. So, after allocating pages from CMA area for loading the crash kernel, there won't be any chance of DMA on the memory.

Thus is a prototype patch. Please share your opinions on my approach. This patch is only for x86 and x86_64. Please note, this patch is only a prototype just to explain my approach and get the review. This patch is on kernel version v4.4.11.

CMA depends on page migration and only uses movable pages. But, the movable pages become unmovable momentarily for pinning. The CMA fails for this reason. I don't have any solution for that right now. This approach will work when the this problems with CMA will be fixed. The patch is enabled by a kernel configuration option CONFIG_KEXEC_CMA.

Ronit Halder (4):
Creating one or two CMA area at Boot time Functions for memory reservation and release Adding a new kernel configuration to enable the feature Enable memory allocation through sysfs interface

 arch/x86/kernel/setup.c | 44 ++++++++++++++++++++++++--
 include/linux/kexec.h   | 11 ++++++-
 kernel/kexec_core.c     | 83 +++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c         | 23 +++++++++++++-
 mm/Kconfig              |  6 ++++
 5 files changed, 162 insertions(+), 5 deletions(-)
Signature · 7 lines
2.9.0.GIT

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

RFC 1/4 Creating one or two CMA area at Boot time

This patch create CMA area(s) at boot time. In case of x86_32 only one CMA area will be created. In case of x86_64 if the user wants to reserve high memory for crash kernel, then there must be at least 256MB (needed for swiotlb and DMA buffers) low memory. In that case two CMA areas (one in low memory and one in high memory) will be created.

---
 arch/x86/kernel/setup.c | 44 +++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 41 insertions(+), 3 deletions(-)
arch/x86/kernel/setup.c +41 -3
index d2bbe34..87c16c7 100644
@@ -69,6 +69,7 @@
 #include <linux/crash_dump.h>
 #include <linux/tboot.h>
 #include <linux/jiffies.h>
+#include <linux/cma.h>
 
 #include <video/edid.h>
 
@@ -123,6 +124,10 @@
 unsigned long max_low_pfn_mapped;
 unsigned long max_pfn_mapped;
 
+#ifdef CONFIG_KEXEC_CMA
+struct cma *crashk_cma;
+struct cma *crashk_cma_low;
+#endif
 #ifdef CONFIG_DMI
 RESERVE_BRK(dmi_alloc, 65536);
 #endif
@@ -532,6 +537,18 @@ static int __init reserve_crashkernel_low(void)
 		return -ENOMEM;
 	}
 
+#ifdef CONFIG_KEXEC_CMA
+	ret =  cma_declare_contiguous(low_base, low_size, 0, CRASH_ALIGN, 0, 1, &crashk_cma_low);
+	if (ret) {
+		pr_err("%s: Error reserving CMA area for crashkernel low.\n", __func__);
+		return ret;
+	}
+
+	pr_info("Reserving %ldMB of low memory at %ldMB for CMA area for crashkernel low(System low RAM: %ldMB)\n",
+		(unsigned long)(low_size >> 20),
+		(unsigned long)(low_base >> 20),
+		(unsigned long)(total_low_mem >> 20));
+#else
 	ret = memblock_reserve(low_base, low_size);
 	if (ret) {
 		pr_err("%s: Error reserving crashkernel low memblock.\n", __func__);
@@ -547,6 +564,7 @@ static int __init reserve_crashkernel_low(void)
 	crashk_low_res.end   = low_base + low_size - 1;
 	insert_resource(&iomem_resource, &crashk_low_res);
 #endif
+#endif
 	return 0;
 }
 
@@ -578,8 +596,10 @@ static void __init reserve_crashkernel(void)
 						    high ? CRASH_ADDR_HIGH_MAX
 							 : CRASH_ADDR_LOW_MAX,
 						    crash_size, CRASH_ALIGN);
+		pr_info("Crash_base %llu crash_size %llu\n", crash_base, crash_size);
+
 		if (!crash_base) {
-			pr_info("crashkernel reservation failed - No suitable area found.\n");
+			pr_info("Crashkernel reservation failed - No suitable area found.\n");
 			return;
 		}
 
@@ -589,11 +609,28 @@ static void __init reserve_crashkernel(void)
 		start = memblock_find_in_range(crash_base,
 					       crash_base + crash_size,
 					       crash_size, 1 << 20);
+		pr_info("Base_mentioned crash_base %llu crash_size %llu\n", crash_base, crash_size);
 		if (start != crash_base) {
 			pr_info("crashkernel reservation failed - memory is in use.\n");
 			return;
 		}
 	}
+#ifdef CONFIG_KEXEC_CMA
+	crashk_cma = NULL;
+	crashk_cma_low = NULL;
+	if (crash_base >= (1ULL << 32) && reserve_crashkernel_low())
+		return;
+	ret =  cma_declare_contiguous(crash_base, crash_size, 0, CRASH_ALIGN, 0, 1, &crashk_cma);
+	if (ret) {
+		pr_err("%s: Error reserving CMA area for crashkernel.\n", __func__);
+		return;
+	}
+
+	pr_info("Reserving %ldMB of memory at %ldMB for crashkernel (System RAM: %ldMB)\n",
+		(unsigned long)(crash_size >> 20),
+		(unsigned long)(crash_base >> 20),
+		(unsigned long)(total_mem >> 20));
+#else
 	ret = memblock_reserve(crash_base, crash_size);
 	if (ret) {
 		pr_err("%s: Error reserving crashkernel memblock.\n", __func__);
@@ -613,6 +650,7 @@ static void __init reserve_crashkernel(void)
 	crashk_res.start = crash_base;
 	crashk_res.end   = crash_base + crash_size - 1;
 	insert_resource(&iomem_resource, &crashk_res);
+#endif
 }
 #else
 static void __init reserve_crashkernel(void)
@@ -720,7 +758,7 @@ static void __init trim_snb_memory(void)
 	 * already been reserved.
 	 */
 	memblock_reserve(0, 1<<20);
-	
+
 	for (i = 0; i < ARRAY_SIZE(bad_pages); i++) {
 		if (memblock_reserve(bad_pages[i], PAGE_SIZE))
 			printk(KERN_WARNING "failed to reserve 0x%08lx\n",
@@ -812,7 +850,7 @@ static void __init trim_low_memory_range(void)
 {
 	memblock_reserve(0, ALIGN(reserve_low, PAGE_SIZE));
 }
-	
+
 /*
  * Dump out kernel offset information on panic.
  */
Signature · 7 lines
2.9.0.GIT

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#2 lore raw

RFC 2/4 Functions for memory reservation and release

In reply to Ronit Halder

Functions reserve and release memory from CMA area(s).

---
 include/linux/kexec.h | 11 ++++++-
 kernel/kexec_core.c   | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 93 insertions(+), 1 deletion(-)
include/linux/kexec.h +10 -1
index d140b1e..9a1af44 100644
@@ -307,7 +307,6 @@ extern size_t vmcoreinfo_max_size;
 
 /* flag to track if kexec reboot is in progress */
 extern bool kexec_in_progress;
-
 int __init parse_crashkernel(char *cmdline, unsigned long long system_ram,
 		unsigned long long *crash_size, unsigned long long *crash_base);
 int parse_crashkernel_high(char *cmdline, unsigned long long system_ram,
@@ -318,6 +317,16 @@ int crash_shrink_memory(unsigned long new_size);
 size_t crash_get_memory_size(void);
 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
 
+#ifdef CONFIG_KEXEC_CMA
+#ifdef CONFIG_X86
+	extern struct cma *crashk_cma, *crashk_cma_low;
+	int crash_free_memory(unsigned int size);
+	int crash_alloc_memory(unsigned int size);
+	int crash_alloc_memory_low(void);
+	int crash_free_memory_low(void);
+	size_t crash_get_memory_size_low(void);
+#endif
+#endif
 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
 					 unsigned long buf_len);
 void * __weak arch_kexec_kernel_image_load(struct kimage *image);
kernel/kexec_core.c +83 -0

108 lines not on this page · raw message

Signature · 7 lines
2.9.0.GIT

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#3 lore raw

RFC 3/4 Adding a new kernel configuration to enable the feature

In reply to Ronit Halder

Kernel configuration option added to enable run time memory reservation feature for kexec.

---
 mm/Kconfig | 6 ++++++
 1 file changed, 6 insertions(+)
mm/Kconfig +6 -0
index 97a4e06..8b1533d 100644
@@ -668,3 +668,9 @@ config ZONE_DEVICE
 
 config FRAME_VECTOR
 	bool
+config KEXEC_CMA
+	bool "Use CMA for Kexec crash kernel"
+	depends on CMA
+	help
+	  This configuration option is to use CMA for Kexec.
+	  CMA helps us to allocate memory for crash kernel at runtime.
\ No newline at end of file
Signature · 7 lines
2.9.0.GIT

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#4 lore raw

RFC 4/4 Enable memory allocation through sysfs interface

In reply to Ronit Halder

Modify the sysfs entry "kernel/kexec_crash_size" to allocate or release memory at run time. The memory size will be written to the entry in MB. If the user uses high memory (in x86_64). Then size will be only set for high memory. The low memory will be allocated automatically. If the size set is zero, the both the allocated region in low and high memory will be released.

---
 kernel/ksysfs.c | 23 ++++++++++++++++++++++-
 1 file changed, 22 insertions(+), 1 deletion(-)
kernel/ksysfs.c +22 -1
index e83b264..4cc286d 100644
@@ -116,10 +116,31 @@ static ssize_t kexec_crash_size_store(struct kobject *kobj,
 {
 	unsigned long cnt;
 	int ret;
+	int size;
 
 	if (kstrtoul(buf, 0, &cnt))
 		return -EINVAL;
-
+#ifdef CONFIG_KEXEC_CMA
+#ifdef CONFIG_X86
+	size = cnt << 20;
+	if (cnt == 0) {
+		crash_free_memory_low();
+		ret = crash_free_memory(crash_get_memory_size());
+	} else if (cnt > 0) {
+		if (!crash_get_memory_size_low() && crash_alloc_memory_low())
+			return -ENOMEM;
+		ret = crash_free_memory(crash_get_memory_size());
+		if (ret)
+			return ret;
+		ret = crash_alloc_memory(size);
+		if (ret)
+			return ret;
+	} else {
+		return -EINVAL;
+	}
+	return count;
+#endif
+#endif
 	ret = crash_shrink_memory(cnt);
 	return ret < 0 ? ret : count;
 }
Signature · 7 lines
2.9.0.GIT

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw
In reply to Ronit Halder
Quoted 4 lines · Ronit Halder

Currenty linux kernel reserves memory at the boot time for crash kernel. It will be very useful if we can reserve memory in run time. The user can reserve the memory whenerver needed instead of reserving at the boot time.

Quoted 8 lines

It is possible to reserve memory for crash kernel at the run time using CMA (Contiguous Memory Allocator). CMA is capable of allocating big chunk of memory. At the boot time we will create one (if only low memory is used) or two (if we use both high memory in case of x86_64) CMA areas of size given in "crashkernel" boot time command line parameter. This memory in CMA areas can be used as movable pages (used for disk caches, process pages etc) if not allocated. Then the user can reserve or free memory from those CMA areas using "/sys/kernel/kexec_crash_size" sysfs entry. If the user

But the cma_alloc() is not a guaranteed allocation function, whereas memblock api will guarantee that crashkerenel memory is available.
More over, most of the system starts kdump service at boot time, so not sure if it could be useful enough. Lets see what other says....

Quoted 29 lines

usee high memory it will automatically at least 256MB low memory (needed for swiotlb and DMA buffers) when the user allocates memory using mentioned sysfs enrty. In case of high memory reservation the user controls the size of reserved region in high memory with "/sys/kernel/kexec_crash_size" entry. If the size set is zero then the memory allocated in low memory will automatically be freed.

As the pages under CMA area (when not allocated by CMA) can only be used by movable pages. The pages won't be used for DMA. So, after allocating pages from CMA area for loading the crash kernel, there won't be any chance of DMA on the memory.

Thus is a prototype patch. Please share your opinions on my approach. This patch is only for x86 and x86_64. Please note, this patch is only a prototype just to explain my approach and get the review. This patch is on kernel version v4.4.11.

CMA depends on page migration and only uses movable pages. But, the movable pages become unmovable momentarily for pinning. The CMA fails for this reason. I don't have any solution for that right now. This approach will work when the this problems with CMA will be fixed. The patch is enabled by a kernel configuration option CONFIG_KEXEC_CMA.

Ronit Halder (4):
Creating one or two CMA area at Boot time Functions for memory reservation and release Adding a new kernel configuration to enable the feature Enable memory allocation through sysfs interface

Quoted 6 lines
 arch/x86/kernel/setup.c | 44 ++++++++++++++++++++++++--
 include/linux/kexec.h   | 11 ++++++-
 kernel/kexec_core.c     | 83 +++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c         | 23 +++++++++++++-
 mm/Kconfig              |  6 ++++
 5 files changed, 162 insertions(+), 5 deletions(-)

~Pratyush

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw
Quoted 13 lines · Pratyush Anand
Quoted 4 lines · Ronit Halder

Currenty linux kernel reserves memory at the boot time for crash kernel. It will be very useful if we can reserve memory in run time. The user can reserve the memory whenerver needed instead of reserving at the boot time.

Quoted 8 lines

It is possible to reserve memory for crash kernel at the run time using CMA (Contiguous Memory Allocator). CMA is capable of allocating big chunk of memory. At the boot time we will create one (if only low memory is used) or two (if we use both high memory in case of x86_64) CMA areas of size given in "crashkernel" boot time command line parameter. This memory in CMA areas can be used as movable pages (used for disk caches, process pages etc) if not allocated. Then the user can reserve or free memory from those CMA areas using "/sys/kernel/kexec_crash_size" sysfs entry. If the user

Quoted 4 lines

But the cma_alloc() is not a guaranteed allocation function, whereas memblock api will guarantee that crashkerenel memory is available.
More over, most of the system starts kdump service at boot time, so not sure if it could be useful enough. Lets see what other says....

Maybe this is useful for debug purpose, after you shrunk the memory and realized you just made a mistake, you can use this function to expand it without reboot to modify the cmdline. Otherwise, I can't think of other use cases.

But it still relys on the "crashkernel" cmdline, and I think it would be more useful(at least for me) if you can throw away "crashkernel", and use the sysfs entry directly to reserve or expand the memory if possible. Because sometimes when I want to debug some kdump issue, I found the system I was using didn't specify the right (none or smaller)"crashkernel" cmdline, so I must reboot it.

Regards,
Xunlei

Quoted 36 lines
Quoted 30 lines

usee high memory it will automatically at least 256MB low memory (needed for swiotlb and DMA buffers) when the user allocates memory using mentioned sysfs enrty. In case of high memory reservation the user controls the size of reserved region in high memory with "/sys/kernel/kexec_crash_size" entry. If the size set is zero then the memory allocated in low memory will automatically be freed.

As the pages under CMA area (when not allocated by CMA) can only be used by movable pages. The pages won't be used for DMA. So, after allocating pages from CMA area for loading the crash kernel, there won't be any chance of DMA on the memory.

Thus is a prototype patch. Please share your opinions on my approach. This patch is only for x86 and x86_64. Please note, this patch is only a prototype just to explain my approach and get the review. This patch is on kernel version v4.4.11.

CMA depends on page migration and only uses movable pages. But, the movable pages become unmovable momentarily for pinning. The CMA fails for this reason. I don't have any solution for that right now. This approach will work when the this problems with CMA will be fixed. The patch is enabled by a kernel configuration option CONFIG_KEXEC_CMA.

Ronit Halder (4):
Creating one or two CMA area at Boot time Functions for memory reservation and release Adding a new kernel configuration to enable the feature Enable memory allocation through sysfs interface

Quoted 6 lines
 arch/x86/kernel/setup.c | 44 ++++++++++++++++++++++++--
 include/linux/kexec.h   | 11 ++++++-
 kernel/kexec_core.c     | 83 +++++++++++++++++++++++++++++++++++++++++++++++++
 kernel/ksysfs.c         | 23 +++++++++++++-
 mm/Kconfig              |  6 ++++
 5 files changed, 162 insertions(+), 5 deletions(-)
Quoted 6 lines

~Pratyush

_______________________________________________
kexec mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/kexec

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>