Skip to contentSkip to conversation

x86/kexec: fix a kexec_file_load failure

PATCH In review Dave Young 3 participants 8 messages Dec 28, 2018 – Jan 15, 2019

Subsystems (3)
lore
Share & tools
lore

Dave Young

author
#0 lore raw

PATCH v2 1/1 x86/kexec: fix a kexec_file_load failure

The code cleanup mentioned in Fixes tag changed the behavior of kexec_locate_mem_hole. The kexec_locate_mem_hole will try to allocate free memory only when kbuf.mem is initialized as zero.

But in x86 kexec_file_load implementation there are a few places the kbuf.mem is reused like below:
/* kbuf initialized, kbuf.mem = 0 */
...
kexec_add_buffer()
...
kexec_add_buffer()

The second kexec_add_buffer will reuse previous kbuf but not reinitialize the kbuf.mem.

Thus kexec_file_load failed because the sanity check failed.

So explictily reset kbuf.mem to fix the issue.

Fixes b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")

---
V1 -> V2: use KEXEC_BUF_MEM_UNKNOWN in code.

 arch/x86/kernel/crash.c           | 1 +
 arch/x86/kernel/kexec-bzimage64.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/kernel/crash.c +1 -0
index f631a3f15587..6b7890c7889b 100644
@@ -469,6 +469,7 @@ int crash_load_segments(struct kimage *image)
 
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
arch/x86/kernel/kexec-bzimage64.c +2 -0
index 278cd07228dd..0d5efa34f359 100644
@@ -434,6 +434,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	kbuf.memsz = PAGE_ALIGN(header->init_size);
 	kbuf.buf_align = header->kernel_alignment;
 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out_free_params;
@@ -448,6 +449,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz = kbuf.memsz = initrd_len;
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
+		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			goto out_free_params;
Signature · 1 line
2.17.0
author
#1 lore raw
Quoted 63 lines · Dave Young

The code cleanup mentioned in Fixes tag changed the behavior of kexec_locate_mem_hole. The kexec_locate_mem_hole will try to allocate free memory only when kbuf.mem is initialized as zero.

But in x86 kexec_file_load implementation there are a few places the kbuf.mem is reused like below:
/* kbuf initialized, kbuf.mem = 0 */
...
kexec_add_buffer()
...
kexec_add_buffer()

The second kexec_add_buffer will reuse previous kbuf but not reinitialize the kbuf.mem.

Thus kexec_file_load failed because the sanity check failed.

So explictily reset kbuf.mem to fix the issue.

Fixes b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")

---
V1 -> V2: use KEXEC_BUF_MEM_UNKNOWN in code.

 arch/x86/kernel/crash.c           | 1 +
 arch/x86/kernel/kexec-bzimage64.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/kernel/crash.c +1 -0
index f631a3f15587..6b7890c7889b 100644
@@ -469,6 +469,7 @@ int crash_load_segments(struct kimage *image)
 
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
arch/x86/kernel/kexec-bzimage64.c +2 -0
index 278cd07228dd..0d5efa34f359 100644
@@ -434,6 +434,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	kbuf.memsz = PAGE_ALIGN(header->init_size);
 	kbuf.buf_align = header->kernel_alignment;
 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out_free_params;
@@ -448,6 +449,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz = kbuf.memsz = initrd_len;
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
+		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			goto out_free_params;
Signature · 1 line
2.17.0

Ping, this is a regression issue, can we get this fixed?

Thanks
Dave

#2 lore raw
In reply to Dave Young
Quoted 33 lines · Dave Young

The code cleanup mentioned in Fixes tag changed the behavior of kexec_locate_mem_hole. The kexec_locate_mem_hole will try to allocate free memory only when kbuf.mem is initialized as zero.

But in x86 kexec_file_load implementation there are a few places the kbuf.mem is reused like below:
/* kbuf initialized, kbuf.mem = 0 */
...
kexec_add_buffer()
...
kexec_add_buffer()

The second kexec_add_buffer will reuse previous kbuf but not reinitialize the kbuf.mem.

Thus kexec_file_load failed because the sanity check failed.

So explictily reset kbuf.mem to fix the issue.

Fixes b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")

---
V1 -> V2: use KEXEC_BUF_MEM_UNKNOWN in code.

 arch/x86/kernel/crash.c           | 1 +
 arch/x86/kernel/kexec-bzimage64.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/kernel/crash.c +0 -0
index f631a3f15587..6b7890c7889b 100644

arch/x86/kernel/crash.c int crash_load_segments(struct kimage *image) · line 469

Quoted 1 line
arch/x86/kernel/crash.c +0 -0
@@ -469,6 +469,7 @@ int crash_load_segments(struct kimage *image)
 

Wondering why this place doesn't need the initialization assignment. Isn't it to assign in all places before kexec_add_buffer() calling?

	/* Add backup segment. */
        if (image->arch.backup_src_sz) { 
	}
Quoted 11 lines
arch/x86/kernel/crash.c +1 -0
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
arch/x86/kernel/kexec-bzimage64.c +0 -0
index 278cd07228dd..0d5efa34f359 100644

arch/x86/kernel/kexec-bzimage64.c static void *bzImage64_load(struct kimage *image, char *kernel, · line 436

Quoted 3 lines
arch/x86/kernel/kexec-bzimage64.c +0 -0
@@ -434,6 +434,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	kbuf.memsz = PAGE_ALIGN(header->init_size);
 	kbuf.buf_align = header->kernel_alignment;
 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;

Same question for bzImage64_load(), there are three kexec_add_buffer() calling, I only saw two initialization in this patch.

Quoted 15 lines
arch/x86/kernel/kexec-bzimage64.c +2 -0
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out_free_params;
@@ -448,6 +449,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz = kbuf.memsz = initrd_len;
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
+		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			goto out_free_params;
Signature · 1 line
2.17.0
author
#3 lore raw
Quoted 29 lines · Baoquan He
Quoted 28 lines · Dave Young

The code cleanup mentioned in Fixes tag changed the behavior of kexec_locate_mem_hole. The kexec_locate_mem_hole will try to allocate free memory only when kbuf.mem is initialized as zero.

But in x86 kexec_file_load implementation there are a few places the kbuf.mem is reused like below:
/* kbuf initialized, kbuf.mem = 0 */
...
kexec_add_buffer()
...
kexec_add_buffer()

The second kexec_add_buffer will reuse previous kbuf but not reinitialize the kbuf.mem.

Thus kexec_file_load failed because the sanity check failed.

So explictily reset kbuf.mem to fix the issue.

Fixes b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")

---
V1 -> V2: use KEXEC_BUF_MEM_UNKNOWN in code.

 arch/x86/kernel/crash.c           | 1 +
 arch/x86/kernel/kexec-bzimage64.c | 2 ++
 2 files changed, 3 insertions(+)
Quoted 9 lines

arch/x86/kernel/crash.c int crash_load_segments(struct kimage *image) · line 469

Quoted 6 lines
arch/x86/kernel/crash.c +0 -0
index f631a3f15587..6b7890c7889b 100644
@@ -469,6 +469,7 @@ int crash_load_segments(struct kimage *image)
 

Wondering why this place doesn't need the initialization assignment. Isn't it to assign in all places before kexec_add_buffer() calling?

C designated initializers will make sure to initialize it as zero. We set KEXEC_BUF_MEM_UNKNOWN as 0 so it just works.

Quoted 23 lines
	/* Add backup segment. */
        if (image->arch.backup_src_sz) { 
	}
Quoted 11 lines
arch/x86/kernel/crash.c +1 -0
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
arch/x86/kernel/kexec-bzimage64.c +0 -0
index 278cd07228dd..0d5efa34f359 100644

arch/x86/kernel/kexec-bzimage64.c static void *bzImage64_load(struct kimage *image, char *kernel, · line 436

Quoted 3 lines
arch/x86/kernel/kexec-bzimage64.c +0 -0
@@ -434,6 +434,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	kbuf.memsz = PAGE_ALIGN(header->init_size);
 	kbuf.buf_align = header->kernel_alignment;
 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;

Same question for bzImage64_load(), there are three kexec_add_buffer() calling, I only saw two initialization in this patch.

Quoted 15 lines
Quoted 15 lines
arch/x86/kernel/kexec-bzimage64.c +2 -0
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out_free_params;
@@ -448,6 +449,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz = kbuf.memsz = initrd_len;
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
+		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			goto out_free_params;
Signature · 1 line
2.17.0

Thanks
Dave

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

Acked-by
#4 lore raw
Quoted 5 lines · Dave Young
Quoted 2 lines

Wondering why this place doesn't need the initialization assignment. Isn't it to assign in all places before kexec_add_buffer() calling?

C designated initializers will make sure to initialize it as zero. We set KEXEC_BUF_MEM_UNKNOWN as 0 so it just works.

Got it, it works, thanks. People may need check code to find out KEXEC_BUF_MEM_UNKNOWN is 0, then realize this fact.

Other than this, it looks good to me, ack it.

Thanks
Baoquan

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

author
#5 lore raw
Quoted 9 lines · Baoquan He
Quoted 5 lines · Dave Young
Quoted 2 lines

Wondering why this place doesn't need the initialization assignment. Isn't it to assign in all places before kexec_add_buffer() calling?

C designated initializers will make sure to initialize it as zero. We set KEXEC_BUF_MEM_UNKNOWN as 0 so it just works.

Got it, it works, thanks. People may need check code to find out KEXEC_BUF_MEM_UNKNOWN is 0, then realize this fact.

Agreed, it is not very clear now. It's better to improve it with some explict initial value since we have the macro. But since this is a regression I suggest to fix the bug first, I can send a patch later for the improvement.

Thanks!

Quoted 7 lines

Other than this, it looks good to me, ack it.

Thanks
Baoquan

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

author
#6 lore raw
In reply to Dave Young
Quoted 63 lines · Dave Young

The code cleanup mentioned in Fixes tag changed the behavior of kexec_locate_mem_hole. The kexec_locate_mem_hole will try to allocate free memory only when kbuf.mem is initialized as zero.

But in x86 kexec_file_load implementation there are a few places the kbuf.mem is reused like below:
/* kbuf initialized, kbuf.mem = 0 */
...
kexec_add_buffer()
...
kexec_add_buffer()

The second kexec_add_buffer will reuse previous kbuf but not reinitialize the kbuf.mem.

Thus kexec_file_load failed because the sanity check failed.

So explictily reset kbuf.mem to fix the issue.

Fixes b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")

---
V1 -> V2: use KEXEC_BUF_MEM_UNKNOWN in code.

 arch/x86/kernel/crash.c           | 1 +
 arch/x86/kernel/kexec-bzimage64.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/kernel/crash.c +1 -0
index f631a3f15587..6b7890c7889b 100644
@@ -469,6 +469,7 @@ int crash_load_segments(struct kimage *image)
 
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
arch/x86/kernel/kexec-bzimage64.c +2 -0
index 278cd07228dd..0d5efa34f359 100644
@@ -434,6 +434,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	kbuf.memsz = PAGE_ALIGN(header->init_size);
 	kbuf.buf_align = header->kernel_alignment;
 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out_free_params;
@@ -448,6 +449,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz = kbuf.memsz = initrd_len;
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
+		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			goto out_free_params;
Signature · 1 line
2.17.0

Andrew, Boris, can any of you take this patch? Without this fix we have a regression.

Thanks
Dave

#7 lore raw

[tip:x86/urgent] x86/kexec: Fix a kexec_file_load() failure

In reply to Dave Young
x86/kexec: Fix a kexec_file_load() failure
Commit-ID:  993a110319a4a60aadbd02f6defdebe048f7773b
Gitweb:     https://git.kernel.org/tip/993a110319a4a60aadbd02f6defdebe048f7773b
Author:     Dave Young <[email protected]>
AuthorDate: Fri, 28 Dec 2018 09:12:47 +0800
Committer:  Borislav Petkov <[email protected]>
CommitDate: Tue, 15 Jan 2019 12:12:50 +0100

x86/kexec: Fix a kexec_file_load() failure

Commit

b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")

changed the behavior of kexec_locate_mem_hole(): it will try to allocate free memory only when kbuf.mem is initialized to zero.

However, x86's kexec_file_load() implementation reuses a struct kexec_buf allocated on the stack and its kbuf.mem member gets set by each kexec_add_buffer() invocation.

The second kexec_add_buffer() will reuse the same kbuf but not reinitialize kbuf.mem.

Therefore, explictily reset kbuf.mem each time in order for kexec_locate_mem_hole() to locate a free memory region each time.

[ bp: massage commit message. ]

Fixes b6664ba42f14 ("s390, kexec_file: drop arch_kexec_mem_walk()")
Cc "Eric W. Biederman" <[email protected]>
Cc "H. Peter Anvin" <[email protected]>
Cc AKASHI Takahiro <[email protected]>
Cc Andrew Morton <[email protected]>
Cc Ingo Molnar <[email protected]>
Cc Martin Schwidefsky <[email protected]>
Cc Philipp Rudo <[email protected]>
Cc Thomas Gleixner <[email protected]>
Cc Vivek Goyal <[email protected]>
Cc Yannik Sembritzki <[email protected]>
Cc Yi Wang <[email protected]>
Cc x86-ml <[email protected]>
---
 arch/x86/kernel/crash.c           | 1 +
 arch/x86/kernel/kexec-bzimage64.c | 2 ++
 2 files changed, 3 insertions(+)
arch/x86/kernel/crash.c +1 -0
index c8b07d8ea5a2..17ffc869cab8 100644
@@ -470,6 +470,7 @@ int crash_load_segments(struct kimage *image)
 
 	kbuf.memsz = kbuf.bufsz;
 	kbuf.buf_align = ELF_CORE_HEADER_ALIGN;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret) {
 		vfree((void *)image->arch.elf_headers);
arch/x86/kernel/kexec-bzimage64.c +2 -0
index 278cd07228dd..0d5efa34f359 100644
@@ -434,6 +434,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 	kbuf.memsz = PAGE_ALIGN(header->init_size);
 	kbuf.buf_align = header->kernel_alignment;
 	kbuf.buf_min = MIN_KERNEL_LOAD_ADDR;
+	kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 	ret = kexec_add_buffer(&kbuf);
 	if (ret)
 		goto out_free_params;
@@ -448,6 +449,7 @@ static void *bzImage64_load(struct kimage *image, char *kernel,
 		kbuf.bufsz = kbuf.memsz = initrd_len;
 		kbuf.buf_align = PAGE_SIZE;
 		kbuf.buf_min = MIN_INITRD_LOAD_ADDR;
+		kbuf.mem = KEXEC_BUF_MEM_UNKNOWN;
 		ret = kexec_add_buffer(&kbuf);
 		if (ret)
 			goto out_free_params;