diff mbox series

[v2,2/2] reboot: fix parsing of reboot cpu number

Message ID 20201027133545.58625-3-mcroce@linux.microsoft.com
State New
Headers show
Series fix parsing of reboot= cmdline | expand

Commit Message

Matteo Croce Oct. 27, 2020, 1:35 p.m. UTC
From: Matteo Croce <mcroce@microsoft.com>

The kernel cmdline reboot= argument allows to specify the CPU used
for rebooting, with the syntax `s####` among the other flags, e.g.

  reboot=soft,s4
  reboot=warm,s31,force

In the early days the parsing was done with simple_strtoul(), later
deprecated in favor of the safer kstrtoint() which handles overflow.

But kstrtoint() returns -EINVAL if there are non-digit characters
in a string, so if this flag is not the last given, it's silently
ignored as well as the subsequent ones.

To fix it, revert the usage of simple_strtoul(), which is no longer
deprecated, and restore the old behaviour.

While at it, merge two identical code blocks into one.

Fixes: 616feab75397 ("kernel/reboot.c: convert simple_strtoul to kstrtoint")
Signed-off-by: Matteo Croce <mcroce@microsoft.com>
---
 kernel/reboot.c | 30 ++++++++++++------------------
 1 file changed, 12 insertions(+), 18 deletions(-)

Comments

Greg KH Oct. 27, 2020, 1:42 p.m. UTC | #1
On Tue, Oct 27, 2020 at 02:35:45PM +0100, Matteo Croce wrote:
> From: Matteo Croce <mcroce@microsoft.com>

> 

> The kernel cmdline reboot= argument allows to specify the CPU used

> for rebooting, with the syntax `s####` among the other flags, e.g.

> 

>   reboot=soft,s4

>   reboot=warm,s31,force

> 

> In the early days the parsing was done with simple_strtoul(), later

> deprecated in favor of the safer kstrtoint() which handles overflow.

> 

> But kstrtoint() returns -EINVAL if there are non-digit characters

> in a string, so if this flag is not the last given, it's silently

> ignored as well as the subsequent ones.

> 

> To fix it, revert the usage of simple_strtoul(), which is no longer

> deprecated, and restore the old behaviour.

> 

> While at it, merge two identical code blocks into one.

> 

> Fixes: 616feab75397 ("kernel/reboot.c: convert simple_strtoul to kstrtoint")

> Signed-off-by: Matteo Croce <mcroce@microsoft.com>

> ---

>  kernel/reboot.c | 30 ++++++++++++------------------

>  1 file changed, 12 insertions(+), 18 deletions(-)

> 


<formletter>

This is not the correct way to submit patches for inclusion in the
stable kernel tree.  Please read:
    https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>
Petr Mladek Oct. 30, 2020, 2:30 p.m. UTC | #2
On Tue 2020-10-27 14:35:45, Matteo Croce wrote:
> From: Matteo Croce <mcroce@microsoft.com>
> 
> The kernel cmdline reboot= argument allows to specify the CPU used
> for rebooting, with the syntax `s####` among the other flags, e.g.
> 
>   reboot=soft,s4
>   reboot=warm,s31,force
> 
> In the early days the parsing was done with simple_strtoul(), later
> deprecated in favor of the safer kstrtoint() which handles overflow.
> 
> But kstrtoint() returns -EINVAL if there are non-digit characters
> in a string, so if this flag is not the last given, it's silently
> ignored as well as the subsequent ones.
> 
> To fix it, revert the usage of simple_strtoul(), which is no longer
> deprecated, and restore the old behaviour.
> 
> While at it, merge two identical code blocks into one.

> --- a/kernel/reboot.c
> +++ b/kernel/reboot.c
> @@ -552,25 +552,19 @@ static int __init reboot_setup(char *str)
>  
>  		case 's':
>  		{
> -			int rc;
> -
> -			if (isdigit(*(str+1))) {
> -				rc = kstrtoint(str+1, 0, &reboot_cpu);
> -				if (rc)
> -					return rc;
> -				if (reboot_cpu >= num_possible_cpus()) {
> -					reboot_cpu = 0;
> -					return -ERANGE;
> -				}
> -			} else if (str[1] == 'm' && str[2] == 'p' &&
> -				   isdigit(*(str+3))) {
> -				rc = kstrtoint(str+3, 0, &reboot_cpu);
> -				if (rc)
> -					return rc;
> -				if (reboot_cpu >= num_possible_cpus()) {
> -					reboot_cpu = 0;

                                                     ^^^^^^

> +			int cpu;
> +
> +			/*
> +			 * reboot_cpu is s[mp]#### with #### being the processor
> +			 * to be used for rebooting. Skip 's' or 'smp' prefix.
> +			 */
> +			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
> +
> +			if (isdigit(str[0])) {
> +				cpu = simple_strtoul(str, NULL, 0);
> +				if (cpu >= num_possible_cpus())
>  					return -ERANGE;
> -				}
> +				reboot_cpu = cpu;

The original value stays when the new one is out of range. It is
small functional change that should get mentioned in the commit
message or better fixed separately.

Hmm, I suggest to split this into 3 patches and switch the order:

  + 1st patch should simply revert the commit 616feab75397
   ("kernel/reboot.c: convert simple_strtoul to kstrtoint").

  + 2nd patch should merge the two branches without any
    functional change.

  + 3rd patch should add the check for num_possible_cpus()
    and update the value only when it is valid.

I am sorry that I did not suggested this when reviewed v1.
I have missed this functional change at that time.

Best Regards,
Petr
diff mbox series

Patch

diff --git a/kernel/reboot.c b/kernel/reboot.c
index c4e7965c39b9..a09c5937c0b6 100644
--- a/kernel/reboot.c
+++ b/kernel/reboot.c
@@ -552,25 +552,19 @@  static int __init reboot_setup(char *str)
 
 		case 's':
 		{
-			int rc;
-
-			if (isdigit(*(str+1))) {
-				rc = kstrtoint(str+1, 0, &reboot_cpu);
-				if (rc)
-					return rc;
-				if (reboot_cpu >= num_possible_cpus()) {
-					reboot_cpu = 0;
-					return -ERANGE;
-				}
-			} else if (str[1] == 'm' && str[2] == 'p' &&
-				   isdigit(*(str+3))) {
-				rc = kstrtoint(str+3, 0, &reboot_cpu);
-				if (rc)
-					return rc;
-				if (reboot_cpu >= num_possible_cpus()) {
-					reboot_cpu = 0;
+			int cpu;
+
+			/*
+			 * reboot_cpu is s[mp]#### with #### being the processor
+			 * to be used for rebooting. Skip 's' or 'smp' prefix.
+			 */
+			str += str[1] == 'm' && str[2] == 'p' ? 3 : 1;
+
+			if (isdigit(str[0])) {
+				cpu = simple_strtoul(str, NULL, 0);
+				if (cpu >= num_possible_cpus())
 					return -ERANGE;
-				}
+				reboot_cpu = cpu;
 			} else
 				*mode = REBOOT_SOFT;
 			break;