* Re: [PATCH -next] ata: libata: Fix memory leak for error path in ata_host_alloc() [not found] <20240816035820.2055747-1-zhengqixing@huawei.com> @ 2024-08-16 6:42 ` Yu Kuai 2024-08-16 10:32 ` Damien Le Moal 0 siblings, 1 reply; 3+ messages in thread From: Yu Kuai @ 2024-08-16 6:42 UTC (permalink / raw) To: Zheng Qixing, dlemoal, cassel Cc: linux-ide, linux-kernel, yi.zhang, yangerkun, yukuai (C) 在 2024/08/16 11:58, Zheng Qixing 写道: > In ata_host_alloc(), if ata_port_alloc(host) fails to allocate memory > for a port, the allocated 'host' structure is not freed before returning > from the function. This results in a potential memory leak. > > This patch adds a kfree(host) before the error handling code is executed > to ensure that the 'host' structure is properly freed in case of an > allocation failure. > > Signed-off-by: Zheng Qixing <zhengqixing@huawei.com> > --- > drivers/ata/libata-core.c | 4 +++- > 1 file changed, 3 insertions(+), 1 deletion(-) > > diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c > index e4023fc288ac..f27a18990c38 100644 > --- a/drivers/ata/libata-core.c > +++ b/drivers/ata/libata-core.c > @@ -5663,8 +5663,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int n_ports) > } > > dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); > - if (!dr) > + if (!dr) { > + kfree(host); > goto err_out; > + } Looks correct, dev_set_drvdata(dev, host) is not called yet. ata_devres_release won't free host in this case. I'll suggest to return NULL directly here, and then the 'err_out' tag can be removed as well. Anyway, with or without the cleanup: Reviewed-by: Yu Kuai <yukuai3@huawei.com> Thanks! > > devres_add(dev, dr); > dev_set_drvdata(dev, host); > ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH -next] ata: libata: Fix memory leak for error path in ata_host_alloc() 2024-08-16 6:42 ` [PATCH -next] ata: libata: Fix memory leak for error path in ata_host_alloc() Yu Kuai @ 2024-08-16 10:32 ` Damien Le Moal 2024-08-19 1:17 ` Yu Kuai 0 siblings, 1 reply; 3+ messages in thread From: Damien Le Moal @ 2024-08-16 10:32 UTC (permalink / raw) To: Yu Kuai, Zheng Qixing, cassel Cc: linux-ide, linux-kernel, yi.zhang, yangerkun, yukuai (C) On 8/16/24 15:42, Yu Kuai wrote: > 在 2024/08/16 11:58, Zheng Qixing 写道: >> In ata_host_alloc(), if ata_port_alloc(host) fails to allocate memory >> for a port, the allocated 'host' structure is not freed before returning >> from the function. This results in a potential memory leak. >> >> This patch adds a kfree(host) before the error handling code is executed >> to ensure that the 'host' structure is properly freed in case of an >> allocation failure. >> >> Signed-off-by: Zheng Qixing <zhengqixing@huawei.com> I did not receive this patch and I do not see it on the list either. Something went wrong... Can you resend please ? Thanks. >> --- >> drivers/ata/libata-core.c | 4 +++- >> 1 file changed, 3 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c >> index e4023fc288ac..f27a18990c38 100644 >> --- a/drivers/ata/libata-core.c >> +++ b/drivers/ata/libata-core.c >> @@ -5663,8 +5663,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int n_ports) >> } >> >> dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); >> - if (!dr) >> + if (!dr) { >> + kfree(host); >> goto err_out; >> + } > > Looks correct, dev_set_drvdata(dev, host) is not called yet. > ata_devres_release won't free host in this case. > > I'll suggest to return NULL directly here, and then the 'err_out' > tag can be removed as well. I do not think so. Since devres_open_group() was called, need to call either devres_remove_group() or devres_release_group(). > > Anyway, with or without the cleanup: > Reviewed-by: Yu Kuai <yukuai3@huawei.com>> > Thanks! >> >> devres_add(dev, dr); >> dev_set_drvdata(dev, host); >> > -- Damien Le Moal Western Digital Research ^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH -next] ata: libata: Fix memory leak for error path in ata_host_alloc() 2024-08-16 10:32 ` Damien Le Moal @ 2024-08-19 1:17 ` Yu Kuai 0 siblings, 0 replies; 3+ messages in thread From: Yu Kuai @ 2024-08-19 1:17 UTC (permalink / raw) To: Damien Le Moal, Yu Kuai, Zheng Qixing, cassel Cc: linux-ide, linux-kernel, yi.zhang, yangerkun, yukuai (C) Hi, 在 2024/08/16 18:32, Damien Le Moal 写道: > On 8/16/24 15:42, Yu Kuai wrote: >> 在 2024/08/16 11:58, Zheng Qixing 写道: >>> In ata_host_alloc(), if ata_port_alloc(host) fails to allocate memory >>> for a port, the allocated 'host' structure is not freed before returning >>> from the function. This results in a potential memory leak. >>> >>> This patch adds a kfree(host) before the error handling code is executed >>> to ensure that the 'host' structure is properly freed in case of an >>> allocation failure. >>> >>> Signed-off-by: Zheng Qixing <zhengqixing@huawei.com> > > I did not receive this patch and I do not see it on the list either. Something > went wrong... > Can you resend please ? Thanks. > >>> --- >>> drivers/ata/libata-core.c | 4 +++- >>> 1 file changed, 3 insertions(+), 1 deletion(-) >>> >>> diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c >>> index e4023fc288ac..f27a18990c38 100644 >>> --- a/drivers/ata/libata-core.c >>> +++ b/drivers/ata/libata-core.c >>> @@ -5663,8 +5663,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int n_ports) >>> } >>> >>> dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL); >>> - if (!dr) >>> + if (!dr) { >>> + kfree(host); >>> goto err_out; >>> + } >> >> Looks correct, dev_set_drvdata(dev, host) is not called yet. >> ata_devres_release won't free host in this case. >> >> I'll suggest to return NULL directly here, and then the 'err_out' >> tag can be removed as well. > > I do not think so. Since devres_open_group() was called, need to call either > devres_remove_group() or devres_release_group(). Yes, you're right. Thanks, Kuai > >> >> Anyway, with or without the cleanup: >> Reviewed-by: Yu Kuai <yukuai3@huawei.com>> >> Thanks! >>> >>> devres_add(dev, dr); >>> dev_set_drvdata(dev, host); >>> >> > ^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2024-08-19 1:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
[not found] <20240816035820.2055747-1-zhengqixing@huawei.com>
2024-08-16 6:42 ` [PATCH -next] ata: libata: Fix memory leak for error path in ata_host_alloc() Yu Kuai
2024-08-16 10:32 ` Damien Le Moal
2024-08-19 1:17 ` Yu Kuai
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®