Skip to contentSkip to conversation

mm: slab - __cache_alloc NULL prefetch fix

PATCH Cyrill Gorcunov 4 participants 8 messages Nov 20, 2008 Memory management

lore
Share & tools
lore
#0 lore raw

Do not prefetch NULL

---

Please check

 mm/slab.c |    5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
mm/slab.c +3 -2
Index: linux-2.6.git/mm/slab.c
===================================================================
@@ -3395,10 +3395,11 @@ __cache_alloc(struct kmem_cache *cachep,
 	objp = __do_cache_alloc(cachep, flags);
 	local_irq_restore(save_flags);
 	objp = cache_alloc_debugcheck_after(cachep, flags, objp, caller);
-	prefetchw(objp);
 
-	if (likely(objp))
+	if (likely(objp)) {
+		prefetchw(objp);
 		kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
+	}
 
 	if (unlikely((flags & __GFP_ZERO) && objp))
 		memset(objp, 0, obj_size(cachep));
#1 lore raw

[Cyrill Gorcunov - Thu, Nov 20, 2008 at 07:44:00PM +0300]

| Do not prefetch NULL
| 
| Signed-off-by: Cyrill Gorcunov <[email protected]>
| ---
| 
| Please check
|

...

even having prefetch(NULL) as valid we do check if object is not NULL anyway so I thought there is no need to prefetch(NULL) in case of fail.

- Cyrill -

#2 lore raw
Quoted 3 lines · Cyrill Gorcunov

even having prefetch(NULL) as valid we do check if object is not NULL anyway so I thought there is no need to prefetch(NULL) in case of fail.

The object is only checked if SLAB_DEBUG is on.

#3 lore raw

[Christoph Lameter - Thu, Nov 20, 2008 at 12:16:28PM -0600]

| On Thu, 20 Nov 2008, Cyrill Gorcunov wrote:
| 
| > even having prefetch(NULL) as valid we do check if
| > object is not NULL anyway so I thought there is no
| > need to prefetch(NULL) in case of fail.
| 
| The object is only checked if SLAB_DEBUG is on.
| 

My fault Christoph, I've been messed by kmemcheck_slab_alloc which in turn actually becomes zero function if not tuned on. So -- all is fine, sorry for noise.

- Cyrill -

#5 lore raw
In reply to Cyrill Gorcunov

mm/slab.c __cache_alloc(struct kmem_cache *cachep, · line 3402

Quoted 7 lines · Cyrill Gorcunov
mm/slab.c +3 -2
-	prefetchw(objp);
 
-	if (likely(objp))
+	if (likely(objp)) {
+		prefetchw(objp);
 		kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
+	}

Although it probably makes sense to not bother prefetching NULL, I also need to wonder how useful it is to prefetch something that we then turn around and dereference in the very next line of code.

Maybe we should just lose the prefetch entirely?

#6 lore raw
Quoted 4 lines · [email protected] wrote:
Quoted 2 lines · Cyrill Gorcunov
mm/slab.c +0 -1
-	prefetchw(objp);
Quoted 9 lines

mm/slab.c __cache_alloc(struct kmem_cache *cachep, · line 3402

Quoted 5 lines
mm/slab.c +3 -1
-	if (likely(objp))
+	if (likely(objp)) {
+		prefetchw(objp);
 		kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
+	}

Although it probably makes sense to not bother prefetching NULL, I also need to wonder how useful it is to prefetch something that we then turn around and dereference in the very next line of code.

Note that kmemcheck_slab_alloc() is going to be a no-op on anything but a special developer or tester debug config.

#7 lore raw
In reply to [email protected]

[[email protected] - Thu, Nov 20, 2008 at 02:58:28PM -0500]

| On Thu, 20 Nov 2008 19:44:00 +0300, Cyrill Gorcunov said:
| 
| > -	prefetchw(objp);
| >  
| > -	if (likely(objp))
| > +	if (likely(objp)) {
| > +		prefetchw(objp);
| >  		kmemcheck_slab_alloc(cachep, flags, objp, obj_size(cachep));
| > +	}
| 
| Although it probably makes sense to not bother prefetching NULL, I also
| need to wonder how useful it is to prefetch something that we then
| turn around and dereference in the very next line of code.
| 
| Maybe we should just lose the prefetch entirely?

In case of obj == NULL it would not be a really penalty.
I heard there was a problem passing NULL to prefetch on some PPC machines but PPC code already well protected against it. So in most cases obj would not be null and hint cpu that we will use this obj soon is good choise
I think. In case of kmemcheck it's true if only kmemcheck was turned on, otherwise this if(obj) with empty kmemcheck_slab_alloc body will be eliminated by compiler but in case of having there (inside of 'if' block) prefetchw(objp) I'm not sure what gcc will decide :-) Anyway -- it was a false alram from me since having prefetch in original place is not problem. And maybe modern cpus even have a special trap for NULL prefetch target and eliminate it out on decoding stage without penalty (just a guess).

- Cyrill -