2006-04-27 19:40:12 -06:00
|
|
|
#ifndef _ASM_S390_FUTEX_H
|
|
|
|
#define _ASM_S390_FUTEX_H
|
[PATCH] FUTEX_WAKE_OP: pthread_cond_signal() speedup
ATM pthread_cond_signal is unnecessarily slow, because it wakes one waiter
(which at least on UP usually means an immediate context switch to one of
the waiter threads). This waiter wakes up and after a few instructions it
attempts to acquire the cv internal lock, but that lock is still held by
the thread calling pthread_cond_signal. So it goes to sleep and eventually
the signalling thread is scheduled in, unlocks the internal lock and wakes
the waiter again.
Now, before 2003-09-21 NPTL was using FUTEX_REQUEUE in pthread_cond_signal
to avoid this performance issue, but it was removed when locks were
redesigned to the 3 state scheme (unlocked, locked uncontended, locked
contended).
Following scenario shows why simply using FUTEX_REQUEUE in
pthread_cond_signal together with using lll_mutex_unlock_force in place of
lll_mutex_unlock is not enough and probably why it has been disabled at
that time:
The number is value in cv->__data.__lock.
thr1 thr2 thr3
0 pthread_cond_wait
1 lll_mutex_lock (cv->__data.__lock)
0 lll_mutex_unlock (cv->__data.__lock)
0 lll_futex_wait (&cv->__data.__futex, futexval)
0 pthread_cond_signal
1 lll_mutex_lock (cv->__data.__lock)
1 pthread_cond_signal
2 lll_mutex_lock (cv->__data.__lock)
2 lll_futex_wait (&cv->__data.__lock, 2)
2 lll_futex_requeue (&cv->__data.__futex, 0, 1, &cv->__data.__lock)
# FUTEX_REQUEUE, not FUTEX_CMP_REQUEUE
2 lll_mutex_unlock_force (cv->__data.__lock)
0 cv->__data.__lock = 0
0 lll_futex_wake (&cv->__data.__lock, 1)
1 lll_mutex_lock (cv->__data.__lock)
0 lll_mutex_unlock (cv->__data.__lock)
# Here, lll_mutex_unlock doesn't know there are threads waiting
# on the internal cv's lock
Now, I believe it is possible to use FUTEX_REQUEUE in pthread_cond_signal,
but it will cost us not one, but 2 extra syscalls and, what's worse, one of
these extra syscalls will be done for every single waiting loop in
pthread_cond_*wait.
We would need to use lll_mutex_unlock_force in pthread_cond_signal after
requeue and lll_mutex_cond_lock in pthread_cond_*wait after lll_futex_wait.
Another alternative is to do the unlocking pthread_cond_signal needs to do
(the lock can't be unlocked before lll_futex_wake, as that is racy) in the
kernel.
I have implemented both variants, futex-requeue-glibc.patch is the first
one and futex-wake_op{,-glibc}.patch is the unlocking inside of the kernel.
The kernel interface allows userland to specify how exactly an unlocking
operation should look like (some atomic arithmetic operation with optional
constant argument and comparison of the previous futex value with another
constant).
It has been implemented just for ppc*, x86_64 and i?86, for other
architectures I'm including just a stub header which can be used as a
starting point by maintainers to write support for their arches and ATM
will just return -ENOSYS for FUTEX_WAKE_OP. The requeue patch has been
(lightly) tested just on x86_64, the wake_op patch on ppc64 kernel running
32-bit and 64-bit NPTL and x86_64 kernel running 32-bit and 64-bit NPTL.
With the following benchmark on UP x86-64 I get:
for i in nptl-orig nptl-requeue nptl-wake_op; do echo time elf/ld.so --library-path .:$i /tmp/bench; \
for j in 1 2; do echo ( time elf/ld.so --library-path .:$i /tmp/bench ) 2>&1; done; done
time elf/ld.so --library-path .:nptl-orig /tmp/bench
real 0m0.655s user 0m0.253s sys 0m0.403s
real 0m0.657s user 0m0.269s sys 0m0.388s
time elf/ld.so --library-path .:nptl-requeue /tmp/bench
real 0m0.496s user 0m0.225s sys 0m0.271s
real 0m0.531s user 0m0.242s sys 0m0.288s
time elf/ld.so --library-path .:nptl-wake_op /tmp/bench
real 0m0.380s user 0m0.176s sys 0m0.204s
real 0m0.382s user 0m0.175s sys 0m0.207s
The benchmark is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00001.txt
Older futex-requeue-glibc.patch version is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00002.txt
Older futex-wake_op-glibc.patch version is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00003.txt
Will post a new version (just x86-64 fixes so that the patch
applies against pthread_cond_signal.S) to libc-hacker ml soon.
Attached is the kernel FUTEX_WAKE_OP patch as well as a simple-minded
testcase that will not test the atomicity of the operation, but at least
check if the threads that should have been woken up are woken up and
whether the arithmetic operation in the kernel gave the expected results.
Acked-by: Ingo Molnar <mingo@redhat.com>
Cc: Ulrich Drepper <drepper@redhat.com>
Cc: Jamie Lokier <jamie@shareable.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-06 16:16:25 -06:00
|
|
|
|
2006-04-27 19:40:12 -06:00
|
|
|
#ifdef __KERNEL__
|
[PATCH] FUTEX_WAKE_OP: pthread_cond_signal() speedup
ATM pthread_cond_signal is unnecessarily slow, because it wakes one waiter
(which at least on UP usually means an immediate context switch to one of
the waiter threads). This waiter wakes up and after a few instructions it
attempts to acquire the cv internal lock, but that lock is still held by
the thread calling pthread_cond_signal. So it goes to sleep and eventually
the signalling thread is scheduled in, unlocks the internal lock and wakes
the waiter again.
Now, before 2003-09-21 NPTL was using FUTEX_REQUEUE in pthread_cond_signal
to avoid this performance issue, but it was removed when locks were
redesigned to the 3 state scheme (unlocked, locked uncontended, locked
contended).
Following scenario shows why simply using FUTEX_REQUEUE in
pthread_cond_signal together with using lll_mutex_unlock_force in place of
lll_mutex_unlock is not enough and probably why it has been disabled at
that time:
The number is value in cv->__data.__lock.
thr1 thr2 thr3
0 pthread_cond_wait
1 lll_mutex_lock (cv->__data.__lock)
0 lll_mutex_unlock (cv->__data.__lock)
0 lll_futex_wait (&cv->__data.__futex, futexval)
0 pthread_cond_signal
1 lll_mutex_lock (cv->__data.__lock)
1 pthread_cond_signal
2 lll_mutex_lock (cv->__data.__lock)
2 lll_futex_wait (&cv->__data.__lock, 2)
2 lll_futex_requeue (&cv->__data.__futex, 0, 1, &cv->__data.__lock)
# FUTEX_REQUEUE, not FUTEX_CMP_REQUEUE
2 lll_mutex_unlock_force (cv->__data.__lock)
0 cv->__data.__lock = 0
0 lll_futex_wake (&cv->__data.__lock, 1)
1 lll_mutex_lock (cv->__data.__lock)
0 lll_mutex_unlock (cv->__data.__lock)
# Here, lll_mutex_unlock doesn't know there are threads waiting
# on the internal cv's lock
Now, I believe it is possible to use FUTEX_REQUEUE in pthread_cond_signal,
but it will cost us not one, but 2 extra syscalls and, what's worse, one of
these extra syscalls will be done for every single waiting loop in
pthread_cond_*wait.
We would need to use lll_mutex_unlock_force in pthread_cond_signal after
requeue and lll_mutex_cond_lock in pthread_cond_*wait after lll_futex_wait.
Another alternative is to do the unlocking pthread_cond_signal needs to do
(the lock can't be unlocked before lll_futex_wake, as that is racy) in the
kernel.
I have implemented both variants, futex-requeue-glibc.patch is the first
one and futex-wake_op{,-glibc}.patch is the unlocking inside of the kernel.
The kernel interface allows userland to specify how exactly an unlocking
operation should look like (some atomic arithmetic operation with optional
constant argument and comparison of the previous futex value with another
constant).
It has been implemented just for ppc*, x86_64 and i?86, for other
architectures I'm including just a stub header which can be used as a
starting point by maintainers to write support for their arches and ATM
will just return -ENOSYS for FUTEX_WAKE_OP. The requeue patch has been
(lightly) tested just on x86_64, the wake_op patch on ppc64 kernel running
32-bit and 64-bit NPTL and x86_64 kernel running 32-bit and 64-bit NPTL.
With the following benchmark on UP x86-64 I get:
for i in nptl-orig nptl-requeue nptl-wake_op; do echo time elf/ld.so --library-path .:$i /tmp/bench; \
for j in 1 2; do echo ( time elf/ld.so --library-path .:$i /tmp/bench ) 2>&1; done; done
time elf/ld.so --library-path .:nptl-orig /tmp/bench
real 0m0.655s user 0m0.253s sys 0m0.403s
real 0m0.657s user 0m0.269s sys 0m0.388s
time elf/ld.so --library-path .:nptl-requeue /tmp/bench
real 0m0.496s user 0m0.225s sys 0m0.271s
real 0m0.531s user 0m0.242s sys 0m0.288s
time elf/ld.so --library-path .:nptl-wake_op /tmp/bench
real 0m0.380s user 0m0.176s sys 0m0.204s
real 0m0.382s user 0m0.175s sys 0m0.207s
The benchmark is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00001.txt
Older futex-requeue-glibc.patch version is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00002.txt
Older futex-wake_op-glibc.patch version is at:
http://sourceware.org/ml/libc-alpha/2005-03/txt00003.txt
Will post a new version (just x86-64 fixes so that the patch
applies against pthread_cond_signal.S) to libc-hacker ml soon.
Attached is the kernel FUTEX_WAKE_OP patch as well as a simple-minded
testcase that will not test the atomicity of the operation, but at least
check if the threads that should have been woken up are woken up and
whether the arithmetic operation in the kernel gave the expected results.
Acked-by: Ingo Molnar <mingo@redhat.com>
Cc: Ulrich Drepper <drepper@redhat.com>
Cc: Jamie Lokier <jamie@shareable.org>
Cc: Rusty Russell <rusty@rustcorp.com.au>
Signed-off-by: Yoichi Yuasa <yuasa@hh.iij4u.or.jp>
Signed-off-by: Andrew Morton <akpm@osdl.org>
Signed-off-by: Linus Torvalds <torvalds@osdl.org>
2005-09-06 16:16:25 -06:00
|
|
|
|
2006-04-27 19:40:12 -06:00
|
|
|
#include <linux/futex.h>
|
|
|
|
#include <asm/errno.h>
|
|
|
|
#include <asm/uaccess.h>
|
|
|
|
|
|
|
|
#ifndef __s390x__
|
|
|
|
#define __futex_atomic_fixup \
|
|
|
|
".section __ex_table,\"a\"\n" \
|
|
|
|
" .align 4\n" \
|
2006-06-08 02:36:20 -06:00
|
|
|
" .long 0b,4b,2b,4b,3b,4b\n" \
|
2006-04-27 19:40:12 -06:00
|
|
|
".previous"
|
|
|
|
#else /* __s390x__ */
|
|
|
|
#define __futex_atomic_fixup \
|
|
|
|
".section __ex_table,\"a\"\n" \
|
|
|
|
" .align 8\n" \
|
2006-06-08 02:36:20 -06:00
|
|
|
" .quad 0b,4b,2b,4b,3b,4b\n" \
|
2006-04-27 19:40:12 -06:00
|
|
|
".previous"
|
|
|
|
#endif /* __s390x__ */
|
|
|
|
|
|
|
|
#define __futex_atomic_op(insn, ret, oldval, newval, uaddr, oparg) \
|
2006-06-08 02:36:20 -06:00
|
|
|
asm volatile(" sacf 256\n" \
|
|
|
|
"0: l %1,0(%6)\n" \
|
|
|
|
"1: " insn \
|
|
|
|
"2: cs %1,%2,0(%6)\n" \
|
|
|
|
"3: jl 1b\n" \
|
2006-04-27 19:40:12 -06:00
|
|
|
" lhi %0,0\n" \
|
2006-06-08 02:36:20 -06:00
|
|
|
"4: sacf 0\n" \
|
2006-04-27 19:40:12 -06:00
|
|
|
__futex_atomic_fixup \
|
|
|
|
: "=d" (ret), "=&d" (oldval), "=&d" (newval), \
|
|
|
|
"=m" (*uaddr) \
|
|
|
|
: "0" (-EFAULT), "d" (oparg), "a" (uaddr), \
|
|
|
|
"m" (*uaddr) : "cc" );
|
|
|
|
|
|
|
|
static inline int futex_atomic_op_inuser (int encoded_op, int __user *uaddr)
|
|
|
|
{
|
|
|
|
int op = (encoded_op >> 28) & 7;
|
|
|
|
int cmp = (encoded_op >> 24) & 15;
|
|
|
|
int oparg = (encoded_op << 8) >> 20;
|
|
|
|
int cmparg = (encoded_op << 20) >> 20;
|
|
|
|
int oldval = 0, newval, ret;
|
|
|
|
if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
|
|
|
|
oparg = 1 << oparg;
|
|
|
|
|
|
|
|
if (! access_ok (VERIFY_WRITE, uaddr, sizeof(int)))
|
|
|
|
return -EFAULT;
|
|
|
|
|
|
|
|
inc_preempt_count();
|
|
|
|
|
|
|
|
switch (op) {
|
|
|
|
case FUTEX_OP_SET:
|
|
|
|
__futex_atomic_op("lr %2,%5\n",
|
|
|
|
ret, oldval, newval, uaddr, oparg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_ADD:
|
|
|
|
__futex_atomic_op("lr %2,%1\nar %2,%5\n",
|
|
|
|
ret, oldval, newval, uaddr, oparg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_OR:
|
|
|
|
__futex_atomic_op("lr %2,%1\nor %2,%5\n",
|
|
|
|
ret, oldval, newval, uaddr, oparg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_ANDN:
|
|
|
|
__futex_atomic_op("lr %2,%1\nnr %2,%5\n",
|
|
|
|
ret, oldval, newval, uaddr, oparg);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_XOR:
|
|
|
|
__futex_atomic_op("lr %2,%1\nxr %2,%5\n",
|
|
|
|
ret, oldval, newval, uaddr, oparg);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
|
|
|
|
dec_preempt_count();
|
|
|
|
|
|
|
|
if (!ret) {
|
|
|
|
switch (cmp) {
|
|
|
|
case FUTEX_OP_CMP_EQ: ret = (oldval == cmparg); break;
|
|
|
|
case FUTEX_OP_CMP_NE: ret = (oldval != cmparg); break;
|
|
|
|
case FUTEX_OP_CMP_LT: ret = (oldval < cmparg); break;
|
|
|
|
case FUTEX_OP_CMP_GE: ret = (oldval >= cmparg); break;
|
|
|
|
case FUTEX_OP_CMP_LE: ret = (oldval <= cmparg); break;
|
|
|
|
case FUTEX_OP_CMP_GT: ret = (oldval > cmparg); break;
|
|
|
|
default: ret = -ENOSYS;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
futex_atomic_cmpxchg_inatomic(int __user *uaddr, int oldval, int newval)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (! access_ok (VERIFY_WRITE, uaddr, sizeof(int)))
|
|
|
|
return -EFAULT;
|
2006-07-12 08:39:55 -06:00
|
|
|
asm volatile(" sacf 256\n"
|
|
|
|
" cs %1,%4,0(%5)\n"
|
2006-04-27 19:40:12 -06:00
|
|
|
"0: lr %0,%1\n"
|
2006-07-12 08:39:55 -06:00
|
|
|
"1: sacf 0\n"
|
2006-04-27 19:40:12 -06:00
|
|
|
#ifndef __s390x__
|
|
|
|
".section __ex_table,\"a\"\n"
|
|
|
|
" .align 4\n"
|
|
|
|
" .long 0b,1b\n"
|
|
|
|
".previous"
|
|
|
|
#else /* __s390x__ */
|
|
|
|
".section __ex_table,\"a\"\n"
|
|
|
|
" .align 8\n"
|
|
|
|
" .quad 0b,1b\n"
|
|
|
|
".previous"
|
|
|
|
#endif /* __s390x__ */
|
|
|
|
: "=d" (ret), "+d" (oldval), "=m" (*uaddr)
|
|
|
|
: "0" (-EFAULT), "d" (newval), "a" (uaddr), "m" (*uaddr)
|
|
|
|
: "cc", "memory" );
|
|
|
|
return oldval;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* __KERNEL__ */
|
|
|
|
#endif /* _ASM_S390_FUTEX_H */
|