2022-11-19 09:38:25

by Xiu Jianfeng

[permalink] [raw]
Subject: [PATCH 0/2] x86/xen: Fix memory leak issue

The new string allocated by kasprintf() is leaked on error path

Xiu Jianfeng (2):
x86/xen: Fix memory leak in xen_smp_intr_init{_pv}()
x86/xen: Fix memory leak in xen_init_lock_cpu()

arch/x86/xen/smp.c | 16 ++++++++++++----
arch/x86/xen/smp_pv.c | 8 ++++++--
arch/x86/xen/spinlock.c | 3 ++-
3 files changed, 20 insertions(+), 7 deletions(-)

--
2.17.1



2022-11-19 09:48:49

by Xiu Jianfeng

[permalink] [raw]
Subject: [PATCH 2/2] x86/xen: Fix memory leak in xen_init_lock_cpu()

In xen_init_lock_cpu(), the @name has allocated new string by kasprintf(),
if bind_ipi_to_irqhandler() fails, it should be freed, otherwise may lead
to a memory leak issue, fix it.

Fixes: 2d9e1e2f58b5 ("xen: implement Xen-specific spinlocks")
Signed-off-by: Xiu Jianfeng <[email protected]>
---
arch/x86/xen/spinlock.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/arch/x86/xen/spinlock.c b/arch/x86/xen/spinlock.c
index 043c73dfd2c9..156d3e04c9ef 100644
--- a/arch/x86/xen/spinlock.c
+++ b/arch/x86/xen/spinlock.c
@@ -86,7 +86,8 @@ void xen_init_lock_cpu(int cpu)
disable_irq(irq); /* make sure it's never delivered */
per_cpu(lock_kicker_irq, cpu) = irq;
per_cpu(irq_name, cpu) = name;
- }
+ } else
+ kfree(name);

printk("cpu %d spinlock event irq %d\n", cpu, irq);
}
--
2.17.1


2022-11-23 15:51:30

by Jürgen Groß

[permalink] [raw]
Subject: Re: [PATCH 0/2] x86/xen: Fix memory leak issue

On 19.11.22 09:59, Xiu Jianfeng wrote:
> The new string allocated by kasprintf() is leaked on error path
>
> Xiu Jianfeng (2):
> x86/xen: Fix memory leak in xen_smp_intr_init{_pv}()
> x86/xen: Fix memory leak in xen_init_lock_cpu()
>
> arch/x86/xen/smp.c | 16 ++++++++++++----
> arch/x86/xen/smp_pv.c | 8 ++++++--
> arch/x86/xen/spinlock.c | 3 ++-
> 3 files changed, 20 insertions(+), 7 deletions(-)
>

Hmm, I think it would make more sense to always store the name generated
via kasprintf() in the percpu variable (independently from succeeding to
bind the irq), and in the related free function to always kfree() it and
set it to NULL again.

This would result in less code.


Juergen


Attachments:
OpenPGP_0xB0DE9DD628BF132F.asc (3.08 kB)
OpenPGP public key
OpenPGP_signature (505.00 B)
OpenPGP digital signature
Download all attachments

2022-11-23 16:33:10

by Xiu Jianfeng

[permalink] [raw]
Subject: Re: [PATCH 0/2] x86/xen: Fix memory leak issue

Hi,

在 2022/11/23 23:23, Juergen Gross 写道:
> On 19.11.22 09:59, Xiu Jianfeng wrote:
>> The new string allocated by kasprintf() is leaked on error path
>>
>> Xiu Jianfeng (2):
>>    x86/xen: Fix memory leak in xen_smp_intr_init{_pv}()
>>    x86/xen: Fix memory leak in xen_init_lock_cpu()
>>
>>   arch/x86/xen/smp.c      | 16 ++++++++++++----
>>   arch/x86/xen/smp_pv.c   |  8 ++++++--
>>   arch/x86/xen/spinlock.c |  3 ++-
>>   3 files changed, 20 insertions(+), 7 deletions(-)
>>
>
> Hmm, I think it would make more sense to always store the name generated
> via kasprintf() in the percpu variable (independently from succeeding to
> bind the irq), and in the related free function to always kfree() it and
> set it to NULL again.
>
> This would result in less code.

Thanks, It's good to me, already sent v2.
>
>
> Juergen