lkml.org 
[lkml]   [2014]   [Oct]   [16]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
Patch in this message
/
From
SubjectRe: krealloc in kernel/params.c
Date
Rasmus Villemoes <linux@rasmusvillemoes.dk> writes:
> On Wed, Oct 15 2014, Rusty Russell <rusty@rustcorp.com.au> wrote:
>
>> Rasmus Villemoes <linux@rasmusvillemoes.dk> writes:
>> The kzalloc-then-always-krealloc pattern is perhaps overly simplistic,
>> but this code has clearly confused people. It worked on me...
>>
>
> I think kzalloc immediately followed by kreallocing the returned value
> is rather ugly. Other than that:

Indeed, but it's an obvious pattern. "If not initialized, initialize".

>> - num = mk->mp->num;
>> - attrs = mk->mp->grp.attrs;
>> + /* First allocation. */
>> + mk->mp = kzalloc(sizeof(*mk->mp), GFP_KERNEL);
>> + if (!mk->mp)
>> + return -ENOMEM;
>
> free_module_param_attrs does not check mk->mp for being NULL before
> kfree'ing mk->mp->grp.attrs, so this will oops.

Nice catch, folded this in:

diff --git a/kernel/params.c b/kernel/params.c
index 3ebe6c64aa67..ee92e67f2cee 100644
--- a/kernel/params.c
+++ b/kernel/params.c
@@ -650,7 +650,8 @@ static __modinit int add_sysfs_param(struct module_kobject *mk,
#ifdef CONFIG_MODULES
static void free_module_param_attrs(struct module_kobject *mk)
{
- kfree(mk->mp->grp.attrs);
+ if (mk->mp)
+ kfree(mk->mp->grp.attrs);
kfree(mk->mp);
mk->mp = NULL;
}
Thanks!
Rusty.


\
 
 \ /
  Last update: 2014-10-17 01:41    [W:0.044 / U:0.044 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site