Skip to contentSkip to conversation

kexec, x86: fix kexec when boot_params.hardware_subarch != 0

PATCH Superseded Chris Leech 3 participants 10 messages Mar 28, 2011 – Apr 27, 2011

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

PATCH 1/1 kexec, x86: fix kexec when boot_params.hardware_subarch != 0

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

---
 kexec/arch/i386/x86-linux-setup.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
kexec/arch/i386/x86-linux-setup.c +21 -0
index f843ca4..b7f8bd8 100644
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <errno.h>
 #include <limits.h>
 #include <sys/types.h>
@@ -396,12 +397,32 @@ out:
 		real_mode->eddbuf_entries);
 }
 
+#define BOOT_PARAMS_DBGFS	"/sys/kernel/debug/boot_params/data"
+
+void setup_subarch(struct x86_linux_param_header *real_mode)
+{
+	int data_file;
+	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+
+	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	if (data_file < 0)
+		return;
+	if (lseek(data_file, offset, SEEK_SET) < 0)
+		goto close;
+	read(data, &real_mode->hardware_subarch, sizeof(uint32_t));
+close:
+	close(data_file);
+}
+
 void setup_linux_system_parameters(struct x86_linux_param_header *real_mode,
 					unsigned long kexec_flags)
 {
 	/* Fill in information the BIOS would usually provide */
 	struct memory_range *range;
 	int i, ranges;
+
+	/* get subarch from running kernel */
+	setup_subarch(real_mode);
 	
 	/* Default screen size */
 	real_mode->orig_x = 0;

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

#1 lore raw
Quoted 34 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

---
 kexec/arch/i386/x86-linux-setup.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
kexec/arch/i386/x86-linux-setup.c +4 -0
index f843ca4..b7f8bd8 100644
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <errno.h>
 #include <limits.h>
 #include <sys/types.h>
@@ -396,12 +397,32 @@ out:
 		real_mode->eddbuf_entries);
 }
 
+#define BOOT_PARAMS_DBGFS	"/sys/kernel/debug/boot_params/data"
+
+void setup_subarch(struct x86_linux_param_header *real_mode)

kexec/arch/i386/x86-linux-setup.c out: · line 412

Quoted 10 lines
kexec/arch/i386/x86-linux-setup.c +10 -0
+{
+	int data_file;
+	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+
+	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	if (data_file < 0)
+		return;
+	if (lseek(data_file, offset, SEEK_SET) < 0)
+		goto close;
+	read(data, &real_mode->hardware_subarch, sizeof(uint32_t));

Should data be data_file?

Quoted 18 lines
kexec/arch/i386/x86-linux-setup.c +7 -0
+close:
+	close(data_file);
+}
+
 void setup_linux_system_parameters(struct x86_linux_param_header *real_mode,
 					unsigned long kexec_flags)
 {
 	/* Fill in information the BIOS would usually provide */
 	struct memory_range *range;
 	int i, ranges;
+
+	/* get subarch from running kernel */
+	setup_subarch(real_mode);
 
 	/* Default screen size */
 	real_mode->orig_x = 0;
Quoted 5 lines

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

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

#2 lore raw
Quoted 5 lines · Simon Horman
Quoted 5 lines
kexec/arch/i386/x86-linux-setup.c +5 -0
+void setup_subarch(struct x86_linux_param_header *real_mode)
+{
+	int data_file;
+	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+
Quoted 8 lines

kexec/arch/i386/x86-linux-setup.c out: · line 412

Quoted 6 lines
kexec/arch/i386/x86-linux-setup.c +6 -0
+	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	if (data_file < 0)
+		return;
+	if (lseek(data_file, offset, SEEK_SET) < 0)
+		goto close;
+	read(data, &real_mode->hardware_subarch, sizeof(uint32_t));

Should data be data_file?

Yes. Sorry, looks like I sent an unrefreshed patch.

- Chris

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

#3 lore raw

PATCH v2 1/1 kexec, x86: fix kexec when boot_params.hardware_subarch != 0

In reply to Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

---
 kexec/arch/i386/x86-linux-setup.c |   21 +++++++++++++++++++++
 1 files changed, 21 insertions(+), 0 deletions(-)
kexec/arch/i386/x86-linux-setup.c +21 -0
index f843ca4..50b32be 100644
@@ -20,6 +20,7 @@
 #include <string.h>
 #include <stdarg.h>
 #include <stdlib.h>
+#include <stddef.h>
 #include <errno.h>
 #include <limits.h>
 #include <sys/types.h>
@@ -396,12 +397,32 @@ out:
 		real_mode->eddbuf_entries);
 }
 
+#define BOOT_PARAMS_DBGFS	"/sys/kernel/debug/boot_params/data"
+
+void setup_subarch(struct x86_linux_param_header *real_mode)
+{
+	int data_file;
+	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+
+	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	if (data_file < 0)
+		return;
+	if (lseek(data_file, offset, SEEK_SET) < 0)
+		goto close;
+	read(data_file, &real_mode->hardware_subarch, sizeof(uint32_t));
+close:
+	close(data_file);
+}
+
 void setup_linux_system_parameters(struct x86_linux_param_header *real_mode,
 					unsigned long kexec_flags)
 {
 	/* Fill in information the BIOS would usually provide */
 	struct memory_range *range;
 	int i, ranges;
+
+	/* get subarch from running kernel */
+	setup_subarch(real_mode);
 	
 	/* Default screen size */
 	real_mode->orig_x = 0;

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

Applied
#4 lore raw
Quoted 9 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

Thanks, applied.

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

#5 lore raw
In reply to Chris Leech
Quoted 10 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

...

Quoted 2 lines

+#define BOOT_PARAMS_DBGFS "/sys/kernel/debug/boot_params/data"

A minor issue here is that you are using a hard-coded debugfs path, debugfs can be also mounted to /debug too, so it is better that if we can search the path dynamically here.

Thanks.

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

#6 lore raw
Quoted 12 lines · WANG Cong
Quoted 10 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

Quoted 7 lines

...

Quoted 2 lines

+#define BOOT_PARAMS_DBGFS "/sys/kernel/debug/boot_params/data"

A minor issue here is that you are using a hard-coded debugfs path, debugfs can be also mounted to /debug too, so it is better that if we can search the path dynamically here.

Do you think it would be better for the code to loop through a few common alternatives for the path?

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

#7 lore raw
Quoted 16 lines · Simon Horman
Quoted 14 lines · WANG Cong
Quoted 10 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

...

Quoted 1 line
Quoted 9 lines
Quoted 5 lines
Quoted 1 line

+#define BOOT_PARAMS_DBGFS "/sys/kernel/debug/boot_params/data"

A minor issue here is that you are using a hard-coded debugfs path, debugfs can be also mounted to /debug too, so it is better that if we can search the path dynamically here.

Do you think it would be better for the code to loop through a few common alternatives for the path?

Not fully tested, but instead of looping through possible mount points maybe look through mtab in search of debugfs by fs type.

Something like this?

kexec, x86: search for debugfs mountpoint in /etc/mtab

From: Chris Leech <[email protected]>

---
 kexec/arch/i386/x86-linux-setup.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)
kexec/arch/i386/x86-linux- renamed from kexec/arch/i386/x86-linux-setup.c +0 -0

setup.c
index 50b32be..0528cea 100644

kexec/arch/i386/x86-linux-setup.c +33 -2
@@ -30,6 +30,7 @@
 #include <linux/fb.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <mntent.h>
 #include <x86/x86-linux.h>
 #include "../../kexec.h"
 #include "kexec-x86.h"
@@ -397,14 +398,44 @@ out:
 		real_mode->eddbuf_entries);
 }
 
-#define BOOT_PARAMS_DBGFS	"/sys/kernel/debug/boot_params/data"
+/*
+ * This really only makes sense for virtual filesystems that are only expected
+ * to be mounted once (sysfs, debugsfs, proc), as it will return the first
+ * instance listed in mtab.
+ */
+char *find_mnt_by_fsname(char *fsname)
+{
+	FILE *mtab;
+	struct mntent *mnt;
+	char *mntdir;
+
+	mtab = setmntent("/etc/mtab", "r");
+	if (!mtab)
+		return NULL;
+	for(mnt = getmntent(mtab); mnt; mnt = getmntent(mtab)) {
+		if (strcmp(mnt->mnt_fsname, fsname) == 0)
+			break;
+	}
+	mntdir = mnt ? strdup(mnt->mnt_dir) : NULL;
+	endmntent(mtab);
+	return mntdir;
+}
 
 void setup_subarch(struct x86_linux_param_header *real_mode)
 {
 	int data_file;
 	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+	char *debugfs_mnt;
+	char filename[PATH_MAX];
+
+	debugfs_mnt = find_mnt_by_fsname("debugfs");
+	if (!debugfs_mnt)
+		return;
+	snprintf(filename, PATH_MAX, "%s/%s", debugfs_mnt, "boot_params/data");
+	filename[PATH_MAX-1] = 0;
+	free(debugfs_mnt);
 
-	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	data_file = open(filename, O_RDONLY);
 	if (data_file < 0)
 		return;
 	if (lseek(data_file, offset, SEEK_SET) < 0)

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

#8 lore raw

Re: [PATCH V2] kexec, x86: fix kexec when?boot_params.hardware_subarch != 0

Quoted 24 lines · Chris Leech
Quoted 22 lines · Simon Horman
Quoted 19 lines · WANG Cong
Quoted 10 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

...

Quoted 2 lines

+#define BOOT_PARAMS_DBGFS "/sys/kernel/debug/boot_params/data"

A minor issue here is that you are using a hard-coded debugfs path, debugfs can be also mounted to /debug too, so it is better that if we can search the path dynamically here.

Quoted 8 lines
Quoted 3 lines

Do you think it would be better for the code to loop through a few common alternatives for the path?

Not fully tested, but instead of looping through possible mount points maybe look through mtab in search of debugfs by fs type.

Something like this?

This approach looks good to me.
Could you do some more testing?

Quoted 51 lines

kexec, x86: search for debugfs mountpoint in /etc/mtab

From: Chris Leech <[email protected]>

---
 kexec/arch/i386/x86-linux-setup.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)
kexec/arch/i386/x86-linux- renamed from kexec/arch/i386/x86-linux-setup.c +0 -0

setup.c
index 50b32be..0528cea 100644

kexec/arch/i386/x86-linux-setup.c +23 -1
@@ -30,6 +30,7 @@
 #include <linux/fb.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <mntent.h>
 #include <x86/x86-linux.h>
 #include "../../kexec.h"
 #include "kexec-x86.h"
@@ -397,14 +398,44 @@ out:
 		real_mode->eddbuf_entries);
 }
 
-#define BOOT_PARAMS_DBGFS	"/sys/kernel/debug/boot_params/data"
+/*
+ * This really only makes sense for virtual filesystems that are only expected
+ * to be mounted once (sysfs, debugsfs, proc), as it will return the first
+ * instance listed in mtab.
+ */
+char *find_mnt_by_fsname(char *fsname)
+{
+	FILE *mtab;
+	struct mntent *mnt;
+	char *mntdir;
+
+	mtab = setmntent("/etc/mtab", "r");
+	if (!mtab)
+		return NULL;
+	for(mnt = getmntent(mtab); mnt; mnt = getmntent(mtab)) {
+		if (strcmp(mnt->mnt_fsname, fsname) == 0)
+			break;
+	}
+	mntdir = mnt ? strdup(mnt->mnt_dir) : NULL;
+	endmntent(mtab);
+	return mntdir;
+}
 
 void setup_subarch(struct x86_linux_param_header *real_mode)

kexec/arch/i386/x86-linux-setup.c out: · line 434

Quoted 10 lines
kexec/arch/i386/x86-linux-setup.c +7 -0
 {
 	int data_file;
 	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+	char *debugfs_mnt;
+	char filename[PATH_MAX];
+
+	debugfs_mnt = find_mnt_by_fsname("debugfs");
+	if (!debugfs_mnt)
+		return;
+	snprintf(filename, PATH_MAX, "%s/%s", debugfs_mnt, "boot_params/data");

It might be worth bailing out on truncation here.
Perhaps with an error message?

kexec/arch/i386/x86-linux-setup.c out: · line 441

Quoted 8 lines
kexec/arch/i386/x86-linux-setup.c +3 -1
+	filename[PATH_MAX-1] = 0;
+	free(debugfs_mnt);
 
-	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	data_file = open(filename, O_RDONLY);
 	if (data_file < 0)
 		return;
 	if (lseek(data_file, offset, SEEK_SET) < 0)

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

#9 lore raw

Re: [PATCH V2] kexec, x86: fix kexec when?boot_params.hardware_subarch != 0

Quoted 29 lines · Simon Horman
Quoted 28 lines · Chris Leech
Quoted 25 lines · Simon Horman
Quoted 12 lines · WANG Cong
Quoted 10 lines · Chris Leech

kexec needs to keep the subarch setting the same as the running kernel in the boot parameters, or the kernel will die in early setup. I ran into this with X86_SUBARCH_MRST, but it should apply to CE4100 and any future subarch that uses non-default early setup code.

This patch requires debugfs mounted at /sys/kernel/debug, as that's the only way I know of to get at the running kernels boot_params. Without debugfs mounted it falls back to the current behavior of assuming subarch 0.

Quoted 7 lines

...

Quoted 2 lines

+#define BOOT_PARAMS_DBGFS "/sys/kernel/debug/boot_params/data"

A minor issue here is that you are using a hard-coded debugfs path, debugfs can be also mounted to /debug too, so it is better that if we can search the path dynamically here.

Do you think it would be better for the code to loop through a few common alternatives for the path?

Quoted 7 lines
Quoted 4 lines

Not fully tested, but instead of looping through possible mount points maybe look through mtab in search of debugfs by fs type.

Something like this?

This approach looks good to me.
Could you do some more testing?

Ping.

Quoted 68 lines
Quoted 51 lines

kexec, x86: search for debugfs mountpoint in /etc/mtab

From: Chris Leech <[email protected]>

---
 kexec/arch/i386/x86-linux-setup.c |   35 +++++++++++++++++++++++++++++++++--
 1 files changed, 33 insertions(+), 2 deletions(-)
kexec/arch/i386/x86-linux- renamed from kexec/arch/i386/x86-linux-setup.c +0 -0

setup.c
index 50b32be..0528cea 100644

kexec/arch/i386/x86-linux-setup.c +23 -1
@@ -30,6 +30,7 @@
 #include <linux/fb.h>
 #include <unistd.h>
 #include <dirent.h>
+#include <mntent.h>
 #include <x86/x86-linux.h>
 #include "../../kexec.h"
 #include "kexec-x86.h"
@@ -397,14 +398,44 @@ out:
 		real_mode->eddbuf_entries);
 }
 
-#define BOOT_PARAMS_DBGFS	"/sys/kernel/debug/boot_params/data"
+/*
+ * This really only makes sense for virtual filesystems that are only expected
+ * to be mounted once (sysfs, debugsfs, proc), as it will return the first
+ * instance listed in mtab.
+ */
+char *find_mnt_by_fsname(char *fsname)
+{
+	FILE *mtab;
+	struct mntent *mnt;
+	char *mntdir;
+
+	mtab = setmntent("/etc/mtab", "r");
+	if (!mtab)
+		return NULL;
+	for(mnt = getmntent(mtab); mnt; mnt = getmntent(mtab)) {
+		if (strcmp(mnt->mnt_fsname, fsname) == 0)
+			break;
+	}
+	mntdir = mnt ? strdup(mnt->mnt_dir) : NULL;
+	endmntent(mtab);
+	return mntdir;
+}
 
 void setup_subarch(struct x86_linux_param_header *real_mode)

kexec/arch/i386/x86-linux-setup.c out: · line 434

Quoted 10 lines
kexec/arch/i386/x86-linux-setup.c +7 -0
 {
 	int data_file;
 	const off_t offset = offsetof(typeof(*real_mode), hardware_subarch);
+	char *debugfs_mnt;
+	char filename[PATH_MAX];
+
+	debugfs_mnt = find_mnt_by_fsname("debugfs");
+	if (!debugfs_mnt)
+		return;
+	snprintf(filename, PATH_MAX, "%s/%s", debugfs_mnt, "boot_params/data");

It might be worth bailing out on truncation here.
Perhaps with an error message?

Quoted 3 lines
kexec/arch/i386/x86-linux-setup.c +2 -0
+	filename[PATH_MAX-1] = 0;
+	free(debugfs_mnt);
 
Quoted 11 lines

kexec/arch/i386/x86-linux-setup.c out: · line 441

Quoted 5 lines
kexec/arch/i386/x86-linux-setup.c +1 -1
-	data_file = open(BOOT_PARAMS_DBGFS, O_RDONLY);
+	data_file = open(filename, O_RDONLY);
 	if (data_file < 0)
 		return;
 	if (lseek(data_file, offset, SEEK_SET) < 0)

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

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