Skip to contentSkip to conversation

s390/boot: Fix couple of early boot edge cases

PATCH In review Vasily Gorbik 3 participants 11 messages Aug 27 s390

lore
Share & tools
lore

Vasily Gorbik

author maintainer
#0 lore raw

cover s390/boot: Fix couple of early boot edge cases

Fix three independent minor early-boot issues involving the physical memory search range, full command lines with IPL parameters, and unbounded facility ranges.

Vasily Gorbik (3):
s390/boot: Fix physical memory search range s390/boot: Avoid IPL parameter append past command line s390/boot: Bound command line facility ranges

 arch/s390/boot/alternative.c  |  5 +++--
 arch/s390/boot/ipl_parm.c     | 23 ++++++++++++-----------
 arch/s390/boot/physmem_info.c |  2 +-
 3 files changed, 16 insertions(+), 14 deletions(-)
Signature · 1 line
2.53.0
author maintainer
#1 lore raw

PATCH 1/3 s390/boot: Fix physical memory search range

search_mem_end() calculates the number of 1MB blocks with a signed int literal. CONFIG_MAX_PHYSMEM_BITS values of 51 and above either overflow the signed int or shift beyond its width. This produces an invalid search range when the binary-search memory detection fallback is used.

Use an unsigned long literal so the full supported physical address range is represented.

Fixes 4d14c2666b6b ("s390/mem_detect: replace tprot loop with binary search")
---
 arch/s390/boot/physmem_info.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/s390/boot/physmem_info.c +1 -1
index 1f2ca5435838..0ebb2174713f 100644
@@ -141,7 +141,7 @@ static int tprot(unsigned long addr)
 
 static unsigned long search_mem_end(void)
 {
-	unsigned long range = 1 << (MAX_PHYSMEM_BITS - 20); /* in 1MB blocks */
+	unsigned long range = 1UL << (MAX_PHYSMEM_BITS - 20); /* in 1MB blocks */
 	unsigned long offset = 0;
 	unsigned long pivot;
 
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH 1/3] s390/boot: Fix physical memory search range

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 10 lines

search_mem_end() calculates the number of 1MB blocks with a signed int literal. CONFIG_MAX_PHYSMEM_BITS values of 51 and above either overflow the signed int or shift beyond its width. This produces an invalid search range when the binary-search memory detection fallback is used.

Use an unsigned long literal so the full supported physical address range is represented.

Fixes 4d14c2666b6b ("s390/mem_detect: replace tprot loop with binary search")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Reviewed-by on v1 1/3
#3 lore raw

Re: [PATCH 1/3] s390/boot: Fix physical memory search range

In reply to Vasily Gorbik
Quoted 5 lines · Vasily Gorbik

search_mem_end() calculates the number of 1MB blocks with a signed int literal. CONFIG_MAX_PHYSMEM_BITS values of 51 and above either overflow the signed int or shift beyond its width. This produces an invalid search range when the binary-search memory detection fallback is used.

Quoted 8 lines

Use an unsigned long literal so the full supported physical address range is represented.

Fixes 4d14c2666b6b ("s390/mem_detect: replace tprot loop with binary search")
---
 arch/s390/boot/physmem_info.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
author maintainer
#4 lore raw

PATCH 2/3 s390/boot: Avoid IPL parameter append past command line

In reply to Vasily Gorbik

A command line may occupy all but the terminating byte of COMMAND_LINE_SIZE. In that case append_ipl_block_parm() passes a zero size to the IPL parameter conversion helpers and points the destination one byte past early_command_line. The helpers subtract one from the unsigned size and write the converted parameter outside the command line buffer.

Convert the IPL parameter in the command line parsing buffer first. A parameter beginning with '=' can then replace the existing command line regardless of its length, while other parameters are appended only when space remains.

Fixes 5ecb2da660ab ("s390: support command lines longer than 896 bytes")
---
 arch/s390/boot/ipl_parm.c | 21 +++++++++++----------
 1 file changed, 11 insertions(+), 10 deletions(-)
arch/s390/boot/ipl_parm.c +11 -10
index 6bc950b92be7..8aa7639ae0cb 100644
@@ -23,6 +23,7 @@ struct parmarea parmarea __section(".parmarea") = {
 };
 
 char __bootdata(early_command_line)[COMMAND_LINE_SIZE];
+static char command_line_buf[COMMAND_LINE_SIZE];
 
 unsigned int __bootdata_preserved(zlib_dfltcc_support) = ZLIB_DFLTCC_FULL;
 struct ipl_parameter_block __bootdata_preserved(ipl_block);
@@ -135,31 +136,32 @@ static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size,
 
 static void append_ipl_block_parm(void)
 {
-	char *parm, *delim;
+	char *delim;
 	size_t len, rc = 0;
 
 	len = strlen(early_command_line);
-
-	delim = early_command_line + len;    /* '\0' character position */
-	parm = early_command_line + len + 1; /* append right after '\0' */
+	delim = early_command_line + len; /* '\0' character position */
 
 	switch (ipl_block.pb0_hdr.pbt) {
 	case IPL_PBT_CCW:
 		rc = ipl_block_get_ascii_vmparm(
-			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
+			command_line_buf, sizeof(command_line_buf), &ipl_block);
 		break;
 	case IPL_PBT_FCP:
 	case IPL_PBT_NVME:
 	case IPL_PBT_ECKD:
 		rc = ipl_block_get_ascii_scpdata(
-			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
+			command_line_buf, sizeof(command_line_buf), &ipl_block);
 		break;
 	}
 	if (rc) {
-		if (*parm == '=')
-			memmove(early_command_line, parm + 1, rc);
-		else
+		if (command_line_buf[0] == '=') {
+			memmove(early_command_line, command_line_buf + 1, rc);
+		} else if (len < COMMAND_LINE_SIZE - 2) {
 			*delim = ' '; /* replace '\0' with space */
+			sized_strscpy(delim + 1, command_line_buf,
+				       COMMAND_LINE_SIZE - len - 1);
+		}
 	}
 }
 
@@ -245,7 +247,6 @@ static void modify_fac_list(char *str)
 	check_cleared_facilities();
 }
 
-static char command_line_buf[COMMAND_LINE_SIZE];
 void parse_boot_command_line(void)
 {
 	char *param, *val;
Signature · 1 line
2.53.0
#5 lore raw

Re: [PATCH 2/3] s390/boot: Avoid IPL parameter append past command line

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

A command line may occupy all but the terminating byte of COMMAND_LINE_SIZE. In that case append_ipl_block_parm() passes a zero size to the IPL parameter conversion helpers and points the destination one byte past early_command_line. The helpers subtract one from the unsigned size and write the converted parameter outside the command line buffer.

Quoted 7 lines

Convert the IPL parameter in the command line parsing buffer first. A parameter beginning with '=' can then replace the existing command line regardless of its length, while other parameters are appended only when space remains.

Fixes 5ecb2da660ab ("s390: support command lines longer than 896 bytes")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Reviewed-by on v1 2/3
#6 lore raw

Re: [PATCH 2/3] s390/boot: Avoid IPL parameter append past command line

In reply to Vasily Gorbik

arch/s390/boot/ipl_parm.c static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size, · line 140

Quoted 5 lines · Vasily Gorbik

static void append_ipl_block_parm(void)
{

arch/s390/boot/ipl_parm.c +1 -1
-	char *parm, *delim;
+	char *delim;
 	size_t len, rc = 0;

Reverse x-mas tree?

Quoted 5 lines
arch/s390/boot/ipl_parm.c +1 -1
 	switch (ipl_block.pb0_hdr.pbt) {
 	case IPL_PBT_CCW:
 		rc = ipl_block_get_ascii_vmparm(
-			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
+			command_line_buf, sizeof(command_line_buf), &ipl_block);

...

arch/s390/boot/ipl_parm.c static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size, · line 154

Quoted 3 lines
arch/s390/boot/ipl_parm.c +1 -1
 		rc = ipl_block_get_ascii_scpdata(
-			parm, COMMAND_LINE_SIZE - len - 1, &ipl_block);
+			command_line_buf, sizeof(command_line_buf), &ipl_block);

Both could be long single line statements. :)

arch/s390/boot/ipl_parm.c static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size, · line 159

Quoted 6 lines
arch/s390/boot/ipl_parm.c +2 -3
 	if (rc) {
-		if (*parm == '=')
-			memmove(early_command_line, parm + 1, rc);
-		else
+		if (command_line_buf[0] == '=') {
+			memmove(early_command_line, command_line_buf + 1, rc);

Would it be possible to use something different than "rc" as name for the variable? It is actually a length. Maybe something like "append", or "extra"? That would make the code easier to read.

arch/s390/boot/ipl_parm.c static size_t ipl_block_get_ascii_scpdata(char *dest, size_t size, · line 163

Quoted 4 lines
arch/s390/boot/ipl_parm.c +3 -0
+		} else if (len < COMMAND_LINE_SIZE - 2) {
 			*delim = ' '; /* replace '\0' with space */
+			sized_strscpy(delim + 1, command_line_buf,
+				       COMMAND_LINE_SIZE - len - 1);

One line?

Anyway, feel free to ignore the style comments. Code looks good.

author maintainer
#7 lore raw

PATCH 3/3 s390/boot: Bound command line facility ranges

In reply to Vasily Gorbik

The facilities and debug-alternative command line parsers iterate over inclusive numeric ranges. If a range ends at ULONG_MAX, incrementing the current value wraps to zero and the loop never terminates. Large finite out-of-range values also cause unnecessary early boot iterations even though the bitmap helpers ignore them.

Stop each loop at the size of the bitmap it modifies. This preserves all meaningful range values while guaranteeing termination.

Fixes b5e804598d59 ("s390: allow overriding facilities via command line")
Fixes 15c33a5bc9b8 ("s390/alternatives: Add debug functionality")
---
 arch/s390/boot/alternative.c | 5 +++--
 arch/s390/boot/ipl_parm.c    | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
arch/s390/boot/alternative.c +3 -2
index 19ea7934b918..77e8bad560c5 100644
@@ -45,11 +45,12 @@ static void alt_debug_modify(int type, unsigned int nr, bool clear)
 
 static char *alt_debug_parse(int type, char *str)
 {
-	unsigned long val, endval;
+	unsigned long val, endval, limit;
 	char *endp;
 	bool clear;
 	int i;
 
+	limit = type == ALT_TYPE_FACILITY ? MAX_FACILITY_BIT : MAX_MFEATURE_BIT;
 	if (*str == ':') {
 		str++;
 	} else {
@@ -73,7 +74,7 @@ static char *alt_debug_parse(int type, char *str)
 			if (str == endp)
 				break;
 			str = endp;
-			while (val <= endval) {
+			while (val <= endval && val < limit) {
 				alt_debug_modify(type, val, clear);
 				val++;
 			}
arch/s390/boot/ipl_parm.c +1 -1
index 8aa7639ae0cb..6d8b9d61bcad 100644
@@ -233,7 +233,7 @@ static void modify_fac_list(char *str)
 			if (str == endp)
 				break;
 			str = endp;
-			while (val <= endval) {
+			while (val <= endval && val < MAX_FACILITY_BIT) {
 				modify_facility(val, clear);
 				val++;
 			}
Signature · 1 line
2.53.0
#8 lore raw

Re: [PATCH 3/3] s390/boot: Bound command line facility ranges

Sashiko has reviewed this patch and found no issues. It looks great!
Quoted 6 lines

The facilities and debug-alternative command line parsers iterate over inclusive numeric ranges. If a range ends at ULONG_MAX, incrementing the current value wraps to zero and the loop never terminates. Large finite out-of-range values also cause unnecessary early boot iterations even though the bitmap helpers ignore them.

Quoted 6 lines

Stop each loop at the size of the bitmap it modifies. This preserves all meaningful range values while guaranteeing termination.

Fixes b5e804598d59 ("s390: allow overriding facilities via command line")
Fixes 15c33a5bc9b8 ("s390/alternatives: Add debug functionality")

Sashiko has reviewed this patch and found no issues. It looks great!

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
maintainer Reviewed-by on v1 3/3
#9 lore raw

Re: [PATCH 3/3] s390/boot: Bound command line facility ranges

In reply to Vasily Gorbik
Quoted 11 lines · Vasily Gorbik

The facilities and debug-alternative command line parsers iterate over inclusive numeric ranges. If a range ends at ULONG_MAX, incrementing the current value wraps to zero and the loop never terminates. Large finite out-of-range values also cause unnecessary early boot iterations even though the bitmap helpers ignore them.

Stop each loop at the size of the bitmap it modifies. This preserves all meaningful range values while guaranteeing termination.

Fixes b5e804598d59 ("s390: allow overriding facilities via command line")
Fixes 15c33a5bc9b8 ("s390/alternatives: Add debug functionality")

This is quite esoteric. Not sure if this justifies Fixes tags.

Quoted 5 lines
---
 arch/s390/boot/alternative.c | 5 +++--
 arch/s390/boot/ipl_parm.c    | 2 +-
 2 files changed, 4 insertions(+), 3 deletions(-)
author maintainer
#10 lore raw
In reply to Vasily Gorbik
Quoted 9 lines · Vasily Gorbik

Fix three independent minor early-boot issues involving the physical memory search range, full command lines with IPL parameters, and unbounded facility ranges.

Vasily Gorbik (3):
s390/boot: Fix physical memory search range s390/boot: Avoid IPL parameter append past command line s390/boot: Bound command line facility ranges

Quoted 4 lines
 arch/s390/boot/alternative.c  |  5 +++--
 arch/s390/boot/ipl_parm.c     | 23 ++++++++++++-----------
 arch/s390/boot/physmem_info.c |  2 +-
 3 files changed, 16 insertions(+), 14 deletions(-)

Adopted Heiko's proposed style changes, dropped Fixes for patch 3 and applied