public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Paulo Marques <pmarques@grupopie.com>
To: LKML <linux-kernel@vger.kernel.org>
Subject: RFC: turn kmalloc+memset(,0,) into kcalloc
Date: Tue, 05 Apr 2005 17:26:31 +0100	[thread overview]
Message-ID: <4252BC37.8030306@grupopie.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 1668 bytes --]


Hi,

I noticed there are a number of places in the kernel that do:

	ptr = kmalloc(n * size, ...)
	if (!ptr)
		goto out;
	memset(ptr, 0, n * size);

It seems that these could be replaced by:

	ptr = kcalloc(n, size, ...)
	if (!ptr)
		goto out;

saving a few bytes.

Most of the times the size isn't something "n * size", but simply "n". 
These could be replaced by kcalloc(n, 1, ...) or we could create a 
special "kmalloc_zero" function to do this without the need for the 
extra "1" parameter.

A quick (and lame) grep through the tree shows about 1200 of these 
cases. This means that about one quarter of all the kmallocs in the 
kernel are actually zeroed right after allocation.

I could send patches to slowly clean this up, like Adrian Bunk did for 
the static functions and Jesper Juhl for the kfree NULL checks.

There are pros and cons to doing this:

pros:
   - smaller kernel image size
   - smaller (and more readable) source code
   - explicit interface to request zeroed data. If in the future we have 
a good way of providing some zeroed-cachehot-super-duper-slabs, we can 
allocate space from there and avoid the memset altogether

cons:
   - the NULL test is done twice
   - memset will not be optimized for constant sizes

The disadvantages don't seem to matter much for non-critical paths. For 
really fast paths we should probably keep the kmalloc + memset.

Attached is a sample of what one of those patches would look like.

Would this be a good thing to clean up, or isn't it worth the effort at all?

-- 
Paulo Marques - www.grupopie.com

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

[-- Attachment #2: patchsample --]
[-- Type: text/plain, Size: 486 bytes --]

--- ./lib/kobject_uevent.c.orig	2005-04-05 16:39:09.000000000 +0100
+++ ./lib/kobject_uevent.c	2005-04-05 17:01:26.000000000 +0100
@@ -234,10 +234,9 @@ void kobject_hotplug(struct kobject *kob
 	if (!action_string)
 		return;
 
-	envp = kmalloc(NUM_ENVP * sizeof (char *), GFP_KERNEL);
+	envp = kmalloc_zero(NUM_ENVP * sizeof (char *), GFP_KERNEL);
 	if (!envp)
 		return;
-	memset (envp, 0x00, NUM_ENVP * sizeof (char *));
 
 	buffer = kmalloc(BUFFER_SIZE, GFP_KERNEL);
 	if (!buffer)

             reply	other threads:[~2005-04-05 16:28 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-04-05 16:26 Paulo Marques [this message]
2005-04-05 18:00 ` RFC: turn kmalloc+memset(,0,) into kcalloc 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
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=4252BC37.8030306@grupopie.com \
    --to=pmarques@grupopie.com \
    --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