* SLUB: Avoid atomic operation for slab_unlock
@ 2007-10-18 22:15 Christoph Lameter
2007-10-18 22:31 ` Andrew Morton
2007-10-18 23:49 ` Nick Piggin
0 siblings, 2 replies; 9+ messages in thread
From: Christoph Lameter @ 2007-10-18 22:15 UTC (permalink / raw)
To: akpm; +Cc: linux-mm
Currently page flags are only modified in SLUB under page lock. This means
that we do not need an atomic operation to release the lock since there
is nothing we can race against that is modifying page flags. We can simply
clear the bit without the use of an atomic operation and make sure that this
change becomes visible after the other changes to slab metadata through
a memory barrier.
The performance of slab_free() increases 10-15% (SMP configuration doing
a long series of remote frees).
Signed-off-by: Christoph Lameter <clameter@sgi.com>
Index: linux-2.6/mm/slub.c
===================================================================
--- linux-2.6.orig/mm/slub.c 2007-10-18 14:12:59.000000000 -0700
+++ linux-2.6/mm/slub.c 2007-10-18 14:24:43.000000000 -0700
@@ -1180,9 +1180,22 @@ static __always_inline void slab_lock(st
bit_spin_lock(PG_locked, &page->flags);
}
+/*
+ * Slab unlock version that avoids having to use atomic operations
+ * (echos some of the code of bit_spin_unlock!)
+ */
static __always_inline void slab_unlock(struct page *page)
{
- bit_spin_unlock(PG_locked, &page->flags);
+#ifdef CONFIG_SMP
+ unsigned long flags;
+
+ flags = page->flags & ~(1 << PG_locked);
+
+ smp_wmb();
+ page->flags = flags;
+#endif
+ preempt_enable();
+ __release(bitlock);
}
static __always_inline int slab_trylock(struct page *page)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-18 22:15 SLUB: Avoid atomic operation for slab_unlock Christoph Lameter
@ 2007-10-18 22:31 ` Andrew Morton
2007-10-18 23:49 ` Nick Piggin
1 sibling, 0 replies; 9+ messages in thread
From: Andrew Morton @ 2007-10-18 22:31 UTC (permalink / raw)
To: Christoph Lameter; +Cc: linux-mm
On Thu, 18 Oct 2007 15:15:33 -0700 (PDT)
Christoph Lameter <clameter@sgi.com> wrote:
> Currently page flags are only modified in SLUB under page lock. This means
> that we do not need an atomic operation to release the lock since there
> is nothing we can race against that is modifying page flags. We can simply
> clear the bit without the use of an atomic operation and make sure that this
> change becomes visible after the other changes to slab metadata through
> a memory barrier.
>
> The performance of slab_free() increases 10-15% (SMP configuration doing
> a long series of remote frees).
>
> Signed-off-by: Christoph Lameter <clameter@sgi.com>
>
> Index: linux-2.6/mm/slub.c
> ===================================================================
> --- linux-2.6.orig/mm/slub.c 2007-10-18 14:12:59.000000000 -0700
> +++ linux-2.6/mm/slub.c 2007-10-18 14:24:43.000000000 -0700
> @@ -1180,9 +1180,22 @@ static __always_inline void slab_lock(st
> bit_spin_lock(PG_locked, &page->flags);
> }
>
> +/*
> + * Slab unlock version that avoids having to use atomic operations
> + * (echos some of the code of bit_spin_unlock!)
> + */
> static __always_inline void slab_unlock(struct page *page)
> {
> - bit_spin_unlock(PG_locked, &page->flags);
> +#ifdef CONFIG_SMP
> + unsigned long flags;
> +
> + flags = page->flags & ~(1 << PG_locked);
> +
> + smp_wmb();
> + page->flags = flags;
> +#endif
> + preempt_enable();
> + __release(bitlock);
> }
>
as always, attention zooms in on the barrier.
Can you tell us about the thinking here?
(bit_spin_unlock() uses a full smp_mb() here - you went for smp_wmb()?)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-18 22:15 SLUB: Avoid atomic operation for slab_unlock Christoph Lameter
2007-10-18 22:31 ` Andrew Morton
@ 2007-10-18 23:49 ` Nick Piggin
2007-10-19 1:21 ` Christoph Lameter
2007-10-19 11:20 ` SLUB: Avoid atomic operation for slab_unlock Christoph Lameter
1 sibling, 2 replies; 9+ messages in thread
From: Nick Piggin @ 2007-10-18 23:49 UTC (permalink / raw)
To: Christoph Lameter, Linux Kernel Mailing List; +Cc: akpm, linux-mm
[-- Attachment #1: Type: text/plain, Size: 2425 bytes --]
[sorry, I read and replied to my inbox before mailing lists...
please ignore the last mail on this patch, and reply to this
one which is properly threaded]
On Friday 19 October 2007 08:15, Christoph Lameter wrote:
> Currently page flags are only modified in SLUB under page lock. This means
> that we do not need an atomic operation to release the lock since there
> is nothing we can race against that is modifying page flags. We can simply
> clear the bit without the use of an atomic operation and make sure that
> this change becomes visible after the other changes to slab metadata
> through a memory barrier.
>
> The performance of slab_free() increases 10-15% (SMP configuration doing
> a long series of remote frees).
Ah, thanks, but can we just use my earlier patch that does the
proper __bit_spin_unlock which is provided by
bit_spin_lock-use-lock-bitops.patch
This primitive should have a better chance at being correct, and
also potentially be more optimised for each architecture (it
only has to provide release consistency).
I have attached the patch here just for reference, but actually
I am submitting it properly as part of a patch series today, now
that the base bit lock patches have been sent upstream.
> mm/slub.c | 15 ++++++++++++++-
> 1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff -puN mm/slub.c~slub-avoid-atomic-operation-for-slab_unlock mm/slub.c
> --- a/mm/slub.c~slub-avoid-atomic-operation-for-slab_unlock
> +++ a/mm/slub.c
> @@ -1181,9 +1181,22 @@ static __always_inline void slab_lock(st
> bit_spin_lock(PG_locked, &page->flags);
> }
>
> +/*
> + * Slab unlock version that avoids having to use atomic operations
> + * (echos some of the code of bit_spin_unlock!)
> + */
> static __always_inline void slab_unlock(struct page *page)
> {
> - bit_spin_unlock(PG_locked, &page->flags);
> +#ifdef CONFIG_SMP
> + unsigned long flags;
> +
> + flags = page->flags & ~(1 << PG_locked);
> +
> + smp_wmb();
> + page->flags = flags;
> +#endif
> + preempt_enable();
> + __release(bitlock);
> }
This looks wrong, because it would allow the store unlocking
flags to pass a load within the critical section.
stores aren't allowed to pass loads in x86 (only vice versa),
so you might have been confused by looking at x86's spinlocks
into thinking this will work. However on powerpc and sparc, I
don't think it gives you the right types of barriers.
[-- Attachment #2: slub-use-nonatomic-unlock.patch --]
[-- Type: text/x-diff, Size: 659 bytes --]
Slub can use the non-atomic version to unlock because other flags will not
get modified with the lock held.
Signed-off-by: Nick Piggin <npiggin@suse.de>
---
mm/slub.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Index: linux-2.6/mm/slub.c
===================================================================
--- linux-2.6.orig/mm/slub.c
+++ linux-2.6/mm/slub.c
@@ -1185,7 +1185,7 @@ static __always_inline void slab_lock(st
static __always_inline void slab_unlock(struct page *page)
{
- bit_spin_unlock(PG_locked, &page->flags);
+ __bit_spin_unlock(PG_locked, &page->flags);
}
static __always_inline int slab_trylock(struct page *page)
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-18 23:49 ` Nick Piggin
@ 2007-10-19 1:21 ` Christoph Lameter
2007-10-19 1:56 ` Nick Piggin
2007-10-19 11:20 ` SLUB: Avoid atomic operation for slab_unlock Christoph Lameter
1 sibling, 1 reply; 9+ messages in thread
From: Christoph Lameter @ 2007-10-19 1:21 UTC (permalink / raw)
To: Nick Piggin; +Cc: Linux Kernel Mailing List, akpm, linux-mm
On Fri, 19 Oct 2007, Nick Piggin wrote:
> Ah, thanks, but can we just use my earlier patch that does the
> proper __bit_spin_unlock which is provided by
> bit_spin_lock-use-lock-bitops.patch
Ok.
> This primitive should have a better chance at being correct, and
> also potentially be more optimised for each architecture (it
> only has to provide release consistency).
Yes that is what I attempted to do with the write barrier. To my knowledge
there are no reads that could bleed out and I wanted to avoid a full fence
instruction there.
> I have attached the patch here just for reference, but actually
> I am submitting it properly as part of a patch series today, now
> that the base bit lock patches have been sent upstream.
Good. Andrew: Drop my patch when this goes in.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-19 1:21 ` Christoph Lameter
@ 2007-10-19 1:56 ` Nick Piggin
2007-10-19 2:01 ` Christoph Lameter
0 siblings, 1 reply; 9+ messages in thread
From: Nick Piggin @ 2007-10-19 1:56 UTC (permalink / raw)
To: Christoph Lameter; +Cc: Linux Kernel Mailing List, akpm, linux-mm
On Friday 19 October 2007 11:21, Christoph Lameter wrote:
> On Fri, 19 Oct 2007, Nick Piggin wrote:
> > Ah, thanks, but can we just use my earlier patch that does the
> > proper __bit_spin_unlock which is provided by
> > bit_spin_lock-use-lock-bitops.patch
>
> Ok.
>
> > This primitive should have a better chance at being correct, and
> > also potentially be more optimised for each architecture (it
> > only has to provide release consistency).
>
> Yes that is what I attempted to do with the write barrier. To my knowledge
> there are no reads that could bleed out and I wanted to avoid a full fence
> instruction there.
Oh, OK. Bit risky ;) You might be right, but anyway I think it
should be just as fast with the optimised bit_unlock on most
architectures.
Which reminds me, it would be interesting to test the ia64
implementation I did. For the non-atomic unlock, I'm actually
doing an atomic operation there so that it can use the release
barrier rather than the mf. Maybe it's faster the other way around
though? Will be useful to test with something that isn't a trivial
loop, so the slub case would be a good benchmark.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-19 1:56 ` Nick Piggin
@ 2007-10-19 2:01 ` Christoph Lameter
2007-10-19 2:12 ` Nick Piggin
0 siblings, 1 reply; 9+ messages in thread
From: Christoph Lameter @ 2007-10-19 2:01 UTC (permalink / raw)
To: Nick Piggin; +Cc: Linux Kernel Mailing List, akpm, linux-mm
On Fri, 19 Oct 2007, Nick Piggin wrote:
> > Yes that is what I attempted to do with the write barrier. To my knowledge
> > there are no reads that could bleed out and I wanted to avoid a full fence
> > instruction there.
>
> Oh, OK. Bit risky ;) You might be right, but anyway I think it
> should be just as fast with the optimised bit_unlock on most
> architectures.
How expensive is the fence? An store with release semantics would be safer
and okay for IA64.
> Which reminds me, it would be interesting to test the ia64
> implementation I did. For the non-atomic unlock, I'm actually
> doing an atomic operation there so that it can use the release
> barrier rather than the mf. Maybe it's faster the other way around
> though? Will be useful to test with something that isn't a trivial
> loop, so the slub case would be a good benchmark.
Lets avoid mf (too expensive) and just use a store with release semantics.
Where can I find your patchset? I looked through lkml but did not see it.
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-19 2:01 ` Christoph Lameter
@ 2007-10-19 2:12 ` Nick Piggin
2007-10-19 3:26 ` [IA64] Reduce __clear_bit_unlock overhead Christoph Lameter
0 siblings, 1 reply; 9+ messages in thread
From: Nick Piggin @ 2007-10-19 2:12 UTC (permalink / raw)
To: Christoph Lameter; +Cc: Linux Kernel Mailing List, akpm, linux-mm
On Friday 19 October 2007 12:01, Christoph Lameter wrote:
> On Fri, 19 Oct 2007, Nick Piggin wrote:
> > > Yes that is what I attempted to do with the write barrier. To my
> > > knowledge there are no reads that could bleed out and I wanted to avoid
> > > a full fence instruction there.
> >
> > Oh, OK. Bit risky ;) You might be right, but anyway I think it
> > should be just as fast with the optimised bit_unlock on most
> > architectures.
>
> How expensive is the fence? An store with release semantics would be safer
> and okay for IA64.
I'm not sure, I had an idea it was relatively expensive on ia64,
but I didn't really test with a good workload (a microbenchmark
probably isn't that good because it won't generate too much out
of order memory traffic that needs to be fenced).
> > Which reminds me, it would be interesting to test the ia64
> > implementation I did. For the non-atomic unlock, I'm actually
> > doing an atomic operation there so that it can use the release
> > barrier rather than the mf. Maybe it's faster the other way around
> > though? Will be useful to test with something that isn't a trivial
> > loop, so the slub case would be a good benchmark.
>
> Lets avoid mf (too expensive) and just use a store with release semantics.
OK, that's what I've done at the moment.
> Where can I find your patchset? I looked through lkml but did not see it.
Infrastructure in -mm, starting at bitops-introduce-lock-ops.patch.
bit_spin_lock-use-lock-bitops.patch and ia64-lock-bitops.patch are
ones to look at.
The rest of the patches I have queued here, apart from the SLUB patch,
I guess aren't so interesting to you (they don't do anything fancy
like convert to non-atomic unlocks, just switch things like page and
buffer locks to use new bitops).
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* [IA64] Reduce __clear_bit_unlock overhead
2007-10-19 2:12 ` Nick Piggin
@ 2007-10-19 3:26 ` Christoph Lameter
0 siblings, 0 replies; 9+ messages in thread
From: Christoph Lameter @ 2007-10-19 3:26 UTC (permalink / raw)
To: Nick Piggin; +Cc: Linux Kernel Mailing List, akpm, linux-mm, linux-ia64
On Fri, 19 Oct 2007, Nick Piggin wrote:
> I'm not sure, I had an idea it was relatively expensive on ia64,
> but I didn't really test with a good workload (a microbenchmark
> probably isn't that good because it won't generate too much out
> of order memory traffic that needs to be fenced).
Its expensive on IA64. Is it any less expensive on x86?
> > Where can I find your patchset? I looked through lkml but did not see it.
>
> Infrastructure in -mm, starting at bitops-introduce-lock-ops.patch.
> bit_spin_lock-use-lock-bitops.patch and ia64-lock-bitops.patch are
> ones to look at.
ia64-lock-bitops.patch defines:
static __inline__ void
clear_bit_unlock (int nr, volatile void *addr)
{
__u32 mask, old, new;
volatile __u32 *m;
CMPXCHG_BUGCHECK_DECL
m = (volatile __u32 *) addr + (nr >> 5);
mask = ~(1 << (nr & 31));
do {
CMPXCHG_BUGCHECK(m);
old = *m;
new = old & mask;
} while (cmpxchg_rel(m, old, new) != old);
}
/**
* __clear_bit_unlock - Non-atomically clear a bit with release
*
* This is like clear_bit_unlock, but the implementation may use a non-atomic
* store (this one uses an atomic, however).
*/
#define __clear_bit_unlock clear_bit_unlock
A non atomic store is a misaligned store on IA64. That is not
relevant here. The data is properly aligned. I guess it was intended to
refer to the cmpxchg.
How about this patch? [Works fine on IA64 simulator...]
IA64: Slim down __clear_bit_unlock
__clear_bit_unlock does not need to perform atomic operations on the variable.
Avoid a cmpxchg and simply do a store with release semantics. Add a barrier to
be safe that the compiler does not do funky things.
Signed-off-by: Christoph Lameter <clameter@sgi.com>
---
include/asm-ia64/bitops.h | 12 ++++++++++++
1 file changed, 12 insertions(+)
Index: linux-2.6.23-mm1/include/asm-ia64/bitops.h
===================================================================
--- linux-2.6.23-mm1.orig/include/asm-ia64/bitops.h 2007-10-18 19:37:22.000000000 -0700
+++ linux-2.6.23-mm1/include/asm-ia64/bitops.h 2007-10-18 19:50:22.000000000 -0700
@@ -124,10 +124,21 @@ clear_bit_unlock (int nr, volatile void
/**
* __clear_bit_unlock - Non-atomically clear a bit with release
*
- * This is like clear_bit_unlock, but the implementation may use a non-atomic
- * store (this one uses an atomic, however).
+ * This is like clear_bit_unlock, but the implementation uses a store
+ * with release semantics. See also __raw_spin_unlock().
*/
-#define __clear_bit_unlock clear_bit_unlock
+static __inline__ void
+__clear_bit_unlock (int nr, volatile void *addr)
+{
+ __u32 mask, new;
+ volatile __u32 *m;
+
+ m = (volatile __u32 *) addr + (nr >> 5);
+ mask = ~(1 << (nr & 31));
+ new = *m & mask;
+ barrier();
+ asm volatile ("st4.rel.nta [%0] = %1\n\t" :: "r"(m), "r"(new));
+}
/**
* __clear_bit - Clears a bit in memory (non-atomic version)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
* Re: SLUB: Avoid atomic operation for slab_unlock
2007-10-18 23:49 ` Nick Piggin
2007-10-19 1:21 ` Christoph Lameter
@ 2007-10-19 11:20 ` Christoph Lameter
1 sibling, 0 replies; 9+ messages in thread
From: Christoph Lameter @ 2007-10-19 11:20 UTC (permalink / raw)
To: Nick Piggin; +Cc: Linux Kernel Mailing List, akpm, linux-mm
Slub can use the non-atomic version to unlock because other flags will not
get modified with the lock held.
Signed-off-by: Nick Piggin <npiggin@suse.de>
---
mm/slub.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Index: linux-2.6/mm/slub.c
===================================================================
--- linux-2.6.orig/mm/slub.c
+++ linux-2.6/mm/slub.c
@@ -1185,7 +1185,7 @@ static __always_inline void slab_lock(st
static __always_inline void slab_unlock(struct page *page)
{
- bit_spin_unlock(PG_locked, &page->flags);
+ __bit_spin_unlock(PG_locked, &page->flags);
}
static __always_inline int slab_trylock(struct page *page)
--
To unsubscribe, send a message with 'unsubscribe linux-mm' in
the body to majordomo@kvack.org. For more info on Linux MM,
see: http://www.linux-mm.org/ .
Don't email: <a href=mailto:"dont@kvack.org"> email@kvack.org </a>
^ permalink raw reply [flat|nested] 9+ messages in thread
end of thread, other threads:[~2007-10-19 11:20 UTC | newest]
Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-10-18 22:15 SLUB: Avoid atomic operation for slab_unlock Christoph Lameter
2007-10-18 22:31 ` Andrew Morton
2007-10-18 23:49 ` Nick Piggin
2007-10-19 1:21 ` Christoph Lameter
2007-10-19 1:56 ` Nick Piggin
2007-10-19 2:01 ` Christoph Lameter
2007-10-19 2:12 ` Nick Piggin
2007-10-19 3:26 ` [IA64] Reduce __clear_bit_unlock overhead Christoph Lameter
2007-10-19 11:20 ` SLUB: Avoid atomic operation for slab_unlock Christoph Lameter
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).