Skip to contentSkip to conversation

kfree(NULL)

DISCUSSION Tilman Schmidt 5 participants 7 messages Apr 21, 2006 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw
Quoted 10 lines · Andrew Morton
Quoted 8 lines · James Morris
Quoted 5 lines · Daniel Walker

I included a patch , not like it's needed . Recently I've been
evaluating likely/unlikely branch prediction .. One thing that I found is that the kfree function is often called with a NULL "objp" . In fact it's so frequent that the "unlikely" branch predictor should be inverted! Or at least on my configuration.

Quoted 4 lines
Quoted 2 lines

It would be helpful to collect some stats on this so we can look at the ratio.

Yes, kfree(NULL) is supposed to be uncommon.

Not anymore, after the recent campaign to elliminate explicit NULL checks before calls to kfree().

Quoted 1 line

If someone's doing it a lot then we should fix up the callers.

If that fixup amounts to re-adding the NULL check just elliminated then that's no improvement. It would be better to drop the assumption that kfree() calls with a NULL argument are uncommon, and consequently remove the unlikely() predictor. Adding likely() instead may or may not be a good idea.

Signature · 4 lines
Tilman Schmidt                          E-Mail: [email protected]
Bonn, Germany
Diese Nachricht besteht zu 100% aus wiederverwerteten Bits.
Ungeöffnet mindestens haltbar bis: (siehe Rückseite)
#4 lore raw
Quoted 20 lines · Tilman Schmidt
Quoted 10 lines · Andrew Morton
Quoted 8 lines · James Morris
Quoted 5 lines · Daniel Walker

I included a patch , not like it's needed . Recently I've been
evaluating likely/unlikely branch prediction .. One thing that I found is that the kfree function is often called with a NULL "objp" . In fact it's so frequent that the "unlikely" branch predictor should be inverted! Or at least on my configuration.

Quoted 4 lines
Quoted 2 lines

It would be helpful to collect some stats on this so we can look at the ratio.

Yes, kfree(NULL) is supposed to be uncommon.

Not anymore, after the recent campaign to elliminate explicit NULL checks before calls to kfree().

Quoted 7 lines
Quoted 1 line

If someone's doing it a lot then we should fix up the callers.

If that fixup amounts to re-adding the NULL check just elliminated then that's no improvement. It would be better to drop the assumption that kfree() calls with a NULL argument are uncommon, and consequently remove the unlikely() predictor. Adding likely() instead may or may not be a good idea.

After reviewing some of the callers of kfree(NULL) they appear to be errors by the caller .. Where there's some false assumptions going on during looping or repeated calls to the same function.

I agree with Andrew , I think the calls should be investigated while retaining the unlikley() predictor .

Daniel

#5 lore raw
Quoted 7 lines · Daniel Walker

After reviewing some of the callers of kfree(NULL) they appear to be errors by the caller .. Where there's some false assumptions going on during looping or repeated calls to the same function.

I agree with Andrew , I think the calls should be investigated while retaining the unlikley() predictor .

Those calls that frequently call kfree(NULL). ;)

Jörn

Signature · 3 lines
Linux [...] existed just for discussion between people who wanted
to show off how geeky they were.
-- Rob Enderle
#6 lore raw
In reply to Daniel Walker
Quoted 8 lines · Daniel Walker

After reviewing some of the callers of kfree(NULL) they appear to be errors by the caller .. Where there's some false assumptions going on during looping or repeated calls to the same function.

I agree with Andrew , I think the calls should be investigated while retaining the unlikley() predictor .

Below is a quick patch to do statistics on kfree. It records up to 1000 locations of those that use kfree(NULL). Yes I know that the adding/searching the list is O(n), but it's only for statistics, and it only happens on the NULL case.

Then it allows for showing what it found through the sysrq-l

Anyway, after booting with this patch I got the following output:

SysRq : Show stats on kfree
Total number of NULL frees:      16129
Total number of non NULL frees:  18119
Callers of NULL frees:
[       27]  c0154bcd - do_tune_cpucache+0x13d/0x230
[      631]  c025b9dd - class_device_add+0xcd/0x300
[       30]  c019523c - sysfs_d_iput+0x3c/0x8e
[       44]  c0193750 - sysfs_hash_and_remove+0xd0/0x110
[        1]  c01f4787 - kobject_cleanup+0x37/0x90
[        1]  c025bf73 - class_dev_release+0x23/0x90
[       14]  c021b615 - tty_write+0x105/0x220
[       20]  c025b5ff - class_device_del+0x16f/0x190
[        6]  c021cd34 - release_mem+0x174/0x2a0
[       36]  c011e804 - do_sysctl+0x94/0x250
[     6491]  c01aafc4 - start_this_handle+0x234/0x4b0
[     8404]  c01aba66 - do_get_write_access+0x2e6/0x5a0
[      263]  c01abdf0 - journal_get_undo_access+0xd0/0x120
[       28]  c01a3c9f - ext3_clear_inode+0x2f/0x40
[        3]  c0194a0c - sysfs_dir_close+0x6c/0x90
[       59]  c0304e1d - inet_sock_destruct+0xad/0x1f0
[        1]  c030a698 - ip_rt_ioctl+0xe8/0x130
[       64]  c02e2669 - ip_push_pending_frames+0x2d9/0x400
[        6]  c02d69b0 - netlink_release+0x1c0/0x300

The numbers in the []'s is the number of times they call kfree with NULL.

This was right after a boot, but I can play on the system and see what else is doing a heavy kfree(NULL). Maybe Jan Engelhardt's idea of doing the inline unless CONFIG_CC_OPTIMIZE_FOR_SIZE is set is the way to go.

Anyway, if you want to play, or make this a better patch, here it is.

-- Steve

drivers/char/sysrq.c +14 -1
Index: linux-2.6.17-rc2/drivers/char/sysrq.c
===================================================================
@@ -271,6 +271,19 @@ static struct sysrq_key_op sysrq_unrt_op
 	.enable_mask	= SYSRQ_ENABLE_RTNICE,
 };
 
+extern void kfree_show_stats(void);
+static void sysrq_handle_kfree_stat(int key, struct pt_regs *pt_regs,
+                                    struct tty_struct *tty)
+{
+        kfree_show_stats();
+}
+static struct sysrq_key_op sysrq_kfree_op = {
+	.handler	= sysrq_handle_kfree_stat,
+	.help_msg	= "KfreeStats",
+	.action_msg	= "Show stats on kfree",
+	.enable_mask	= SYSRQ_ENABLE_RTNICE,
+};
+
 /* Key Operations table and lock */
 static DEFINE_SPINLOCK(sysrq_key_table_lock);
 
@@ -301,7 +314,7 @@ static struct sysrq_key_op *sysrq_key_ta
 	&sysrq_kill_op,			/* i */
 	NULL,				/* j */
 	&sysrq_SAK_op,			/* k */
-	NULL,				/* l */
+	&sysrq_kfree_op,		/* l */
 	&sysrq_showmem_op,		/* m */
 	&sysrq_unrt_op,			/* n */
 	/* This will often be registered as 'Off' at init time */
mm/slab.c +67 -1

82 lines not on this page · raw message

#7 lore raw
Quoted 2 lines · Steven Rostedt
[     6491]  c01aafc4 - start_this_handle+0x234/0x4b0
[     8404]  c01aba66 - do_get_write_access+0x2e6/0x5a0

IMO , these two are overloaded with goto's it makes it hard to know whats going on .

Daniel

#8 lore raw
Quoted 9 lines · Daniel Walker
Quoted 2 lines · Steven Rostedt
[     6491]  c01aafc4 - start_this_handle+0x234/0x4b0
[     8404]  c01aba66 - do_get_write_access+0x2e6/0x5a0

IMO , these two are overloaded with goto's it makes it hard to know whats going on .

(I've added Stephen Tweedie to the CC list since these two functions are his)

Perhaps this patch is something that can be useful. It can tell us where we need to either fix the logic so that a kfree(NULL) doesn't happen, or it can tell us where we should have a "if (obj) kfree(obj)" since it is quicker than doing the function call.

Right now, these two locations seem to be good candidates to putting in the NULL check before calling kfree.

-- Steve

#9 lore raw
In reply to Steven Rostedt
Quoted 6 lines · Steven Rostedt

Total number of NULL frees: 16129
Total number of non NULL frees: 18119
....

 [     6491]  c01aafc4 - start_this_handle+0x234/0x4b0
 [     8404]  c01aba66 - do_get_write_access+0x2e6/0x5a0

eh. I'll fix those up.