[v3] x86/xen: Add some null pointer checking to smp.c

Message ID 20240119094948.275390-1-chentao@kylinos.cn
State New
Headers
Series [v3] x86/xen: Add some null pointer checking to smp.c |

Commit Message

Kunwu Chan Jan. 19, 2024, 9:49 a.m. UTC
  kasprintf() returns a pointer to dynamically allocated memory
which can be NULL upon failure. Ensure the allocation was successful
by checking the pointer validity.

Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202401161119.iof6BQsf-lkp@intel.com/
Suggested-by: Markus Elfring <Markus.Elfring@web.de>
---
Changes in v3:
    - Remove rc initialization
    - Simply error paths by adding a new label 'fail_mem'
Changes in v2:
    - Initial rc and return errno in error paths
---
 arch/x86/xen/smp.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
  

Comments

Markus Elfring Jan. 19, 2024, 10:40 a.m. UTC | #1
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.> ---
> Changes in v3:
>     - Remove rc initialization
>     - Simply error paths by adding a new label 'fail_mem'
…

I became curious if you would like to simplify further source code places.


> +++ b/arch/x86/xen/smp.c
> @@ -65,6 +65,8 @@ int xen_smp_intr_init(unsigned int cpu)
>  	char *resched_name, *callfunc_name, *debug_name;
>
>  	resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
> +	if (!resched_name)
> +		goto fail_mem;

Would you like to add a blank line after such a statement?


>  	per_cpu(xen_resched_irq, cpu).name = resched_name;
…

Please compare with your subsequent suggestion.

…
> @@ -101,6 +108,9 @@ int xen_smp_intr_init(unsigned int cpu)
>  	}
>
>  	callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
> +	if (!callfunc_name)
> +		goto fail_mem;
> +
>  	per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
…

Regards,
Markus
  
Markus Elfring Jan. 20, 2024, 2:45 p.m. UTC | #2
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.

How do you think about to refer to the function name
instead of the file name in the patch subject?


…
> +++ b/arch/x86/xen/smp.c> @@ -114,6 +124,8 @@ int xen_smp_intr_init(unsigned int cpu)
>
>  	return 0;
>
> + fail_mem:
> +	rc = -ENOMEM;
>   fail:
>  	xen_smp_intr_free(cpu);
>  	return rc;

Is it currently preferred to start labels in the first text column?

Regards,
Markus
  
Kunwu Chan Jan. 22, 2024, 7:43 a.m. UTC | #3
On 2024/1/20 22:45, Markus Elfring wrote:
>> kasprintf() returns a pointer to dynamically allocated memory
>> which can be NULL upon failure. Ensure the allocation was successful
>> by checking the pointer validity.
> 
> How do you think about to refer to the function name
> instead of the file name in the patch subject?
> 
The main goal is to assign a errno to rc. So use 'fail_mem is good to 
understand.
> 
> …
>> +++ b/arch/x86/xen/smp.c
> …
>> @@ -114,6 +124,8 @@ int xen_smp_intr_init(unsigned int cpu)
>>
>>   	return 0;
>>
>> + fail_mem:
>> +	rc = -ENOMEM;
>>    fail:
>>   	xen_smp_intr_free(cpu);
>>   	return rc;
> 
> Is it currently preferred to start labels in the first text column?
Just the same as the old one. I could fix it if necessary.

> 
> Regards,
> Markus
  
Kunwu Chan Jan. 22, 2024, 7:47 a.m. UTC | #4
On 2024/1/19 18:40, Markus Elfring wrote:
>> kasprintf() returns a pointer to dynamically allocated memory
>> which can be NULL upon failure. Ensure the allocation was successful
>> by checking the pointer validity.
> …
>> ---
>> Changes in v3:
>>      - Remove rc initialization
>>      - Simply error paths by adding a new label 'fail_mem'
> …
> 
> I became curious if you would like to simplify further source code places.
This function hasn't changed in years, so it's OK for now.
> 
> 
>> +++ b/arch/x86/xen/smp.c
>> @@ -65,6 +65,8 @@ int xen_smp_intr_init(unsigned int cpu)
>>   	char *resched_name, *callfunc_name, *debug_name;
>>
>>   	resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
>> +	if (!resched_name)
>> +		goto fail_mem;
> 
> Would you like to add a blank line after such a statement?
Sure, I could do it in next patch.
> 
> 
>>   	per_cpu(xen_resched_irq, cpu).name = resched_name;
> …
> 
> Please compare with your subsequent suggestion.
I’ve seend a reply.
> 
> …
>> @@ -101,6 +108,9 @@ int xen_smp_intr_init(unsigned int cpu)
>>   	}
>>
>>   	callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
>> +	if (!callfunc_name)
>> +		goto fail_mem;
>> +
>>   	per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
> …
> 
> Regards,
> Markus
  
Markus Elfring Jan. 22, 2024, 9:50 a.m. UTC | #5
>> How do you think about to refer to the function name
>> instead of the file name in the patch subject?
>>
> The main goal is to assign a errno to rc. So use 'fail_mem is good to understand.

You responded with information which can fit to the patch body.

How do you think about consequences for a subject variant like the following?

x86/xen: Add some null pointer checks in xen_smp_intr_init()

Regards,
Markus
  
Juergen Gross Feb. 12, 2024, 5:07 p.m. UTC | #6
On 19.01.24 10:49, Kunwu Chan wrote:
> kasprintf() returns a pointer to dynamically allocated memory
> which can be NULL upon failure. Ensure the allocation was successful
> by checking the pointer validity.
> 
> Signed-off-by: Kunwu Chan <chentao@kylinos.cn>
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/oe-kbuild-all/202401161119.iof6BQsf-lkp@intel.com/
> Suggested-by: Markus Elfring <Markus.Elfring@web.de>

Reviewed-by: Juergen Gross <jgross@suse.com>


Juergen
  

Patch

diff --git a/arch/x86/xen/smp.c b/arch/x86/xen/smp.c
index 4b0d6fff88de..1fb9a1644d94 100644
--- a/arch/x86/xen/smp.c
+++ b/arch/x86/xen/smp.c
@@ -65,6 +65,8 @@  int xen_smp_intr_init(unsigned int cpu)
 	char *resched_name, *callfunc_name, *debug_name;
 
 	resched_name = kasprintf(GFP_KERNEL, "resched%d", cpu);
+	if (!resched_name)
+		goto fail_mem;
 	per_cpu(xen_resched_irq, cpu).name = resched_name;
 	rc = bind_ipi_to_irqhandler(XEN_RESCHEDULE_VECTOR,
 				    cpu,
@@ -77,6 +79,8 @@  int xen_smp_intr_init(unsigned int cpu)
 	per_cpu(xen_resched_irq, cpu).irq = rc;
 
 	callfunc_name = kasprintf(GFP_KERNEL, "callfunc%d", cpu);
+	if (!callfunc_name)
+		goto fail_mem;
 	per_cpu(xen_callfunc_irq, cpu).name = callfunc_name;
 	rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_VECTOR,
 				    cpu,
@@ -90,6 +94,9 @@  int xen_smp_intr_init(unsigned int cpu)
 
 	if (!xen_fifo_events) {
 		debug_name = kasprintf(GFP_KERNEL, "debug%d", cpu);
+		if (!debug_name)
+			goto fail_mem;
+
 		per_cpu(xen_debug_irq, cpu).name = debug_name;
 		rc = bind_virq_to_irqhandler(VIRQ_DEBUG, cpu,
 					     xen_debug_interrupt,
@@ -101,6 +108,9 @@  int xen_smp_intr_init(unsigned int cpu)
 	}
 
 	callfunc_name = kasprintf(GFP_KERNEL, "callfuncsingle%d", cpu);
+	if (!callfunc_name)
+		goto fail_mem;
+
 	per_cpu(xen_callfuncsingle_irq, cpu).name = callfunc_name;
 	rc = bind_ipi_to_irqhandler(XEN_CALL_FUNCTION_SINGLE_VECTOR,
 				    cpu,
@@ -114,6 +124,8 @@  int xen_smp_intr_init(unsigned int cpu)
 
 	return 0;
 
+ fail_mem:
+	rc = -ENOMEM;
  fail:
 	xen_smp_intr_free(cpu);
 	return rc;