kfree(NULL)
Subsystems (2)
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.
Here are some examples of the warnings that I observed ..
printk: 66 messages suppressed.
BUG: warning at mm/slab.c:3384/kfree() <c01043d3> show_trace+0x13/0x20 <c01043fe> dump_stack+0x1e/0x20 <c015e334> kfree+0xa4/0xc0 <c032584b> make_request+0x36b/0x670 <c0210305> generic_make_request+0x175/0x240 <c02107d7> submit_bio+0x57/0x100 <c01648f6> submit_bh+0x106/0x160 <c0165a62> __block_write_full_page+0x222/0x3f0 <c0165d28> block_write_full_page+0xf8/0x100 <c016a4b1> blkdev_writepage+0x21/0x30 <c0188c1e> mpage_writepages+0x1ae/0x3d0 <c016a46e> generic_writepages+0x1e/0x20 <c0148f9d> do_writepages+0x2d/0x50 <c0186b70> __writeback_single_inode+0xa0/0x400 <c018716b> sync_sb_inodes+0x1bb/0x2a0 <c01877bf> writeback_inodes+0xaf/0xe5 <c0148d53> wb_kupdate+0x83/0x100 <c0149ab2> pdflush+0x102/0x1c0 <c0131fa4> kthread+0xc4/0xf0 <c0100ed5> kernel_thread_helper+0x5/0x10 printk: 157 messages suppressed. BUG: warning at mm/slab.c:3384/kfree() <c01043d3> show_trace+0x13/0x20 <c01043fe> dump_stack+0x1e/0x20 <c015e334> kfree+0xa4/0xc0 <c0140405> audit_syscall_exit+0x405/0x430 <c0106a56> do_syscall_trace+0x1d6/0x245 <c010320a> syscall_exit_work+0x16/0x1c
Daniel
mm/slab.c +3 -1
@@ -3380,8 +3380,10 @@ void kfree(const void *objp)struct kmem_cache *c;unsigned long flags; -if (unlikely (!objp)) +if (unlikely (!objp)){ +WARN_ON (printk_ratelimit ());return ; + }local_irq_save (flags);kfree_debugcheck (objp); c =virt_to_cache (objp);
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.
It would be helpful to collect some stats on this so we can look at the ratio.
- James
Signature · 2 lines
James Morris <[email protected]>
Quoted 12 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.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. If someone's doing it a lot then we should fix up the callers.
Quoted 8 lines · you
Quoted 7 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 5 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. If someone's doing it a lot then we should fix up the callers.
Part of the reason it gets done a lot is because some developers on this list have told others NOT to check for NULL before calling kfree because kfree does that internally. I was told that. I can't remember who told me though.
Maybe kfree should really be a wrapper around __kfree which does the real work. Then kfree could be a inlined function or a #define that does the NULL pointer check. Something like the patch below. This has the advantage of both worlds. If you know your pointer is NULL, call __kfree. If you are not sure and would check anyway, the inline version of kfree checks for you and then calls __kfree.
include/linux/slab.h 2006-04-21 07:47:32.000000000 -0700 renamed from include/linux/slab.h 2006-03-19 21:53:29.000000000 -0800 +8 -1
@@ -123,7 +123,14 @@ static inline void *kcalloc(size_t n, si return kzalloc(n * size, flags); } -extern void kfree(const void *); +extern void __kfree(const void *); +static inline void kfree(const void *obj) +{ + if (!obj) + return; + __kfree(obj); +} + extern unsigned int ksize(const void *); #ifdef CONFIG_NUMA
mm/slab.c 2006-04-21 07:49:56.000000000 -0700 renamed from mm/slab.c 2006-04-21 07:49:42.000000000 -0700 +2 -4
@@ -3275,13 +3275,11 @@ EXPORT_SYMBOL(kmem_cache_free); * Don't free memory not originally allocated by kmalloc() * or you will run into trouble. */ -void kfree(const void *objp) +void __kfree(const void *objp) { struct kmem_cache *c; unsigned long flags; - if (unlikely(!objp)) - return; local_irq_save(flags); kfree_debugcheck(objp); c = virt_to_cache(objp);
@@ -3289,7 +3287,7 @@ void kfree(const void *objp) __cache_free(c, (void *)objp); local_irq_restore(flags); } -EXPORT_SYMBOL(kfree); +EXPORT_SYMBOL(__kfree); #ifdef CONFIG_SMP /**
2006/4/21, Vernon Mauery <[email protected]>:
Quoted 3 lines
Maybe kfree should really be a wrapper around
__kfreewhich does the real work. Then kfree could be a inlined function or a #define that does the NULL pointer check.
NULL pointer check in kfree saves lot of small code fragments in callers. It is one of many reasons why kfree does it.
Making kfree inline wrapper eliminates this save.
Quoted 7 lines
Quoted 3 lines
Maybe kfree should really be a wrapper around
__kfreewhich does the real work. Then kfree could be a inlined function or a #define that does the NULL pointer check.NULL pointer check in kfree saves lot of small code fragments in callers. It is one of many reasons why kfree does it.
Making kfree inline wrapper eliminates this save.
How about
slab.h:
#ifndef CONFIG_OPTIMIZING_FOR_SIZE
static inline void kfree(const void *p) {
if(p != NULL)
__kfree(p);
}#else
extern void kfree(const void *);
#endif
slab.c:
#ifdef CONFIG_OPTIMIZING_FOR_SIZE
void kfree(const void *p) {
if(p != NUILL)
_kfree(p);
}#endif
That way, you get your time saving with -O2 and your space saving with -Os.
Jan Engelhardt
Quoted 22 lines · Jan Engelhardt
Quoted 7 lines
Quoted 3 lines
Maybe kfree should really be a wrapper around
__kfreewhich does the real work. Then kfree could be a inlined function or a #define that does the NULL pointer check.NULL pointer check in kfree saves lot of small code fragments in callers. It is one of many reasons why kfree does it.
Making kfree inline wrapper eliminates this save.How about
slab.h:
#ifndefCONFIG_OPTIMIZING_FOR_SIZEstatic inline void kfree(const void *p) { if(p != NULL) __kfree(p); }#else
extern voidkfree(const void *);
#endif
slab.c:
#ifdefCONFIG_OPTIMIZING_FOR_SIZE
Quoted 7 lines
void kfree(const void *p) { if(p != NUILL) _kfree(p); }#endif
That way, you get your time saving with -O2 and your space saving with -Os.
What makes you confident that the static inline version gives a time saving?
Quoted 1 line
Jan Engelhardt
cu
Adrian
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 29 lines · you
Quoted 28 lines · Jan Engelhardt
Quoted 7 lines
Quoted 3 lines
Maybe kfree should really be a wrapper around
__kfreewhich does the real work. Then kfree could be a inlined function or a #define that does the NULL pointer check.NULL pointer check in kfree saves lot of small code fragments in callers. It is one of many reasons why kfree does it. Making kfree inline wrapper eliminates this save.
How about
slab.h:
#ifndefCONFIG_OPTIMIZING_FOR_SIZEstatic inline void kfree(const void *p) { if(p != NULL) __kfree(p); }#else
extern voidkfree(const void *);
#endif
slab.c:
#ifdefCONFIG_OPTIMIZING_FOR_SIZEvoid kfree(const void *p) { if(p != NUILL) _kfree(p); }#endif
Quoted 5 lines
Quoted 2 lines
That way, you get your time saving with -O2 and your space saving with -Os.
What makes you confident that the static inline version gives a time saving?
A static inline wrapper would mean that it wouldn't have to make a function call just to check if the pointer is NULL. A simple NULL check is faster than a function call and then a simple NULL check. In other words, there would be no pushing and popping the stack. In almost all cases, replacing an inline function with a non-inline function means a trade-off between speed and size.
--Vernon
Quoted 11 lines · Vernon Mauery
Quoted 2 lines · you
What makes you confident that the static inline version gives a time saving?
A static inline wrapper would mean that it wouldn't have to make a function call just to check if the pointer is NULL. A simple NULL check is faster than a function call and then a simple NULL check. In other words, there would be no pushing and popping the stack. In almost all cases, replacing an inline function with a non-inline function means a trade-off between speed and size.
Andrew Morton just submitted a patch to -mm that fixes the two problem places that called kfree(NULL) more than it calls kfree(non-NULL).
Besides the places that are now fixed, the inline doesn't save much. Since most cases kfree(non-NULL) is called, so the NULL is really an unlikely case. Thus you just increased the size of the kernel, for virtually no speed savings.
-- Steve
Quoted 36 lines · Vernon Mauery
Quoted 29 lines · you
Quoted 28 lines · Jan Engelhardt
Quoted 7 lines
Quoted 3 lines
Maybe kfree should really be a wrapper around
__kfreewhich does the real work. Then kfree could be a inlined function or a #define that does the NULL pointer check.NULL pointer check in kfree saves lot of small code fragments in callers. It is one of many reasons why kfree does it. Making kfree inline wrapper eliminates this save.
How about
slab.h:
#ifndefCONFIG_OPTIMIZING_FOR_SIZEstatic inline void kfree(const void *p) { if(p != NULL) __kfree(p); }#else
extern voidkfree(const void *);
#endif
slab.c:
#ifdefCONFIG_OPTIMIZING_FOR_SIZEvoid kfree(const void *p) { if(p != NUILL) _kfree(p); }#endif
Quoted 5 lines
Quoted 2 lines
That way, you get your time saving with -O2 and your space saving with -Os.
What makes you confident that the static inline version gives a time saving?
Quoted 6 lines
A static inline wrapper would mean that it wouldn't have to make a function call just to check if the pointer is NULL. A simple NULL check is faster than a function call and then a simple NULL check. In other words, there would be no pushing and popping the stack. In almost all cases, replacing an inline function with a non-inline function means a trade-off between speed and size.
It's not that simple - inline's make the code bigger causing more cache misses and therefore also having a negative impact on performance.
This is the reason why e.g. the gcc -O3 option usually results in _worse_ performance than the -O2 option.
Quoted 1 line
--Vernon
cu
Adrian
BTW: Don't strip the Cc when replying to linux-kernel.
Signature · 4 lines
"Is there not promise of rain?" Ling Tan asked suddenly out
of the darkness. There had been need of rain for many days.
"Only a promise," Lao Er said.
Pearl S. Buck - Dragon SeedQuoted 3 lines · Vernon Mauery
A simple NULL check is faster
than a function call and then a simple NULL check.
I am not sure whether this is still true for any non-ancient CPUs. The cost of a branch misprediction is in the order of _many_ predictable instructions. That makes conditionals more expensive than they used to be. And the cost of branch mispredictions keeps increasing.
Jörn
Signature · 2 lines
You can take my soul, but not my lack of enthusiasm. -- Wally
Quoted 2 lines · Andrew Morton
Yes, kfree(NULL) is supposed to be uncommon. If someone's doing it a lot then we should fix up the callers.
Well, we'd have to start by fixing up the janitors that run around taking out the if statements in the callers. :)
Paul.
Quoted 2 lines · Andrew Morton
Quoted 2 lines
Yes, kfree(NULL) is supposed to be uncommon. If someone's doing it a lot then we should fix up the callers.
Quoted 2 lines · Paul Mackerras
Well, we'd have to start by fixing up the janitors that run around taking out the if statements in the callers. :)
No, it's not the janitors fault that we have paths doing lots of kfree(NULL) calls. NULL check removal didn't create the problem, but it makes it more visible definitely.
Pekka
Quoted 3 lines · Pekka Enberg
No, it's not the janitors fault that we have paths doing lots of kfree(NULL) calls. NULL check removal didn't create the problem, but it makes it more visible definitely.
There is a judgement to be made at each call site of kfree (and similar functions) about whether the argument is rarely NULL, or could often be NULL. If the janitors have been making this judgement, I apologise, but I haven't seen them doing that.
Paul.
Quoted 4 lines · Paul Mackerras
There is a judgement to be made at each call site of kfree (and similar functions) about whether the argument is rarely NULL, or could often be NULL. If the janitors have been making this judgement, I apologise, but I haven't seen them doing that.
I don't think anyone should be calling kfree with NULL pointer often in the first place. Keeping the extra check in place is masking the real problem. So yeah, we should be looking at the NULL checks more carefully to see if they require more fundamental fixes, but no, I don't see why janitors can't continue to remove the extra checks.
Pekka
Quoted 7 lines
There is a judgement to be made at each call site of kfree (and similar functions) about whether the argument is rarely NULL, or could often be NULL. If the janitors have been making this judgement, I apologise, but I haven't seen them doing that.
Paul.
Even if the caller passes NULL most of the time, the check should be removed.
It's just crazy talk to say "you should not check NULL before calling kfree, as long as you make sure it's not NULL most of the time".
Hua
Quoted 11 lines · Hua Zhong
Quoted 5 lines
There is a judgement to be made at each call site of kfree
(and similar functions) about whether the argument is rarely NULL, or could often be NULL. If the janitors have been making this judgement, I apologise, but I haven't seen them doing that.
Paul.
Even if the caller passes NULL most of the time, the check should be removed.
It's just crazy talk to say "you should not check NULL before calling kfree, as long as you make sure it's not NULL most of the time".
It can reduce readability of the code [unless it is used in error path simplification, kfree(something) usually suggests kfree-an-object].
If the caller passes NULL most of the time, it could be in need of redesign.
I don't actually like kfree(NULL) any time except error paths. It is subjective, not crazy talk.
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
Quoted 3 lines
It can reduce readability of the code [unless it is used in error path simplification, kfree(something) usually suggests kfree-an-object].
Consistency in coding style improves readability. Redundancy reduces readability.
The interface is simple and clear, and has been documented for decades, that is kfree (and free) accepts NULL. There is no ambiguity here.
If you think "if (obj) kfree (obj);" is more readable than "kfree(obj);", fix the API to enforce it.
But if the kernel tree is full of "some caller checks NULL while others not", I hardly see it as readable. It'd just be confusing.
Quoted 2 lines
I don't actually like kfree(NULL) any time except error paths. It is subjective, not crazy talk.
Documented interface is not subjective.
Quoted 10 lines · Hua Zhong
Quoted 1 line
It can reduce readability of the code [unless it is used in error path simplification, kfree(something) usually suggests kfree-an-object].
Consistency in coding style improves readability. Redundancy reduces readability.
The interface is simple and clear, and has been documented for decades, that is kfree (and free) accepts NULL. There is no ambiguity here.
If you think "if (obj) kfree (obj);" is more readable than "kfree(obj);", fix the API to enforce it.
Quoted 7 lines
But if the kernel tree is full of "some caller checks NULL while others not", I hardly see it as readable. It'd just be confusing.
Quoted 1 line
I don't actually like kfree(NULL) any time except error paths. It is subjective, not crazy talk.
Documented interface is not subjective.
That's great. I don't know quite how to reply, or even if I should if you don't read what I write.
Signature · 2 lines
SUSE Labs, Novell Inc. Send instant messages to your online friends http://au.messenger.yahoo.com
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
I don't actually like kfree(NULL) any time except error paths. It is subjective, not crazy talk.
Documented interface is not subjective.
That's great. I don't know quite how to reply, or even if I should if you don't read what I write.
Where's the problem, if a developer does not know whether an object is NULL or not, he may call kfree(). If, on the other hand, he is sure it is NULL, there is no need to refree it, and if he is sure it is non-NULL, he can directly call __kfree(). So the readability can never suffer - you never need an if(x==NULL) anymore.
Jan Engelhardt
Quoted 16 lines · Nick Piggin
Quoted 7 lines · Hua Zhong
Quoted 7 lines
There is a judgement to be made at each call site of kfree
(and similar functions) about whether the argument is rarely NULL, or could often be NULL. If the janitors have been making this judgement, I apologise, but I haven't seen them doing that.
Quoted 7 lines
Quoted 1 line
Paul.
Even if the caller passes NULL most of the time, the check should be removed.
It's just crazy talk to say "you should not check NULL before calling kfree, as long as you make sure it's not NULL most of the time".
Quoted 8 lines
It can reduce readability of the code [unless it is used in error path simplification, kfree(something) usually suggests kfree-an-object].
If the caller passes NULL most of the time, it could be in need of redesign.
I don't actually like kfree(NULL) any time except error paths. It is subjective, not crazy talk.
I wrote a little hack that detects up to 1000 callers of kfree(NULL) and outputs what it finds with sysrq-l.
http://marc.theaimsgroup.com/?l=linux-kernel&m=114564257500757&w=2
It found right away, two function in transaction.c from the jbd code, that were freeing an object that sometimes gets allocated. Andrew Morton already submitted the patch in the -mm tree to fix it:
- kfree(new_transaction);
+ if (unlikely(new_transaction)) /* It's usually NULL */
+ kfree(new_transaction);
return ret;}
@@ -724,7 +725,8 @@ done:
journal_cancel_revoke(handle, jh);
out:
- kfree(frozen_buffer);
+ if (unlikely(frozen_buffer)) /* It's usually NULL */
+ kfree(frozen_buffer);
Where he uses unlikely and nicely documents that it is usually NULL (of course the "unlikely" sort of says that already ;)
I've been running this patched kernel for a couple of days on a mostly idle machine, (I don't need it right now, so I just let it run) and it has shown some more problem areas. probably occurred when updatedb kicked off.
Here's the dump:
SysRq : Show stats on kfree Total number of NULL frees: 1589709 Total number of non NULL frees: 69448 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 [ 79] c011e804 - do_sysctl+0x94/0x250 [ 352161] c01aafc4 - start_this_handle+0x234/0x4b0 [ 430089] c01aba66 - do_get_write_access+0x2e6/0x5a0 [ 16730] c01abdf0 - journal_get_undo_access+0xd0/0x120 [ 788641] c01a3c9f - ext3_clear_inode+0x2f/0x40 [ 3] c0194a0c - sysfs_dir_close+0x6c/0x90 [ 252] c0304e1d - inet_sock_destruct+0xad/0x1f0 [ 1] c030a698 - ip_rt_ioctl+0xe8/0x130 [ 968] c02e2669 - ip_push_pending_frames+0x2d9/0x400 [ 6] c02d69b0 - netlink_release+0x1c0/0x300 [ 5] c02ba79b - sock_fasync+0x13b/0x150
start_this_handle and do_get_write_access have already been fixed, but now it's looking like journal_get_undo_access and ext3_clear_inode are problem children too.
-- Steve
Quoted 8 lines · Paul Mackerras
Quoted 2 lines · Andrew Morton
Yes, kfree(NULL) is supposed to be uncommon. If someone's doing it a lot then we should fix up the callers.
Well, we'd have to start by fixing up the janitors that run around taking out the if statements in the callers. :)
I think there was pretty good agreement, when we started doing that, that taking out the if statements in the callers was a good idea. If it turns out to have been a net loss that's not good, but I don't think it's been a wasted effort - there were a *lot* of places that checked for NULL before calling [kv]free, and now that we've gotten rid of them we can consider adding them back where it makes sense, not just all over the place.
We could also consider changing the
if (unlikely(!obj))
return;
in kfree into simply
if (!obj)
return;
--
Jesper Juhl <[email protected]>
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Quoted 10 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.It would be helpful to collect some stats on this so we can look at the ratio.
On my system it was roughly 31 million kfree(NULL) calls, to 4 million calls with other values . That was over 4 hours of run time .
Daniel