Skip to contentSkip to conversation

kexec: Introduce "/sys/kernel/kexec_crash_low_size"

PATCH In review Xunlei Pang 4 participants 8 messages Aug 17, 2016 – Aug 24, 2016

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

PATCH v2 1/2 kexec: Introduce "/sys/kernel/kexec_crash_low_size"

"/sys/kernel/kexec_crash_size" only handles crashk_res, it is fine in most cases, but sometimes we have crashk_low_res. For example, when "crashkernel=size[KMG],high" combined with "crashkernel=size[KMG],low" is used for 64-bit x86.

Like crashk_res, we introduce the corresponding sysfs file "/sys/kernel/kexec_crash_low_size" for crashk_low_res.

So, the exact total reserved memory is the sum of the two.

crashk_low_res can also be shrunk via this new interface, and users should be aware of what they are doing.

---
 include/linux/kexec.h |  4 ++--
 kernel/kexec_core.c   | 23 ++++++++++++-----------
 kernel/ksysfs.c       | 25 +++++++++++++++++++++++--
 3 files changed, 37 insertions(+), 15 deletions(-)
include/linux/kexec.h +2 -2
index d743777..4f271fc 100644
@@ -304,8 +304,8 @@ int parse_crashkernel_high(char *cmdline, unsigned long long system_ram,
 		unsigned long long *crash_size, unsigned long long *crash_base);
 int parse_crashkernel_low(char *cmdline, unsigned long long system_ram,
 		unsigned long long *crash_size, unsigned long long *crash_base);
-int crash_shrink_memory(unsigned long new_size);
-size_t crash_get_memory_size(void);
+int crash_shrink_memory(struct resource *res, unsigned long new_size);
+size_t crash_get_memory_size(struct resource *res);
 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
 
 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
kernel/kexec_core.c +12 -11
index 5616755..707d18e 100644
@@ -925,13 +925,13 @@ void crash_kexec(struct pt_regs *regs)
 	}
 }
 
-size_t crash_get_memory_size(void)
+size_t crash_get_memory_size(struct resource *res)
 {
 	size_t size = 0;
 
 	mutex_lock(&kexec_mutex);
-	if (crashk_res.end != crashk_res.start)
-		size = resource_size(&crashk_res);
+	if (res->end != res->start)
+		size = resource_size(res);
 	mutex_unlock(&kexec_mutex);
 	return size;
 }
@@ -945,7 +945,7 @@ void __weak crash_free_reserved_phys_range(unsigned long begin,
 		free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
 }
 
-int crash_shrink_memory(unsigned long new_size)
+int crash_shrink_memory(struct resource *res, unsigned long new_size)
 {
 	int ret = 0;
 	unsigned long start, end;
@@ -958,8 +958,9 @@ int crash_shrink_memory(unsigned long new_size)
 		ret = -ENOENT;
 		goto unlock;
 	}
-	start = crashk_res.start;
-	end = crashk_res.end;
+
+	start = res->start;
+	end = res->end;
 	old_size = (end == 0) ? 0 : end - start + 1;
 	if (new_size >= old_size) {
 		ret = (new_size == old_size) ? 0 : -EINVAL;
@@ -975,17 +976,17 @@ int crash_shrink_memory(unsigned long new_size)
 	start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
 	end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
 
-	crash_free_reserved_phys_range(end, crashk_res.end);
+	crash_free_reserved_phys_range(end, res->end);
 
-	if ((start == end) && (crashk_res.parent != NULL))
-		release_resource(&crashk_res);
+	if ((start == end) && (res->parent != NULL))
+		release_resource(res);
 
 	ram_res->start = end;
-	ram_res->end = crashk_res.end;
+	ram_res->end = res->end;
 	ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
 	ram_res->name = "System RAM";
 
-	crashk_res.end = end - 1;
+	res->end = end - 1;
 
 	insert_resource(&iomem_resource, ram_res);
 
kernel/ksysfs.c +23 -2
index ee1bc1b..3336fd5 100644
@@ -105,10 +105,30 @@ static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
 }
 KERNEL_ATTR_RO(kexec_crash_loaded);
 
+static ssize_t kexec_crash_low_size_show(struct kobject *kobj,
+				       struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%zu\n", crash_get_memory_size(&crashk_low_res));
+}
+static ssize_t kexec_crash_low_size_store(struct kobject *kobj,
+				   struct kobj_attribute *attr,
+				   const char *buf, size_t count)
+{
+	unsigned long cnt;
+	int ret;
+
+	if (kstrtoul(buf, 0, &cnt))
+		return -EINVAL;
+
+	ret = crash_shrink_memory(&crashk_low_res, cnt);
+	return ret < 0 ? ret : count;
+}
+KERNEL_ATTR_RW(kexec_crash_low_size);
+
 static ssize_t kexec_crash_size_show(struct kobject *kobj,
 				       struct kobj_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%zu\n", crash_get_memory_size());
+	return sprintf(buf, "%zu\n", crash_get_memory_size(&crashk_res));
 }
 static ssize_t kexec_crash_size_store(struct kobject *kobj,
 				   struct kobj_attribute *attr,
@@ -120,7 +140,7 @@ static ssize_t kexec_crash_size_store(struct kobject *kobj,
 	if (kstrtoul(buf, 0, &cnt))
 		return -EINVAL;
 
-	ret = crash_shrink_memory(cnt);
+	ret = crash_shrink_memory(&crashk_res, cnt);
 	return ret < 0 ? ret : count;
 }
 KERNEL_ATTR_RW(kexec_crash_size);
@@ -218,6 +238,7 @@ static struct attribute * kernel_attrs[] = {
 #ifdef CONFIG_KEXEC_CORE
 	&kexec_loaded_attr.attr,
 	&kexec_crash_loaded_attr.attr,
+	&kexec_crash_low_size_attr.attr,
 	&kexec_crash_size_attr.attr,
 	&vmcoreinfo_attr.attr,
 #endif
Signature · 1 line
1.8.3.1
#1 lore raw

PATCH v2 2/2 kexec: Consider crashk_low_res in sanity_check_segment_list()

We have crashk_res only in most cases, but sometimes we have crashk_low_res.

For example, on 64-bit x86 systems, when "crashkernel=32M,high" combined with "crashkernel=128M,low" is used, so some segments may have the chance to be loaded into crashk_low_res area. We can't fail it as a memory violation in these cases.

Thus, we add the case to regard the segment as valid if it is within crashk_low_res.

---
 kernel/kexec_core.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/kexec_core.c +8 -3
index 707d18e..9012a60 100644
@@ -248,9 +248,14 @@ int sanity_check_segment_list(struct kimage *image)
 			mstart = image->segment[i].mem;
 			mend = mstart + image->segment[i].memsz - 1;
 			/* Ensure we are within the crash kernel limits */
-			if ((mstart < phys_to_boot_phys(crashk_res.start)) ||
-			    (mend > phys_to_boot_phys(crashk_res.end)))
-				return -EADDRNOTAVAIL;
+			if ((mstart >= phys_to_boot_phys(crashk_res.start)) &&
+			    (mend <= phys_to_boot_phys(crashk_res.end)))
+				continue;
+			if ((mstart >= phys_to_boot_phys(crashk_low_res.start)) &&
+			    (mend <= phys_to_boot_phys(crashk_low_res.end)))
+				continue;
+
+			return -EADDRNOTAVAIL;
 		}
 	}
 
Signature · 1 line
1.8.3.1
#2 lore raw

Re: [PATCH v2 2/2] kexec: Consider crashk_low_res in sanity_check_segment_list()

Hi, Xunlei,

Quoted 10 lines · Xunlei Pang

We have crashk_res only in most cases, but sometimes we have crashk_low_res.

For example, on 64-bit x86 systems, when "crashkernel=32M,high" combined with "crashkernel=128M,low" is used, so some segments may have the chance to be loaded into crashk_low_res area. We can't fail it as a memory violation in these cases.

Thus, we add the case to regard the segment as valid if it is within crashk_low_res.

crashkernel low is meant for swiotlb, it can be reserved automaticlly in case there's only crashkernel high specified in cmdline, I'm not sure it is useful to use crashk_res_low for other purpose and likely kdump can fail in the case.

I'm not sure it is really necessary to add this check now, we may handle it only when there is an actual use case and bug report in the future.

Thanks
Dave

Quoted 31 lines
---
 kernel/kexec_core.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/kexec_core.c +8 -3
index 707d18e..9012a60 100644
@@ -248,9 +248,14 @@ int sanity_check_segment_list(struct kimage *image)
 			mstart = image->segment[i].mem;
 			mend = mstart + image->segment[i].memsz - 1;
 			/* Ensure we are within the crash kernel limits */
-			if ((mstart < phys_to_boot_phys(crashk_res.start)) ||
-			    (mend > phys_to_boot_phys(crashk_res.end)))
-				return -EADDRNOTAVAIL;
+			if ((mstart >= phys_to_boot_phys(crashk_res.start)) &&
+			    (mend <= phys_to_boot_phys(crashk_res.end)))
+				continue;
+			if ((mstart >= phys_to_boot_phys(crashk_low_res.start)) &&
+			    (mend <= phys_to_boot_phys(crashk_low_res.end)))
+				continue;
+
+			return -EADDRNOTAVAIL;
 		}
 	}
 
Signature · 1 line
1.8.3.1
#3 lore raw

Re: [PATCH v2 2/2] kexec: Consider crashk_low_res in sanity_check_segment_list()

Quoted 13 lines · Dave Young

Hi, Xunlei,

Quoted 10 lines · Xunlei Pang

We have crashk_res only in most cases, but sometimes we have crashk_low_res.

For example, on 64-bit x86 systems, when "crashkernel=32M,high" combined with "crashkernel=128M,low" is used, so some segments may have the chance to be loaded into crashk_low_res area. We can't fail it as a memory violation in these cases.

Thus, we add the case to regard the segment as valid if it is within crashk_low_res.

Quoted 8 lines

crashkernel low is meant for swiotlb, it can be reserved automaticlly in case there's only crashkernel high specified in cmdline, I'm not sure it is useful to use crashk_res_low for other purpose and likely kdump can fail in the case.

I'm not sure it is really necessary to add this check now, we may handle it only when there is an actual use case and bug report in the future.

Thanks for the review.
The reason I added this is that crashk_res is allowed to be shrunk, so the segment will surely fall into crashk_low_res if crashk_res was shrunk to be a small range.

But yes, this should be a corner case, but seems it does no harm adding this check. Anyway, if you think it's not necessary, let's simply ignore it :-)

Regards,
Xunlei

Quoted 37 lines

Thanks
Dave

Quoted 30 lines
---
 kernel/kexec_core.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)
kernel/kexec_core.c +8 -3
index 707d18e..9012a60 100644
@@ -248,9 +248,14 @@ int sanity_check_segment_list(struct kimage *image)
 			mstart = image->segment[i].mem;
 			mend = mstart + image->segment[i].memsz - 1;
 			/* Ensure we are within the crash kernel limits */
-			if ((mstart < phys_to_boot_phys(crashk_res.start)) ||
-			    (mend > phys_to_boot_phys(crashk_res.end)))
-				return -EADDRNOTAVAIL;
+			if ((mstart >= phys_to_boot_phys(crashk_res.start)) &&
+			    (mend <= phys_to_boot_phys(crashk_res.end)))
+				continue;
+			if ((mstart >= phys_to_boot_phys(crashk_low_res.start)) &&
+			    (mend <= phys_to_boot_phys(crashk_low_res.end)))
+				continue;
+
+			return -EADDRNOTAVAIL;
 		}
 	}
 
Signature · 1 line
1.8.3.1

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

#4 lore raw
In reply to Xunlei Pang
Quoted 5 lines · Xunlei Pang

"/sys/kernel/kexec_crash_size" only handles crashk_res, it is fine in most cases, but sometimes we have crashk_low_res. For example, when "crashkernel=size[KMG],high" combined with "crashkernel=size[KMG],low" is used for 64-bit x86.

Quoted 7 lines

Like crashk_res, we introduce the corresponding sysfs file "/sys/kernel/kexec_crash_low_size" for crashk_low_res.

So, the exact total reserved memory is the sum of the two.

crashk_low_res can also be shrunk via this new interface, and users should be aware of what they are doing.

Cc Yinghai Lu for review since he introduced the ,high and ,low logic.

Quoted 145 lines
---
 include/linux/kexec.h |  4 ++--
 kernel/kexec_core.c   | 23 ++++++++++++-----------
 kernel/ksysfs.c       | 25 +++++++++++++++++++++++--
 3 files changed, 37 insertions(+), 15 deletions(-)
include/linux/kexec.h +2 -2
index d743777..4f271fc 100644
@@ -304,8 +304,8 @@ int parse_crashkernel_high(char *cmdline, unsigned long long system_ram,
 		unsigned long long *crash_size, unsigned long long *crash_base);
 int parse_crashkernel_low(char *cmdline, unsigned long long system_ram,
 		unsigned long long *crash_size, unsigned long long *crash_base);
-int crash_shrink_memory(unsigned long new_size);
-size_t crash_get_memory_size(void);
+int crash_shrink_memory(struct resource *res, unsigned long new_size);
+size_t crash_get_memory_size(struct resource *res);
 void crash_free_reserved_phys_range(unsigned long begin, unsigned long end);
 
 int __weak arch_kexec_kernel_image_probe(struct kimage *image, void *buf,
kernel/kexec_core.c +12 -11
index 5616755..707d18e 100644
@@ -925,13 +925,13 @@ void crash_kexec(struct pt_regs *regs)
 	}
 }
 
-size_t crash_get_memory_size(void)
+size_t crash_get_memory_size(struct resource *res)
 {
 	size_t size = 0;
 
 	mutex_lock(&kexec_mutex);
-	if (crashk_res.end != crashk_res.start)
-		size = resource_size(&crashk_res);
+	if (res->end != res->start)
+		size = resource_size(res);
 	mutex_unlock(&kexec_mutex);
 	return size;
 }
@@ -945,7 +945,7 @@ void __weak crash_free_reserved_phys_range(unsigned long begin,
 		free_reserved_page(boot_pfn_to_page(addr >> PAGE_SHIFT));
 }
 
-int crash_shrink_memory(unsigned long new_size)
+int crash_shrink_memory(struct resource *res, unsigned long new_size)
 {
 	int ret = 0;
 	unsigned long start, end;
@@ -958,8 +958,9 @@ int crash_shrink_memory(unsigned long new_size)
 		ret = -ENOENT;
 		goto unlock;
 	}
-	start = crashk_res.start;
-	end = crashk_res.end;
+
+	start = res->start;
+	end = res->end;
 	old_size = (end == 0) ? 0 : end - start + 1;
 	if (new_size >= old_size) {
 		ret = (new_size == old_size) ? 0 : -EINVAL;
@@ -975,17 +976,17 @@ int crash_shrink_memory(unsigned long new_size)
 	start = roundup(start, KEXEC_CRASH_MEM_ALIGN);
 	end = roundup(start + new_size, KEXEC_CRASH_MEM_ALIGN);
 
-	crash_free_reserved_phys_range(end, crashk_res.end);
+	crash_free_reserved_phys_range(end, res->end);
 
-	if ((start == end) && (crashk_res.parent != NULL))
-		release_resource(&crashk_res);
+	if ((start == end) && (res->parent != NULL))
+		release_resource(res);
 
 	ram_res->start = end;
-	ram_res->end = crashk_res.end;
+	ram_res->end = res->end;
 	ram_res->flags = IORESOURCE_BUSY | IORESOURCE_SYSTEM_RAM;
 	ram_res->name = "System RAM";
 
-	crashk_res.end = end - 1;
+	res->end = end - 1;
 
 	insert_resource(&iomem_resource, ram_res);
 
kernel/ksysfs.c +23 -2
index ee1bc1b..3336fd5 100644
@@ -105,10 +105,30 @@ static ssize_t kexec_crash_loaded_show(struct kobject *kobj,
 }
 KERNEL_ATTR_RO(kexec_crash_loaded);
 
+static ssize_t kexec_crash_low_size_show(struct kobject *kobj,
+				       struct kobj_attribute *attr, char *buf)
+{
+	return sprintf(buf, "%zu\n", crash_get_memory_size(&crashk_low_res));
+}
+static ssize_t kexec_crash_low_size_store(struct kobject *kobj,
+				   struct kobj_attribute *attr,
+				   const char *buf, size_t count)
+{
+	unsigned long cnt;
+	int ret;
+
+	if (kstrtoul(buf, 0, &cnt))
+		return -EINVAL;
+
+	ret = crash_shrink_memory(&crashk_low_res, cnt);
+	return ret < 0 ? ret : count;
+}
+KERNEL_ATTR_RW(kexec_crash_low_size);
+
 static ssize_t kexec_crash_size_show(struct kobject *kobj,
 				       struct kobj_attribute *attr, char *buf)
 {
-	return sprintf(buf, "%zu\n", crash_get_memory_size());
+	return sprintf(buf, "%zu\n", crash_get_memory_size(&crashk_res));
 }
 static ssize_t kexec_crash_size_store(struct kobject *kobj,
 				   struct kobj_attribute *attr,
@@ -120,7 +140,7 @@ static ssize_t kexec_crash_size_store(struct kobject *kobj,
 	if (kstrtoul(buf, 0, &cnt))
 		return -EINVAL;
 
-	ret = crash_shrink_memory(cnt);
+	ret = crash_shrink_memory(&crashk_res, cnt);
 	return ret < 0 ? ret : count;
 }
 KERNEL_ATTR_RW(kexec_crash_size);
@@ -218,6 +238,7 @@ static struct attribute * kernel_attrs[] = {
 #ifdef CONFIG_KEXEC_CORE
 	&kexec_loaded_attr.attr,
 	&kexec_crash_loaded_attr.attr,
+	&kexec_crash_low_size_attr.attr,
 	&kexec_crash_size_attr.attr,
 	&vmcoreinfo_attr.attr,
 #endif
Signature · 1 line
1.8.3.1
#5 lore raw
Quoted 13 lines · Dave Young
Quoted 12 lines · Xunlei Pang

"/sys/kernel/kexec_crash_size" only handles crashk_res, it is fine in most cases, but sometimes we have crashk_low_res. For example, when "crashkernel=size[KMG],high" combined with "crashkernel=size[KMG],low" is used for 64-bit x86.

Like crashk_res, we introduce the corresponding sysfs file "/sys/kernel/kexec_crash_low_size" for crashk_low_res.

So, the exact total reserved memory is the sum of the two.

crashk_low_res can also be shrunk via this new interface, and users should be aware of what they are doing.

...

Quoted 8 lines

kernel/ksysfs.c static struct attribute * kernel_attrs[] = { · line 244

Quoted 8 lines
kernel/ksysfs.c +1 -0
@@ -218,6 +238,7 @@ static struct attribute * kernel_attrs[] = {
 #ifdef CONFIG_KEXEC_CORE
      &kexec_loaded_attr.attr,
      &kexec_crash_loaded_attr.attr,
+     &kexec_crash_low_size_attr.attr,
      &kexec_crash_size_attr.attr,
      &vmcoreinfo_attr.attr,
 #endif

would be better if you can use attribute_group .is_visible to control showing of crash_low_size only when the crash_base is above 4G.

Thanks

Yinghai

#6 lore raw
Quoted 15 lines · Yinghai Lu
Quoted 13 lines · Dave Young
Quoted 12 lines · Xunlei Pang

"/sys/kernel/kexec_crash_size" only handles crashk_res, it is fine in most cases, but sometimes we have crashk_low_res. For example, when "crashkernel=size[KMG],high" combined with "crashkernel=size[KMG],low" is used for 64-bit x86.

Like crashk_res, we introduce the corresponding sysfs file "/sys/kernel/kexec_crash_low_size" for crashk_low_res.

So, the exact total reserved memory is the sum of the two.

crashk_low_res can also be shrunk via this new interface, and users should be aware of what they are doing.

...

Quoted 11 lines
Quoted 8 lines

kernel/ksysfs.c static struct attribute * kernel_attrs[] = { · line 244

Quoted 8 lines
kernel/ksysfs.c +1 -0
@@ -218,6 +238,7 @@ static struct attribute * kernel_attrs[] = {
 #ifdef CONFIG_KEXEC_CORE
      &kexec_loaded_attr.attr,
      &kexec_crash_loaded_attr.attr,
+     &kexec_crash_low_size_attr.attr,
      &kexec_crash_size_attr.attr,
      &vmcoreinfo_attr.attr,
 #endif

would be better if you can use attribute_group .is_visible to control showing of crash_low_size only when the crash_base is above 4G.

I have same feeling that it looks odd to show low in sysfs in case no crashkernel=,high being used. Even if crashkernel=,high is used only in x86 the resource crashk_low is in common code. What do you think to move it to x86?

Thanks
Dave

Quoted 9 lines

Thanks

Yinghai

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

#7 lore raw
Quoted 26 lines · Dave Young
Quoted 15 lines · Yinghai Lu
Quoted 13 lines · Dave Young
Quoted 12 lines · Xunlei Pang

"/sys/kernel/kexec_crash_size" only handles crashk_res, it is fine in most cases, but sometimes we have crashk_low_res. For example, when "crashkernel=size[KMG],high" combined with "crashkernel=size[KMG],low" is used for 64-bit x86.

Like crashk_res, we introduce the corresponding sysfs file "/sys/kernel/kexec_crash_low_size" for crashk_low_res.

So, the exact total reserved memory is the sum of the two.

crashk_low_res can also be shrunk via this new interface, and users should be aware of what they are doing.

...

Quoted 10 lines
Quoted 8 lines

kernel/ksysfs.c static struct attribute * kernel_attrs[] = { · line 244

Quoted 8 lines
kernel/ksysfs.c +1 -0
@@ -218,6 +238,7 @@ static struct attribute * kernel_attrs[] = {
 #ifdef CONFIG_KEXEC_CORE
      &kexec_loaded_attr.attr,
      &kexec_crash_loaded_attr.attr,
+     &kexec_crash_low_size_attr.attr,
      &kexec_crash_size_attr.attr,
      &vmcoreinfo_attr.attr,
 #endif

would be better if you can use attribute_group .is_visible to control showing of crash_low_size only when the crash_base is above 4G.

Quoted 4 lines

I have same feeling that it looks odd to show low in sysfs in case no crashkernel=,high being used. Even if crashkernel=,high is used only in x86 the resource crashk_low is in common code. What do you think to move it to x86?

If want to put some restriction on it, I'd prefer to move crashk_low to arch x86, to make it x86-specific.

We can show the interface unconditionally. If it isn't used, its size is 0, it doesn't matter.

Regards,
Xunlei

Quoted 12 lines

Thanks
Dave

Quoted 8 lines

Thanks

Yinghai

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

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