Skip to contentSkip to conversation

Put each per-cpu kdump ELF notes into a single page

PATCH In review Petr Tesarik 2 participants 7 messages Sep 5, 2014 – Sep 12, 2014

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

PATCH 1/1 Put each per-cpu kdump ELF notes into a single page

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space. However, fs/proc/vmcore.c expects all ELF notes to be contiguous. If the ELF note happens to occupy two non-adjacent physical pages, part of the note may be read from an incorrect memory location by the kdump kernel, resulting in failure to initialize /proc/vmcore (if the content of the following physical page, incorrectly interpreted as an ELF note specifies a large number), wrong register values or other apparent random memory corruption.

There is currently no mechanism to pass the virtual-to-physical mapping of the percpu allocation to the kdump kernel. So, instead, I'm changing the alignment of the ELF note buffer. Since sizeof(note_buf_t) is less than PAGE_SIZE, aligning the buffer to the nearest higher power of 2 is enough to make sure that the buffer cannot cross a page boundary, effectively ensuring that the whole buffer is contiguous in physical space.

---
 kernel/kexec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kexec.c +2 -1
index 2bee072..cdab59d 100644
@@ -1610,7 +1610,8 @@ void crash_save_cpu(struct pt_regs *regs, int cpu)
 static int __init crash_notes_memory_init(void)
 {
 	/* Allocate memory for saving cpu registers. */
-	crash_notes = alloc_percpu(note_buf_t);
+	crash_notes = __alloc_percpu(sizeof(note_buf_t),
+				     roundup_pow_of_two(sizeof(note_buf_t)));
 	if (!crash_notes) {
 		pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
 		return -ENOMEM;
Signature · 1 line
1.8.4.5
#1 lore raw

Hi all,

is anything wrong with the patch below? Are there questions? I thought this would be an easy one-line bugfix...

Regards,
Petr Tesarik

Quoted 36 lines · On Fri, 5 Sep 2014 18:33:14 +0200 Petr Tesarik <[email protected]> wrote:

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space. However, fs/proc/vmcore.c expects all ELF notes to be contiguous. If the ELF note happens to occupy two non-adjacent physical pages, part of the note may be read from an incorrect memory location by the kdump kernel, resulting in failure to initialize /proc/vmcore (if the content of the following physical page, incorrectly interpreted as an ELF note specifies a large number), wrong register values or other apparent random memory corruption.

There is currently no mechanism to pass the virtual-to-physical mapping of the percpu allocation to the kdump kernel. So, instead, I'm changing the alignment of the ELF note buffer. Since sizeof(note_buf_t) is less than PAGE_SIZE, aligning the buffer to the nearest higher power of 2 is enough to make sure that the buffer cannot cross a page boundary, effectively ensuring that the whole buffer is contiguous in physical space.

---
 kernel/kexec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kexec.c +2 -1
index 2bee072..cdab59d 100644
@@ -1610,7 +1610,8 @@ void crash_save_cpu(struct pt_regs *regs, int cpu)
 static int __init crash_notes_memory_init(void)
 {
 	/* Allocate memory for saving cpu registers. */
-	crash_notes = alloc_percpu(note_buf_t);
+	crash_notes = __alloc_percpu(sizeof(note_buf_t),
+				     roundup_pow_of_two(sizeof(note_buf_t)));
 	if (!crash_notes) {
 		pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
 		return -ENOMEM;
#2 lore raw
In reply to Petr Tesarik
Quoted 2 lines · Petr Tesarik

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space.

Petr,

Which are those arches?

Quoted 26 lines

However, fs/proc/vmcore.c expects
all ELF notes to be contiguous. If the ELF note happens to occupy two non-adjacent physical pages, part of the note may be read from an incorrect memory location by the kdump kernel, resulting in failure to initialize /proc/vmcore (if the content of the following physical page, incorrectly interpreted as an ELF note specifies a large number), wrong register values or other apparent random memory corruption.

There is currently no mechanism to pass the virtual-to-physical mapping of the percpu allocation to the kdump kernel. So, instead, I'm changing the alignment of the ELF note buffer. Since sizeof(note_buf_t) is less than PAGE_SIZE, aligning the buffer to the nearest higher power of 2 is enough to make sure that the buffer cannot cross a page boundary, effectively ensuring that the whole buffer is contiguous in physical space.

---
 kernel/kexec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/kexec.c +0 -0
index 2bee072..cdab59d 100644

kernel/kexec.c void crash_save_cpu(struct pt_regs *regs, int cpu) · line 1614

Quoted 6 lines
kernel/kexec.c +2 -1
@@ -1610,7 +1610,8 @@ void crash_save_cpu(struct pt_regs *regs, int cpu)
 static int __init crash_notes_memory_init(void)
 {
 	/* Allocate memory for saving cpu registers. */
-	crash_notes = alloc_percpu(note_buf_t);
+	crash_notes = __alloc_percpu(sizeof(note_buf_t),
+				     roundup_pow_of_two(sizeof(note_buf_t)));

I think some of the changelog should show up here as comment in short form. I don't think it is obvious that why we are using __alloc_percpu() and why aligning to nearst higher power of 2 is needed here. Please also mention here which arches run into issues.

Thanks
Vivek

Quoted 10 lines
kernel/kexec.c +0 -0
 	if (!crash_notes) {
 		pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
 		return -ENOMEM;
Signature · 6 lines
1.8.4.5

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

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

#3 lore raw
Quoted 7 lines · On Thu, 11 Sep 2014 16:01:10 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 2 lines · Petr Tesarik

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space.

Petr,

Which are those arches?

All except nommu. Actually, percpu-km will be used instead even on MMU if SMP is disabled, but since SMP is pretty standard now, I guess the vast majority of all kernels out there is affected. ;-)

Quoted 21 lines
Quoted 21 lines

However, fs/proc/vmcore.c expects
all ELF notes to be contiguous. If the ELF note happens to occupy two non-adjacent physical pages, part of the note may be read from an incorrect memory location by the kdump kernel, resulting in failure to initialize /proc/vmcore (if the content of the following physical page, incorrectly interpreted as an ELF note specifies a large number), wrong register values or other apparent random memory corruption.

There is currently no mechanism to pass the virtual-to-physical mapping of the percpu allocation to the kdump kernel. So, instead, I'm changing the alignment of the ELF note buffer. Since sizeof(note_buf_t) is less than PAGE_SIZE, aligning the buffer to the nearest higher power of 2 is enough to make sure that the buffer cannot cross a page boundary, effectively ensuring that the whole buffer is contiguous in physical space.

---
 kernel/kexec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines

kernel/kexec.c void crash_save_cpu(struct pt_regs *regs, int cpu) · line 1614

Quoted 11 lines
kernel/kexec.c +2 -1
index 2bee072..cdab59d 100644
@@ -1610,7 +1610,8 @@ void crash_save_cpu(struct pt_regs *regs, int cpu)
 static int __init crash_notes_memory_init(void)
 {
 	/* Allocate memory for saving cpu registers. */
-	crash_notes = alloc_percpu(note_buf_t);
+	crash_notes = __alloc_percpu(sizeof(note_buf_t),
+				     roundup_pow_of_two(sizeof(note_buf_t)));

I think some of the changelog should show up here as comment in short form. I don't think it is obvious that why we are using __alloc_percpu() and why aligning to nearst higher power of 2 is needed here. Please also mention here which arches run into issues.

OK, I'll add it as a comment in the code. I'll see if I can make it short but still understandable.

Thanks,
Petr Tesarik

Quoted 18 lines

Thanks
Vivek

Quoted 10 lines
kernel/kexec.c +0 -0
 	if (!crash_notes) {
 		pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
 		return -ENOMEM;
Signature · 6 lines
1.8.4.5

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

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

#4 lore raw
Quoted 7 lines · Petr Tesarik
Quoted 4 lines · On Thu, 11 Sep 2014 16:01:10 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 2 lines · Petr Tesarik

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space.

Quoted 7 lines
Quoted 3 lines

Petr,

Which are those arches?

All except nommu. Actually, percpu-km will be used instead even on MMU if SMP is disabled, but since SMP is pretty standard now, I guess the vast majority of all kernels out there is affected. ;-)

Hi Petr,

To make sure I understand it correctly I will just summarize what you said.

alloc_percpu() code does not guarantee that an object will be on physically contiguous pages if object crosses page boundary. That's why we are forcing allocation of object aligned to nearest higher power of two boundary of object size and that way object will always be on same page (as long as object is not bigger than a page).

Is that a fair summary?

Thanks
Vivek

Quoted 39 lines
Quoted 22 lines
Quoted 22 lines

However, fs/proc/vmcore.c expects
all ELF notes to be contiguous. If the ELF note happens to occupy two non-adjacent physical pages, part of the note may be read from an incorrect memory location by the kdump kernel, resulting in failure to initialize /proc/vmcore (if the content of the following physical page, incorrectly interpreted as an ELF note specifies a large number), wrong register values or other apparent random memory corruption.

There is currently no mechanism to pass the virtual-to-physical mapping of the percpu allocation to the kdump kernel. So, instead, I'm changing the alignment of the ELF note buffer. Since sizeof(note_buf_t) is less than PAGE_SIZE, aligning the buffer to the nearest higher power of 2 is enough to make sure that the buffer cannot cross a page boundary, effectively ensuring that the whole buffer is contiguous in physical space.

---
 kernel/kexec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines

kernel/kexec.c void crash_save_cpu(struct pt_regs *regs, int cpu) · line 1614

Quoted 11 lines
kernel/kexec.c +2 -1
index 2bee072..cdab59d 100644
@@ -1610,7 +1610,8 @@ void crash_save_cpu(struct pt_regs *regs, int cpu)
 static int __init crash_notes_memory_init(void)
 {
 	/* Allocate memory for saving cpu registers. */
-	crash_notes = alloc_percpu(note_buf_t);
+	crash_notes = __alloc_percpu(sizeof(note_buf_t),
+				     roundup_pow_of_two(sizeof(note_buf_t)));

I think some of the changelog should show up here as comment in short form. I don't think it is obvious that why we are using __alloc_percpu() and why aligning to nearst higher power of 2 is needed here. Please also mention here which arches run into issues.

Quoted 24 lines

OK, I'll add it as a comment in the code. I'll see if I can make it short but still understandable.

Thanks,
Petr Tesarik

Quoted 18 lines

Thanks
Vivek

Quoted 10 lines
kernel/kexec.c +0 -0
 	if (!crash_notes) {
 		pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
 		return -ENOMEM;
Signature · 6 lines
1.8.4.5

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

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

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

#5 lore raw
Quoted 21 lines · On Thu, 11 Sep 2014 17:16:37 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 7 lines · Petr Tesarik
Quoted 4 lines · On Thu, 11 Sep 2014 16:01:10 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 2 lines · Petr Tesarik

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space.

Quoted 7 lines
Quoted 3 lines

Petr,

Which are those arches?

All except nommu. Actually, percpu-km will be used instead even on MMU if SMP is disabled, but since SMP is pretty standard now, I guess the vast majority of all kernels out there is affected. ;-)

Hi Petr,

To make sure I understand it correctly I will just summarize what you said.

Quoted 7 lines

alloc_percpu() code does not guarantee that an object will be on physically contiguous pages if object crosses page boundary. That's why we are forcing allocation of object aligned to nearest higher power of two boundary of object size and that way object will always be on same page (as long as object is not bigger than a page).

Is that a fair summary?

Yes. I might add a note why physically contiguous memory is needed here, but maybe it's obvious to anyone dealing with kdump.

Thanks,
Petr Tesarik

Quoted 71 lines

Thanks
Vivek

Quoted 39 lines
Quoted 22 lines
Quoted 22 lines

However, fs/proc/vmcore.c expects
all ELF notes to be contiguous. If the ELF note happens to occupy two non-adjacent physical pages, part of the note may be read from an incorrect memory location by the kdump kernel, resulting in failure to initialize /proc/vmcore (if the content of the following physical page, incorrectly interpreted as an ELF note specifies a large number), wrong register values or other apparent random memory corruption.

There is currently no mechanism to pass the virtual-to-physical mapping of the percpu allocation to the kdump kernel. So, instead, I'm changing the alignment of the ELF note buffer. Since sizeof(note_buf_t) is less than PAGE_SIZE, aligning the buffer to the nearest higher power of 2 is enough to make sure that the buffer cannot cross a page boundary, effectively ensuring that the whole buffer is contiguous in physical space.

---
 kernel/kexec.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Quoted 16 lines

kernel/kexec.c void crash_save_cpu(struct pt_regs *regs, int cpu) · line 1614

Quoted 11 lines
kernel/kexec.c +2 -1
index 2bee072..cdab59d 100644
@@ -1610,7 +1610,8 @@ void crash_save_cpu(struct pt_regs *regs, int cpu)
 static int __init crash_notes_memory_init(void)
 {
 	/* Allocate memory for saving cpu registers. */
-	crash_notes = alloc_percpu(note_buf_t);
+	crash_notes = __alloc_percpu(sizeof(note_buf_t),
+				     roundup_pow_of_two(sizeof(note_buf_t)));

I think some of the changelog should show up here as comment in short form. I don't think it is obvious that why we are using __alloc_percpu() and why aligning to nearst higher power of 2 is needed here. Please also mention here which arches run into issues.

Quoted 24 lines

OK, I'll add it as a comment in the code. I'll see if I can make it short but still understandable.

Thanks,
Petr Tesarik

Quoted 18 lines

Thanks
Vivek

Quoted 10 lines
kernel/kexec.c +0 -0
 	if (!crash_notes) {
 		pr_warn("Kexec: Memory allocation for saving cpu register states failed\n");
 		return -ENOMEM;
Signature · 6 lines
1.8.4.5

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

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

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

#6 lore raw
Quoted 30 lines · Petr Tesarik
Quoted 27 lines · On Thu, 11 Sep 2014 17:16:37 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 7 lines · Petr Tesarik
Quoted 4 lines · On Thu, 11 Sep 2014 16:01:10 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 2 lines · Petr Tesarik

On architectures that use percpu-vm, the percpu region is not guaranteed to be contiguous in physical space.

Quoted 7 lines
Quoted 3 lines

Petr,

Which are those arches?

All except nommu. Actually, percpu-km will be used instead even on MMU if SMP is disabled, but since SMP is pretty standard now, I guess the vast majority of all kernels out there is affected. ;-)

Hi Petr,

To make sure I understand it correctly I will just summarize what you said.

alloc_percpu() code does not guarantee that an object will be on physically contiguous pages if object crosses page boundary. That's why we are forcing allocation of object aligned to nearest higher power of two boundary of object size and that way object will always be on same page (as long as object is not bigger than a page).

Quoted 4 lines
Quoted 1 line

Is that a fair summary?

Yes. I might add a note why physically contiguous memory is needed here, but maybe it's obvious to anyone dealing with kdump.

I think adding couple of lines to explain why physically contiguous notes are needed is a good idea. It will not be ovious to anybody new to kdump.

Thanks
Vivek

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