Skip to contentSkip to conversation

slub: prefetch next freelist pointer in __slab_alloc()

PATCH Applied Joonsoo Kim 4 participants 26 messages Jun 22, 2012 – Aug 16, 2012

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/3 slub: prefetch next freelist pointer in __slab_alloc()

Commit 0ad9500e16fe24aa55809a2b00e0d2d0e658fc71 ('slub: prefetch next freelist pointer in slab_alloc') add prefetch instruction to fast path of allocation.

Same benefit is also available in slow path of allocation, but it is not large portion of overall allocation. Nevertheless we could get some benifit from it, so prefetch next freelist pointer in __slab_alloc.

mm/slub.c +1 -0
index f96d8bc..92f1c0e 100644
@@ -2248,6 +2248,7 @@ load_freelist:
 	VM_BUG_ON(!c->page->frozen);
 	c->freelist = get_freepointer(s, freelist);
 	c->tid = next_tid(c->tid);
+	prefetch_freepointer(s, c->freelist);
 	local_irq_restore(flags);
 	return freelist;
 
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#1 lore raw

PATCH 2/3 slub: reduce failure of this_cpu_cmpxchg in put_cpu_partial() after unfreezing

In current implementation, after unfreezing, we doesn't touch oldpage, so it remain 'NOT NULL'. When we call this_cpu_cmpxchg() with this old oldpage, this_cpu_cmpxchg() is mostly be failed.

We can change value of oldpage to NULL after unfreezing, because unfreeze_partial() ensure that all the cpu partial slabs is removed from cpu partial list. In this time, we could expect that this_cpu_cmpxchg is mostly succeed.

mm/slub.c +1 -0
index 92f1c0e..531d8ed 100644
@@ -1968,6 +1968,7 @@ int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 				local_irq_save(flags);
 				unfreeze_partials(s);
 				local_irq_restore(flags);
+				oldpage = NULL;
 				pobjects = 0;
 				pages = 0;
 				stat(s, CPU_PARTIAL_DRAIN);
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#5 lore raw

PATCH 3/3 slub: release a lock if freeing object with a lock is failed in __slab_free()

In reply to Joonsoo Kim

In some case of __slab_free(), we need a lock for manipulating partial list. If freeing object with a lock is failed, a lock doesn't needed anymore for some reasons.

Case 1. prior is NULL, kmem_cache_debug(s) is true

In this case, another free is occured before our free is succeed. When slab is full(prior is NULL), only possible operation is slab_free(). So in this case, we guess another free is occured.
It may make a slab frozen, so lock is not needed anymore.

Case 2. inuse is NULL

In this case, acquire_slab() is occured before out free is succeed. We have a last object for slab, so other operation for this slab is not possible except acquire_slab().
Acquire_slab() makes a slab frozen, so lock is not needed anymore.

Above two reason explain why we don't need a lock
when freeing object with a lock is failed.

So, when cmpxchg_double_slab() is failed, releasing a lock is more suitable. This may reduce lock contention.

This also make logic somehow simple that 'was_frozen with a lock' case is never occured. Remove it.

mm/slub.c +14 -20
index 531d8ed..3e0b9db 100644
@@ -2438,7 +2438,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 	void *prior;
 	void **object = (void *)x;
 	int was_frozen;
-	int inuse;
 	struct page new;
 	unsigned long counters;
 	struct kmem_cache_node *n = NULL;
@@ -2450,13 +2449,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 		return;
 
 	do {
+		if (unlikely(n)) {
+			spin_unlock_irqrestore(&n->list_lock, flags);
+			n = NULL;
+		}
 		prior = page->freelist;
 		counters = page->counters;
 		set_freepointer(s, object, prior);
 		new.counters = counters;
 		was_frozen = new.frozen;
 		new.inuse--;
-		if ((!new.inuse || !prior) && !was_frozen && !n) {
+		if ((!new.inuse || !prior) && !was_frozen) {
 
 			if (!kmem_cache_debug(s) && !prior)
 
@@ -2481,7 +2484,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 
 			}
 		}
-		inuse = new.inuse;
 
 	} while (!cmpxchg_double_slab(s, page,
 		prior, counters,
@@ -2507,25 +2509,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
                 return;
         }
 
+	if (unlikely(!new.inuse && n->nr_partial > s->min_partial))
+		goto slab_empty;
+
 	/*
-	 * was_frozen may have been set after we acquired the list_lock in
-	 * an earlier loop. So we need to check it here again.
+	 * Objects left in the slab. If it was not on the partial list before
+	 * then add it.
 	 */
-	if (was_frozen)
-		stat(s, FREE_FROZEN);
-	else {
-		if (unlikely(!inuse && n->nr_partial > s->min_partial))
-                        goto slab_empty;
-
-		/*
-		 * Objects left in the slab. If it was not on the partial list before
-		 * then add it.
-		 */
-		if (unlikely(!prior)) {
-			remove_full(s, page);
-			add_partial(n, page, DEACTIVATE_TO_TAIL);
-			stat(s, FREE_ADD_PARTIAL);
-		}
+	if (kmem_cache_debug(s) && unlikely(!prior)) {
+		remove_full(s, page);
+		add_partial(n, page, DEACTIVATE_TO_TAIL);
+		stat(s, FREE_ADD_PARTIAL);
 	}
 	spin_unlock_irqrestore(&n->list_lock, flags);
 	return;
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#14 lore raw

PATCH v2 1/3 slub: prefetch next freelist pointer in __slab_alloc()

In reply to Joonsoo Kim

Commit 0ad9500e16fe24aa55809a2b00e0d2d0e658fc71 ('slub: prefetch next freelist pointer in slab_alloc') add prefetch instruction to fast path of allocation.

Same benefit is also available in slow path of allocation, but it is not large portion of overall allocation. Nevertheless we could get some benifit from it, so prefetch next freelist pointer in __slab_alloc.

---
Add 'Cc: Eric Dumazet <[email protected]>'

mm/slub.c +1 -0
index f96d8bc..92f1c0e 100644
@@ -2248,6 +2248,7 @@ load_freelist:
 	VM_BUG_ON(!c->page->frozen);
 	c->freelist = get_freepointer(s, freelist);
 	c->tid = next_tid(c->tid);
+	prefetch_freepointer(s, c->freelist);
 	local_irq_restore(flags);
 	return freelist;
 
Signature · 7 lines
1.7.9.5

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to [email protected].  For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>
#16 lore raw
In reply to Joonsoo Kim
Quoted 18 lines · Joonsoo Kim

Commit 0ad9500e16fe24aa55809a2b00e0d2d0e658fc71 ('slub: prefetch next freelist pointer in slab_alloc') add prefetch instruction to fast path of allocation.

Same benefit is also available in slow path of allocation, but it is not large portion of overall allocation. Nevertheless we could get some benifit from it, so prefetch next freelist pointer in __slab_alloc.

---
Add 'Cc: Eric Dumazet <[email protected]>'

mm/slub.c +0 -0
index f96d8bc..92f1c0e 100644

mm/slub.c load_freelist: · line 2253

Quoted 6 lines
mm/slub.c +1 -0
@@ -2248,6 +2248,7 @@ load_freelist:
        VM_BUG_ON(!c->page->frozen);
        c->freelist = get_freepointer(s, freelist);
        c->tid = next_tid(c->tid);
+       prefetch_freepointer(s, c->freelist);
        local_irq_restore(flags);
        return freelist;

Well, can you show improvement in any benchmark or workload? Prefetching is not always an obvious win and the reason we merged Eric's patch was that he was able to show an improvement in hackbench.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#2 lore raw

Re: [PATCH 2/3] slub: reduce failure of this_cpu_cmpxchg in put_cpu_partial() after unfreezing

In reply to Joonsoo Kim
Quoted 16 lines · Joonsoo Kim

In current implementation, after unfreezing, we doesn't touch oldpage, so it remain 'NOT NULL'. When we call this_cpu_cmpxchg() with this old oldpage, this_cpu_cmpxchg() is mostly be failed.

We can change value of oldpage to NULL after unfreezing, because unfreeze_partial() ensure that all the cpu partial slabs is removed from cpu partial list. In this time, we could expect that this_cpu_cmpxchg is mostly succeed.

mm/slub.c +0 -0
index 92f1c0e..531d8ed 100644

mm/slub.c int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) · line 1974

Quoted 7 lines
mm/slub.c +1 -0
@@ -1968,6 +1968,7 @@ int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
                                local_irq_save(flags);
                                unfreeze_partials(s);
                                local_irq_restore(flags);
+                               oldpage = NULL;
                                pobjects = 0;
                                pages = 0;
                                stat(s, CPU_PARTIAL_DRAIN);

Makes sense. Christoph, David?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#6 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

In reply to Joonsoo Kim
Quoted 93 lines · Joonsoo Kim

In some case of __slab_free(), we need a lock for manipulating partial list. If freeing object with a lock is failed, a lock doesn't needed anymore for some reasons.

Case 1. prior is NULL, kmem_cache_debug(s) is true

In this case, another free is occured before our free is succeed. When slab is full(prior is NULL), only possible operation is slab_free(). So in this case, we guess another free is occured.
It may make a slab frozen, so lock is not needed anymore.

Case 2. inuse is NULL

In this case, acquire_slab() is occured before out free is succeed. We have a last object for slab, so other operation for this slab is not possible except acquire_slab().
Acquire_slab() makes a slab frozen, so lock is not needed anymore.

Above two reason explain why we don't need a lock
when freeing object with a lock is failed.

So, when cmpxchg_double_slab() is failed, releasing a lock is more suitable. This may reduce lock contention.

This also make logic somehow simple that 'was_frozen with a lock' case is never occured. Remove it.

mm/slub.c +10 -17
index 531d8ed..3e0b9db 100644
@@ -2438,7 +2438,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
        void *prior;
        void **object = (void *)x;
        int was_frozen;
-       int inuse;
        struct page new;
        unsigned long counters;
        struct kmem_cache_node *n = NULL;
@@ -2450,13 +2449,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
                return;
 
        do {
+               if (unlikely(n)) {
+                       spin_unlock_irqrestore(&n->list_lock, flags);
+                       n = NULL;
+               }
                prior = page->freelist;
                counters = page->counters;
                set_freepointer(s, object, prior);
                new.counters = counters;
                was_frozen = new.frozen;
                new.inuse--;
-               if ((!new.inuse || !prior) && !was_frozen && !n) {
+               if ((!new.inuse || !prior) && !was_frozen) {
 
                        if (!kmem_cache_debug(s) && !prior)
 
@@ -2481,7 +2484,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 
                        }
                }
-               inuse = new.inuse;
 
        } while (!cmpxchg_double_slab(s, page,
                prior, counters,
@@ -2507,25 +2509,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
                 return;
         }
 
+       if (unlikely(!new.inuse && n->nr_partial > s->min_partial))
+               goto slab_empty;
+
        /*
-        * was_frozen may have been set after we acquired the list_lock in
-        * an earlier loop. So we need to check it here again.
+        * Objects left in the slab. If it was not on the partial list before
+        * then add it.
         */
-       if (was_frozen)
-               stat(s, FREE_FROZEN);
-       else {
-               if (unlikely(!inuse && n->nr_partial > s->min_partial))
-                        goto slab_empty;
-
-               /*
-                * Objects left in the slab. If it was not on the partial list before
-                * then add it.
-                */
-               if (unlikely(!prior)) {
-                       remove_full(s, page);

mm/slub.c static void __slab_free(struct kmem_cache *s, struct page *page, · line 2525

Quoted 10 lines
mm/slub.c +4 -3
-                       add_partial(n, page, DEACTIVATE_TO_TAIL);
-                       stat(s, FREE_ADD_PARTIAL);
-               }
+       if (kmem_cache_debug(s) && unlikely(!prior)) {
+               remove_full(s, page);
+               add_partial(n, page, DEACTIVATE_TO_TAIL);
+               stat(s, FREE_ADD_PARTIAL);
        }
        spin_unlock_irqrestore(&n->list_lock, flags);
        return;

I'm confused. Does this fix a bug or is it an optimization?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#17 lore raw
In reply to Pekka Enberg

2012/7/4 Pekka Enberg <[email protected]>:

Quoted 3 lines

Well, can you show improvement in any benchmark or workload? Prefetching is not always an obvious win and the reason we merged Eric's patch was that he was able to show an improvement in hackbench.

I thinks that this patch is perfectly same effect as Eric's patch, so doesn't include benchmark result.
Eric's patch which add "prefetch instruction" in fastpath works for second ~ last object of cpu slab.
This patch which add "prefetch instrunction" in slowpath works for first object of cpu slab.

But, I do test "./perf stat -r 20 ./hackbench 50 process 4000 > /dev/null" and gain following outputs.

***** vanilla *****

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     114189.571311 task-clock                #    7.924 CPUs utilized
          ( +-  0.29% )
         2,978,515 context-switches          #    0.026 M/sec
          ( +-  3.45% )
           102,635 CPU-migrations            #    0.899 K/sec
          ( +-  5.63% )
           123,948 page-faults               #    0.001 M/sec
          ( +-  0.16% )
   422,477,120,134 cycles                    #    3.700 GHz
          ( +-  0.29% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,943,851,074 instructions # 0.60 insns per
cycle ( +- 0.14% )

    46,214,207,979 branches                  #  404.715 M/sec
          ( +-  0.15% )
       215,342,095 branch-misses             #    0.47% of all

branches ( +- 0.53% )

      14.409990448 seconds time elapsed
          ( +-  0.30% )

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     114576.053284 task-clock                #    7.921 CPUs utilized
          ( +-  0.35% )
         2,810,138 context-switches          #    0.025 M/sec
          ( +-  3.21% )
            85,641 CPU-migrations            #    0.747 K/sec
          ( +-  5.05% )
           124,299 page-faults               #    0.001 M/sec
          ( +-  0.18% )
   423,906,539,517 cycles                    #    3.700 GHz
          ( +-  0.35% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,354,351,283 instructions # 0.59 insns per
cycle ( +- 0.13% )

    46,098,601,012 branches                  #  402.341 M/sec
          ( +-  0.13% )
       213,448,657 branch-misses             #    0.46% of all

branches ( +- 0.50% )

      14.464325969 seconds time elapsed
          ( +-  0.34% )

***** patch applied *****

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     112935.199731 task-clock                #    7.926 CPUs utilized
          ( +-  0.29% )
         2,810,157 context-switches          #    0.025 M/sec
          ( +-  2.95% )
           104,278 CPU-migrations            #    0.923 K/sec
          ( +-  6.83% )
           123,999 page-faults               #    0.001 M/sec
          ( +-  0.17% )
   417,834,406,420 cycles                    #    3.700 GHz
          ( +-  0.29% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,291,523,926 instructions # 0.60 insns per
cycle ( +- 0.11% )

    46,083,091,476 branches                  #  408.049 M/sec
          ( +-  0.12% )
       213,714,228 branch-misses             #    0.46% of all

branches ( +- 0.43% )

      14.248980376 seconds time elapsed
          ( +-  0.29% )

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     113640.944855 task-clock                #    7.926 CPUs utilized
          ( +-  0.28% )
         2,776,983 context-switches          #    0.024 M/sec
          ( +-  5.66% )
            95,962 CPU-migrations            #    0.844 K/sec
          ( +- 10.69% )
           123,849 page-faults               #    0.001 M/sec
          ( +-  0.15% )
   420,446,572,595 cycles                    #    3.700 GHz
          ( +-  0.28% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,174,259,429 instructions # 0.60 insns per
cycle ( +- 0.21% )

    46,060,683,039 branches                  #  405.318 M/sec
          ( +-  0.23% )
       213,480,999 branch-misses             #    0.46% of all

branches ( +- 0.75% )

      14.336843534 seconds time elapsed
          ( +-  0.28% )

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#7 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

In reply to Pekka Enberg

2012/7/4 Pekka Enberg <[email protected]>:

Quoted 98 lines · Joonsoo Kim
Quoted 97 lines

In some case of __slab_free(), we need a lock for manipulating partial list. If freeing object with a lock is failed, a lock doesn't needed anymore for some reasons.

Case 1. prior is NULL, kmem_cache_debug(s) is true

In this case, another free is occured before our free is succeed. When slab is full(prior is NULL), only possible operation is slab_free(). So in this case, we guess another free is occured.
It may make a slab frozen, so lock is not needed anymore.

Case 2. inuse is NULL

In this case, acquire_slab() is occured before out free is succeed. We have a last object for slab, so other operation for this slab is not possible except acquire_slab().
Acquire_slab() makes a slab frozen, so lock is not needed anymore.

Above two reason explain why we don't need a lock
when freeing object with a lock is failed.

So, when cmpxchg_double_slab() is failed, releasing a lock is more suitable. This may reduce lock contention.

This also make logic somehow simple that 'was_frozen with a lock' case is never occured. Remove it.

mm/slub.c +11 -20
index 531d8ed..3e0b9db 100644
@@ -2438,7 +2438,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
        void *prior;
        void **object = (void *)x;
        int was_frozen;
-       int inuse;
        struct page new;
        unsigned long counters;
        struct kmem_cache_node *n = NULL;
@@ -2450,13 +2449,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
                return;
 
        do {
+               if (unlikely(n)) {
+                       spin_unlock_irqrestore(&n->list_lock, flags);
+                       n = NULL;
+               }
                prior = page->freelist;
                counters = page->counters;
                set_freepointer(s, object, prior);
                new.counters = counters;
                was_frozen = new.frozen;
                new.inuse--;
-               if ((!new.inuse || !prior) && !was_frozen && !n) {
+               if ((!new.inuse || !prior) && !was_frozen) {
 
                        if (!kmem_cache_debug(s) && !prior)
 
@@ -2481,7 +2484,6 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
 
                        }
                }
-               inuse = new.inuse;
 
        } while (!cmpxchg_double_slab(s, page,
                prior, counters,
@@ -2507,25 +2509,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,
                 return;
         }
 
+       if (unlikely(!new.inuse && n->nr_partial > s->min_partial))
+               goto slab_empty;
+
        /*
-        * was_frozen may have been set after we acquired the list_lock in
-        * an earlier loop. So we need to check it here again.
+        * Objects left in the slab. If it was not on the partial list before
+        * then add it.
         */
-       if (was_frozen)
-               stat(s, FREE_FROZEN);
-       else {
-               if (unlikely(!inuse && n->nr_partial > s->min_partial))
-                        goto slab_empty;
-
-               /*
-                * Objects left in the slab. If it was not on the partial list before
-                * then add it.
-                */
-               if (unlikely(!prior)) {
-                       remove_full(s, page);
-                       add_partial(n, page, DEACTIVATE_TO_TAIL);
-                       stat(s, FREE_ADD_PARTIAL);
-               }
+       if (kmem_cache_debug(s) && unlikely(!prior)) {
Quoted 8 lines

mm/slub.c static void __slab_free(struct kmem_cache *s, struct page *page, · line 2525

Quoted 6 lines
mm/slub.c +3 -0
+               remove_full(s, page);
+               add_partial(n, page, DEACTIVATE_TO_TAIL);
+               stat(s, FREE_ADD_PARTIAL);
        }
        spin_unlock_irqrestore(&n->list_lock, flags);
        return;

I'm confused. Does this fix a bug or is it an optimization?

It is for reducing lock contention and code clean-up.

If we aquire a lock and cmpxchg_double failed, we do releasing a lock. This result in reducing lock contention.
And this remove "was_frozen and having a lock" case, so logic slightly simpler than before.

Commit message which confuse u means that this patch do not decrease performance for two reasons.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#18 lore raw
In reply to JoonSoo Kim
Quoted 4 lines

2012/7/4 Pekka Enberg <[email protected]>:

Quoted 3 lines

Well, can you show improvement in any benchmark or workload? Prefetching is not always an obvious win and the reason we merged Eric's patch was that he was able to show an improvement in hackbench.

Quoted 6 lines · JoonSoo Kim

I thinks that this patch is perfectly same effect as Eric's patch, so doesn't include benchmark result.
Eric's patch which add "prefetch instruction" in fastpath works for second ~ last object of cpu slab.
This patch which add "prefetch instrunction" in slowpath works for first object of cpu slab.

Prefetching can also have negative effect on overall performance:

http://lwn.net/Articles/444336/

Quoted 96 lines

But, I do test "./perf stat -r 20 ./hackbench 50 process 4000 > /dev/null" and gain following outputs.

***** vanilla *****

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     114189.571311 task-clock                #    7.924 CPUs utilized
          ( +-  0.29% )
         2,978,515 context-switches          #    0.026 M/sec
          ( +-  3.45% )
           102,635 CPU-migrations            #    0.899 K/sec
          ( +-  5.63% )
           123,948 page-faults               #    0.001 M/sec
          ( +-  0.16% )
   422,477,120,134 cycles                    #    3.700 GHz
          ( +-  0.29% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,943,851,074 instructions # 0.60 insns per
cycle ( +- 0.14% )

    46,214,207,979 branches                  #  404.715 M/sec
          ( +-  0.15% )
       215,342,095 branch-misses             #    0.47% of all

branches ( +- 0.53% )

      14.409990448 seconds time elapsed
          ( +-  0.30% )

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     114576.053284 task-clock                #    7.921 CPUs utilized
          ( +-  0.35% )
         2,810,138 context-switches          #    0.025 M/sec
          ( +-  3.21% )
            85,641 CPU-migrations            #    0.747 K/sec
          ( +-  5.05% )
           124,299 page-faults               #    0.001 M/sec
          ( +-  0.18% )
   423,906,539,517 cycles                    #    3.700 GHz
          ( +-  0.35% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,354,351,283 instructions # 0.59 insns per
cycle ( +- 0.13% )

    46,098,601,012 branches                  #  402.341 M/sec
          ( +-  0.13% )
       213,448,657 branch-misses             #    0.46% of all

branches ( +- 0.50% )

      14.464325969 seconds time elapsed
          ( +-  0.34% )

***** patch applied *****

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     112935.199731 task-clock                #    7.926 CPUs utilized
          ( +-  0.29% )
         2,810,157 context-switches          #    0.025 M/sec
          ( +-  2.95% )
           104,278 CPU-migrations            #    0.923 K/sec
          ( +-  6.83% )
           123,999 page-faults               #    0.001 M/sec
          ( +-  0.17% )
   417,834,406,420 cycles                    #    3.700 GHz
          ( +-  0.29% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,291,523,926 instructions # 0.60 insns per
cycle ( +- 0.11% )

    46,083,091,476 branches                  #  408.049 M/sec
          ( +-  0.12% )
       213,714,228 branch-misses             #    0.46% of all

branches ( +- 0.43% )

      14.248980376 seconds time elapsed
          ( +-  0.29% )

Performance counter stats for './hackbench 50 process 4000' (20 runs):

     113640.944855 task-clock                #    7.926 CPUs utilized
          ( +-  0.28% )
         2,776,983 context-switches          #    0.024 M/sec
          ( +-  5.66% )
            95,962 CPU-migrations            #    0.844 K/sec
          ( +- 10.69% )
           123,849 page-faults               #    0.001 M/sec
          ( +-  0.15% )
   420,446,572,595 cycles                    #    3.700 GHz
          ( +-  0.28% )

<not supported> stalled-cycles-frontend
<not supported> stalled-cycles-backend
251,174,259,429 instructions # 0.60 insns per
cycle ( +- 0.21% )

Quoted 7 lines
    46,060,683,039 branches                  #  405.318 M/sec
          ( +-  0.23% )
       213,480,999 branch-misses             #    0.46% of all

branches ( +- 0.75% )

      14.336843534 seconds time elapsed
          ( +-  0.28% )

That doesn't seem like that obvious win to me... Eric, Christoph?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#19 lore raw
Quoted 1 line · Pekka Enberg

That doesn't seem like that obvious win to me... Eric, Christoph?

Its the slow path. I am not convinced its useful on real workloads (not a benchmark)

I mean, if a workload hits badly slow path, some more important work should be done to avoid this at a higher level.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#23 lore raw
In reply to Pekka Enberg

2012/7/5 Pekka Enberg <[email protected]>:

Quoted 13 lines
Quoted 4 lines

2012/7/4 Pekka Enberg <[email protected]>:

Quoted 3 lines

Well, can you show improvement in any benchmark or workload? Prefetching is not always an obvious win and the reason we merged Eric's patch was that he was able to show an improvement in hackbench.

Quoted 6 lines · JoonSoo Kim

I thinks that this patch is perfectly same effect as Eric's patch, so doesn't include benchmark result.
Eric's patch which add "prefetch instruction" in fastpath works for second ~ last object of cpu slab.
This patch which add "prefetch instrunction" in slowpath works for first object of cpu slab.

Quoted 4 lines

Prefetching can also have negative effect on overall performance:

http://lwn.net/Articles/444336/

Thanks for good article which is very helpful to me.

Quoted 1 line

That doesn't seem like that obvious win to me... Eric, Christoph?

Could you tell me how I test this patch more deeply, plz? I am a kernel newbie and in the process of learning.
I doesn't know what I can do more for this.
I googling previous patch related to slub, some people use netperf.

Just do below is sufficient?
How is this test related to slub?

for in in seq 1 32
do
netperf -H 192.168.0.8 -v 0 -l -100000 -t TCP_RR > /dev/null &
done
wait

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#20 lore raw
In reply to Eric Dumazet

2012/7/5 Eric Dumazet <[email protected]>:

Quoted 6 lines

Its the slow path. I am not convinced its useful on real workloads (not a benchmark)

I mean, if a workload hits badly slow path, some more important work should be done to avoid this at a higher level.

In hackbench test, fast path allocation is about to 93%. Is it insufficient?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#24 lore raw
In reply to JoonSoo Kim
Quoted 13 lines · JoonSoo Kim
Quoted 3 lines

Prefetching can also have negative effect on overall performance:

http://lwn.net/Articles/444336/

Thanks for good article which is very helpful to me.

Quoted 1 line

That doesn't seem like that obvious win to me... Eric, Christoph?

Could you tell me how I test this patch more deeply, plz? I am a kernel newbie and in the process of learning.
I doesn't know what I can do more for this.
I googling previous patch related to slub, some people use netperf.

Quoted 8 lines

Just do below is sufficient?
How is this test related to slub?

for in in seq 1 32
do
netperf -H 192.168.0.8 -v 0 -l -100000 -t TCP_RR > /dev/null &
done
wait

The networking subsystem is sensitive to slab allocator performance which makes netperf an interesting benchmark, that's all.

As for slab benchmarking, you might want to look at what Mel Gorman has done in the past:

https://lkml.org/lkml/2009/2/16/252

For something like prefetch optimization, you'd really want to see a noticeable win in some benchmark. The kind of improvement you're seeing with your patch is likely to be lost in the noise - or even worse, cause negative performance for real world workloads.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#25 lore raw

2012/7/5 Pekka Enberg <[email protected]>:

Quoted 29 lines · JoonSoo Kim
Quoted 13 lines
Quoted 3 lines

Prefetching can also have negative effect on overall performance:

http://lwn.net/Articles/444336/

Thanks for good article which is very helpful to me.

Quoted 1 line

That doesn't seem like that obvious win to me... Eric, Christoph?

Could you tell me how I test this patch more deeply, plz? I am a kernel newbie and in the process of learning.
I doesn't know what I can do more for this.
I googling previous patch related to slub, some people use netperf.

Quoted 8 lines

Just do below is sufficient?
How is this test related to slub?

for in in seq 1 32
do
netperf -H 192.168.0.8 -v 0 -l -100000 -t TCP_RR > /dev/null &
done
wait

The networking subsystem is sensitive to slab allocator performance which makes netperf an interesting benchmark, that's all.

As for slab benchmarking, you might want to look at what Mel Gorman has done in the past:

Quoted 6 lines

https://lkml.org/lkml/2009/2/16/252

For something like prefetch optimization, you'd really want to see a noticeable win in some benchmark. The kind of improvement you're seeing with your patch is likely to be lost in the noise - or even worse, cause negative performance for real world workloads.

Okay.
Thanks for comments.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#21 lore raw
In reply to JoonSoo Kim
Quoted 10 lines · JoonSoo Kim

2012/7/5 Eric Dumazet <[email protected]>:

Quoted 6 lines

Its the slow path. I am not convinced its useful on real workloads (not a benchmark)

I mean, if a workload hits badly slow path, some more important work should be done to avoid this at a higher level.

In hackbench test, fast path allocation is about to 93%. Is it insufficient?

7% is insufficient I am afraid.

One prefetch() in the fast path serves 93% of the allocations, so added icache pressure is ok.

One prefetch() in slow path serves 7% of the allocations, do you see the difference ?

Adding a prefetch() is usually a win when a benchmark uses the path one million times per second.

But adding prefetches also increases kernel size and it hurts globally. (Latency of the kernel depends on its size, when cpu caches are cold)

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#22 lore raw

2012/7/5 Eric Dumazet <[email protected]>:

Quoted 17 lines · JoonSoo Kim
Quoted 10 lines

2012/7/5 Eric Dumazet <[email protected]>:

Quoted 6 lines

Its the slow path. I am not convinced its useful on real workloads (not a benchmark)

I mean, if a workload hits badly slow path, some more important work should be done to avoid this at a higher level.

In hackbench test, fast path allocation is about to 93%. Is it insufficient?

7% is insufficient I am afraid.

One prefetch() in the fast path serves 93% of the allocations, so added icache pressure is ok.

Quoted 9 lines

One prefetch() in slow path serves 7% of the allocations, do you see the difference ?

Adding a prefetch() is usually a win when a benchmark uses the path one million times per second.

But adding prefetches also increases kernel size and it hurts globally. (Latency of the kernel depends on its size, when cpu caches are cold)

Okay.
Thanks for comments which is very helpful to me.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#3 lore raw

Re: [PATCH 2/3] slub: reduce failure of this_cpu_cmpxchg in put_cpu_partial() after unfreezing

In reply to Pekka Enberg
Quoted 12 lines · Pekka Enberg
Quoted 11 lines · Joonsoo Kim

In current implementation, after unfreezing, we doesn't touch oldpage, so it remain 'NOT NULL'. When we call this_cpu_cmpxchg() with this old oldpage, this_cpu_cmpxchg() is mostly be failed.

We can change value of oldpage to NULL after unfreezing, because unfreeze_partial() ensure that all the cpu partial slabs is removed from cpu partial list. In this time, we could expect that this_cpu_cmpxchg is mostly succeed.

Quoted 14 lines

mm/slub.c int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) · line 1974

Quoted 12 lines
mm/slub.c +1 -0
index 92f1c0e..531d8ed 100644
@@ -1968,6 +1968,7 @@ int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
                                local_irq_save(flags);
                                unfreeze_partials(s);
                                local_irq_restore(flags);
+                               oldpage = NULL;
                                pobjects = 0;
                                pages = 0;
                                stat(s, CPU_PARTIAL_DRAIN);

Makes sense. Christoph, David?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#8 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

In reply to Joonsoo Kim
Quoted 10 lines · Joonsoo Kim

In some case of __slab_free(), we need a lock for manipulating partial list. If freeing object with a lock is failed, a lock doesn't needed anymore for some reasons.

Case 1. prior is NULL, kmem_cache_debug(s) is true

In this case, another free is occured before our free is succeed. When slab is full(prior is NULL), only possible operation is slab_free(). So in this case, we guess another free is occured.
It may make a slab frozen, so lock is not needed anymore.

A free cannot freeze the slab without taking the lock. The taken lock makes sure that the thread that first enters slab_free() will be able to hold back the thread that wants to freeze the slab.

Quoted 6 lines

Case 2. inuse is NULL

In this case, acquire_slab() is occured before out free is succeed. We have a last object for slab, so other operation for this slab is not possible except acquire_slab().
Acquire_slab() makes a slab frozen, so lock is not needed anymore.

acquire_slab() also requires lock acquisition and would be held of by slab_free holding the lock.

Quoted 2 lines

This also make logic somehow simple that 'was_frozen with a lock' case is never occured. Remove it.

That is actually interesting and would be a good optimization.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#9 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

2012/7/5 Christoph Lameter <[email protected]>:

Quoted 8 lines · Joonsoo Kim
Quoted 7 lines

In some case of __slab_free(), we need a lock for manipulating partial list. If freeing object with a lock is failed, a lock doesn't needed anymore for some reasons.

Case 1. prior is NULL, kmem_cache_debug(s) is true

Quoted 8 lines
Quoted 4 lines

In this case, another free is occured before our free is succeed. When slab is full(prior is NULL), only possible operation is slab_free(). So in this case, we guess another free is occured.
It may make a slab frozen, so lock is not needed anymore.

A free cannot freeze the slab without taking the lock. The taken lock makes sure that the thread that first enters slab_free() will be able to hold back the thread that wants to freeze the slab.

I don't mean we can freeze the slab without taking the lock. We can fail cmpxchg_double_slab with taking the lock.
And in this case, we don't need lock anymore, so let's release lock.

For example,
When we try to free object A at cpu 1, another process try to free object B at cpu 2 at the same time.
object A, B is in same slab, and this slab is in full list.

CPU 1 CPU 2
prior = page->freelist; prior = page->freelist
.... ...
new.inuse--; new.inuse--;
taking lock try to take the lock, but failed, so
spinning...
free success spinning...
add_partial
release lock taking lock
fail cmpxchg_double_slab retry
currently, we don't need lock

At CPU2, we don't need lock anymore, because this slab already in partial list.

Case 2 is similar as case 1.
So skip explain.

Case 1, 2 in commit message explain almost retry case with taking lock. So, below is reasonable.

@@ -2450,13 +2449,17 @@ static void __slab_free(struct kmem_cache *s, struct page *page,

                return;

        do {

+ if (unlikely(n)) {
+ spin_unlock_irqrestore(&n->list_lock, flags);
+ n = NULL;
+ }

                prior = page->freelist;
                counters = page->counters;
                set_freepointer(s, object, prior);
Quoted 9 lines
Quoted 6 lines

Case 2. inuse is NULL

In this case, acquire_slab() is occured before out free is succeed. We have a last object for slab, so other operation for this slab is not possible except acquire_slab().
Acquire_slab() makes a slab frozen, so lock is not needed anymore.

acquire_slab() also requires lock acquisition and would be held of by slab_free holding the lock.

See above explain.

Quoted 5 lines
Quoted 2 lines

This also make logic somehow simple that 'was_frozen with a lock' case is never occured. Remove it.

That is actually interesting and would be a good optimization.

So, I think patch is valid.
Thanks for comments.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#10 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

Quoted 18 lines · JoonSoo Kim

For example,
When we try to free object A at cpu 1, another process try to free object B at cpu 2 at the same time.
object A, B is in same slab, and this slab is in full list.

CPU 1 CPU 2
prior = page->freelist; prior = page->freelist
.... ...
new.inuse--; new.inuse--;
taking lock try to take the lock, but failed, so
spinning...
free success spinning...
add_partial
release lock taking lock
fail cmpxchg_double_slab retry
currently, we don't need lock

Quoted 1 line

At CPU2, we don't need lock anymore, because this slab already in partial list.

For that scenario we could also simply do a trylock there and redo the loop if we fail. But still what guarantees that another process will not modify the page struct between fetching the data and a successful trylock?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#11 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

2012/7/6 Christoph Lameter <[email protected]>:

Quoted 20 lines · JoonSoo Kim
Quoted 19 lines

For example,
When we try to free object A at cpu 1, another process try to free object B at cpu 2 at the same time.
object A, B is in same slab, and this slab is in full list.

CPU 1 CPU 2
prior = page->freelist; prior = page->freelist
.... ...
new.inuse--; new.inuse--;
taking lock try to take the lock, but failed, so
spinning...
free success spinning...
add_partial
release lock taking lock
fail cmpxchg_double_slab retry
currently, we don't need lock

Quoted 6 lines
Quoted 1 line

At CPU2, we don't need lock anymore, because this slab already in partial list.

For that scenario we could also simply do a trylock there and redo the loop if we fail. But still what guarantees that another process will not modify the page struct between fetching the data and a successful trylock?

I'm not familiar with English, so take my ability to understand into consideration.

we don't need guarantees that another process will not modify the page struct between fetching the data and a successful trylock.

As I understand, do u ask below scenario?

CPU A CPU B
lock
cmpxchg fail
retry
unlock
... modify page strcut
...
cmpxchg~~

In this case, cmpxchg will fail and just redo the loop. If we need the lock again during redo, re-take the lock. But I think this is not common case.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#12 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

Quoted 10 lines · JoonSoo Kim
Quoted 6 lines
Quoted 1 line

At CPU2, we don't need lock anymore, because this slab already in partial list.

For that scenario we could also simply do a trylock there and redo the loop if we fail. But still what guarantees that another process will not modify the page struct between fetching the data and a successful trylock?

I'm not familiar with English, so take my ability to understand into consideration.

I have a hard time understanding what you want to accomplish here.

Quoted 2 lines

we don't need guarantees that another process will not modify the page struct between fetching the data and a successful trylock.

No we do not need that since the cmpxchg will then fail.

Maybe it would be useful to split this patch into two?

One where you introduce the dropping of the lock and the other where you get rid of certain code paths?

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

#13 lore raw

Re: [PATCH 3/3] slub: release a lock if freeing object with a lock is failed in __slab_free()

2012/7/7 Christoph Lameter <[email protected]>:

Quoted 18 lines · JoonSoo Kim
Quoted 11 lines
Quoted 7 lines
Quoted 2 lines

At CPU2, we don't need lock anymore, because this slab already in partial list.

For that scenario we could also simply do a trylock there and redo the loop if we fail. But still what guarantees that another process will not modify the page struct between fetching the data and a successful trylock?

I'm not familiar with English, so take my ability to understand into consideration.

I have a hard time understanding what you want to accomplish here.

Quoted 2 lines

we don't need guarantees that another process will not modify the page struct between fetching the data and a successful trylock.

Quoted 7 lines

No we do not need that since the cmpxchg will then fail.

Maybe it would be useful to split this patch into two?

One where you introduce the dropping of the lock and the other where you get rid of certain code paths?

Dropping of the lock is need for getting rid of certain code paths. So, I can't split this patch into two.

Sorry for confusing all the people.
I think that I don't explain my purpose well.
I will prepare new version in which I explain purpose of patch better.

Thanks for kind review.

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>

Applied
#4 lore raw

Re: [PATCH 2/3] slub: reduce failure of this_cpu_cmpxchg in put_cpu_partial() after unfreezing

In reply to Joonsoo Kim
Quoted 16 lines · Joonsoo Kim

In current implementation, after unfreezing, we doesn't touch oldpage, so it remain 'NOT NULL'. When we call this_cpu_cmpxchg() with this old oldpage, this_cpu_cmpxchg() is mostly be failed.

We can change value of oldpage to NULL after unfreezing, because unfreeze_partial() ensure that all the cpu partial slabs is removed from cpu partial list. In this time, we could expect that this_cpu_cmpxchg is mostly succeed.

mm/slub.c +0 -0
index 92f1c0e..531d8ed 100644

mm/slub.c int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain) · line 1974

Quoted 7 lines
mm/slub.c +1 -0
@@ -1968,6 +1968,7 @@ int put_cpu_partial(struct kmem_cache *s, struct page *page, int drain)
 				local_irq_save(flags);
 				unfreeze_partials(s);
 				local_irq_restore(flags);
+				oldpage = NULL;
 				pobjects = 0;
 				pages = 0;
 				stat(s, CPU_PARTIAL_DRAIN);

Applied, thanks!

--
To unsubscribe, send a message with 'unsubscribe linux-mm' in the body to [email protected]. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"[email protected]"> [email protected] </a>