public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Paulo Marques <pmarques@grupopie.com>
To: Adrian Bunk <bunk@stusta.de>
Cc: LKML <linux-kernel@vger.kernel.org>
Subject: Re: RFC: turn kmalloc+memset(,0,) into kcalloc
Date: Fri, 08 Apr 2005 13:38:22 +0100	[thread overview]
Message-ID: <42567B3E.8010403@grupopie.com> (raw)
In-Reply-To: <20050407214747.GD4325@stusta.de>

Adrian Bunk wrote:
> On Tue, Apr 05, 2005 at 05:26:31PM +0100, Paulo Marques wrote:
>>[...]
> 
> Hi Paulo,

Hi Adrian,

>>[...]
>>pros:
>>  - smaller kernel image size
>>  - smaller (and more readable) source code
> 
> Which is better readable depends on what you are used to.

That's true to some degree, but look at code like this (in 
drivers/usb/input/hid-core.c):

> 	if (!(field = kmalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
> 		+ values * sizeof(unsigned), GFP_KERNEL))) return NULL;
> 
> 	memset(field, 0, sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
> 		+ values * sizeof(unsigned));

and compare to this:

> 	field = kzalloc(sizeof(struct hid_field) + usages * sizeof(struct hid_usage)
> 			+ values * sizeof(unsigned), GFP_KERNEL);
> 	if (!field) 
> 		return NULL;

In the first case you have to read carefully to make sure that the size 
argument in both the kmalloc and the memset are the same. Even more, if 
the size needs to be updated to include something more, a mistake can be 
made by inserting the extra size just in the kmalloc call. Also, you are 
assuming that the compiler is smart enough to notice that the two 
expressions are the same and cache the result, but this is probably true 
for gcc, anyway.

I think most will agree that the second piece of code is more "readable".

>>cons:
>>  - the NULL test is done twice
>>  - memset will not be optimized for constant sizes
>>...
>>Would this be a good thing to clean up, or isn't it worth the effort at all?
> 
> You do plan to patch 1200 places in the kernel for this 
> micro-optimization? 

I was actually planning on sending a patch at a time for a reasonably 
sized subsection. Like "use kzalloc in usb/serial drivers", or "use 
kzalloc in sound/core", etc.

This way, it shouldn't be more than 1150 patches ;)

> This sounds like a really big overhead for a pretty small gain.

Yes it is. But at least it will remove 1000+ lines of redundant kernel code.

I really don't see this as a priority at all. I'll probably submit a 
patch shortly to create the kzalloc function. This way developers become 
aware that it exists and that it should be used, and we don't get a lot 
of new code with the same constructs.

The cleanup can then progress at a slowly pace, without breaking things 
and without producing too much merging problems.

> There are tasks of higher value that can be done.

I couldn't agree more :)

> E.g. read my "Stack usage tasks" email. The benefits would only be 
> present for people using GNU gcc 3.4 or SuSE gcc 3.3 on i386, but this 
> is a reasonable subset of the kernel users - and it brings them a
> 2% kernel size improvement.

I've read that thread, but it seems that it is at a dead end right now, 
since we don't have a good tool to find out which functions are abusing 
the stack with unit-at-a-time.

Is there some way to even limit the search, like using a stack usage log 
from a compilation without unit-at-a-time, and going over the hotspots 
to check for problems?

If we can get a list, even if it contains a lot of false positives, I 
would more than happy to help out...

-- 
Paulo Marques - www.grupopie.com

All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke (1729 - 1797)

  reply	other threads:[~2005-04-08 12:39 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-05 16:26 RFC: turn kmalloc+memset(,0,) into kcalloc Paulo Marques
2005-04-05 18:00 ` Jörn Engel
2005-04-06 12:09   ` Denis Vlasenko
2005-04-05 18:54 ` Jesper Juhl
2005-04-05 19:20   ` Roland Dreier
2005-04-05 20:01     ` Jesper Juhl
2005-04-06 11:28       ` Jörn Engel
2005-04-06 12:15         ` Paulo Marques
2005-04-06 13:10           ` Pekka Enberg
2005-04-06 15:50             ` Paulo Marques
2005-04-07 23:54               ` Kyle Moffett
2005-04-09  2:11         ` Jesper Juhl
2005-04-07 21:47 ` Adrian Bunk
2005-04-08 12:38   ` Paulo Marques [this message]
2005-04-08 13:00     ` Adrian Bunk
2005-04-08 13:20       ` Jörn Engel
2005-04-08 13:29         ` Adrian Bunk
2005-04-08 16:24       ` Paulo Marques
2005-04-08 19:43         ` Adrian Bunk
2005-04-08 19:49           ` Randy.Dunlap
2005-04-08 13:00     ` stack checking (was: Re: RFC: turn kmalloc+memset(,0,) into kcalloc) Jörn Engel
2005-04-09 14:19     ` RFC: turn kmalloc+memset(,0,) into kcalloc Paul Jackson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=42567B3E.8010403@grupopie.com \
    --to=pmarques@grupopie.com \
    --cc=bunk@stusta.de \
    --cc=linux-kernel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox