linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
@ 2021-06-17  8:27 Zhen Lei
  2021-06-25 23:31 ` Stephen Boyd
  0 siblings, 1 reply; 6+ messages in thread
From: Zhen Lei @ 2021-06-17  8:27 UTC (permalink / raw)
  To: Peter De Schrijver, Prashant Gaikwad, Michael Turquette,
	Stephen Boyd, Thierry Reding, Jonathan Hunter, Dmitry Osipenko,
	linux-clk, linux-tegra, linux-kernel
  Cc: Zhen Lei

When krealloc() fails to expand the memory and returns NULL, the original
memory is not released. In this case, the original "timings" scale should
be maintained.

Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
---
 drivers/clk/tegra/clk-tegra124-emc.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/drivers/clk/tegra/clk-tegra124-emc.c b/drivers/clk/tegra/clk-tegra124-emc.c
index 74c1d894cca8..1a329202b543 100644
--- a/drivers/clk/tegra/clk-tegra124-emc.c
+++ b/drivers/clk/tegra/clk-tegra124-emc.c
@@ -450,11 +450,12 @@ static int load_timings_from_dt(struct tegra_clk_emc *tegra,
 
 	size = (tegra->num_timings + child_count) * sizeof(struct emc_timing);
 
-	tegra->timings = krealloc(tegra->timings, size, GFP_KERNEL);
-	if (!tegra->timings)
+	timings_ptr = krealloc(tegra->timings, size, GFP_KERNEL);
+	if (!timings_ptr)
 		return -ENOMEM;
 
-	timings_ptr = tegra->timings + tegra->num_timings;
+	tegra->timings = timings_ptr;
+	timings_ptr += tegra->num_timings;
 	tegra->num_timings += child_count;
 
 	for_each_child_of_node(node, child) {
-- 
2.25.1



^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
  2021-06-17  8:27 [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak Zhen Lei
@ 2021-06-25 23:31 ` Stephen Boyd
  2021-06-26  1:32   ` Leizhen (ThunderTown)
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Boyd @ 2021-06-25 23:31 UTC (permalink / raw)
  To: Dmitry Osipenko, Jonathan Hunter, Michael Turquette,
	Peter De Schrijver, Prashant Gaikwad, Thierry Reding, Zhen Lei,
	linux-clk, linux-kernel, linux-tegra
  Cc: Zhen Lei

Quoting Zhen Lei (2021-06-17 01:27:59)
> When krealloc() fails to expand the memory and returns NULL, the original
> memory is not released. In this case, the original "timings" scale should
> be maintained.
> 
> Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> ---

Looks correct, but when does krealloc() return NULL? My read of the
kerneldoc is that it would return the original memory if the new
allocation "failed".

Reviewed-by: Stephen Boyd <sboyd@kernel.org>

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
  2021-06-25 23:31 ` Stephen Boyd
@ 2021-06-26  1:32   ` Leizhen (ThunderTown)
  2021-06-27 23:44     ` Stephen Boyd
  0 siblings, 1 reply; 6+ messages in thread
From: Leizhen (ThunderTown) @ 2021-06-26  1:32 UTC (permalink / raw)
  To: Stephen Boyd, Dmitry Osipenko, Jonathan Hunter,
	Michael Turquette, Peter De Schrijver, Prashant Gaikwad,
	Thierry Reding, linux-clk, linux-kernel, linux-tegra



On 2021/6/26 7:31, Stephen Boyd wrote:
> Quoting Zhen Lei (2021-06-17 01:27:59)
>> When krealloc() fails to expand the memory and returns NULL, the original
>> memory is not released. In this case, the original "timings" scale should
>> be maintained.
>>
>> Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>> ---
> 
> Looks correct, but when does krealloc() return NULL? My read of the
> kerneldoc is that it would return the original memory if the new
> allocation "failed".

That must be the wrong description in the document. For example, the original
100-byte memory needs to be expanded to 200 bytes. If the memory allocation fails,
a non-null pointer is returned. People must think they've applied for it and
continue to use it, the end result is memory crashed.

I don't think the kernel needs to be different from libc's realloc().

The implementation of __do_krealloc() illustrates this as well:
        /* If the object still fits, repoison it precisely. */
        if (ks >= new_size) {
                p = kasan_krealloc((void *)p, new_size, flags);
                return (void *)p;
        }

        ret = kmalloc_track_caller(new_size, flags);			//enlarge, allocate new memory
	if (ret && p) {
                memcpy(ret, kasan_reset_tag(p), ks);			//copy the old content from 'p' to new memory 'ret'
        }

	return ret;							//ret may be NULL, if kmalloc_track_caller() failed



> 
> Reviewed-by: Stephen Boyd <sboyd@kernel.org>
> 
> .
> 


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
  2021-06-26  1:32   ` Leizhen (ThunderTown)
@ 2021-06-27 23:44     ` Stephen Boyd
  2021-06-28  0:29       ` Dmitry Osipenko
  0 siblings, 1 reply; 6+ messages in thread
From: Stephen Boyd @ 2021-06-27 23:44 UTC (permalink / raw)
  To: Dmitry Osipenko, Jonathan Hunter, Leizhen, Michael Turquette,
	Peter De Schrijver, Prashant Gaikwad, Thierry Reding, linux-clk,
	linux-kernel, linux-tegra

Quoting Leizhen (ThunderTown) (2021-06-25 18:32:46)
> 
> 
> On 2021/6/26 7:31, Stephen Boyd wrote:
> > Quoting Zhen Lei (2021-06-17 01:27:59)
> >> When krealloc() fails to expand the memory and returns NULL, the original
> >> memory is not released. In this case, the original "timings" scale should
> >> be maintained.
> >>
> >> Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
> >> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> >> ---
> > 
> > Looks correct, but when does krealloc() return NULL? My read of the
> > kerneldoc is that it would return the original memory if the new
> > allocation "failed".
> 
> That must be the wrong description in the document. For example, the original

Can you fix the kernel doc then?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
  2021-06-27 23:44     ` Stephen Boyd
@ 2021-06-28  0:29       ` Dmitry Osipenko
  2021-06-28  1:52         ` Stephen Boyd
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Osipenko @ 2021-06-28  0:29 UTC (permalink / raw)
  To: Stephen Boyd, Jonathan Hunter, Leizhen (ThunderTown),
	Michael Turquette, Peter De Schrijver, Prashant Gaikwad,
	Thierry Reding, linux-clk, linux-kernel, linux-tegra

28.06.2021 02:44, Stephen Boyd пишет:
> Quoting Leizhen (ThunderTown) (2021-06-25 18:32:46)
>>
>>
>> On 2021/6/26 7:31, Stephen Boyd wrote:
>>> Quoting Zhen Lei (2021-06-17 01:27:59)
>>>> When krealloc() fails to expand the memory and returns NULL, the original
>>>> memory is not released. In this case, the original "timings" scale should
>>>> be maintained.
>>>>
>>>> Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")

The memory is still not released on error and this is not the only one
place in that code which doesn't release memory on error.

All this code is executed only once during early kernel boot, perhaps
not really worthwhile fixing it or at least this should be done properly.

>>>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
>>>> ---
>>>
>>> Looks correct, but when does krealloc() return NULL? My read of the
>>> kerneldoc is that it would return the original memory if the new
>>> allocation "failed".
>>
>> That must be the wrong description in the document. For example, the original
> 
> Can you fix the kernel doc then?
> 

The doc is clearly saying that it returns NULL, am I missing something?

* Return: pointer to the allocated memory or %NULL in case of error

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
  2021-06-28  0:29       ` Dmitry Osipenko
@ 2021-06-28  1:52         ` Stephen Boyd
  0 siblings, 0 replies; 6+ messages in thread
From: Stephen Boyd @ 2021-06-28  1:52 UTC (permalink / raw)
  To: Dmitry Osipenko, Jonathan Hunter, Leizhen, Michael Turquette,
	Peter De Schrijver, Prashant Gaikwad, Thierry Reding, linux-clk,
	linux-kernel, linux-tegra

Quoting Dmitry Osipenko (2021-06-27 17:29:20)
> 28.06.2021 02:44, Stephen Boyd пишет:
> > Quoting Leizhen (ThunderTown) (2021-06-25 18:32:46)
> >>
> >>
> >> On 2021/6/26 7:31, Stephen Boyd wrote:
> >>> Quoting Zhen Lei (2021-06-17 01:27:59)
> >>>> When krealloc() fails to expand the memory and returns NULL, the original
> >>>> memory is not released. In this case, the original "timings" scale should
> >>>> be maintained.
> >>>>
> >>>> Fixes: 888ca40e2843 ("clk: tegra: emc: Support multiple RAM codes")
> 
> The memory is still not released on error and this is not the only one
> place in that code which doesn't release memory on error.
> 
> All this code is executed only once during early kernel boot, perhaps
> not really worthwhile fixing it or at least this should be done properly.
> 
> >>>> Signed-off-by: Zhen Lei <thunder.leizhen@huawei.com>
> >>>> ---
> >>>
> >>> Looks correct, but when does krealloc() return NULL? My read of the
> >>> kerneldoc is that it would return the original memory if the new
> >>> allocation "failed".
> >>
> >> That must be the wrong description in the document. For example, the original
> > 
> > Can you fix the kernel doc then?
> > 
> 
> The doc is clearly saying that it returns NULL, am I missing something?
> 
> * Return: pointer to the allocated memory or %NULL in case of error

See the paragraph

 * The contents of the object pointed to are preserved up to the
 * lesser of the new and old sizes (__GFP_ZERO flag is effectively ignored). 

which confused me into thinking the memory that is being reallocated is
guaranteed to be preserved so it would be returned if the larger size
failed. I suppose there's nothing to fix in the kernel-doc, just my
understanding of that paragraph. Maybe it should say

	The __GFP_ZERO flag is effectively ignored as the contents of
	the objected pointed to @p are preserved up to the lesser of the
	@new_size and old size.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2021-06-28  1:53 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-06-17  8:27 [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak Zhen Lei
2021-06-25 23:31 ` Stephen Boyd
2021-06-26  1:32   ` Leizhen (ThunderTown)
2021-06-27 23:44     ` Stephen Boyd
2021-06-28  0:29       ` Dmitry Osipenko
2021-06-28  1:52         ` Stephen Boyd

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).