login
Header Space

 
 

[PATCH] Slub Freeoffset check overflow (updated)

Score:
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]
To: Christoph Lameter <clameter@...>
Cc: Eric Dumazet <dada1@...>, Pekka Enberg <penberg@...>, Torsten Kaiser <just.for.lkml@...>, Ingo Molnar <mingo@...>, Linus Torvalds <torvalds@...>, Linux Kernel Mailing List <linux-kernel@...>
Date: Tuesday, March 4, 2008 - 2:17 am

Check for overflow of the freeoffset version number.

I just thought adding this check in CONFIG_SLUB_DEBUG makes sense. It's
really unlikely that enough interrupt handlers will nest over the slub
fast path, and each of them do about a million alloc/free on 32 bits or
a huge amount of alloc/free on 64 bits, but just in case, it seems good
to warn if we detect we are half-way to a version overflow.

Changelog :
- Mask out the LSB because of alloc fast path. See comment in source.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@polymtl.ca>
---
 mm/slub.c |   40 ++++++++++++++++++++++++++++++++++------
 1 file changed, 34 insertions(+), 6 deletions(-)

Index: linux-2.6-lttng/mm/slub.c
===================================================================
--- linux-2.6-lttng.orig/mm/slub.c	2008-03-04 00:59:01.000000000 -0500
+++ linux-2.6-lttng/mm/slub.c	2008-03-04 01:03:44.000000000 -0500
@@ -1660,7 +1660,7 @@ static __always_inline void *slab_alloc(
  */
 
 #ifdef SLUB_FASTPATH
-	unsigned long freeoffset, newoffset;
+	unsigned long freeoffset, newoffset, resoffset;
 
 	c = get_cpu_slab(s, raw_smp_processor_id());
 	do {
@@ -1682,8 +1682,22 @@ static __always_inline void *slab_alloc(
 		newoffset = freeoffset;
 		newoffset &= ~c->off_mask;
 		newoffset |= (unsigned long)object[c->offset] & c->off_mask;
-	} while (cmpxchg_local(&c->freeoffset, freeoffset, newoffset)
-			!= freeoffset);
+		resoffset = cmpxchg_local(&c->freeoffset, freeoffset,
+				newoffset);
+#ifdef CONFIG_SLUB_DEBUG
+		/*
+		 * Just to be paranoid : warn if we detect that enough free or
+		 * slow paths nested on top of us to get the counter to go
+		 * half-way to overflow. That would be insane to do that much
+		 * allocations/free in interrupt handers, but check it anyway.
+		 * Mask out the LSBs because alloc fast path does not increment
+		 * the sequence number, which may cause the overall values to go
+		 * backward.
+		 */
+		WARN_ON((resoffset & ~c->off_mask)
+			- (freeoffset & ~c->off_mask) > -1UL >> 1);
+#endif
+	} while (resoffset != freeoffset);
 #else
 	unsigned long flags;
 
@@ -1822,7 +1836,7 @@ static __always_inline void slab_free(st
 	struct kmem_cache_cpu *c;
 
 #ifdef SLUB_FASTPATH
-	unsigned long freeoffset, newoffset;
+	unsigned long freeoffset, newoffset, resoffset;
 
 	c = get_cpu_slab(s, raw_smp_processor_id());
 	debug_check_no_locks_freed(object, s->objsize);
@@ -1850,8 +1864,22 @@ static __always_inline void slab_free(st
 		newoffset = freeoffset + c->off_mask + 1;
 		newoffset &= ~c->off_mask;
 		newoffset |= (unsigned long)object & c->off_mask;
-	} while (cmpxchg_local(&c->freeoffset, freeoffset, newoffset)
-			!= freeoffset);
+		resoffset = cmpxchg_local(&c->freeoffset, freeoffset,
+				newoffset);
+#ifdef CONFIG_SLUB_DEBUG
+		/*
+		 * Just to be paranoid : warn if we detect that enough free or
+		 * slow paths nested on top of us to get the counter to go
+		 * half-way to overflow. That would be insane to do that much
+		 * allocations/free in interrupt handers, but check it anyway.
+		 * Mask out the LSBs because alloc fast path does not increment
+		 * the sequence number, which may cause the overall values to go
+		 * backward.
+		 */
+		WARN_ON((resoffset & ~c->off_mask)
+			- (freeoffset & ~c->off_mask) > -1UL >> 1);
+#endif
+	} while (resoffset != freeoffset);
 #else
 	unsigned long flags;
 
-- 
Mathieu Desnoyers
Computer Engineering Ph.D. Student, Ecole Polytechnique de Montreal
OpenPGP key fingerprint: 8CD5 52C3 8E3C 4140 715F  BA06 3F25 A8FE 3BAE 9A68
--
Previous message: [thread] [date] [author]
Next message: [thread] [date] [author]

Messages in current thread:
Linux 2.6.25-rc2, Linus Torvalds, (Fri Feb 15, 5:23 pm)
Re: Linux 2.6.25-rc2, Torsten Kaiser, (Sat Feb 16, 5:38 pm)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 2:11 am)
Re: Linux 2.6.25-rc2, Torsten Kaiser, (Tue Feb 19, 2:54 am)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Tue Feb 19, 3:21 am)
Re: Linux 2.6.25-rc2, Mathieu Desnoyers, (Tue Feb 19, 10:02 am)
Re: Linux 2.6.25-rc2, Torsten Kaiser, (Tue Feb 19, 2:39 pm)
Re: Linux 2.6.25-rc2, Eric Dumazet, (Tue Feb 19, 12:27 pm)
Re: Linux 2.6.25-rc2, Christoph Lameter, (Wed Feb 27, 7:32 pm)
Re: Linux 2.6.25-rc2, Mathieu Desnoyers, (Tue Feb 19, 4:03 pm)
Re: Linux 2.6.25-rc2, Christoph Lameter, (Wed Feb 27, 7:34 pm)
[PATCH] Implement slub fastpath in terms of freebase and fre..., Mathieu Desnoyers, (Thu Feb 28, 1:55 am)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Christoph Lameter, (Thu Feb 28, 3:08 pm)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Mathieu Desnoyers, (Thu Feb 28, 7:25 pm)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Christoph Lameter, (Thu Feb 28, 8:57 pm)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Mathieu Desnoyers, (Thu Feb 28, 9:56 pm)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Christoph Lameter, (Thu Feb 28, 10:12 pm)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Mathieu Desnoyers, (Thu Feb 28, 11:32 pm)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Christoph Lameter, (Fri Feb 29, 1:11 am)
[PATCH] Slub Freeoffset check overflow, Mathieu Desnoyers, (Fri Feb 29, 9:28 am)
[PATCH] Slub Freeoffset check overflow (updated), Mathieu Desnoyers, (Tue Mar 4, 2:17 am)
Re: [PATCH] Slub Freeoffset check overflow (updated), Christoph Lameter, (Tue Mar 4, 3:15 am)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Mathieu Desnoyers, (Fri Feb 29, 9:03 am)
Re: [PATCH] Implement slub fastpath in terms of freebase and..., Christoph Lameter, (Fri Feb 29, 3:57 pm)
Re: Linux 2.6.25-rc2, Linus Torvalds, (Tue Feb 19, 12:38 pm)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Tue Feb 19, 10:21 am)
Re: Linux 2.6.25-rc2, Mathieu Desnoyers, (Tue Feb 19, 4:08 pm)
Re: Linux 2.6.25-rc2, Christoph Lameter, (Wed Feb 27, 7:32 pm)
Re: Linux 2.6.25-rc2, Andrew Morton, (Wed Feb 27, 9:57 pm)
Re: Linux 2.6.25-rc2, Jiri Kosina, (Thu Feb 28, 7:13 am)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Thu Feb 28, 4:14 am)
Re: Linux 2.6.25-rc2, Alan Cox, (Thu Feb 28, 7:15 am)
Re: Linux 2.6.25-rc2, Christoph Lameter, (Wed Feb 27, 10:43 pm)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Tue Feb 19, 10:38 am)
Re: Linux 2.6.25-rc2, Linus Torvalds, (Tue Feb 19, 12:20 pm)
Re: Linux 2.6.25-rc2, Torsten Kaiser, (Tue Feb 19, 3:27 pm)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 12:45 pm)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 12:48 pm)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 10:55 am)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Tue Feb 19, 11:52 am)
Re: Linux 2.6.25-rc2, Zhang, Yanmin, (Tue Feb 19, 8:36 pm)
Re: Linux 2.6.25-rc2, Zhang, Yanmin, (Tue Feb 19, 10:08 pm)
Re: Linux 2.6.25-rc2, Zhang, Yanmin, (Wed Feb 20, 2:53 am)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Wed Feb 20, 3:10 am)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 10:57 am)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Tue Feb 19, 11:54 am)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 6:27 am)
Re: Linux 2.6.25-rc2, Mathieu Desnoyers, (Tue Feb 19, 9:02 am)
Re: Linux 2.6.25-rc2, Ingo Molnar, (Tue Feb 19, 10:00 am)
Re: Linux 2.6.25-rc2, Pekka Enberg, (Tue Feb 19, 6:45 am)
Re: Linux 2.6.25-rc2, Linus Torvalds, (Mon Feb 18, 7:54 pm)
Re: Linux 2.6.25-rc2, Torsten Kaiser, (Tue Feb 19, 2:44 am)
Re: Linux 2.6.25-rc2, Rafael J. Wysocki, (Sun Feb 17, 4:25 pm)
Re: Linux 2.6.25-rc2, Torsten Kaiser, (Sun Feb 17, 5:32 pm)
Linux 2.6.25-rc2 regression: LVM cannot find volume group, Tilman Schmidt, (Sat Feb 16, 3:14 pm)
Re: Linux 2.6.25-rc2 regression: LVM cannot find volume group, Alasdair G Kergon, (Mon Feb 18, 9:53 pm)
Re: Linux 2.6.25-rc2, Jan Engelhardt, (Sat Feb 16, 12:52 pm)
Re: [BUG] Linux 2.6.25-rc2 - Regression from 2.6.24-rc1-git1..., Rafael J. Wysocki, (Sun Feb 17, 4:08 pm)
Re: [BUG] Linux 2.6.25-rc2 - Regression from 2.6.24-rc1-git1..., KAMEZAWA Hiroyuki, (Tue Feb 19, 4:04 am)
Re: [BUG] Linux 2.6.25-rc2 - Regression from 2.6.24-rc1-git1..., KAMEZAWA Hiroyuki, (Tue Feb 19, 5:02 am)
Re: [BUG] Linux 2.6.25-rc2 - Regression from 2.6.24-rc1-git1..., KAMEZAWA Hiroyuki, (Tue Feb 19, 4:47 am)
Re: [BUG] Linux 2.6.25-rc2 - Regression from 2.6.24-rc1-git1..., KAMEZAWA Hiroyuki, (Tue Feb 19, 5:07 am)
[BUG] Linux 2.6.25-rc2 - Kernel Ooops while running dbench, Kamalesh Babulal, (Sat Feb 16, 1:44 am)
Re: Linux 2.6.25-rc2, Rafael J. Wysocki, (Fri Feb 15, 10:08 pm)
speck-geostationary