Re: [PATCH 1/1] clk: tegra: tegra124-emc: Fix possible memory leak
From: Leizhen (ThunderTown)
Date: Fri Jun 25 2021 - 21:33:05 EST
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@xxxxxxxxxx>
>> ---
>
> 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@xxxxxxxxxx>
>
> .
>