Skip to contentSkip to conversation

Add method to pass initrd through cmdline

PATCH New Hui Li 3 participants 6 messages Apr 18, 2022 – Jun 10, 2022

Subsystems (2)
lore
Share & tools
lore

Hui Li

author
#0 lore raw

PATCH 1/1 Add method to pass initrd through cmdline

Problem description:
Under loongson platform,use command:
kexec -l vmlinux... --append="root=UUID=28e1..." --initrd=... kexec -e
quick restart failed.

The reason of this problem:
The kernel cannot parse the UUID,UUID is parsed in the initrd. Loongson platform obtain the initrd through cmdline in kernel. In kexec-tools, initrd is not added to cmdline.

The solution:
Add initrd parameter to cmdline. and add a CONFIG_LOONGSON configuration to distinguish PAGE_OFFSET between different platforms under mips.

---
 configure.ac                     |  5 ++++
 kexec/arch/mips/crashdump-mips.h |  6 ++++-
 kexec/arch/mips/kexec-elf-mips.c | 43 ++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
configure.ac +5 -0
index cf8e8a2..26bfbcd 100644
@@ -111,6 +111,11 @@ AC_ARG_WITH([booke],
 		AC_DEFINE(CONFIG_BOOKE,1,
 			[Define to build for BookE]))
 
+AC_ARG_WITH([loongson],
+		AC_HELP_STRING([--with-loongson],[build for loongson]),
+		AC_DEFINE(CONFIG_LOONGSON,1,
+			[Define to build for LoongsoN]))
+
 dnl ---Programs
 dnl To specify a different compiler, just 'export CC=/path/to/compiler'
 if test "${build}" != "${host}" ; then
kexec/arch/mips/crashdump-mips.h +5 -1
index 7edd859..d53c696 100644
@@ -5,7 +5,11 @@ struct kexec_info;
 int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
 				unsigned long max_addr, unsigned long min_base);
 #ifdef __mips64
-#define PAGE_OFFSET	0xa800000000000000ULL
+#ifdef CONFIG_LOONGSON
+#define PAGE_OFFSET 0xFFFFFFFF80000000ULL
+#else
+#define PAGE_OFFSET 0xa800000000000000ULL
+#endif
 #define MAXMEM		0
 #else
 #define PAGE_OFFSET	0x80000000
kexec/arch/mips/kexec-elf-mips.c +43 -0

55 lines not on this page · raw message

Signature · 1 line
2.20.1
#1 lore raw

PATCH 1/1 Add method to pass initrd through cmdline

Hi Hui Li,

thanks for your patch.

Quoted 10 lines · Hui Li

Problem description:
Under loongson platform,use command:
kexec -l vmlinux... --append="root=UUID=28e1..." --initrd=... kexec -e
quick restart failed.

The reason of this problem:
The kernel cannot parse the UUID,UUID is parsed in the initrd. Loongson platform obtain the initrd through cmdline in kernel. In kexec-tools, initrd is not added to cmdline.

Is this common to other platforms?
If not, is there a reason that Loongson takes this approach?

Quoted 32 lines

The solution:
Add initrd parameter to cmdline. and add a CONFIG_LOONGSON configuration to distinguish PAGE_OFFSET between different platforms under mips.

---
 configure.ac                     |  5 ++++
 kexec/arch/mips/crashdump-mips.h |  6 ++++-
 kexec/arch/mips/kexec-elf-mips.c | 43 ++++++++++++++++++++++++++++++++
 3 files changed, 53 insertions(+), 1 deletion(-)
configure.ac +5 -0
index cf8e8a2..26bfbcd 100644
@@ -111,6 +111,11 @@ AC_ARG_WITH([booke],
 		AC_DEFINE(CONFIG_BOOKE,1,
 			[Define to build for BookE]))
 
+AC_ARG_WITH([loongson],
+		AC_HELP_STRING([--with-loongson],[build for loongson]),
+		AC_DEFINE(CONFIG_LOONGSON,1,
+			[Define to build for LoongsoN]))
+
 dnl ---Programs
 dnl To specify a different compiler, just 'export CC=/path/to/compiler'
 if test "${build}" != "${host}" ; then
kexec/arch/mips/crashdump-mips.h +0 -0
index 7edd859..d53c696 100644

kexec/arch/mips/crashdump-mips.h struct kexec_info; · line 12

Quoted 9 lines
kexec/arch/mips/crashdump-mips.h +5 -1
@@ -5,7 +5,11 @@ struct kexec_info;
 int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
 				unsigned long max_addr, unsigned long min_base);
 #ifdef __mips64
-#define PAGE_OFFSET	0xa800000000000000ULL
+#ifdef CONFIG_LOONGSON
+#define PAGE_OFFSET 0xFFFFFFFF80000000ULL
+#else
+#define PAGE_OFFSET 0xa800000000000000ULL
+#endif

Could this be detected at runtime?

It seems awkward to set the PAGE_OFFSET at compile time and thus need separate builds for separate platforms (with no warning if the wrong one is used).

Quoted 11 lines
kexec/arch/mips/crashdump-mips.h +0 -0
 #define MAXMEM		0
 #else
 #define PAGE_OFFSET	0x80000000
kexec/arch/mips/kexec-elf-mips.c +0 -0
index a2d11fc..1de418e 100644
@@ -40,6 +40,44 @@ static const int probe_debug = 0;
 #define CMDLINE_PREFIX "kexec "
 static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;
 

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 52

Quoted 10 lines
kexec/arch/mips/kexec-elf-mips.c +10 -0
+/* Converts unsigned long to ascii string. */
+static void ultoa(unsigned long i, char *str)
+{
+	int j = 0, k;
+	char tmp;
+
+	do {
+		str[j++] = i % 10 + '0';
+	} while ((i /= 10) > 0);
+	str[j] = '\0';

Could the above be achieved using printf?

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 58

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +6 -0
+	/* Reverse the string. */
+	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
+		tmp = str[k];
+		str[k] = str[j];
+		str[j] = tmp;
+	}

I'm confused as to why the string needs to be reversed.
Could it be avoided by byte-swapping 'i' before rendering it to a string?

Quoted 26 lines
kexec/arch/mips/kexec-elf-mips.c +22 -0
+}
+
+/* Adds initrd parameters to command line. */
+static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
+{
+	int cmdlen, len;
+	char str[30], *ptr;
+
+	ptr = str;
+	strcpy(str, new_para);
+	ptr += strlen(str);
+	ultoa(addr, ptr);
+	len = strlen(str);
+	cmdlen = strlen(cmdline) + len;
+	if (cmdlen > (COMMAND_LINE_SIZE - 1))
+		die("Command line overflow\n");
+	strcat(cmdline, str);
+
+	return 0;
+}
+
+
 int elf_mips_probe(const char *buf, off_t len)
 {
 	struct mem_ehdr ehdr;

kexec/arch/mips/kexec-elf-mips.c int elf_mips_load(int argc, char **argv, const char *buf, off_t len, · line 216

Quoted 8 lines
kexec/arch/mips/kexec-elf-mips.c +5 -0
@@ -171,6 +209,11 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
 		/* Now that the buffer for initrd is prepared, update the dtb
 		 * with an appropriate location */
 		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
+
+		/* Add the initrd parameters to cmdline */
+		cmdline_add_initrd(cmdline_buf, PAGE_OFFSET + initrd_base, " rd_start=");
+		cmdline_add_initrd(cmdline_buf, initrd_size, " rd_size=");
+

Will this be safe for existing use-cases?

author
#2 lore raw

PATCH 1/1 Add method to pass initrd through cmdline

Hi Simon Horman,

Thanks for your review.

Quoted 11 lines · Simon Horman

Hi Hui Li,

thanks for your patch.

Quoted 6 lines · Hui Li

Problem description:
Under loongson platform,use command:
kexec -l vmlinux... --append="root=UUID=28e1..." --initrd=... kexec -e
quick restart failed.

Quoted 8 lines
Quoted 4 lines

The reason of this problem:
The kernel cannot parse the UUID,UUID is parsed in the initrd. Loongson platform obtain the initrd through cmdline in kernel. In kexec-tools, initrd is not added to cmdline.

Is this common to other platforms?
If not, is there a reason that Loongson takes this approach?

in kernel code,arch/mips/configs/loongson3_defconfig, CONFIG_BLK_DEV_INITRD=y is defined.
In arch/mips/kernel/setup.c file, the following code?

#ifdef CONFIG_BLK_DEV_INITRD

static int __init rd_start_early(char *p)

{
     unsigned long start = memparse(p, &p);
#ifdef CONFIG_64BIT
     /* Guess if the sign extension was forgotten by bootloader */
     if (start < XKPHYS)
         start = (int)start;
#endif
     initrd_start = start;
     initrd_end += start;
     return 0;
}

early_param("rd_start", rd_start_early);

static int __init rd_size_early(char *p)

{
     initrd_end += memparse(p, &p);
     return 0;
}

early_param("rd_size", rd_size_early);
...
...
...
#endif

The purpose of parsing initrd parameters through cmdline on the loongson is to compatible with previous platforms

Maybe other platforms use DTB to parse initrd, but it can be seen that the kernel also supports the use of cmdline to parse initrd.

Quoted 44 lines
Quoted 32 lines

The solution:
Add initrd parameter to cmdline. and add a CONFIG_LOONGSON configuration to distinguish PAGE_OFFSET between different platforms under mips.

---
  configure.ac                     |  5 ++++
  kexec/arch/mips/crashdump-mips.h |  6 ++++-
  kexec/arch/mips/kexec-elf-mips.c | 43 ++++++++++++++++++++++++++++++++
  3 files changed, 53 insertions(+), 1 deletion(-)
configure.ac +5 -0
index cf8e8a2..26bfbcd 100644
@@ -111,6 +111,11 @@ AC_ARG_WITH([booke],
  		AC_DEFINE(CONFIG_BOOKE,1,
  			[Define to build for BookE]))
 
+AC_ARG_WITH([loongson],
+		AC_HELP_STRING([--with-loongson],[build for loongson]),
+		AC_DEFINE(CONFIG_LOONGSON,1,
+			[Define to build for LoongsoN]))
+
  dnl ---Programs
  dnl To specify a different compiler, just 'export CC=/path/to/compiler'
  if test "${build}" != "${host}" ; then
kexec/arch/mips/crashdump-mips.h +0 -0
index 7edd859..d53c696 100644

kexec/arch/mips/crashdump-mips.h struct kexec_info; · line 12

Quoted 9 lines
kexec/arch/mips/crashdump-mips.h +5 -1
@@ -5,7 +5,11 @@ struct kexec_info;
  int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
  				unsigned long max_addr, unsigned long min_base);
  #ifdef __mips64
-#define PAGE_OFFSET	0xa800000000000000ULL
+#ifdef CONFIG_LOONGSON
+#define PAGE_OFFSET 0xFFFFFFFF80000000ULL
+#else
+#define PAGE_OFFSET 0xa800000000000000ULL
+#endif

Could this be detected at runtime?

Quoted 4 lines

It seems awkward to set the PAGE_OFFSET at compile time and thus need separate builds for separate platforms (with no warning if the wrong one is used).

Thank you very much for this good suggestion.
I see that there is a way to get different platform information at runtime via "/proc/cpuinfo", which is used to distinguish PAGE_OFFSET for different platforms.

Quoted 24 lines
Quoted 11 lines
kexec/arch/mips/crashdump-mips.h +0 -0
  #define MAXMEM		0
  #else
  #define PAGE_OFFSET	0x80000000
kexec/arch/mips/kexec-elf-mips.c +0 -0
index a2d11fc..1de418e 100644
@@ -40,6 +40,44 @@ static const int probe_debug = 0;
  #define CMDLINE_PREFIX "kexec "
  static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;
 

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 52

Quoted 10 lines
kexec/arch/mips/kexec-elf-mips.c +10 -0
+/* Converts unsigned long to ascii string. */
+static void ultoa(unsigned long i, char *str)
+{
+	int j = 0, k;
+	char tmp;
+
+	do {
+		str[j++] = i % 10 + '0';
+	} while ((i /= 10) > 0);
+	str[j] = '\0';

Could the above be achieved using printf?

Quoted 10 lines

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 58

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +6 -0
+	/* Reverse the string. */
+	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
+		tmp = str[k];
+		str[k] = str[j];
+		str[j] = tmp;
+	}

I'm confused as to why the string needs to be reversed.
Could it be avoided by byte-swapping 'i' before rendering it to a string? Thanks a lot for your suggestions on these details.

ultoa() function is used in many places.I see many other platforms have the same implementation like:
/kexec/arch/i386/crashdump-x86.c /kexec/arch/ppc64/crashdump-ppc64.c /kexec/arch/ppc64/crashdump-sh.c

So, Is it possible to keep the existing implementation ?

Quoted 28 lines
Quoted 28 lines
kexec/arch/mips/kexec-elf-mips.c +22 -0
+}
+
+/* Adds initrd parameters to command line. */
+static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
+{
+	int cmdlen, len;
+	char str[30], *ptr;
+
+	ptr = str;
+	strcpy(str, new_para);
+	ptr += strlen(str);
+	ultoa(addr, ptr);
+	len = strlen(str);
+	cmdlen = strlen(cmdline) + len;
+	if (cmdlen > (COMMAND_LINE_SIZE - 1))
+		die("Command line overflow\n");
+	strcat(cmdline, str);
+
+	return 0;
+}
+
+
  int elf_mips_probe(const char *buf, off_t len)
  {
  	struct mem_ehdr ehdr;
@@ -171,6 +209,11 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
  		/* Now that the buffer for initrd is prepared, update the dtb
  		 * with an appropriate location */
Quoted 9 lines

kexec/arch/mips/kexec-elf-mips.c int elf_mips_load(int argc, char **argv, const char *buf, off_t len, · line 216

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +5 -0
  		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
+
+		/* Add the initrd parameters to cmdline */
+		cmdline_add_initrd(cmdline_buf, PAGE_OFFSET + initrd_base, " rd_start=");
+		cmdline_add_initrd(cmdline_buf, initrd_size, " rd_size=");
+

Will this be safe for existing use-cases?

This modification is safe for other existing use-cases. Now this method is only for the loongson platform to pass the initrd correctly. At this stage, this change is no problem to use on the loongson platform, and it will not affect other platforms. This part can be further constrained, only for the loongson platform.

let me think about the above and try to find a proper way, and then I will send a v3 patch.

Thanks again for your review

Quoted 5 lines

_______________________________________________
kexec mailing list
kexec at lists.infradead.org
http://lists.infradead.org/mailman/listinfo/kexec

#3 lore raw

PATCH 1/1 Add method to pass initrd through cmdline

Quoted 59 lines · Hui Li

Hi Simon Horman,

Thanks for your review.

Quoted 11 lines · Simon Horman

Hi Hui Li,

thanks for your patch.

Quoted 6 lines · Hui Li

Problem description:
Under loongson platform,use command:
kexec -l vmlinux... --append="root=UUID=28e1..." --initrd=... kexec -e
quick restart failed.

Quoted 8 lines
Quoted 4 lines

The reason of this problem:
The kernel cannot parse the UUID,UUID is parsed in the initrd. Loongson platform obtain the initrd through cmdline in kernel. In kexec-tools, initrd is not added to cmdline.

Is this common to other platforms?
If not, is there a reason that Loongson takes this approach?

in kernel code,arch/mips/configs/loongson3_defconfig, CONFIG_BLK_DEV_INITRD=y is defined.
In arch/mips/kernel/setup.c file, the following code?

#ifdef CONFIG_BLK_DEV_INITRD

static int __init rd_start_early(char *p)

{
    unsigned long start = memparse(p, &p);
#ifdef CONFIG_64BIT
    /* Guess if the sign extension was forgotten by bootloader */
    if (start < XKPHYS)
        start = (int)start;
#endif
    initrd_start = start;
    initrd_end += start;
    return 0;
}

early_param("rd_start", rd_start_early);

static int __init rd_size_early(char *p)

{
    initrd_end += memparse(p, &p);
    return 0;
}

early_param("rd_size", rd_size_early);
...
...
...
#endif

Quoted 2 lines

The purpose of parsing initrd parameters through cmdline on the loongson is to compatible with previous platforms

Thanks. I'm very fine with mechanisms that preserve compatibility. But if I understand things correctly then this feature is not specific to Loongson (although it has only been tested on Loongson).

Quoted 46 lines

Maybe other platforms use DTB to parse initrd, but it can be seen that the kernel also supports the use of cmdline to parse initrd.

Quoted 43 lines
Quoted 31 lines

The solution:
Add initrd parameter to cmdline. and add a CONFIG_LOONGSON configuration to distinguish PAGE_OFFSET between different platforms under mips.

---
  configure.ac                     |  5 ++++
  kexec/arch/mips/crashdump-mips.h |  6 ++++-
  kexec/arch/mips/kexec-elf-mips.c | 43 ++++++++++++++++++++++++++++++++
  3 files changed, 53 insertions(+), 1 deletion(-)
configure.ac +5 -0
index cf8e8a2..26bfbcd 100644
@@ -111,6 +111,11 @@ AC_ARG_WITH([booke],
  		AC_DEFINE(CONFIG_BOOKE,1,
  			[Define to build for BookE]))
+AC_ARG_WITH([loongson],
+		AC_HELP_STRING([--with-loongson],[build for loongson]),
+		AC_DEFINE(CONFIG_LOONGSON,1,
+			[Define to build for LoongsoN]))
+
  dnl ---Programs
  dnl To specify a different compiler, just 'export CC=/path/to/compiler'
  if test "${build}" != "${host}" ; then
kexec/arch/mips/crashdump-mips.h +0 -0
index 7edd859..d53c696 100644

kexec/arch/mips/crashdump-mips.h struct kexec_info; · line 12

Quoted 9 lines
kexec/arch/mips/crashdump-mips.h +5 -1
@@ -5,7 +5,11 @@ struct kexec_info;
  int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
  				unsigned long max_addr, unsigned long min_base);
  #ifdef __mips64
-#define PAGE_OFFSET	0xa800000000000000ULL
+#ifdef CONFIG_LOONGSON
+#define PAGE_OFFSET 0xFFFFFFFF80000000ULL
+#else
+#define PAGE_OFFSET 0xa800000000000000ULL
+#endif

Could this be detected at runtime?

Quoted 8 lines
Quoted 4 lines

It seems awkward to set the PAGE_OFFSET at compile time and thus need separate builds for separate platforms (with no warning if the wrong one is used).

Thank you very much for this good suggestion.
I see that there is a way to get different platform information at runtime via "/proc/cpuinfo", which is used to distinguish PAGE_OFFSET for different platforms.

Looking at ARM and arm64 implementations of get_kernel_page_offset() they seem to rely on /proc/kallsyms for autodetection of page_offset.

Perhaps something similar could be done for MIPS.

Also, as an asside, it seems at a glance that ARM and adm64 are the same in this regard. So there seems to be some scope for consolidation.

Quoted 34 lines
Quoted 24 lines
Quoted 11 lines
kexec/arch/mips/crashdump-mips.h +0 -0
  #define MAXMEM		0
  #else
  #define PAGE_OFFSET	0x80000000
kexec/arch/mips/kexec-elf-mips.c +0 -0
index a2d11fc..1de418e 100644
@@ -40,6 +40,44 @@ static const int probe_debug = 0;
  #define CMDLINE_PREFIX "kexec "
  static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 51

Quoted 10 lines
kexec/arch/mips/kexec-elf-mips.c +10 -0
+/* Converts unsigned long to ascii string. */
+static void ultoa(unsigned long i, char *str)
+{
+	int j = 0, k;
+	char tmp;
+
+	do {
+		str[j++] = i % 10 + '0';
+	} while ((i /= 10) > 0);
+	str[j] = '\0';

Could the above be achieved using printf?

Quoted 10 lines

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 58

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +6 -0
+	/* Reverse the string. */
+	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
+		tmp = str[k];
+		str[k] = str[j];
+		str[j] = tmp;
+	}

I'm confused as to why the string needs to be reversed.
Could it be avoided by byte-swapping 'i' before rendering it to a string? Thanks a lot for your suggestions on these details.

Quoted 7 lines

ultoa() function is used in many places.I see many other platforms have the same implementation like:
/kexec/arch/i386/crashdump-x86.c /kexec/arch/ppc64/crashdump-ppc64.c /kexec/arch/ppc64/crashdump-sh.c

So, Is it possible to keep the existing implementation ?

Ok, sure.

Quoted 38 lines
Quoted 28 lines
Quoted 28 lines
kexec/arch/mips/kexec-elf-mips.c +22 -0
+}
+
+/* Adds initrd parameters to command line. */
+static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
+{
+	int cmdlen, len;
+	char str[30], *ptr;
+
+	ptr = str;
+	strcpy(str, new_para);
+	ptr += strlen(str);
+	ultoa(addr, ptr);
+	len = strlen(str);
+	cmdlen = strlen(cmdline) + len;
+	if (cmdlen > (COMMAND_LINE_SIZE - 1))
+		die("Command line overflow\n");
+	strcat(cmdline, str);
+
+	return 0;
+}
+
+
  int elf_mips_probe(const char *buf, off_t len)
  {
  	struct mem_ehdr ehdr;
@@ -171,6 +209,11 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
  		/* Now that the buffer for initrd is prepared, update the dtb
  		 * with an appropriate location */
Quoted 9 lines

kexec/arch/mips/kexec-elf-mips.c int elf_mips_load(int argc, char **argv, const char *buf, off_t len, · line 216

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +5 -0
  		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
+
+		/* Add the initrd parameters to cmdline */
+		cmdline_add_initrd(cmdline_buf, PAGE_OFFSET + initrd_base, " rd_start=");
+		cmdline_add_initrd(cmdline_buf, initrd_size, " rd_size=");
+

Will this be safe for existing use-cases?

Quoted 8 lines

This modification is safe for other existing use-cases.
Now this method is only for the loongson platform to pass the initrd correctly. At this stage, this change is no problem to use on the loongson platform, and it will not affect other platforms.
This part can be further constrained, only for the loongson platform.

let me think about the above and try to find a proper way, and then I will send a v3 patch.

I think it would be best to avoid compile-time constraints. But if a runtime constraint can ensure this runs only for tested platforms and existing behaviour is preserved for other platforms (if any) then I think we would be in good shape.

Kind regards,
Simon

author
#4 lore raw

PATCH 1/1 Add method to pass initrd through cmdline

Hi Simon Horman

Thanks for your reply.

Quoted 60 lines · Simon Horman
Quoted 59 lines · Hui Li

Hi Simon Horman,

Thanks for your review.

Quoted 11 lines · Simon Horman

Hi Hui Li,

thanks for your patch.

Quoted 6 lines · Hui Li

Problem description:
Under loongson platform,use command:
kexec -l vmlinux... --append="root=UUID=28e1..." --initrd=... kexec -e
quick restart failed.

Quoted 8 lines
Quoted 4 lines

The reason of this problem:
The kernel cannot parse the UUID,UUID is parsed in the initrd. Loongson platform obtain the initrd through cmdline in kernel. In kexec-tools, initrd is not added to cmdline.

Is this common to other platforms?
If not, is there a reason that Loongson takes this approach?

in kernel code,arch/mips/configs/loongson3_defconfig, CONFIG_BLK_DEV_INITRD=y is defined.
In arch/mips/kernel/setup.c file, the following code?

#ifdef CONFIG_BLK_DEV_INITRD

static int __init rd_start_early(char *p)

{
     unsigned long start = memparse(p, &p);
#ifdef CONFIG_64BIT
     /* Guess if the sign extension was forgotten by bootloader */
     if (start < XKPHYS)
         start = (int)start;
#endif
     initrd_start = start;
     initrd_end += start;
     return 0;
}

early_param("rd_start", rd_start_early);

static int __init rd_size_early(char *p)

{
     initrd_end += memparse(p, &p);
     return 0;
}

early_param("rd_size", rd_size_early);
...
...
...
#endif

Quoted 7 lines
Quoted 2 lines

The purpose of parsing initrd parameters through cmdline on the loongson is to compatible with previous platforms

Thanks. I'm very fine with mechanisms that preserve compatibility. But if I understand things correctly then this feature is not specific to Loongson (although it has only been tested on Loongson).

Thanks.
This problem was found on the loongson platform,and seeing mips and other platforms, the approach in this part is really different. I want to solve this problem of the loongson cpu without affecting other platforms.
Therefore, the following modifications are based on this principle.

Quoted 55 lines
Quoted 46 lines

Maybe other platforms use DTB to parse initrd, but it can be seen that the kernel also supports the use of cmdline to parse initrd.

Quoted 43 lines
Quoted 31 lines

The solution:
Add initrd parameter to cmdline. and add a CONFIG_LOONGSON configuration to distinguish PAGE_OFFSET between different platforms under mips.

---
   configure.ac                     |  5 ++++
   kexec/arch/mips/crashdump-mips.h |  6 ++++-
   kexec/arch/mips/kexec-elf-mips.c | 43 ++++++++++++++++++++++++++++++++
   3 files changed, 53 insertions(+), 1 deletion(-)
configure.ac +5 -0
index cf8e8a2..26bfbcd 100644
@@ -111,6 +111,11 @@ AC_ARG_WITH([booke],
   		AC_DEFINE(CONFIG_BOOKE,1,
   			[Define to build for BookE]))
+AC_ARG_WITH([loongson],
+		AC_HELP_STRING([--with-loongson],[build for loongson]),
+		AC_DEFINE(CONFIG_LOONGSON,1,
+			[Define to build for LoongsoN]))
+
   dnl ---Programs
   dnl To specify a different compiler, just 'export CC=/path/to/compiler'
   if test "${build}" != "${host}" ; then
kexec/arch/mips/crashdump-mips.h +0 -0
index 7edd859..d53c696 100644

kexec/arch/mips/crashdump-mips.h struct kexec_info; · line 12

Quoted 9 lines
kexec/arch/mips/crashdump-mips.h +5 -1
@@ -5,7 +5,11 @@ struct kexec_info;
   int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
   				unsigned long max_addr, unsigned long min_base);
   #ifdef __mips64
-#define PAGE_OFFSET	0xa800000000000000ULL
+#ifdef CONFIG_LOONGSON
+#define PAGE_OFFSET 0xFFFFFFFF80000000ULL
+#else
+#define PAGE_OFFSET 0xa800000000000000ULL
+#endif

Could this be detected at runtime?

Quoted 8 lines
Quoted 4 lines

It seems awkward to set the PAGE_OFFSET at compile time and thus need separate builds for separate platforms (with no warning if the wrong one is used).

Thank you very much for this good suggestion.
I see that there is a way to get different platform information at runtime via "/proc/cpuinfo", which is used to distinguish PAGE_OFFSET for different platforms.

Quoted 8 lines

Looking at ARM and arm64 implementations of get_kernel_page_offset() they seem to rely on /proc/kallsyms for autodetection of page_offset.

Perhaps something similar could be done for MIPS.

Also, as an asside, it seems at a glance that ARM and adm64 are the same in this regard. So there seems to be some scope for consolidation.

Thanks for the tips.
I analyzed the relevant code of mips and other platforms. It seems that the page_offset will be modified in crashdump-xxx.c

kexec/arch/mips/crashdump-mips.c uses the following method to distinguish the page_offset of different cpus.

static int patch_elf_info(void)

{
     ...
     const char cpuinfo[] = "/proc/cpuinfo";
     ...
     ...
     fp = fopen(cpuinfo, "r");
     ...
     while (fgets(line, sizeof(line), fp) != 0) {
         if (strncmp(line, "cpu model", 9) == 0) {
             /* OCTEON uses a different page_offset. */
             if (strstr(line, "Octeon"))
                 elf_info64.page_offset = 0x8000000000000000ULL;
             break;
         }
     }

}
I will also add the page_offset modification of the loongson CPU here in v3 patch.

But,the modification here is for crash_kernel.

I want to add the following code to kexec/arch/mips/kexec-elf-mips.c.

/* add initrd to cmdline to compatible with previous platforms. */ static int patch_initrd_info(char *cmdline, unsigned long initrd_base, unsigned long initrd_size)

{
     const char cpuinfo[] = "/proc/cpuinfo";
     char line[MAX_LINE];
     FILE *fp;
     unsigned long page_offset = PAGE_OFFSET;

     fp = fopen(cpuinfo, "r");
     if (!fp) {
         fprintf(stderr, "Cannot open %s: %s\n",
         cpuinfo, strerror(errno));
         return -1;
     }
     while (fgets(line, sizeof(line), fp) != 0) {
         if (strncmp(line, "cpu model", 9) == 0) {
             /* LOONGSON64  uses a different page_offset. */
         if (strstr(line, "Loongson")) {
                 if (arch_options.core_header_type == CORE_TYPE_ELF64)
                     page_offset = (unsigned long)0xffffffff80000000ULL;
                 cmdline_add_initrd(cmdline, page_offset + initrd_base, 
" rd_start=");
                 cmdline_add_initrd(cmdline, initrd_size, " rd_size=");
             break;
             }
         }
     }
     fclose(fp);
     return 0;
}

for some reasons:
1?kexec -l and kexec -p are two different branches in elf_mips_load().the crash_elf_info structure is used in the crashdump function. Add this part to kexec-elf-mips.c. The main purpose is to not affect the existing code architecture and functionality.

2?The page_offset is not modified through the symbol table similar to arm ?The main purpose is to be able to distinguish more clearly between the cpu.In this way, modifications on loongson platform will not affect other public code and other mips platform.

3?If other mips platform cpu have the same situation,Just add in this function.

Quoted 40 lines
Quoted 40 lines
Quoted 24 lines
Quoted 11 lines
kexec/arch/mips/crashdump-mips.h +0 -0
   #define MAXMEM		0
   #else
   #define PAGE_OFFSET	0x80000000
kexec/arch/mips/kexec-elf-mips.c +0 -0
index a2d11fc..1de418e 100644
@@ -40,6 +40,44 @@ static const int probe_debug = 0;
   #define CMDLINE_PREFIX "kexec "
   static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 51

Quoted 10 lines
kexec/arch/mips/kexec-elf-mips.c +10 -0
+/* Converts unsigned long to ascii string. */
+static void ultoa(unsigned long i, char *str)
+{
+	int j = 0, k;
+	char tmp;
+
+	do {
+		str[j++] = i % 10 + '0';
+	} while ((i /= 10) > 0);
+	str[j] = '\0';

Could the above be achieved using printf?

Quoted 10 lines

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 58

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +6 -0
+	/* Reverse the string. */
+	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
+		tmp = str[k];
+		str[k] = str[j];
+		str[j] = tmp;
+	}

I'm confused as to why the string needs to be reversed.
Could it be avoided by byte-swapping 'i' before rendering it to a string? Thanks a lot for your suggestions on these details.

ultoa() function is used in many places.I see many other platforms have the same implementation like:
/kexec/arch/i386/crashdump-x86.c /kexec/arch/ppc64/crashdump-ppc64.c /kexec/arch/ppc64/crashdump-sh.c

Quoted 4 lines
Quoted 1 line

So, Is it possible to keep the existing implementation ?

Ok, sure.

Thanks.

Quoted 47 lines
Quoted 38 lines
Quoted 28 lines
Quoted 28 lines
kexec/arch/mips/kexec-elf-mips.c +22 -0
+}
+
+/* Adds initrd parameters to command line. */
+static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
+{
+	int cmdlen, len;
+	char str[30], *ptr;
+
+	ptr = str;
+	strcpy(str, new_para);
+	ptr += strlen(str);
+	ultoa(addr, ptr);
+	len = strlen(str);
+	cmdlen = strlen(cmdline) + len;
+	if (cmdlen > (COMMAND_LINE_SIZE - 1))
+		die("Command line overflow\n");
+	strcat(cmdline, str);
+
+	return 0;
+}
+
+
   int elf_mips_probe(const char *buf, off_t len)
   {
   	struct mem_ehdr ehdr;
@@ -171,6 +209,11 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
   		/* Now that the buffer for initrd is prepared, update the dtb
   		 * with an appropriate location */
Quoted 9 lines

kexec/arch/mips/kexec-elf-mips.c int elf_mips_load(int argc, char **argv, const char *buf, off_t len, · line 216

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +5 -0
   		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
+
+		/* Add the initrd parameters to cmdline */
+		cmdline_add_initrd(cmdline_buf, PAGE_OFFSET + initrd_base, " rd_start=");
+		cmdline_add_initrd(cmdline_buf, initrd_size, " rd_size=");
+

Will this be safe for existing use-cases?

Quoted 8 lines

This modification is safe for other existing use-cases.
Now this method is only for the loongson platform to pass the initrd correctly. At this stage, this change is no problem to use on the loongson platform, and it will not affect other platforms.
This part can be further constrained, only for the loongson platform.

let me think about the above and try to find a proper way, and then I will send a v3 patch.

Quoted 8 lines

I think it would be best to avoid compile-time constraints. But if a runtime constraint can ensure this runs only for tested platforms and existing behaviour is preserved for other platforms (if any) then I think we would be in good shape.

Kind regards,
Simon

I totally agree with your suggestion.
As mentioned above, existing modifications are also based on this principle.

now,Just add patch_initrd_info() function in public code, and it only works on the loongson platform at runtime. other platforms remain as they were before.

Changed in v3 patch,please review.
http://lists.infradead.org/pipermail/kexec/2022-May/024797.html

Thanks!

Hui Li

author
#5 lore raw

Hi Simon,

Sent v4 version, compared to the v3 version, only the commit information was modified.

http://lists.infradead.org/pipermail/kexec/2022-June/025130.html

Kind regards,
Hui

-------- Forwarded Message --------
Subject: Re: [PATCH] Add method to pass initrd through cmdline
Date: Thu, 5 May 2022 16:08:29 +0800
From: Hui Li <[email protected]>
To: Simon Horman <[email protected]>
CC: [email protected], [email protected]

Hi Simon Horman

Thanks for your reply.

Quoted 60 lines · Simon Horman
Quoted 59 lines · Hui Li

Hi Simon Horman,

Thanks for your review.

Quoted 11 lines · Simon Horman

Hi Hui Li,

thanks for your patch.

Quoted 6 lines · Hui Li

Problem description:
Under loongson platform,use command:
kexec -l vmlinux... --append="root=UUID=28e1..." --initrd=... kexec -e
quick restart failed.

Quoted 8 lines
Quoted 4 lines

The reason of this problem:
The kernel cannot parse the UUID,UUID is parsed in the initrd. Loongson platform obtain the initrd through cmdline in kernel. In kexec-tools, initrd is not added to cmdline.

Is this common to other platforms?
If not, is there a reason that Loongson takes this approach?

in kernel code,arch/mips/configs/loongson3_defconfig, CONFIG_BLK_DEV_INITRD=y is defined.
In arch/mips/kernel/setup.c file, the following code:

#ifdef CONFIG_BLK_DEV_INITRD

static int __init rd_start_early(char *p)

{
     unsigned long start = memparse(p, &p);
#ifdef CONFIG_64BIT
     /* Guess if the sign extension was forgotten by bootloader */
     if (start < XKPHYS)
         start = (int)start;
#endif
     initrd_start = start;
     initrd_end += start;
     return 0;
}

early_param("rd_start", rd_start_early);

static int __init rd_size_early(char *p)

{
     initrd_end += memparse(p, &p);
     return 0;
}

early_param("rd_size", rd_size_early);
...
...
...
#endif

Quoted 7 lines
Quoted 2 lines

The purpose of parsing initrd parameters through cmdline on the loongson is to compatible with previous platforms

Thanks. I'm very fine with mechanisms that preserve compatibility. But if I understand things correctly then this feature is not specific to Loongson (although it has only been tested on Loongson).

Thanks.
This problem was found on the loongson platform,and seeing mips and other platforms, the approach in this part is really different. I want to solve this problem of the loongson cpu without affecting other platforms.
Therefore, the following modifications are based on this principle.

Quoted 55 lines
Quoted 46 lines

Maybe other platforms use DTB to parse initrd, but it can be seen that the kernel also supports the use of cmdline to parse initrd.

Quoted 43 lines
Quoted 31 lines

The solution:
Add initrd parameter to cmdline. and add a CONFIG_LOONGSON configuration to distinguish PAGE_OFFSET between different platforms under mips.

---
   configure.ac                     |  5 ++++
   kexec/arch/mips/crashdump-mips.h |  6 ++++-
   kexec/arch/mips/kexec-elf-mips.c | 43 ++++++++++++++++++++++++++++++++
   3 files changed, 53 insertions(+), 1 deletion(-)
configure.ac +5 -0
index cf8e8a2..26bfbcd 100644
@@ -111,6 +111,11 @@ AC_ARG_WITH([booke],
   		AC_DEFINE(CONFIG_BOOKE,1,
   			[Define to build for BookE]))
+AC_ARG_WITH([loongson],
+		AC_HELP_STRING([--with-loongson],[build for loongson]),
+		AC_DEFINE(CONFIG_LOONGSON,1,
+			[Define to build for LoongsoN]))
+
   dnl ---Programs
   dnl To specify a different compiler, just 'export CC=/path/to/compiler'
   if test "${build}" != "${host}" ; then
kexec/arch/mips/crashdump-mips.h +0 -0
index 7edd859..d53c696 100644

kexec/arch/mips/crashdump-mips.h struct kexec_info; · line 12

Quoted 9 lines
kexec/arch/mips/crashdump-mips.h +5 -1
@@ -5,7 +5,11 @@ struct kexec_info;
   int load_crashdump_segments(struct kexec_info *info, char *mod_cmdline,
   				unsigned long max_addr, unsigned long min_base);
   #ifdef __mips64
-#define PAGE_OFFSET	0xa800000000000000ULL
+#ifdef CONFIG_LOONGSON
+#define PAGE_OFFSET 0xFFFFFFFF80000000ULL
+#else
+#define PAGE_OFFSET 0xa800000000000000ULL
+#endif

Could this be detected at runtime?

Quoted 8 lines
Quoted 4 lines

It seems awkward to set the PAGE_OFFSET at compile time and thus need separate builds for separate platforms (with no warning if the wrong one is used).

Thank you very much for this good suggestion.
I see that there is a way to get different platform information at runtime via "/proc/cpuinfo", which is used to distinguish PAGE_OFFSET for different platforms.

Quoted 8 lines

Looking at ARM and arm64 implementations of get_kernel_page_offset() they seem to rely on /proc/kallsyms for autodetection of page_offset.

Perhaps something similar could be done for MIPS.

Also, as an asside, it seems at a glance that ARM and adm64 are the same in this regard. So there seems to be some scope for consolidation.

Thanks for the tips.
I analyzed the relevant code of mips and other platforms. It seems that the page_offset will be modified in crashdump-xxx.c

kexec/arch/mips/crashdump-mips.c uses the following method to distinguish the page_offset of different cpus.

static int patch_elf_info(void)

{
     ...
     const char cpuinfo[] = "/proc/cpuinfo";
     ...
     ...
     fp = fopen(cpuinfo, "r");
     ...
     while (fgets(line, sizeof(line), fp) != 0) {
         if (strncmp(line, "cpu model", 9) == 0) {
             /* OCTEON uses a different page_offset. */
             if (strstr(line, "Octeon"))
                 elf_info64.page_offset = 0x8000000000000000ULL;
             break;
         }
     }

}
I will also add the page_offset modification of the loongson CPU here in v3 patch.

But,the modification here is for crash_kernel.

I want to add the following code to kexec/arch/mips/kexec-elf-mips.c.

/* add initrd to cmdline to compatible with previous platforms. */
static int patch_initrd_info(char *cmdline, unsigned long initrd_base, unsigned long initrd_size)

{
     const char cpuinfo[] = "/proc/cpuinfo";
     char line[MAX_LINE];
     FILE *fp;
     unsigned long page_offset = PAGE_OFFSET;

     fp = fopen(cpuinfo, "r");
     if (!fp) {
         fprintf(stderr, "Cannot open %s: %s\n",
         cpuinfo, strerror(errno));
         return -1;
     }
     while (fgets(line, sizeof(line), fp) != 0) {
         if (strncmp(line, "cpu model", 9) == 0) {
             /* LOONGSON64  uses a different page_offset. */
         if (strstr(line, "Loongson")) {
                 if (arch_options.core_header_type == CORE_TYPE_ELF64)
                     page_offset = (unsigned long)0xffffffff80000000ULL;
                 cmdline_add_initrd(cmdline, page_offset + initrd_base, 
" rd_start=");
                 cmdline_add_initrd(cmdline, initrd_size, " rd_size=");
             break;
             }
         }
     }
     fclose(fp);
     return 0;
}

for some reasons:
1、kexec -l and kexec -p are two different branches in elf_mips_load().the crash_elf_info structure is used in the crashdump function. Add this part to kexec-elf-mips.c. The main purpose is to not affect the existing code architecture and functionality.

2、The page_offset is not modified through the symbol table similar to arm ,The main purpose is to be able to distinguish more clearly between the cpu.In this way, modifications on loongson platform will not affect other public code and other mips platform.

3、If other mips platform cpu have the same situation,Just add in this function.

Quoted 40 lines
Quoted 40 lines
Quoted 24 lines
Quoted 11 lines
kexec/arch/mips/crashdump-mips.h +0 -0
   #define MAXMEM		0
   #else
   #define PAGE_OFFSET	0x80000000
kexec/arch/mips/kexec-elf-mips.c +0 -0
index a2d11fc..1de418e 100644
@@ -40,6 +40,44 @@ static const int probe_debug = 0;
   #define CMDLINE_PREFIX "kexec "
   static char cmdline_buf[COMMAND_LINE_SIZE] = CMDLINE_PREFIX;

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 51

Quoted 10 lines
kexec/arch/mips/kexec-elf-mips.c +10 -0
+/* Converts unsigned long to ascii string. */
+static void ultoa(unsigned long i, char *str)
+{
+	int j = 0, k;
+	char tmp;
+
+	do {
+		str[j++] = i % 10 + '0';
+	} while ((i /= 10) > 0);
+	str[j] = '\0';

Could the above be achieved using printf?

Quoted 10 lines

kexec/arch/mips/kexec-elf-mips.c static const int probe_debug = 0; · line 58

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +6 -0
+	/* Reverse the string. */
+	for (j = 0, k = strlen(str) - 1; j < k; j++, k--) {
+		tmp = str[k];
+		str[k] = str[j];
+		str[j] = tmp;
+	}

I'm confused as to why the string needs to be reversed.
Could it be avoided by byte-swapping 'i' before rendering it to a string? Thanks a lot for your suggestions on these details.

ultoa() function is used in many places.I see many other platforms have the same implementation like:
/kexec/arch/i386/crashdump-x86.c /kexec/arch/ppc64/crashdump-ppc64.c /kexec/arch/ppc64/crashdump-sh.c

Quoted 4 lines
Quoted 1 line

So, Is it possible to keep the existing implementation ?

Ok, sure.

Thanks.

Quoted 47 lines
Quoted 38 lines
Quoted 28 lines
Quoted 28 lines
kexec/arch/mips/kexec-elf-mips.c +22 -0
+}
+
+/* Adds initrd parameters to command line. */
+static int cmdline_add_initrd(char *cmdline, unsigned long addr, char *new_para)
+{
+	int cmdlen, len;
+	char str[30], *ptr;
+
+	ptr = str;
+	strcpy(str, new_para);
+	ptr += strlen(str);
+	ultoa(addr, ptr);
+	len = strlen(str);
+	cmdlen = strlen(cmdline) + len;
+	if (cmdlen > (COMMAND_LINE_SIZE - 1))
+		die("Command line overflow\n");
+	strcat(cmdline, str);
+
+	return 0;
+}
+
+
   int elf_mips_probe(const char *buf, off_t len)
   {
   	struct mem_ehdr ehdr;
@@ -171,6 +209,11 @@ int elf_mips_load(int argc, char **argv, const char *buf, off_t len,
   		/* Now that the buffer for initrd is prepared, update the dtb
   		 * with an appropriate location */
Quoted 9 lines

kexec/arch/mips/kexec-elf-mips.c int elf_mips_load(int argc, char **argv, const char *buf, off_t len, · line 216

Quoted 6 lines
kexec/arch/mips/kexec-elf-mips.c +5 -0
   		dtb_set_initrd(&dtb_buf, &dtb_length, initrd_base, initrd_base + initrd_size);
+
+		/* Add the initrd parameters to cmdline */
+		cmdline_add_initrd(cmdline_buf, PAGE_OFFSET + initrd_base, " rd_start=");
+		cmdline_add_initrd(cmdline_buf, initrd_size, " rd_size=");
+

Will this be safe for existing use-cases?

Quoted 8 lines

This modification is safe for other existing use-cases.
Now this method is only for the loongson platform to pass the initrd correctly. At this stage, this change is no problem to use on the loongson platform, and it will not affect other platforms.
This part can be further constrained, only for the loongson platform.

let me think about the above and try to find a proper way, and then I will send a v3 patch.

Quoted 8 lines

I think it would be best to avoid compile-time constraints. But if a runtime constraint can ensure this runs only for tested platforms and existing behaviour is preserved for other platforms (if any) then I think we would be in good shape.

Kind regards,
Simon

I totally agree with your suggestion.
As mentioned above, existing modifications are also based on this principle.

now,Just add patch_initrd_info() function in public code,
and it only works on the loongson platform at runtime. other platforms remain as they were before.

Changed in v3 patch,please review.
http://lists.infradead.org/pipermail/kexec/2022-May/024797.html

Thanks!

Hui Li

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

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