kref debugging
Subsystems (4)
cover kref debugging
This patch series introduces the DEBUG_KREF config option.
It adds another debug check to detect kref_put() with unreferenced kref object, and splits all kref debug checks into this config option. Also it can be disable or enable in run-time by sysctl.
--
PATCH 1/4 kref: warn kref_put() with unreferenced kref
This patch adds warning to detect kref_put() with unreferenced kref.
lib/kref.c | 14 ++++++++------ 1 files changed, 8 insertions(+), 6 deletions(-)
lib/kref.c +8 -6
@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { +WARN_ON (atomic_read (&kref->refcount) <1 );WARN_ON (release ==NULL );WARN_ON (release == (void (*)(struct kref *))kfree);
@@ -56,12 +57,13 @@ int kref_put(struct kref *kref, void (*r* if current count is one, we are the last user and can release object * right now, avoiding an atomic operation on 'refcount' */ -if ((atomic_read (&kref->refcount) ==1 ) || - (atomic_dec_and_test (&kref->refcount))) { -release (kref); -return 1 ; - } -return 0 ; +if (atomic_read (&kref->refcount) ==1 ) +atomic_set (&kref->refcount,0 ); +else if (!atomic_dec_and_test (&kref->refcount)) +return 0 ; + +release (kref); +return 1 ; }EXPORT_SYMBOL (kref_init);
--
PATCH 2/4 kref debugging config option
In reply to Akinobu MitaThis patch converts all WARN_ON() in kref code to BUG_ON(). And split them into new DEBUG_KREF config option.
lib/Kconfig.debug | 7 +++++++ lib/kref.c | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-)
lib/Kconfig.debug +7 -0
@@ -130,6 +130,13 @@ config DEBUG_KOBJECT If you say Y here, some extra kobject debugging messages will be sent to the syslog. +config DEBUG_KREF + bool "kref debugging" + depends on DEBUG_KERNEL + help + This option enables addition error checking for kref, + library routines for handling generic reference counted objects. + config DEBUG_HIGHMEM bool "Highmem debugging" depends on DEBUG_KERNEL && HIGHMEM
lib/kref.c +25 -5
@@ -20,16 +20,38 @@*/ void kref_init (struct kref *kref) { -atomic_set (&kref->refcount,1 ); +atomic_set (&kref->refcount,1 ); } +#ifdef CONFIG_DEBUG_KREF +static void kref_get_debug_check (struct kref *kref) +{ +BUG_ON (!atomic_read (&kref->refcount)); +} +static void kref_put_debug_check (struct kref *kref, +void (*release)(struct kref *kref)) +{ +BUG_ON (atomic_read (&kref->refcount) <1 ); +BUG_ON (release ==NULL ); +BUG_ON (release == (void (*)(struct kref *))kfree); +} +#else +static void kref_get_debug_check (struct kref *kref) +{ +} +static void kref_put_debug_check (struct kref *kref, +void (*release)(struct kref *kref)) +{ +} +#endif +/** * kref_get - increment refcount for object. * @kref: object. */ void kref_get (struct kref *kref) { -WARN_ON (!atomic_read (&kref->refcount)); +kref_get_debug_check (kref);atomic_inc (&kref->refcount); }
@@ -49,9 +71,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { -WARN_ON (atomic_read (&kref->refcount) <1 ); -WARN_ON (release ==NULL ); -WARN_ON (release == (void (*)(struct kref *))kfree); +kref_put_debug_check (kref, release);/* * if current count is one, we are the last user and can release object
--
PATCH 3/4 dynamic configurable kref debugging
In reply to Akinobu MitaThis patch makes kref-debugging dynamically configurable by sysctl kernel.kref_debug
Documentation/sysctl/kernel.txt | 8 ++++++++ include/linux/sysctl.h | 1 + kernel/sysctl.c | 14 ++++++++++++++ lib/kref.c | 9 +++++++++ 4 files changed, 32 insertions(+)
lib/kref.c +9 -0
@@ -24,13 +24,22 @@ void kref_init(struct kref *kref) }#ifdef CONFIG_DEBUG_KREF + +int kref_debug =1 ; +static void kref_get_debug_check (struct kref *kref) { +if (!kref_debug) +return ; +BUG_ON (!atomic_read (&kref->refcount)); }static void kref_put_debug_check (struct kref *kref,void (*release)(struct kref *kref)) { +if (!kref_debug) +return ; +BUG_ON (atomic_read (&kref->refcount) <1 );BUG_ON (release ==NULL );BUG_ON (release == (void (*)(struct kref *))kfree);
include/linux/sysctl.h +1 -0
@@ -148,6 +148,7 @@ enumKERN_SPIN_RETRY =70 ,/* int: number of spinlock retries */ KERN_ACPI_VIDEO_FLAGS =71 ,/* int: flags for setting up video after ACPI sleep */ KERN_IA64_UNALIGNED =72 ,/* int: ia64 unaligned userland trap enable */ + KERN_KREF_DEBUG=73, /* int: kref debug on or off */ };
kernel/sysctl.c +14 -0
@@ -131,6 +131,10 @@ extern int acct_parm[];extern int no_unaligned_warning;#endif +#ifdef CONFIG_DEBUG_KREF +extern int kref_debug; +#endif +static int parse_table (int __user *,int ,void __user *,size_t __user *,void __user *,size_t ,ctl_table *,void **); static int proc_doutsstring(ctl_table *table, int write, struct file *filp,
@@ -683,6 +687,16 @@ static ctl_table kern_table[] = { .proc_handler = &proc_dointvec, },#endif +#ifdef CONFIG_DEBUG_KREF + { + .ctl_name = KERN_KREF_DEBUG, + .procname = "kref_debug", + .data = &kref_debug, + .maxlen = sizeof(int), + .mode = 0644, + .proc_handler = &proc_dointvec, + }, +#endif { .ctl_name = 0 } };
Documentation/sysctl/kernel.txt +8 -0
@@ -27,6 +27,7 @@ show up in /proc/sys/kernel: - hotplug - java-appletviewer [ binfmt_java, obsolete ] - java-interpreter [ binfmt_java, obsolete ] +- kref_debug - l2cr [ PPC only ] - modprobe ==> Documentation/kmod.txt - msgmax
@@ -161,6 +162,13 @@ Default value is "/sbin/hotplug". ============================================================== +kref_debug: + +This flag controls the kref debugging. If 0, kref debugging is +disabled. Enabled if nonzero. + +============================================================== + l2cr: (PPC only) This flag controls the L2 cache of G3 processor boards. If
--
PATCH 4/4 change slab poison pattern
In reply to Akinobu Mitakref debugging cannot detect kref_put() with unreferenced object, when the structure including kref is allocated by slab and slab debugging config is enabled.
Because use-after-free poisoning make kref counter signed value. So this patch prevents it by changing poisoning pattern.
mm/slab.c | 6 +++--- 1 files changed, 3 insertions(+), 3 deletions(-)
mm/slab.c +3 -3
@@ -503,9 +503,9 @@ struct kmem_cache {#define RED_ACTIVE 0x170FC2A5UL/* when obj is active */ /* ...and for poisoning */ -#define POISON_INUSE 0x5a/* for use-uninitialised poisoning */ -#define POISON_FREE 0x6b/* for use-after-free poisoning */ -#define POISON_END 0xa5/* end-byte of poisoning */ +#define POISON_INUSE 0xa5/* for use-uninitialised poisoning */ +#define POISON_FREE 0xb6/* for use-after-free poisoning */ +#define POISON_END 0xab/* end-byte of poisoning */ /* * memory layout of objects:
--
Re: [patch 4/4] change slab poison pattern
Hi,
Quoted 6 lines · Akinobu Mita
kref debugging cannot detect
kref_put()with unreferenced object, when the structure including kref is allocated by slab and slab debugging config is enabled.Because use-after-free poisoning make kref counter signed value. So this patch prevents it by changing poisoning pattern.
Then why not check against POISON_INUSE when CONFIG_SLAB_DEBUG in the kref debugging code? I would prefer you didn't change the slab constants (they're well known by everyone now) but if you must, at least stick a big fat comment there.
Pekka
Re: [patch 4/4] change slab poison pattern
Quoted 7 lines · Pekka Enberg
Quoted 2 lines
Because use-after-free poisoning make kref counter signed value. So this patch prevents it by changing poisoning pattern.
Then why not check against
POISON_INUSEwhenCONFIG_SLAB_DEBUGin the kref debugging code? I would prefer you didn't change the slab constants (they're well known by everyone now) but if you must, at least stick a big fat comment there.
This slab poisoning pattern change is not very important.
Because even if slab debugging is enalbed, kref_put() with unreferenced kref object will be detected as slab corruption.
Because kref_put() decrements the kref counter in freed slab object.
Slab corruption: start=e0e8b698, len=32
Redzone: 0x5a2cf071/0x5a2cf071.
Last user: [<f08ea008>](release_test_kref+0x8/0x14 [test]) 000: 6b 6b 6b 6b 6a 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b
^^
The main reason I want to propose this kref debugging is because I can find many places where we can replace from atomic_t to struct kref by doing "grep -r atomic_dec_and_test .".
But I warried that replacing by kref will just increase atomic operations because of debugging code in kref (WARN_ON()es in kref.c)
So patch 4/4 is not very important, I want to drop patch 4/4.
Re: [patch 4/4] change slab poison pattern
Quoted 21 lines · Akinobu Mita
Quoted 7 lines · Pekka Enberg
Quoted 2 lines
Because use-after-free poisoning make kref counter signed value. So this patch prevents it by changing poisoning pattern.
Then why not check against
POISON_INUSEwhenCONFIG_SLAB_DEBUGin the kref debugging code? I would prefer you didn't change the slab constants (they're well known by everyone now) but if you must, at least stick a big fat comment there.This slab poisoning pattern change is not very important.
Because even if slab debugging is enalbed,
kref_put()with unreferenced kref object will be detected as slab corruption.Because
kref_put()decrements the kref counter in freed slab object.Slab corruption: start=e0e8b698, len=32
Redzone: 0x5a2cf071/0x5a2cf071.
Last user: [<f08ea008>](release_test_kref+0x8/0x14 [test]) 000: 6b 6b 6b 6b 6a 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b 6b^^
Quoted 8 lines
The main reason I want to propose this kref debugging is because I can find many places where we can replace from
atomic_tto struct kref by doing "grep -ratomic_dec_and_test.".But I warried that replacing by kref will just increase atomic operations because of debugging code in kref (WARN_ON()es in
kref.c)So patch 4/4 is not very important, I want to drop patch 4/4.
It would have conflicted with this, btw:
Signature · 1 line
Mathematics is the supreme nostalgia of our time.
Re: [patch 2/4] kref debugging config option
In reply to Akinobu MitaQuoted 2 lines · Akinobu Mita
This patch converts all
WARN_ON()in kref code toBUG_ON().
Why? This change will irritate testers and will decrease their ability to capture (and hence report) diagnostic info.
Quoted 1 line · Akinobu Mita
This patch series introduces the
DEBUG_KREFconfig option.
Any reason you didn't CC: the kref author and maintainer? :(
thanks,
greg k-h
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to Akinobu MitaQuoted 12 lines · Akinobu Mita
This patch adds warning to detect
kref_put()with unreferenced kref.Signed-off-by Akinobu Mita <[email protected]>lib/kref.c | 14 ++++++++------ 1 files changed, 8 insertions(+), 6 deletions(-)Index:
2.6-git/lib/kref.c
===================================================================
lib/kref.c void kref_get(struct kref *kref) · line 52
Quoted 4 lines
lib/kref.c +1
-0@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { +WARN_ON (atomic_read (&kref->refcount) <1 );
How can this ever be true? If the refcount _ever_ goes below 1, the object is freed.
Quoted 7 lines
lib/kref.c +0
-3@@ -56,12 +57,13 @@ int kref_put(struct kref *kref, void (*r* if current count is one, we are the last user and can release object * right now, avoiding an atomic operation on 'refcount' */ -if ((atomic_read (&kref->refcount) ==1 ) || - (atomic_dec_and_test (&kref->refcount))) { -release (kref);
lib/kref.c int kref_put(struct kref *kref, void (*r · line 66
Quoted 10 lines
lib/kref.c +7
-3-return 1 ; - } -return 0 ; +if (atomic_read (&kref->refcount) ==1 ) +atomic_set (&kref->refcount,0 ); +else if (!atomic_dec_and_test (&kref->refcount)) +return 0 ; + +release (kref); +return 1 ;
What does this change do? What are you trying to help out with? CONFIG_DEBUG_SLAB will check to see if you reference krefs that have already been freed.
thanks,
greg k -h
Re: [patch 2/4] kref debugging config option
In reply to Akinobu MitaQuoted 34 lines · Akinobu Mita
This patch converts all
WARN_ON()in kref code toBUG_ON(). And split them into newDEBUG_KREFconfig option.Signed-off-by Akinobu Mita <[email protected]>lib/Kconfig.debug | 7 +++++++ lib/kref.c | 30 +++++++++++++++++++++++++----- 2 files changed, 32 insertions(+), 5 deletions(-)Index: 2.6-git/lib/Kconfig.debug
===================================================================lib/Kconfig.debug +7
-0@@ -130,6 +130,13 @@ config DEBUG_KOBJECT If you say Y here, some extra kobject debugging messages will be sent to the syslog. +config DEBUG_KREF + bool "kref debugging" + depends on DEBUG_KERNEL + help + This option enables addition error checking for kref, + library routines for handling generic reference counted objects. + config DEBUG_HIGHMEM bool "Highmem debugging" depends on DEBUG_KERNEL && HIGHMEMIndex:
2.6-git/lib/kref.c
===================================================================lib/kref.c +0
-0@@ -20,16 +20,38 @@*/ void kref_init (struct kref *kref)
lib/kref.c line 30
Quoted 10 lines
lib/kref.c +6
-1{ -atomic_set (&kref->refcount,1 ); +atomic_set (&kref->refcount,1 ); } +#ifdef CONFIG_DEBUG_KREF +static void kref_get_debug_check (struct kref *kref) +{ +BUG_ON (!atomic_read (&kref->refcount)); +}
Eeek, no, you do not want to do this. Unless you never want to get a bug report with this enabled :)
thanks,
greg k-h
Re: [patch 3/4] dynamic configurable kref debugging
In reply to Akinobu MitaQuoted 2 lines · Akinobu Mita
This patch makes kref-debugging dynamically configurable by sysctl kernel.kref_debug
No new sysctls please, if anything, make this a sysfs file instead.
But even then, this should be a build option, not something you enable at runtime.
thanks,
greg k-h
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to Greg KHQuoted 12 lines · Greg KH
lib/kref.c void kref_get(struct kref *kref) · line 52
Quoted 7 lines · Akinobu Mita
lib/kref.c +1
-0@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { +WARN_ON (atomic_read (&kref->refcount) <1 );How can this ever be true? If the refcount _ever_ goes below 1, the object is freed.
Maybe it should BUG_ON instead in that case. ;)
And strictly speaking, as long as the kref.c stuff is the only stuff to play with ->refcount, that *should* be true. On the other hand, if somebody has a bad pointer that just did a fandango on core, it would be a nice thing to know that. Looking at the *next* few lines:
if ((atomic_read(&kref->refcount) == 1) ||
(atomic_dec_and_test(&kref->refcount))) {
release(kref);
return 1;
}
return 0;If we managed to get a -1 smashed in there, this won't actually trigger for another 2**32-2 or so kref_put calls - the first test is for "exactly 1", and the dec_and_test is for "exactly zero"...
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to Greg KHQuoted 8 lines · Greg KH
lib/kref.c void kref_get(struct kref *kref) · line 52
Quoted 5 lines
lib/kref.c +1
-0@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { +WARN_ON (atomic_read (&kref->refcount) <1 );How can this ever be true? If the refcount _ever_ goes below 1, the object is freed.
The idea of detection kref_put() with unreferenced object was stolen from BUG_ON()es in blocks/ll_rw_blk.c and fs/bio.c
ll_rw_blk.c: BUG_ON(atomic_read(&ioc->refcount) == 0);
bio.c: BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
But the kref counter usually does not become zero. Because kref is trying to reduce the number of atomic_dec_and_test()
So this patch also set kref counter to zero here:
Quoted 2 lines
Quoted 2 lines
lib/kref.c +2
-0+if (atomic_read (&kref->refcount) ==1 ) +atomic_set (&kref->refcount,0 );
Re: [patch 2/4] kref debugging config option
In reply to Andrew MortonQuoted 6 lines · Andrew Morton
Quoted 2 lines · Akinobu Mita
This patch converts all
WARN_ON()in kref code toBUG_ON().Why? This change will irritate testers and will decrease their ability to capture (and hence report) diagnostic info.
I have no grudge against this BUG_ON().
But BUG_ON() is more prominent than WARN_ON().
Because I often could not realized whether WARN_ON() happned or not.
Should we make warn_counter which will be increment when WARN_ON() happens, and export it as /proc/warn-counter? (also export die_counter as /proc/die-counter) Then I'll make pretty gnome applet.
Or put the shell script which just do "dmesg | grep $warn_on_pattern".
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to Akinobu MitaQuoted 17 lines · Akinobu Mita
Quoted 8 lines · Greg KH
lib/kref.c void kref_get(struct kref *kref) · line 52
Quoted 5 lines
lib/kref.c +1
-0@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { +WARN_ON (atomic_read (&kref->refcount) <1 );How can this ever be true? If the refcount _ever_ goes below 1, the object is freed.
The idea of detection
kref_put()with unreferenced object was stolen from BUG_ON()es inblocks/ll_rw_blk.candfs/bio.c
ll_rw_blk.c:BUG_ON(atomic_read(&ioc->refcount) == 0);
bio.c: BIO_BUG_ON(!atomic_read(&bio->bi_cnt));
Quoted 7 lines
But the kref counter usually does not become zero. Because kref is trying to reduce the number of
atomic_dec_and_test()So this patch also set kref counter to zero here:
Quoted 2 lines
lib/kref.c int kref_put(struct kref *kref, void (*r · line 61
Quoted 2 lines
lib/kref.c +2
-0+if (atomic_read (&kref->refcount) ==1 ) +atomic_set (&kref->refcount,0 );
But again, if this happens the memory will be freed. So again, CONFIG_DEBUG_SLAB will catch this kind of thing.
Is there some reason you created these patches? Were you trying to debug something that was tracked down to a kref issue?
thanks,
greg k-h
Re: [patch 2/4] kref debugging config option
In reply to Akinobu MitaQuoted 11 lines · Akinobu Mita
Quoted 6 lines · Andrew Morton
Quoted 2 lines · Akinobu Mita
This patch converts all
WARN_ON()in kref code toBUG_ON().Why? This change will irritate testers and will decrease their ability to capture (and hence report) diagnostic info.
I have no grudge against this
BUG_ON().But
BUG_ON()is more prominent thanWARN_ON().
Sure, it crashes your kernel, hopefully you notice that better :)
thanks,
greg k-h
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to [email protected]Quoted 28 lines · On Tue, Apr 25, 2006 at 12:19:15AM -0400, [email protected] wrote:
Quoted 12 lines · Greg KH
lib/kref.c void kref_get(struct kref *kref) · line 52
Quoted 7 lines · Akinobu Mita
lib/kref.c +1
-0@@ -49,6 +49,7 @@ void kref_get(struct kref *kref)*/ int kref_put (struct kref *kref,void (*release)(struct kref *kref)) { +WARN_ON (atomic_read (&kref->refcount) <1 );How can this ever be true? If the refcount _ever_ goes below 1, the object is freed.
Maybe it should
BUG_ONinstead in that case. ;)And strictly speaking, as long as the
kref.cstuff is the only stuff to play with ->refcount, that *should* be true. On the other hand, if somebody has a bad pointer that just did a fandango on core, it would be a nice thing to know that. Looking at the *next* few lines:if ((atomic_read(&kref->refcount) == 1) || (atomic_dec_and_test(&kref->refcount))) { release(kref); return 1; } return 0;
Quoted 3 lines
If we managed to get a -1 smashed in there, this won't actually trigger for another 2**32-2 or so
kref_putcalls - the first test is for "exactly 1", and thedec_and_testis for "exactly zero"...
Those two lines were recently added to make this test faster. See the archives for details. If you are really worried about some memory getting clobbered in there, we should worry about this for the entire kernel :)
thanks,
greg k-h
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to Greg KHQuoted 2 lines
Is there some reason you created these patches? Were you trying to debug something that was tracked down to a kref issue?
I can find many places where I can replace refcounter with kref by doing "grep -r atomic_dec_and_test linux/".
If we have this detection of kref_put() with unreferenced object, The work of kref convertions would be more than code cleanup and consolidation.
Re: [patch 1/4] kref: warn kref_put() with unreferenced kref
In reply to Akinobu MitaQuoted 4 lines · Akinobu Mita
lib/kref.c +0
-0@@ -56,12 +57,13 @@ int kref_put(struct kref *kref, void (*r* if current count is one, we are the last user and can release object * right now, avoiding an atomic operation on 'refcount' */
lib/kref.c int kref_put(struct kref *kref, void (*r · line 63
Quoted 10 lines
lib/kref.c +4
-6-if ((atomic_read (&kref->refcount) ==1 ) || - (atomic_dec_and_test (&kref->refcount))) { -release (kref); -return 1 ; - } -return 0 ; +if (atomic_read (&kref->refcount) ==1 ) +atomic_set (&kref->refcount,0 ); +else if (!atomic_dec_and_test (&kref->refcount)) +return 0 ;
This is WRONG. The refcount can be incremented (to two) AFTER the atomic_read ==1 and the "atomic_set". The original code seems ok in this respect.
Roger.
Signature · 5 lines
** [email protected] ** http://www.BitWizard.nl/ ** +31-15-2600998 ** *-- BitWizard writes Linux device drivers for any device you may have! --* Q: It doesn't work. A: Look buddy, doesn't work is an ambiguous statement. Does it sit on the couch all day? Is it unemployed? Please be specific! Define 'it' and what it isn't doing. --------- Adapted from lxrbot FAQ