Futexes IV (Fast Lightweight Userspace Semaphores)
Subsystems (9)
PATCH 1/1 Futexes IV (Fast Lightweight Userspace Semaphores)
1) FUTEX_UP and FUTEX_DOWN defines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.
Example userspace lib attached,
Rusty.
include/linux/futex.h +8 -0
@@ -0,0 +1,8 @@ +#ifndef _LINUX_FUTEX_H +#define _LINUX_FUTEX_H + +/* Second argument to futex syscall */ +#define FUTEX_UP (1) +#define FUTEX_DOWN (-1) + +#endif
kernel/futex.c +208 -0
208 lines not on this page · raw message
arch/i386/kernel/entry.S +1 -0
@@ -716,6 +716,7 @@ .long SYMBOL_NAME(sys_lremovexattr) .long SYMBOL_NAME(sys_fremovexattr) .long SYMBOL_NAME(sys_tkill) + .long SYMBOL_NAME(sys_futex) .rept NR_syscalls-(.-sys_call_table)/4 .long SYMBOL_NAME(sys_ni_syscall)
arch/ppc/kernel/misc.S +1 -0
@@ -1246,6 +1246,7 @@ .long sys_removexattr .long sys_lremovexattr .long sys_fremovexattr /* 220 */ + .long sys_futex .rept NR_syscalls-(.-sys_call_table)/4 .long sys_ni_syscall .endr
include/asm-i386/mman.h +1 -0
@@ -4,6 +4,7 @@#define PROT_READ 0x1/* page can be read */ #define PROT_WRITE 0x2/* page can be written */ #define PROT_EXEC 0x4/* page can be executed */ +#define PROT_SEM 0x8/* page may be used for atomic ops */ #define PROT_NONE 0x0/* page can not be accessed */ #define MAP_SHARED 0x01/* Share changes */
include/asm-i386/unistd.h +1 -0
@@ -243,6 +243,7 @@#define __NR_lremovexattr 236#define __NR_fremovexattr 237#define __NR_tkill 238 +#define __NR_futex 239/* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
include/asm-ppc/mman.h +1 -0
@@ -7,6 +7,7 @@#define PROT_READ 0x1/* page can be read */ #define PROT_WRITE 0x2/* page can be written */ #define PROT_EXEC 0x4/* page can be executed */ +#define PROT_SEM 0x8/* page may be used for atomic ops */ #define PROT_NONE 0x0/* page can not be accessed */ #define MAP_SHARED 0x01/* Share changes */
include/asm-ppc/unistd.h +1 -0
@@ -228,6 +228,7 @@#define __NR_removexattr 218#define __NR_lremovexattr 219#define __NR_fremovexattr 220 +#define __NR_futex 221#define __NR (n) #n
kernel/Makefile +1 -1
@@ -15,7 +15,7 @@ obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \ module.o exit.o itimer.o info.o time.o softirq.o resource.o \ sysctl.o acct.o capability.o ptrace.o timer.o user.o \ - signal.o sys.o kmod.o context.o + signal.o sys.o kmod.o context.o futex.o obj-$(CONFIG_UID16) += uid16.o obj-$(CONFIG_MODULES) += ksyms.o
include/linux/hash.h +58 -0
58 lines not on this page · raw message
include/linux/mmzone.h +2 -3
@@ -51,8 +51,7 @@/* * wait_table -- the array holding the hash table * wait_table_size -- the size of the hash table array -* wait_table_shift -- wait_table_size -* == BITS_PER_LONG (1 << wait_table_bits) +* wait_table_bits -- wait_table_size == (1 << wait_table_bits) * * The purpose of all these is to keep track of the people * waiting for a page to become available and make them
@@ -75,7 +74,7 @@*/ wait_queue_head_t * wait_table;unsigned long wait_table_size; -unsigned long wait_table_shift; +unsigned long wait_table_bits;/* * Discontig memory support fields.
mm/filemap.c +2 -25
@@ -25,6 +25,7 @@#include <linux/iobuf.h> #include <linux/compiler.h> #include <linux/fs.h> +#include <linux/hash.h> #include <asm/pgalloc.h> #include <asm/uaccess.h>
@@ -773,32 +774,8 @@static inline wait_queue_head_t *page_waitqueue (struct page *page) {const zone_t *zone =page_zone (page); -wait_queue_head_t *wait = zone->wait_table; -unsigned long hash = (unsigned long )page; -#if BITS_PER_LONG ==64 -/* Sigh, gcc can't optimise this alone like it does for 32 bits. */ -unsigned long n = hash; - n <<=18 ; - hash -= n; - n <<=33 ; - hash -= n; - n <<=3 ; - hash += n; - n <<=3 ; - hash -= n; - n <<=4 ; - hash += n; - n <<=2 ; - hash += n; -#else -/* On some cpus multiply is faster, on others gcc will do shifts */ - hash *=GOLDEN_RATIO_PRIME ; -#endif - - hash >>= zone->wait_table_shift; - -return &wait[hash]; +return &zone->wait_table[hash_ptr (page, zone->wait_table_bits)]; }/*
mm/mprotect.c +1 -1
@@ -280,7 +280,7 @@ end = start + len;if (end < start)return -EINVAL ; -if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC )) +if (prot & ~(PROT_READ |PROT_WRITE |PROT_EXEC |PROT_SEM ))return -EINVAL ;if (end == start)return 0 ;
mm/page_alloc.c +2 -2
@@ -776,8 +776,8 @@* per zone. */ zone->wait_table_size =wait_table_size (size); - zone->wait_table_shift = -BITS_PER_LONG -wait_table_bits (zone->wait_table_size); + zone->wait_table_bits = +wait_table_bits (zone->wait_table_size); zone->wait_table = (wait_queue_head_t *)alloc_bootmem_node (pgdat, zone->wait_table_size *sizeof (wait_queue_head_t));
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Futexes III : performance numbers
Quoted 6 lines · Rusty Russell
1)
FUTEX_UPandFUTEX_DOWNdefines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.Example userspace lib attached,
Rusty.
I did a quick hack to enable ulockflex to run on the latest interface that Rusty posted.
The command run was
./ulockflex -c 2 -a 1 -t 2 -o 5 -m -1 -R 499 -r 0 -x 0 -L f
that is 2 children, 1 lock, 2 seconds in a tight loop contending for the lock. Tue Mar 5 15:12:56 EST 2002: calibrated at 496
base arguments are <-t 10 -o 5 -m 8 -R 496 -P>
c #number of children
a #number of locks (always one in this case)
t #second of each run
i #number of iterations
L locktype (capital letter means spinning version)
p patience of spinning if any
r mean runtime usecs without holding a lock
x mean runtime usces with lock held.
WC lock contention on write lock
RC read contention on read lock (for shared lock)
R percentage of failed lock attempts resolved by spinning OR
average times in calock a failed attempt needed to retry
COV coefficient of variance between children (fairness)
ThPut Throughput achieved per second
Lock types:
e: empty locks no lock/unlock performed k: sysv semaphores f: usema (Hubertus Franke) m: mutex (Rusty Russell) c: convoy avoidance lock (Hubertus Franke)
RAW OVERHEAD/
First the raw overhead numbers for 1 process
c a t i L p r x WC RC R COV ThPut 1 1 2 1 e 0 0 0 0.00 0.00 0.00 0.000000 2316611 1 1 2 1 k 0 0 0 0.00 0.00 0.00 0.000000 539468 1 1 2 1 m 0 0 0 0.00 0.00 0.00 0.000000 2022462 1 1 2 1 f 0 0 0 0.00 0.00 0.00 0.000000 1979692 1 1 2 1 c 0 0 0 0.00 0.00 0.00 0.000000 1936554
User locks are about 4 times faster than kernel locking techniques.
Let's look at the contention issue 2 processes
2 1 5 4 k 0 0 0 99.77 0.00 0.00 0.003608 289920 2 1 5 4 m 0 0 0 9.67 0.00 0.00 0.086198 843918 2 1 5 4 f 0 0 0 97.90 0.00 0.00 0.028389 328755 2 1 5 4 c 0 0 0 6.80 0.00 0.06 0.001428 933587
This analysis reveals a few interesting points. First again userlocks are much faster.
RR's version does not provide a FIFO locking order !!!!!!!!!! This can be seen by the fact that the only 9.67% of the times the lock was requested, the kernel was called. Also has a high COV 8%. As a result RR's mutex are about 2.5 times faster than my semaphores which process in strict FIFO order. This is due to the fact that I separate user state and kernel state as described in various previous messages.
We need to know whether FIFO processing is required.
I have provided similar locks to RR mutex I called convoy avoidance locks and you see they are about 10% better than RR locks.
Now 3 processes
3 1 5 4 k 0 0 0 99.98 0.00 0.00 0.033284 242040 3 1 5 4 m 0 0 0 0.29 0.00 0.00 0.018406 1979992 3 1 5 4 f 0 0 0 99.71 0.00 0.00 0.028083 306140 3 1 5 4 c 0 0 0 7.79 0.00 4.00 0.437084 774175
Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better. Wohh futex rock, BUT... with 0.29% contention it basically tells me that we are exhausting our entire quantum getting the lock without contention. So their is some serious fairness issue here at least for the tightly scheduled locks. Compare the M numbers for 2 and 3 children.
Let's rock again.... 100 processes
100 1 10 4 k 0 0 0 100.00 0.00 0.00 0.422294 17776 100 1 10 4 m 0 0 0 0.58 0.00 0.00 0.278172 1793520 100 1 10 4 f 0 0 0 99.50 0.00 0.00 0.478905 52834 100 1 10 4 c 0 0 0 8.05 0.00 12.02 0.522363 563151
Same as above, futexes rock, but at very tight arrival rates are highly unfair, but they avoid the so called convoy phenomena.
Linus, or for that matter anybody else, what's your take: is some level of starvation acceptable when deploying fast user locks ?
REALIST ARRIVAL RATE
----------------------------
Let's switch to something more realistic 1 usec mean lock holdtime and
10 usecs mean nonlock time.
3 1 10 4 k 0 10 1 98.70 0.00 0.00 0.005531 129429 3 1 10 4 m 0 10 1 7.29 0.00 0.00 0.113565 122602 3 1 10 4 f 0 10 1 98.19 0.00 0.00 0.009873 138952 3 1 10 4 c 0 10 1 10.61 0.00 8.45 0.325935 139446
Once the convoy disappears actually my locks seem to perform better. Reason for that is likely that the in kernel hashing is faster (guess).
100 1 10 4 k 0 10 1 99.96 0.00 0.00 0.003876 12811 100 1 10 4 m 0 10 1 9.36 0.00 0.00 0.051076 139238 100 1 10 4 f 0 10 1 100.00 0.00 0.00 0.162686 36601 100 1 10 4 c 0 10 1 10.58 0.00 9.33 0.326738 138602
Again at the relevant cases convoy avoidance locks do roughly what the RR's futex do. Fair locks don't do that well, still due to the FIFO order.
Now two takes possible:
(a) we need FIFO ordering even for fast user level locks
(b) I don't give a rat's hyni ; if you want FIFO use sysv locks.
Comments, stands .....
Again, we better settle this one soon.
-- Hubertus
kernel/futex.c line 176
Quoted 6 lines · Rusty Russell
kernel/futex.c +6
-0+ pos_in_page = ((unsigned long )uaddr) %PAGE_SIZE ; + +/* Must be "naturally" aligned, and not on page boundary. */ +if ((pos_in_page % __alignof__(atomic_t )) !=0 + || pos_in_page +sizeof (atomic_t) >PAGE_SIZE ) +return -EINVAL ;
How can this :
(pos_in_page % __alignof__(atomic_t)) != 0
to be false, and together this :
pos_in_page + sizeof(atomic_t) > PAGE_SIZE
to be true ?
This is enough :
if ((pos_in_page % __alignof__(atomic_t)) != 0)
- Davide
Quoted 12 lines · Davide Libenzi
kernel/futex.c line 176
Quoted 6 lines · Rusty Russell
kernel/futex.c +6
-0+ pos_in_page = ((unsigned long )uaddr) %PAGE_SIZE ; + +/* Must be "naturally" aligned, and not on page boundary. */ +if ((pos_in_page % __alignof__(atomic_t )) !=0 + || pos_in_page +sizeof (atomic_t) >PAGE_SIZE ) +return -EINVAL ;How can this :
(pos_in_page % __alignof__(atomic_t)) != 0
Quoted 10 lines
to be false, and together this :
pos_in_page+sizeof(atomic_t)>PAGE_SIZEto be true ?
This is enough :if ((pos_in_page % __alignof__(atomic_t)) != 0)
I believe not all machine have alignof == sizeof
Quoted 10 lines
- Davide
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 17 lines · Hubertus Franke
Quoted 16 lines · Davide Libenzi
kernel/futex.c line 176
Quoted 6 lines · Rusty Russell
kernel/futex.c +6
-0+ pos_in_page = ((unsigned long )uaddr) %PAGE_SIZE ; + +/* Must be "naturally" aligned, and not on page boundary. */ +if ((pos_in_page % __alignof__(atomic_t )) !=0 + || pos_in_page +sizeof (atomic_t) >PAGE_SIZE ) +return -EINVAL ;How can this :
(pos_in_page % __alignof__(atomic_t)) != 0to be false, and together this :
pos_in_page+sizeof(atomic_t)>PAGE_SIZE
Quoted 8 lines
Quoted 6 lines
to be true ?
This is enough :if ((pos_in_page % __alignof__(atomic_t)) != 0)I believe not all machine have alignof == sizeof
Yes but this is always true alignof >= sizeof
- Davide
Quoted 3 lines · Davide Libenzi
Quoted 1 line
I believe not all machine have alignof == sizeof
Yes but this is always true alignof >= sizeof
No, this is not true. As the gcc info pages says:
For example, if the target machine requires a double' value to be aligned on an 8-byte boundary, then __alignof__ (double)' is 8. This is true on many RISC machines. On more traditional machine designs, `__alignof__ (double)' is 4 or even 2.
A later example shows situations where alignof>sizeof.
Peter
--
Peter Svensson ! Pgp key available by finger, fingerprint:
<[email protected]> ! 8A E9 20 98 C1 FF 43 E3 07 FD B9 0A 80 72 70 AF
------------------------------------------------------------------------
Remember, Luke, your source will be with you... always...
Quoted 4 lines · Peter Svensson
Quoted 2 lines · Davide Libenzi
Quoted 1 line
I believe not all machine have alignof == sizeof
Quoted 8 lines
Quoted 1 line
Yes but this is always true alignof >= sizeof
No, this is not true. As the gcc info pages says:
For example, if the target machine requires adouble' value to be aligned on an 8-byte boundary, then__alignof__(double)' is 8. This is true on many RISC machines. On more traditional machine designs, `__alignof__ (double)' is 4 or even 2.
A later example shows situations where alignof>sizeof.
Yes, it's true.
- Davide
In message <[email protected]> y ou write:
Quoted 11 lines · Rusty Russell
kernel/futex.c line 176
Quoted 7 lines
kernel/futex.c +6
-0+ pos_in_page = ((unsigned long )uaddr) %PAGE_SIZE ; + +/* Must be "naturally" aligned, and not on page boundary. */ +if ((pos_in_page % __alignof__(atomic_t )) !=0 + || pos_in_page +sizeof (atomic_t) >PAGE_SIZE ) +return -EINVAL ;How can this :
Quoted 7 lines
(pos_in_page % __alignof__(atomic_t)) != 0to be false, and together this :
pos_in_page+sizeof(atomic_t)>PAGE_SIZEto be true ?
You're assuming that __alignof__(atomic_t) = N * sizeof(atomic_t), where N is an integer.
If alignof == 1, and sizeof == 4, you lose. I prefer to be future-proof.
This means I should clarify the comment...
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 21 lines · Rusty Russell
In message <[email protected]> y ou write:
Quoted 11 lines · Rusty Russell
kernel/futex.c line 176
Quoted 7 lines
kernel/futex.c +6
-0+ pos_in_page = ((unsigned long )uaddr) %PAGE_SIZE ; + +/* Must be "naturally" aligned, and not on page boundary. */ +if ((pos_in_page % __alignof__(atomic_t )) !=0 + || pos_in_page +sizeof (atomic_t) >PAGE_SIZE ) +return -EINVAL ;How can this :
Quoted 7 lines
(pos_in_page % __alignof__(atomic_t)) != 0to be false, and together this :
pos_in_page+sizeof(atomic_t)>PAGE_SIZEto be true ?
Quoted 7 lines
You're assuming that
__alignof__(atomic_t)= N *sizeof(atomic_t), where N is an integer.If alignof == 1, and sizeof == 4, you lose. I prefer to be future-proof.
This means I should clarify the comment...
No i should do less things at a time :-(
- Davide
Re: Futexes III : performance numbers
In reply to Hubertus FrankeIn message <[email protected]> you write:
Quoted 11 lines · Rusty Russell
Quoted 6 lines
1)
FUTEX_UPandFUTEX_DOWNdefines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.Example userspace lib attached,
Rusty.I did a quick hack to enable ulockflex to run on the latest interface that Rusty posted.
Cool... is this 8-way or some such "serious" SMP? How about the below microoptimization (untested, but you get the idea).
Quoted 9 lines
Now 3 processes
3 1 5 4 k 0 0 0 99.98 0.00 0.00 0.033284 242040 3 1 5 4 m 0 0 0 0.29 0.00 0.00 0.018406 1979992 3 1 5 4 f 0 0 0 99.71 0.00 0.00 0.028083 306140 3 1 5 4 c 0 0 0 7.79 0.00 4.00 0.437084 774175Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better.
Hmmm... actually I'm limited FIFO, in that I queue on the tail and do wake one. Of course, someone can come in userspace and grab the lock while the guy in the kernel is waking up, and this is clearly happening here.
This can be fixed, I think, by saying to the one we wake up "you have the lock" and never actually changing the value to 1. This might cost us very little: I'll send another patch this afternoon.
Cheers!
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
kernel/futex.c +29 -22
@@ -51,12 +51,17 @@atomic_t *count; }; -/* The key for the hash is the address + index + offset within page */ -static struct list_head futex_queues[1 <<FUTEX_HASHBITS ]; -static spinlock_t futex_lock =SPIN_LOCK_UNLOCKED ; +/* The key for the hash is the address + offset within page */ +struct futex_head +{ +struct list_head list; +static spinlock_t lock; +}____cacheline_aligned ; -static inline struct list_head *hash_futex (struct page *page, -unsigned long offset) +static struct futex_head futex_queues[1<<FUTEX_HASHBITS] __cacheline_aligned; + +static inline struct futex_head *hash_futex (struct page *page, +unsigned long offset) {unsigned long h;
@@ -66,12 +71,12 @@return &futex_queues[hash_long (h,FUTEX_HASHBITS )]; } -static inline void wake_one_waiter (struct list_head *head,atomic_t *count) +static inline void wake_one_waiter (struct futex_head *head,atomic_t *count) {struct list_head *i; -spin_lock (&futex_lock); -list_for_each (i, head) { +spin_lock (&head->lock); +list_for_each (i, &head->list) { struct futex_q *this = list_entry(i, struct futex_q, list);if (this->count == count) {
@@ -79,27 +84,27 @@break ; } } -spin_unlock (&futex_lock); +spin_unlock (&head->lock); }/* Add at end to avoid starvation */ -static inline void queue_me (struct list_head *head, +static inline void queue_me (struct futex_head *head,struct futex_q *q,atomic_t *count) { q->task = current; q->count = count; -spin_lock (&futex_lock); -list_add_tail (&q->list, head); -spin_unlock (&futex_lock); +spin_lock (&head->lock); +list_add_tail (&q->list, &head->list); +spin_unlock (&head->lock); } -static inline void unqueue_me (struct futex_q *q) +static inline void unqueue_me (struct futex_head *head,struct futex_q *q) { -spin_lock (&futex_lock); +spin_lock (&head->lock);list_del (&q->list); -spin_unlock (&futex_lock); +spin_unlock (&head->lock); }/* Get kernel address of the user page and pin it. */
@@ -124,7 +129,7 @@ }/* Simplified from arch/ppc/kernel/semaphore.c: Paul M. is a genius. */ -static int futex_down (struct list_head *head,atomic_t *count) +static int futex_down (struct futex_head *head,atomic_t *count) {int retval =0 ;struct futex_q q;
@@ -143,7 +148,7 @@ current->state =TASK_INTERRUPTIBLE ; } current->state =TASK_RUNNING ; -unqueue_me (&q); +unqueue_me (head, &q);/* If we were signalled, we might have just been woken: we must wake another one. Otherwise we need to wake someone else (if they are waiting) so they drop the count below 0,
@@ -153,7 +158,7 @@return retval; } -static int futex_up (struct list_head *head,atomic_t *count) +static int futex_up (struct futex_head *head,atomic_t *count) {atomic_set (count,1 );smp_wmb ();
@@ -165,7 +170,7 @@ {int ret;unsigned long pos_in_page; -struct list_head *head; +struct futex_head *head;struct page *page; pos_in_page = ((unsigned long )uaddr) %PAGE_SIZE ;
@@ -201,8 +206,10 @@ {unsigned int i; -for (i =0 ; i <ARRAY_SIZE (futex_queues); i++) -INIT_LIST_HEAD (&futex_queues[i]); +for (i =0 ; i <ARRAY_SIZE (futex_queues); i++) { +INIT_LIST_HEAD (&futex_queues[i].list); +spin_lock_init (&futex_queues[i].lock); + }return 0 ; }__initcall (init);
Re: Futexes III : performance numbers
In reply to Hubertus FrankeQuoted 8 lines · On Tue, 5 Mar 2002 16:23:14 -0500 Hubertus Franke <[email protected]>…
Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better. Wohh futex rock, BUT... with 0.29% contention it basically tells me that we are exhausting our entire quantum getting the lock without contention. So their is some serious fairness issue here at least for the tightly scheduled locks. Compare the M numbers for 2 and 3 children.
Fairness <sigh>. This patch should be much more FIFO: it works by handing the mutex straight to the first one on the queue if there is one, and only actually "freeing" it if there's noone waiting.
Unfortunately, it seems to hurt performance by 50% on tdbtorture (although there are weird scheduler things happening too).
Here's the "fair" patch:
Rusty.
Signature · 491 lines
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/arch/i386/kernel/entry.S working-2.5.6-pre2-futex/arch/i386/kernel/entry.S
--- linux-2.5.6-pre2/arch/i386/kernel/entry.S Wed Feb 20 17:56:59 2002
+++ working-2.5.6-pre2-futex/arch/i386/kernel/entry.S Tue Mar 5 13:53:33 2002
@@ -716,6 +716,7 @@
.long SYMBOL_NAME(sys_lremovexattr)
.long SYMBOL_NAME(sys_fremovexattr)
.long SYMBOL_NAME(sys_tkill)
+ .long SYMBOL_NAME(sys_futex)
.rept NR_syscalls-(.-sys_call_table)/4
.long SYMBOL_NAME(sys_ni_syscall)
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/arch/ppc/kernel/misc.S working-2.5.6-pre2-futex/arch/ppc/kernel/misc.S
--- linux-2.5.6-pre2/arch/ppc/kernel/misc.S Wed Feb 20 17:57:04 2002
+++ working-2.5.6-pre2-futex/arch/ppc/kernel/misc.S Tue Mar 5 13:53:33 2002
@@ -1246,6 +1246,7 @@
.long sys_removexattr
.long sys_lremovexattr
.long sys_fremovexattr /* 220 */
+ .long sys_futex
.rept NR_syscalls-(.-sys_call_table)/4
.long sys_ni_syscall
.endr
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/asm-i386/atomic.h working-2.5.6-pre2-futex/include/asm-i386/atomic.h
--- linux-2.5.6-pre2/include/asm-i386/atomic.h Fri Nov 23 06:46:18 2001
+++ working-2.5.6-pre2-futex/include/asm-i386/atomic.h Tue Mar 5 14:03:15 2002
@@ -2,6 +2,7 @@
#define __ARCH_I386_ATOMIC__
#include <linux/config.h>
+#include <asm/system.h>
/*
* Atomic operations that C can't guarantee us. Useful for
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/asm-i386/mman.h working-2.5.6-pre2-futex/include/asm-i386/mman.h
--- linux-2.5.6-pre2/include/asm-i386/mman.h Wed Mar 15 12:45:20 2000
+++ working-2.5.6-pre2-futex/include/asm-i386/mman.h Tue Mar 5 13:53:33 2002
@@ -4,6 +4,7 @@
#define PROT_READ 0x1 /* page can be read */
#define PROT_WRITE 0x2 /* page can be written */
#define PROT_EXEC 0x4 /* page can be executed */
+#define PROT_SEM 0x8 /* page may be used for atomic ops */
#define PROT_NONE 0x0 /* page can not be accessed */
#define MAP_SHARED 0x01 /* Share changes */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/asm-i386/unistd.h working-2.5.6-pre2-futex/include/asm-i386/unistd.h
--- linux-2.5.6-pre2/include/asm-i386/unistd.h Wed Feb 20 17:56:40 2002
+++ working-2.5.6-pre2-futex/include/asm-i386/unistd.h Tue Mar 5 13:53:33 2002
@@ -243,6 +243,7 @@
#define __NR_lremovexattr 236
#define __NR_fremovexattr 237
#define __NR_tkill 238
+#define __NR_futex 239
/* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/asm-ppc/mman.h working-2.5.6-pre2-futex/include/asm-ppc/mman.h
--- linux-2.5.6-pre2/include/asm-ppc/mman.h Tue May 22 08:02:06 2001
+++ working-2.5.6-pre2-futex/include/asm-ppc/mman.h Tue Mar 5 13:53:33 2002
@@ -7,6 +7,7 @@
#define PROT_READ 0x1 /* page can be read */
#define PROT_WRITE 0x2 /* page can be written */
#define PROT_EXEC 0x4 /* page can be executed */
+#define PROT_SEM 0x8 /* page may be used for atomic ops */
#define PROT_NONE 0x0 /* page can not be accessed */
#define MAP_SHARED 0x01 /* Share changes */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/asm-ppc/unistd.h working-2.5.6-pre2-futex/include/asm-ppc/unistd.h
--- linux-2.5.6-pre2/include/asm-ppc/unistd.h Wed Feb 20 17:57:18 2002
+++ working-2.5.6-pre2-futex/include/asm-ppc/unistd.h Tue Mar 5 13:53:33 2002
@@ -228,6 +228,7 @@
#define __NR_removexattr 218
#define __NR_lremovexattr 219
#define __NR_fremovexattr 220
+#define __NR_futex 221
#define __NR(n) #n
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/linux/futex.h working-2.5.6-pre2-futex/include/linux/futex.h
--- linux-2.5.6-pre2/include/linux/futex.h Thu Jan 1 10:00:00 1970
+++ working-2.5.6-pre2-futex/include/linux/futex.h Tue Mar 5 13:53:33 2002
@@ -0,0 +1,8 @@
+#ifndef _LINUX_FUTEX_H
+#define _LINUX_FUTEX_H
+
+/* Second argument to futex syscall */
+#define FUTEX_UP (1)
+#define FUTEX_DOWN (-1)
+
+#endif
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/linux/hash.h working-2.5.6-pre2-futex/include/linux/hash.h
--- linux-2.5.6-pre2/include/linux/hash.h Thu Jan 1 10:00:00 1970
+++ working-2.5.6-pre2-futex/include/linux/hash.h Tue Mar 5 13:53:33 2002
@@ -0,0 +1,58 @@
+#ifndef _LINUX_HASH_H
+#define _LINUX_HASH_H
+/* Fast hashing routine for a long.
+ (C) 2002 William Lee Irwin III, IBM */
+
+/*
+ * Knuth recommends primes in approximately golden ratio to the maximum
+ * integer representable by a machine word for multiplicative hashing.
+ * Chuck Lever verified the effectiveness of this technique:
+ * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
+ *
+ * These primes are chosen to be bit-sparse, that is operations on
+ * them can use shifts and additions instead of multiplications for
+ * machines where multiplications are slow.
+ */
+#if BITS_PER_LONG == 32
+/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
+#define GOLDEN_RATIO_PRIME 0x9e370001UL
+#elif BITS_PER_LONG == 64
+/* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
+#define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
+#else
+#error Define GOLDEN_RATIO_PRIME for your wordsize.
+#endif
+
+static inline unsigned long hash_long(unsigned long val, unsigned int bits)
+{
+ unsigned long hash = val;
+
+#if BITS_PER_LONG == 64
+ /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
+ unsigned long n = hash;
+ n <<= 18;
+ hash -= n;
+ n <<= 33;
+ hash -= n;
+ n <<= 3;
+ hash += n;
+ n <<= 3;
+ hash -= n;
+ n <<= 4;
+ hash += n;
+ n <<= 2;
+ hash += n;
+#else
+ /* On some cpus multiply is faster, on others gcc will do shifts */
+ hash *= GOLDEN_RATIO_PRIME;
+#endif
+
+ /* High bits are more random, so use them. */
+ return hash >> (BITS_PER_LONG - bits);
+}
+
+static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
+{
+ return hash_long((unsigned long)ptr, bits);
+}
+#endif /* _LINUX_HASH_H */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/include/linux/mmzone.h working-2.5.6-pre2-futex/include/linux/mmzone.h
--- linux-2.5.6-pre2/include/linux/mmzone.h Fri Mar 1 22:58:34 2002
+++ working-2.5.6-pre2-futex/include/linux/mmzone.h Tue Mar 5 14:03:15 2002
@@ -51,8 +51,7 @@
/*
* wait_table -- the array holding the hash table
* wait_table_size -- the size of the hash table array
- * wait_table_shift -- wait_table_size
- * == BITS_PER_LONG (1 << wait_table_bits)
+ * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
*
* The purpose of all these is to keep track of the people
* waiting for a page to become available and make them
@@ -75,7 +74,7 @@
*/
wait_queue_head_t * wait_table;
unsigned long wait_table_size;
- unsigned long wait_table_shift;
+ unsigned long wait_table_bits;
/*
* Discontig memory support fields.
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/kernel/Makefile working-2.5.6-pre2-futex/kernel/Makefile
--- linux-2.5.6-pre2/kernel/Makefile Wed Feb 20 17:56:17 2002
+++ working-2.5.6-pre2-futex/kernel/Makefile Tue Mar 5 13:53:33 2002
@@ -15,7 +15,7 @@
obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \
module.o exit.o itimer.o info.o time.o softirq.o resource.o \
sysctl.o acct.o capability.o ptrace.o timer.o user.o \
- signal.o sys.o kmod.o context.o
+ signal.o sys.o kmod.o context.o futex.o
obj-$(CONFIG_UID16) += uid16.o
obj-$(CONFIG_MODULES) += ksyms.o
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/kernel/futex.c working-2.5.6-pre2-futex/kernel/futex.c
--- linux-2.5.6-pre2/kernel/futex.c Thu Jan 1 10:00:00 1970
+++ working-2.5.6-pre2-futex/kernel/futex.c Wed Mar 6 17:55:09 2002
@@ -0,0 +1,229 @@
+/*
+ * Fast Userspace Mutexes (which I call "Futexes!").
+ * (C) Rusty Russell, IBM 2002
+ *
+ * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
+ * enough at me, Linus for the original (flawed) idea, Matthew
+ * Kirkwood for proof-of-concept implementation.
+ *
+ * "The futexes are also cursed."
+ * "But they come in a choice of three flavours!"
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <linux/kernel.h>
+#include <linux/spinlock.h>
+#include <linux/sched.h>
+#include <linux/mm.h>
+#include <linux/hash.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/futex.h>
+#include <asm/atomic.h>
+
+/* These mutexes are a very simple counter: the winner is the one who
+ decrements from 1 to 0. 1 == free. 0 == noone sleeping.
+
+ This is simple enough to work on all architectures. */
+
+/* FIXME: This may be way too small. --RR */
+#define FUTEX_HASHBITS 6
+
+/* We use this instead of a wait_queue_t, so we can wake only the
+ relevent ones (hashed queues may be shared) */
+struct futex_q {
+ struct list_head list;
+ struct task_struct *task;
+ atomic_t *count;
+};
+
+/* The key for the hash is the address + offset within page */
+static struct list_head futex_queues[1<<FUTEX_HASHBITS];
+static spinlock_t futex_lock = SPIN_LOCK_UNLOCKED;
+
+#define FUTEX_PASSED ((void *)-1)
+
+/* Try to find someone else to pass futex to. */
+static int pass_futex(struct list_head *head, atomic_t *count)
+{
+ struct list_head *i;
+ struct futex_q *recipient = NULL;
+ int more_candidates = 0;
+
+ /* Find first, and keep looking to see if there are others. */
+ list_for_each(i, head) {
+ struct futex_q *this = list_entry(i, struct futex_q, list);
+
+ if (this->count == count) {
+ if (!recipient) recipient = this;
+ else {
+ /* Someone else waiting, too. */
+ more_candidates = 1;
+ break;
+ }
+ }
+ }
+
+ /* Nobody wants it. */
+ if (!recipient)
+ return 0;
+
+ /* Fixup to avoid wasted wakeup when we up() later. */
+ if (!more_candidates)
+ atomic_set(count, 0);
+
+ /* Pass directly to them. */
+ recipient->count = FUTEX_PASSED;
+ smp_wmb();
+ wake_up_process(recipient->task);
+ return 1;
+}
+
+static inline struct list_head *hash_futex(struct page *page,
+ unsigned long offset)
+{
+ unsigned long h;
+
+ /* If someone is sleeping, page is pinned. ie. page_address
+ is a constant when we care about it. */
+ h = (unsigned long)page_address(page) + offset;
+ return &futex_queues[hash_long(h, FUTEX_HASHBITS)];
+}
+
+/* Add at end to make it FIFO. */
+static inline void queue_me(struct list_head *head, struct futex_q *q)
+{
+ q->task = current;
+
+ spin_lock(&futex_lock);
+ list_add_tail(&q->list, head);
+ spin_unlock(&futex_lock);
+}
+
+/* Return true if there is someone else waiting as well */
+static inline void unqueue_me(struct futex_q *q)
+{
+ spin_lock(&futex_lock);
+ list_del(&q->list);
+ spin_unlock(&futex_lock);
+}
+
+/* Get kernel address of the user page and pin it. */
+static struct page *pin_page(unsigned long page_start)
+{
+ struct mm_struct *mm = current->mm;
+ struct page *page;
+ int err;
+
+ down_read(&mm->mmap_sem);
+ err = get_user_pages(current, current->mm, page_start,
+ 1 /* one page */,
+ 1 /* writable */,
+ 0 /* don't force */,
+ &page,
+ NULL /* don't return vmas */);
+ up_read(&mm->mmap_sem);
+
+ if (err < 0)
+ return ERR_PTR(err);
+ return page;
+}
+
+/* Simplified from arch/ppc/kernel/semaphore.c: Paul M. is a genius. */
+static int futex_down(struct list_head *head, atomic_t *count)
+{
+ struct futex_q q;
+
+ current->state = TASK_INTERRUPTIBLE;
+ q.count = count;
+ queue_me(head, &q);
+
+ /* It may have become available while we were adding ourselves
+ to queue? Also, make sure it's -ve so userspace knows
+ there's someone waiting. */
+ while ((atomic_read(count) < 0 || !atomic_dec_and_test(count))
+ && q.count != FUTEX_PASSED) {
+ schedule();
+ current->state = TASK_INTERRUPTIBLE;
+
+ if (signal_pending(current)) {
+ unqueue_me(&q);
+
+ /* We might have been passed futex anyway. */
+ return (q.count == FUTEX_PASSED) ? 0 : -EINTR;
+ }
+ }
+
+ /* We got the futex! */
+ current->state = TASK_RUNNING;
+ unqueue_me(&q);
+ return 0;
+}
+
+static int futex_up(struct list_head *head, atomic_t *count)
+{
+ spin_lock(&futex_lock);
+ if (!pass_futex(head, count))
+ /* Noone to receive: set to one and leave it free. */
+ atomic_set(count, 1);
+ spin_unlock(&futex_lock);
+ return 0;
+}
+
+asmlinkage int sys_futex(void *uaddr, int op)
+{
+ int ret;
+ unsigned long pos_in_page;
+ struct list_head *head;
+ struct page *page;
+
+ pos_in_page = ((unsigned long)uaddr) % PAGE_SIZE;
+
+ /* Must be "naturally" aligned, and not on page boundary. */
+ if ((pos_in_page % __alignof__(atomic_t)) != 0
+ || pos_in_page + sizeof(atomic_t) > PAGE_SIZE)
+ return -EINVAL;
+
+ /* Simpler if it doesn't vanish underneath us. */
+ page = pin_page((unsigned long)uaddr - pos_in_page);
+ if (IS_ERR(page))
+ return PTR_ERR(page);
+
+ head = hash_futex(page, pos_in_page);
+ switch (op) {
+ case FUTEX_UP:
+ ret = futex_up(head, page_address(page) + pos_in_page);
+ break;
+ case FUTEX_DOWN:
+ ret = futex_down(head, page_address(page) + pos_in_page);
+ break;
+ /* Add other lock types here... */
+ default:
+ ret = -EINVAL;
+ }
+ put_page(page);
+
+ return ret;
+}
+
+static int __init init(void)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(futex_queues); i++)
+ INIT_LIST_HEAD(&futex_queues[i]);
+ return 0;
+}
+__initcall(init);
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/mm/filemap.c working-2.5.6-pre2-futex/mm/filemap.c
--- linux-2.5.6-pre2/mm/filemap.c Fri Mar 1 23:27:15 2002
+++ working-2.5.6-pre2-futex/mm/filemap.c Tue Mar 5 13:53:33 2002
@@ -25,6 +25,7 @@
#include <linux/iobuf.h>
#include <linux/compiler.h>
#include <linux/fs.h>
+#include <linux/hash.h>
#include <asm/pgalloc.h>
#include <asm/uaccess.h>
@@ -773,32 +774,8 @@
static inline wait_queue_head_t *page_waitqueue(struct page *page)
{
const zone_t *zone = page_zone(page);
- wait_queue_head_t *wait = zone->wait_table;
- unsigned long hash = (unsigned long)page;
-#if BITS_PER_LONG == 64
- /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
- unsigned long n = hash;
- n <<= 18;
- hash -= n;
- n <<= 33;
- hash -= n;
- n <<= 3;
- hash += n;
- n <<= 3;
- hash -= n;
- n <<= 4;
- hash += n;
- n <<= 2;
- hash += n;
-#else
- /* On some cpus multiply is faster, on others gcc will do shifts */
- hash *= GOLDEN_RATIO_PRIME;
-#endif
-
- hash >>= zone->wait_table_shift;
-
- return &wait[hash];
+ return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
}
/*
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/mm/mprotect.c working-2.5.6-pre2-futex/mm/mprotect.c
--- linux-2.5.6-pre2/mm/mprotect.c Wed Feb 20 17:57:21 2002
+++ working-2.5.6-pre2-futex/mm/mprotect.c Tue Mar 5 13:53:33 2002
@@ -280,7 +280,7 @@
end = start + len;
if (end < start)
return -EINVAL;
- if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
+ if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
return -EINVAL;
if (end == start)
return 0;
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/current-dontdiff --minimal linux-2.5.6-pre2/mm/page_alloc.c working-2.5.6-pre2-futex/mm/page_alloc.c
--- linux-2.5.6-pre2/mm/page_alloc.c Wed Feb 20 17:57:21 2002
+++ working-2.5.6-pre2-futex/mm/page_alloc.c Tue Mar 5 13:53:33 2002
@@ -776,8 +776,8 @@
* per zone.
*/
zone->wait_table_size = wait_table_size(size);
- zone->wait_table_shift =
- BITS_PER_LONG - wait_table_bits(zone->wait_table_size);
+ zone->wait_table_bits =
+ wait_table_bits(zone->wait_table_size);
zone->wait_table = (wait_queue_head_t *)
alloc_bootmem_node(pgdat, zone->wait_table_size
* sizeof(wait_queue_head_t));Re: Futexes III : performance numbers
In reply to Rusty RussellQuoted 25 lines · Rusty Russell
In message <[email protected]> you write:
Quoted 10 lines · Rusty Russell
Quoted 6 lines
1)
FUTEX_UPandFUTEX_DOWNdefines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.Example userspace lib attached,
Rusty.I did a quick hack to enable ulockflex to run on the latest interface that Rusty posted.
Cool... is this 8-way or some such "serious" SMP? How about the below microoptimization (untested, but you get the idea).
Quoted 9 lines
Now 3 processes
3 1 5 4 k 0 0 0 99.98 0.00 0.00 0.033284 242040 3 1 5 4 m 0 0 0 0.29 0.00 0.00 0.018406 1979992 3 1 5 4 f 0 0 0 99.71 0.00 0.00 0.028083 306140 3 1 5 4 c 0 0 0 7.79 0.00 4.00 0.437084 774175Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better.
Quoted 9 lines
Hmmm... actually I'm limited FIFO, in that I queue on the tail and do wake one. Of course, someone can come in userspace and grab the lock while the guy in the kernel is waking up, and this is clearly happening here.
This can be fixed, I think, by saying to the one we wake up "you have the lock" and never actually changing the value to 1. This might cost us very little: I'll send another patch this afternoon.
Well, yes it can be fixed as I did in my package, but it comes at a substantial cost as seen above. The question is whether to simply ignore strict FIFO requirements ?
Doing the FIFO leads to the convoy problem, namely your lock arrival becomes the scheduling queue.
As seen above from the nonexisting contention, mootexes completely exhaust their scheduling quantum before allowing anybocy else to grap the lock. This is desired behavior particular for high traffic, low lockhold time locks, but not for others.
In this case you simply hand over the lock and won't allow anybody in user space to grap it during the time window one is woken up in the kernel.
Also, from my own experience doing a spinning lock that way
Another issue, is a few more operations, what would be nice is to be able to wake up all waiting processes and have them recontent?
Quoted 2 lines
Cheers!
Rusty.
Signature · 1 line
-- Hubertus Franke ([email protected])
Re: Futexes III : performance numbers
In reply to Rusty RussellQuoted 16 lines · Rusty Russell
On Tue, 5 Mar 2002 16:23:14 -0500
Quoted 8 lines · Hubertus Franke
Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better. Wohh futex rock, BUT... with 0.29% contention it basically tells me that we are exhausting our entire quantum getting the lock without contention. So their is some serious fairness issue here at least for the tightly scheduled locks. Compare the M numbers for 2 and 3 children.
Fairness <sigh>. This patch should be much more FIFO: it works by handing the mutex straight to the first one on the queue if there is one, and only actually "freeing" it if there's noone waiting.
Quoted 5 lines
Unfortunately, it seems to hurt performance by 50% on tdbtorture (although there are weird scheduler things happening too).
Here's the "fair" patch:
Rusty.
Thanks, Rusty, man you are a coding machine :-)
Now you are experiencing all the issues that I went through as well. Point I was trying ot make is, that there is not cookie cutter solution. One must provide the various options to the higher level and let the application choose what mootex semantics it wants.
There is applicability for fair futexes and for convoy avoidance futexes.
So let's put both in, and later expand it to read/write stuff.
Signature · 1 line
-- Hubertus Franke ([email protected])
Re: Futexes III : performance numbers
In reply to Rusty RussellQuoted 16 lines · Rusty Russell
On Tue, 5 Mar 2002 16:23:14 -0500
Quoted 8 lines · Hubertus Franke
Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better. Wohh futex rock, BUT... with 0.29% contention it basically tells me that we are exhausting our entire quantum getting the lock without contention. So their is some serious fairness issue here at least for the tightly scheduled locks. Compare the M numbers for 2 and 3 children.
Fairness <sigh>. This patch should be much more FIFO: it works by handing the mutex straight to the first one on the queue if there is one, and only actually "freeing" it if there's noone waiting.
Quoted 5 lines
Unfortunately, it seems to hurt performance by 50% on tdbtorture (although there are weird scheduler things happening too).
Here's the "fair" patch:
Rusty.
Rusty why not provide something along the line <snipped all over the place>.
#define FUTEX_DOWN (0)
#define FUTEX_UP (1)
#define FUTEX_FAIR_UP (2)
static int futex_up(struct list_head *head, atomic_t *count)
{
atomic_set(count, 1);
smp_wmb();
wake_one_waiter(head, count);
return 0;
} static int futex_fair_up(truct list_head *head, atomic_t *count)
{
spin_lock(&futex_lock);
if (!pass_futex(head, count))
/* Noone to receive: set to one and leave it free. */
atomic_set(count, 1);
spin_unlock(&futex_lock);
return 0;
}asmlinkage int sys_futex(void *uaddr, int op)
{
<..... snip ....>
head = hash_futex(page, pos_in_page);
switch (op) {
case FUTEX_DOWN:
ret = futex_down(head, page_address(page) + pos_in_page);
break;
case FUTEX_UP:
ret = futex_up(head, page_address(page) + pos_in_page);
break;
case FUTEX_FAIR_UP:
ret = futex_fair_up(head, page_address(page) + pos_in_page);
break;
default :
<..... snip ....>
}This would satisfy the fair vs. calock issue, you let the app decide what to use. Best of all, it seems to me you can even mix it.
Imagine, if a process knows it will soon reaquire the lock, then it would use FUTEX_UP to avoid being tagged back to the end of wait queue avoiding costly scheduling events. At the same time, if the process knows that its done for a while with that lock, then it issues FUTEX_FAIR_UP. The best of two worlds..
It also shows how cleanly the code can be expanded in the future. The more I look at the hash queues the more I like it. Will look at the rwlock now. Let me know what you think.
Signature · 1 line
-- Hubertus Franke ([email protected])
Re: [Lse-tech] Re: Futexes III : performance numbers
In reply to Rusty RussellQuoted 47 lines · Hubertus Franke
Quoted 25 lines · Rusty Russell
In message <[email protected]> you write:
Quoted 10 lines · Rusty Russell
Quoted 6 lines
1)
FUTEX_UPandFUTEX_DOWNdefines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.Example userspace lib attached,
Rusty.I did a quick hack to enable ulockflex to run on the latest interface that Rusty posted.
Cool... is this 8-way or some such "serious" SMP? How about the below microoptimization (untested, but you get the idea).
Quoted 9 lines
Now 3 processes
3 1 5 4 k 0 0 0 99.98 0.00 0.00 0.033284 242040 3 1 5 4 m 0 0 0 0.29 0.00 0.00 0.018406 1979992 3 1 5 4 f 0 0 0 99.71 0.00 0.00 0.028083 306140 3 1 5 4 c 0 0 0 7.79 0.00 4.00 0.437084 774175Interesting... the strict FIFO ordering of my fast semaphores limits performance as seen by 99.71% contention, so we always ditch into the kernel. Convoy Avoidance locks 2.5 times better.
Quoted 9 lines
Hmmm... actually I'm limited FIFO, in that I queue on the tail and do wake one. Of course, someone can come in userspace and grab the lock while the guy in the kernel is waking up, and this is clearly happening here.
This can be fixed, I think, by saying to the one we wake up "you have the lock" and never actually changing the value to 1. This might cost us very little: I'll send another patch this afternoon.
Well, yes it can be fixed as I did in my package, but it comes at a substantial cost as seen above. The question is whether to simply ignore strict FIFO requirements ?
Doing the FIFO leads to the convoy problem, namely your lock arrival becomes the scheduling queue.
As seen above from the nonexisting contention, mootexes completely exhaust their scheduling quantum before allowing anybocy else to grap the lock. This is desired behavior particular for high traffic, low lockhold time locks, but not for others.
Quoted 7 lines
In this case you simply hand over the lock and won't allow anybody in user space to grap it during the time window one is woken up in the kernel.
Also, from my own experience doing a spinning lock that wayAnother issue, is a few more operations, what would be nice is to be able to wake up all waiting processes and have them recontent?
Unless you are ready to go to priority queues (IMHO the preferred approach) this is the only way to avoid priority inversion, especially in a real time environment.
Signature · 3 lines
George [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Real time sched: http://sourceforge.net/projects/rtsched/
Futexes V :
In reply to Rusty RussellOn Wednesday 06 March 2002 11:13 am, Hubertus Franke wrote:
I cut a new version with what I was previously discussing. Now we have two kind of wakeup mechanism
(a) regular wakeup (as was) which basically gives you convoy avoidance
(b) fair wakeup (will first wake a waiting process up .. FIFO)
Basically integrated 2 prior patches of Rusty
Also changed FUTEX_DOWN, FUTEX_UP and FUTEX_UP_FAIR operands to be linear (0,1,2), should make the case statement faster, particularly when we get more operands.
frankeh:1019:~/futex/ulockflex>
./ulockflex -c 3 -a 1 -t 2 -o 5 -m 2 -R 499 -r 0 -x 0 -L f
SysV: 3 1 2 1 k 0 0 0 99.42 0.00 0.00 0.141423 243718
FAIR: 3 1 2 1 f 0 0 0 93.67 0.00 0.00 0.049231 217001
CA: 3 1 2 1 f 0 0 0 0.01 0.00 0.00 0.154154 2002852
Yesterdays numbers where
3 1 5 4 k 0 0 0 99.98 0.00 0.00 0.033284 242040 3 1 5 4 m 0 0 0 0.29 0.00 0.00 0.018406 1979992 3 1 5 4 f 0 0 0 99.71 0.00 0.00 0.028083 306140 3 1 5 4 c 0 0 0 7.79 0.00 4.00 0.437084 774175
Indicates that fair locking still needs some work, but what Rusty provided on top the current
Signature · 530 lines
-- Hubertus Franke ([email protected]) --------------------------------------------------------------------------------------------- diff -urbN linux-2.5.5/arch/i386/kernel/entry.S linux-2.5.5-futex/arch/i386/kernel/entry.S --- linux-2.5.5/arch/i386/kernel/entry.S Tue Feb 19 21:10:58 2002 +++ linux-2.5.5-futex/arch/i386/kernel/entry.S Wed Mar 6 11:40:12 2002 @@ -716,6 +716,7 @@ .long SYMBOL_NAME(sys_lremovexattr) .long SYMBOL_NAME(sys_fremovexattr) .long SYMBOL_NAME(sys_tkill) + .long SYMBOL_NAME(sys_futex) .rept NR_syscalls-(.-sys_call_table)/4 .long SYMBOL_NAME(sys_ni_syscall) diff -urbN linux-2.5.5/arch/ppc/kernel/misc.S linux-2.5.5-futex/arch/ppc/kernel/misc.S --- linux-2.5.5/arch/ppc/kernel/misc.S Tue Feb 19 21:11:00 2002 +++ linux-2.5.5-futex/arch/ppc/kernel/misc.S Wed Mar 6 11:40:12 2002 @@ -1246,6 +1246,7 @@ .long sys_removexattr .long sys_lremovexattr .long sys_fremovexattr /* 220 */ + .long sys_futex .rept NR_syscalls-(.-sys_call_table)/4 .long sys_ni_syscall .endr diff -urbN linux-2.5.5/include/asm-i386/atomic.h linux-2.5.5-futex/include/asm-i386/atomic.h --- linux-2.5.5/include/asm-i386/atomic.h Tue Feb 19 21:10:58 2002 +++ linux-2.5.5-futex/include/asm-i386/atomic.h Wed Mar 6 11:42:35 2002 @@ -2,6 +2,7 @@ #define __ARCH_I386_ATOMIC__ #include <linux/config.h> +#include <asm/system.h> /* * Atomic operations that C can't guarantee us. Useful for diff -urbN linux-2.5.5/include/asm-i386/mman.h linux-2.5.5-futex/include/asm-i386/mman.h --- linux-2.5.5/include/asm-i386/mman.h Tue Feb 19 21:10:56 2002 +++ linux-2.5.5-futex/include/asm-i386/mman.h Wed Mar 6 11:40:12 2002 @@ -4,6 +4,7 @@ #define PROT_READ 0x1 /* page can be read */ #define PROT_WRITE 0x2 /* page can be written */ #define PROT_EXEC 0x4 /* page can be executed */ +#define PROT_SEM 0x8 /* page may be used for atomic ops */ #define PROT_NONE 0x0 /* page can not be accessed */ #define MAP_SHARED 0x01 /* Share changes */ diff -urbN linux-2.5.5/include/asm-i386/unistd.h linux-2.5.5-futex/include/asm-i386/unistd.h --- linux-2.5.5/include/asm-i386/unistd.h Tue Feb 19 21:11:04 2002 +++ linux-2.5.5-futex/include/asm-i386/unistd.h Wed Mar 6 11:40:12 2002 @@ -243,6 +243,7 @@ #define __NR_lremovexattr 236 #define __NR_fremovexattr 237 #define __NR_tkill 238 +#define __NR_futex 239 /* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */ diff -urbN linux-2.5.5/include/asm-ppc/mman.h linux-2.5.5-futex/include/asm-ppc/mman.h --- linux-2.5.5/include/asm-ppc/mman.h Tue Feb 19 21:11:03 2002 +++ linux-2.5.5-futex/include/asm-ppc/mman.h Wed Mar 6 11:40:12 2002 @@ -7,6 +7,7 @@ #define PROT_READ 0x1 /* page can be read */ #define PROT_WRITE 0x2 /* page can be written */ #define PROT_EXEC 0x4 /* page can be executed */ +#define PROT_SEM 0x8 /* page may be used for atomic ops */ #define PROT_NONE 0x0 /* page can not be accessed */ #define MAP_SHARED 0x01 /* Share changes */ diff -urbN linux-2.5.5/include/asm-ppc/unistd.h linux-2.5.5-futex/include/asm-ppc/unistd.h --- linux-2.5.5/include/asm-ppc/unistd.h Tue Feb 19 21:10:57 2002 +++ linux-2.5.5-futex/include/asm-ppc/unistd.h Wed Mar 6 11:40:12 2002 @@ -228,6 +228,7 @@ #define __NR_removexattr 218 #define __NR_lremovexattr 219 #define __NR_fremovexattr 220 +#define __NR_futex 221 #define __NR(n) #n diff -urbN linux-2.5.5/include/linux/futex.h linux-2.5.5-futex/include/linux/futex.h --- linux-2.5.5/include/linux/futex.h Wed Dec 31 19:00:00 1969 +++ linux-2.5.5-futex/include/linux/futex.h Wed Mar 6 13:58:21 2002 @@ -0,0 +1,9 @@ +#ifndef _LINUX_FUTEX_H +#define _LINUX_FUTEX_H + +/* Second argument to futex syscall */ +#define FUTEX_DOWN (0) +#define FUTEX_UP (1) +#define FUTEX_UP_FAIR (2) + +#endif diff -urbN linux-2.5.5/include/linux/hash.h linux-2.5.5-futex/include/linux/hash.h --- linux-2.5.5/include/linux/hash.h Wed Dec 31 19:00:00 1969 +++ linux-2.5.5-futex/include/linux/hash.h Wed Mar 6 11:40:12 2002 @@ -0,0 +1,58 @@ +#ifndef _LINUX_HASH_H +#define _LINUX_HASH_H +/* Fast hashing routine for a long. + (C) 2002 William Lee Irwin III, IBM */ + +/* + * Knuth recommends primes in approximately golden ratio to the maximum + * integer representable by a machine word for multiplicative hashing. + * Chuck Lever verified the effectiveness of this technique: + * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf + * + * These primes are chosen to be bit-sparse, that is operations on + * them can use shifts and additions instead of multiplications for + * machines where multiplications are slow. + */ +#if BITS_PER_LONG == 32 +/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */ +#define GOLDEN_RATIO_PRIME 0x9e370001UL +#elif BITS_PER_LONG == 64 +/* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */ +#define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL +#else +#error Define GOLDEN_RATIO_PRIME for your wordsize. +#endif + +static inline unsigned long hash_long(unsigned long val, unsigned int bits) +{ + unsigned long hash = val; + +#if BITS_PER_LONG == 64 + /* Sigh, gcc can't optimise this alone like it does for 32 bits. */ + unsigned long n = hash; + n <<= 18; + hash -= n; + n <<= 33; + hash -= n; + n <<= 3; + hash += n; + n <<= 3; + hash -= n; + n <<= 4; + hash += n; + n <<= 2; + hash += n; +#else + /* On some cpus multiply is faster, on others gcc will do shifts */ + hash *= GOLDEN_RATIO_PRIME; +#endif + + /* High bits are more random, so use them. */ + return hash >> (BITS_PER_LONG - bits); +} + +static inline unsigned long hash_ptr(void *ptr, unsigned int bits) +{ + return hash_long((unsigned long)ptr, bits); +} +#endif /* _LINUX_HASH_H */ diff -urbN linux-2.5.5/include/linux/mmzone.h linux-2.5.5-futex/include/linux/mmzone.h --- linux-2.5.5/include/linux/mmzone.h Tue Feb 19 21:10:53 2002 +++ linux-2.5.5-futex/include/linux/mmzone.h Wed Mar 6 11:42:35 2002 @@ -51,8 +51,7 @@ /* * wait_table -- the array holding the hash table * wait_table_size -- the size of the hash table array - * wait_table_shift -- wait_table_size - * == BITS_PER_LONG (1 << wait_table_bits) + * wait_table_bits -- wait_table_size == (1 << wait_table_bits) * * The purpose of all these is to keep track of the people * waiting for a page to become available and make them @@ -75,7 +74,7 @@ */ wait_queue_head_t * wait_table; unsigned long wait_table_size; - unsigned long wait_table_shift; + unsigned long wait_table_bits; /* * Discontig memory support fields. diff -urbN linux-2.5.5/kernel/Makefile linux-2.5.5-futex/kernel/Makefile --- linux-2.5.5/kernel/Makefile Tue Feb 19 21:10:57 2002 +++ linux-2.5.5-futex/kernel/Makefile Wed Mar 6 11:40:12 2002 @@ -15,7 +15,7 @@ obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \ module.o exit.o itimer.o info.o time.o softirq.o resource.o \ sysctl.o acct.o capability.o ptrace.o timer.o user.o \ - signal.o sys.o kmod.o context.o + signal.o sys.o kmod.o context.o futex.o obj-$(CONFIG_UID16) += uid16.o obj-$(CONFIG_MODULES) += ksyms.o diff -urbN linux-2.5.5/kernel/futex.c linux-2.5.5-futex/kernel/futex.c --- linux-2.5.5/kernel/futex.c Wed Dec 31 19:00:00 1969 +++ linux-2.5.5-futex/kernel/futex.c Wed Mar 6 13:59:01 2002 @@ -0,0 +1,255 @@ +/* + * Fast Userspace Mutexes (which I call "Futexes!"). + * (C) Rusty Russell, IBM 2002 + * + * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly + * enough at me, Linus for the original (flawed) idea, Matthew + * Kirkwood for proof-of-concept implementation. + * + * "The futexes are also cursed." + * "But they come in a choice of three flavours!" + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ +#include <linux/kernel.h> +#include <linux/spinlock.h> +#include <linux/sched.h> +#include <linux/mm.h> +#include <linux/hash.h> +#include <linux/init.h> +#include <linux/fs.h> +#include <linux/futex.h> +#include <asm/atomic.h> + +/* These mutexes are a very simple counter: the winner is the one who + decrements from 1 to 0. 1 == free. 0 == noone sleeping. + + This is simple enough to work on all architectures. */ + +/* FIXME: This may be way too small. --RR */ +#define FUTEX_HASHBITS 6 + +/* We use this instead of a wait_queue_t, so we can wake only the + relevent ones (hashed queues may be shared) */ +struct futex_q { + struct list_head list; + struct task_struct *task; + atomic_t *count; +}; + +/* The key for the hash is the address + offset within page */ +static struct list_head futex_queues[1<<FUTEX_HASHBITS]; +static spinlock_t futex_lock = SPIN_LOCK_UNLOCKED; + +#define FUTEX_PASSED ((void *)-1) + +/* Try to find someone else to pass futex to. */ +static int pass_futex(struct list_head *head, atomic_t *count) +{ + struct list_head *i; + struct futex_q *recipient = NULL; + int more_candidates = 0; + + /* Find first, and keep looking to see if there are others. */ + list_for_each(i, head) { + struct futex_q *this = list_entry(i, struct futex_q, list); + + if (this->count == count) { + if (!recipient) recipient = this; + else { + /* Someone else waiting, too. */ + more_candidates = 1; + break; + } + } + } + + /* Nobody wants it. */ + if (!recipient) + return 0; + + /* Fixup to avoid wasted wakeup when we up() later. */ + if (!more_candidates) + atomic_set(count, 0); + + /* Pass directly to them. */ + recipient->count = FUTEX_PASSED; + smp_wmb(); + wake_up_process(recipient->task); + return 1; +} + +static inline void wake_one_waiter(struct list_head *head, atomic_t *count) +{ + struct list_head *i; + + spin_lock(&futex_lock); + list_for_each(i, head) { + struct futex_q *this = list_entry(i, struct futex_q, list); + + if (this->count == count) { + wake_up_process(this->task); + break; + } + } + spin_unlock(&futex_lock); +} + +static inline struct list_head *hash_futex(struct page *page, + unsigned long offset) +{ + unsigned long h; + + /* If someone is sleeping, page is pinned. ie. page_address + is a constant when we care about it. */ + h = (unsigned long)page_address(page) + offset; + return &futex_queues[hash_long(h, FUTEX_HASHBITS)]; +} + +/* Add at end to make it FIFO. */ +static inline void queue_me(struct list_head *head, struct futex_q *q) +{ + q->task = current; + + spin_lock(&futex_lock); + list_add_tail(&q->list, head); + spin_unlock(&futex_lock); +} + +/* Return true if there is someone else waiting as well */ +static inline void unqueue_me(struct futex_q *q) +{ + spin_lock(&futex_lock); + list_del(&q->list); + spin_unlock(&futex_lock); +} + +/* Get kernel address of the user page and pin it. */ +static struct page *pin_page(unsigned long page_start) +{ + struct mm_struct *mm = current->mm; + struct page *page; + int err; + + down_read(&mm->mmap_sem); + err = get_user_pages(current, current->mm, page_start, + 1 /* one page */, + 1 /* writable */, + 0 /* don't force */, + &page, + NULL /* don't return vmas */); + up_read(&mm->mmap_sem); + + if (err < 0) + return ERR_PTR(err); + return page; +} + +/* Simplified from arch/ppc/kernel/semaphore.c: Paul M. is a genius. */ +static int futex_down(struct list_head *head, atomic_t *count) +{ + struct futex_q q; + + current->state = TASK_INTERRUPTIBLE; + q.count = count; + queue_me(head, &q); + + /* It may have become available while we were adding ourselves + to queue? Also, make sure it's -ve so userspace knows + there's someone waiting. */ + while ((atomic_read(count) < 0 || !atomic_dec_and_test(count)) + && q.count != FUTEX_PASSED) { + schedule(); + current->state = TASK_INTERRUPTIBLE; + + if (signal_pending(current)) { + unqueue_me(&q); + + /* We might have been passed futex anyway. */ + return (q.count == FUTEX_PASSED) ? 0 : -EINTR; + } + } + + /* We got the futex! */ + current->state = TASK_RUNNING; + unqueue_me(&q); + return 0; +} + +static int futex_fair_up(struct list_head *head, atomic_t *count) +{ + spin_lock(&futex_lock); + if (!pass_futex(head, count)) + /* Noone to receive: set to one and leave it free. */ + atomic_set(count, 1); + spin_unlock(&futex_lock); + return 0; +} + +static int futex_up(struct list_head *head, atomic_t *count) +{ + atomic_set(count, 1); + smp_wmb(); + wake_one_waiter(head, count); + return 0; +} +asmlinkage int sys_futex(void *uaddr, int op) +{ + int ret; + unsigned long pos_in_page; + struct list_head *head; + struct page *page; + + pos_in_page = ((unsigned long)uaddr) % PAGE_SIZE; + + /* Must be "naturally" aligned, and not on page boundary. */ + if ((pos_in_page % __alignof__(atomic_t)) != 0 + || pos_in_page + sizeof(atomic_t) > PAGE_SIZE) + return -EINVAL; + + /* Simpler if it doesn't vanish underneath us. */ + page = pin_page((unsigned long)uaddr - pos_in_page); + if (IS_ERR(page)) + return PTR_ERR(page); + + head = hash_futex(page, pos_in_page); + switch (op) { + case FUTEX_DOWN: + ret = futex_down(head, page_address(page) + pos_in_page); + break; + case FUTEX_UP: + ret = futex_up(head, page_address(page) + pos_in_page); + break; + case FUTEX_UP_FAIR: + ret = futex_fair_up(head, page_address(page) + pos_in_page); + break; + /* Add other lock types here... */ + default: + ret = -EINVAL; + } + put_page(page); + + return ret; +} + +static int __init init(void) +{ + unsigned int i; + + for (i = 0; i < ARRAY_SIZE(futex_queues); i++) + INIT_LIST_HEAD(&futex_queues[i]); + return 0; +} +__initcall(init); diff -urbN linux-2.5.5/mm/filemap.c linux-2.5.5-futex/mm/filemap.c --- linux-2.5.5/mm/filemap.c Wed Mar 6 15:10:09 2002 +++ linux-2.5.5-futex/mm/filemap.c Wed Mar 6 11:40:12 2002 @@ -25,6 +25,7 @@ #include <linux/iobuf.h> #include <linux/compiler.h> #include <linux/fs.h> +#include <linux/hash.h> #include <asm/pgalloc.h> #include <asm/uaccess.h> @@ -773,32 +774,8 @@ static inline wait_queue_head_t *page_waitqueue(struct page *page) { const zone_t *zone = page_zone(page); - wait_queue_head_t *wait = zone->wait_table; - unsigned long hash = (unsigned long)page; -#if BITS_PER_LONG == 64 - /* Sigh, gcc can't optimise this alone like it does for 32 bits. */ - unsigned long n = hash; - n <<= 18; - hash -= n; - n <<= 33; - hash -= n; - n <<= 3; - hash += n; - n <<= 3; - hash -= n; - n <<= 4; - hash += n; - n <<= 2; - hash += n; -#else - /* On some cpus multiply is faster, on others gcc will do shifts */ - hash *= GOLDEN_RATIO_PRIME; -#endif - - hash >>= zone->wait_table_shift; - - return &wait[hash]; + return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)]; } /* diff -urbN linux-2.5.5/mm/mprotect.c linux-2.5.5-futex/mm/mprotect.c --- linux-2.5.5/mm/mprotect.c Tue Feb 19 21:11:01 2002 +++ linux-2.5.5-futex/mm/mprotect.c Wed Mar 6 11:40:12 2002 @@ -280,7 +280,7 @@ end = start + len; if (end < start) return -EINVAL; - if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC)) + if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM)) return -EINVAL; if (end == start) return 0; diff -urbN linux-2.5.5/mm/page_alloc.c linux-2.5.5-futex/mm/page_alloc.c --- linux-2.5.5/mm/page_alloc.c Tue Feb 19 21:10:55 2002 +++ linux-2.5.5-futex/mm/page_alloc.c Wed Mar 6 11:40:12 2002 @@ -776,8 +776,8 @@ * per zone. */ zone->wait_table_size = wait_table_size(size); - zone->wait_table_shift = - BITS_PER_LONG - wait_table_bits(zone->wait_table_size); + zone->wait_table_bits = + wait_table_bits(zone->wait_table_size); zone->wait_table = (wait_queue_head_t *) alloc_bootmem_node(pgdat, zone->wait_table_size * sizeof(wait_queue_head_t));
Re: Futexes III : performance numbers
In reply to Rusty RussellQuoted 1 line · Rusty Russell
In message <[email protected]> you write:
More on fairness. I hacked ulockflex to keep the history of lock acquisition and print it out after the run, so this doesn't create any overhead and is recorded while the lock is held (history buffer is pretouched)
Read as follows
lock-aquisition [ how often for the same process ] : process id
left out if only 1
First the FUTEX_UP later the FUTEX_UP_FAIR.
two cases (-r2 -x2) and (-r0 -x0)
Summary, its clearly seen how the fairness can be effected. It also show the efficacy of FUTEX_UP and FUTEX_UP_FAIR.
Comments.
./ulockflex -c 3 -a 1 -t 2 -o 5 -m 2 -R 499 -r 2 -x 1 -L f -H 2
===========================================================
(i.e. 2 usecs non-lockholdtime and 1 usec lockhold time)
----------------- UNFAIR LOCKS == FUTEX_UP ------------------
<..snip...>
1067602 [ 4065 ]: 1 1071667 [ 4522 ]: 0 1076189 : 2 1076190 [ 953 ]: 1 1077143 : 2 1077144 [ 2875 ]: 0 1080019 [ 4800 ]: 2 1084819 : 0 1084820 [ 968 ]: 1 1085788 : 0 1085789 : 1 1085790 : 0 1085791 : 1 1085792 : 0 1085793 : 1 1085794 : 0 1085795 : 1 1085796 : 0 1085797 : 1 1085798 : 0 1085799 : 1 1085800 : 0 1085801 : 1 1085802 : 0 1085803 : 1 1085804 : 0 1085805 : 1 1085806 : 0 1085807 : 1 1085808 : 0 1085809 : 1 1085810 : 0 1085811 : 1 1085812 : 0 1085813 : 2 1085814 [ 2 ]: 0 1085816 : 2 1085817 [ 2861 ]: 1 1088678 : 2 1088679 [ 4868 ]: 0 1093547 : 2 1093548 [ 914 ]: 1 1094462 : 2 1094463 [ 4829 ]: 0 1099292 : 2 1099293 [ 963 ]: 1 1100256 : 2 1100257 [ 4789 ]: 0 1105046 : 2 1105047 [ 966 ]: 1 1106013 : 2 1106014 [ 4800 ]: 0 1110814 : 2 1110815 [ 961 ]: 1 1111776 : 2 1111777 [ 2013 ]: 0 1113790 : 2 1113791 : 0 1113792 : 2 1113793 [ 3768 ]: 1 1117561 : 2 1117562 [ 4832 ]: 0 1122394 : 2 1122395 [ 955 ]: 1 1123350 : 2 1123351 [ 4813 ]: 0 1128164 : 2 1128165 [ 982 ]: 1 1129147 : 2 1129148 : 1 1129149 : 2 1129150 [ 4789 ]: 0 1133939 : 2 1133940 : 0 1133941 : 2 1133942 : 0 1133943 : 2 1133944 [ 969 ]: 1 1134913 : 2 1134914 [ 4841 ]: 0 1139755 : 2 1139756 [ 967 ]: 1 1140723 : 2 1140724 [ 4820 ]: 0 1145544 : 2 1145545 [ 969 ]: 1 1146514 : 2 1146515 [ 5007 ]: 0 1151522 : 2 1151523 [ 3678 ]: 1 1155201 : 2 1155202 [ 4756 ]: 0 1159958 : 2 1159959 [ 978 ]: 1
-------------------------- FAIR LOCKS == FUTEX_UP_FAIR ----------------
<... snip ...>
558617 : 0 558618 : 1 558619 : 2 558620 : 1 558621 : 0 558622 : 1 558623 : 2 558624 : 1 558625 : 0 558626 : 1 558627 : 2 558628 : 1 558629 : 0 558630 : 1 558631 : 2
<... and so on ....>
=================================================================.
/ulockflex -c 3 -a 1 -t 2 -o 5 -m 2 -R 499 -r 0 -x 0 -L f -H 2
===========================================================
(i.e. 0 usecs non-lockholdtime and 0 usec lockhold time)
----------------- UNFAIR LOCKS == FUTEX_UP ------------------
<..snip...>
7682404 [ 4593 ]: 1
7686997 : 2 7686998 [ 16336 ]: 0 7703334 : 2 7703335 [ 23875 ]: 1 7727210 [ 4 ]: 2 7727214 [ 20110 ]: 0 7747324 [ 13298 ]: 1 7760622 [ 11612 ]: 2 7772234 [ 8340 ]: 0 7780574 [ 6732 ]: 1 7787306 [ 13388 ]: 2 7800694 [ 3006 ]: 0 7803700 [ 17121 ]: 1 7820821 [ 6726 ]: 2 7827547 [ 13396 ]: 0 7840943 [ 6760 ]: 1 7847703 [ 13375 ]: 2 7861078 [ 4443 ]: 0 7865521 [ 15566 ]: 1 7881087 [ 6730 ]: 2 7887817 [ 13421 ]: 0 7901238 [ 3013 ]: 1 7904251 [ 16995 ]: 2 7921246 [ 6715 ]: 0 7927961 [ 13397 ]: 1 7941358 [ 6716 ]: 2 7948074 [ 13407 ]: 0 7961481 [ 6743 ]: 1 7968224 [ 13309 ]: 2 7981533 [ 6708 ]: 0 7988241 [ 13374 ]: 1 8001615 [ 3411 ]: 2 8005026 [ 16574 ]: 0 8021600 [ 7016 ]: 1
-------------------------- FAIR LOCKS == FUTEX_UP_FAIR ----------------
<... snip ...> same as for -r 2 -x 1
Signature · 1 line
-- Hubertus Franke ([email protected])
Re: Futexes III : performance numbers
In reply to Rusty RussellQuoted 1 line · Rusty Russell
In message <[email protected]> you write:
More on fairness. I hacked ulockflex to keep the history of lock acquisition and print it out after the run, so this doesn't create any overhead and is recorded while the lock is held (history buffer is pretouched)
Read as follows
lock-aquisition [ how often for the same process ] : process id
left out if only 1
First the FUTEX_UP later the FUTEX_UP_FAIR.
two cases (-r2 -x2) and (-r0 -x0)
Summary, its clearly seen how the fairness can be effected. It also show the efficacy of FUTEX_UP and FUTEX_UP_FAIR.
Comments.
./ulockflex -c 3 -a 1 -t 2 -o 5 -m 2 -R 499 -r 2 -x 1 -L f -H 2
===========================================================
(i.e. 2 usecs non-lockholdtime and 1 usec lockhold time)
----------------- UNFAIR LOCKS == FUTEX_UP ------------------
<..snip...>
1067602 [ 4065 ]: 1 1071667 [ 4522 ]: 0 1076189 : 2 1076190 [ 953 ]: 1 1077143 : 2 1077144 [ 2875 ]: 0 1080019 [ 4800 ]: 2 1084819 : 0 1084820 [ 968 ]: 1 1085788 : 0 1085789 : 1 1085790 : 0 1085791 : 1 1085792 : 0 1085793 : 1 1085794 : 0 1085795 : 1 1085796 : 0 1085797 : 1 1085798 : 0 1085799 : 1 1085800 : 0 1085801 : 1 1085802 : 0 1085803 : 1 1085804 : 0 1085805 : 1 1085806 : 0 1085807 : 1 1085808 : 0 1085809 : 1 1085810 : 0 1085811 : 1 1085812 : 0 1085813 : 2 1085814 [ 2 ]: 0 1085816 : 2 1085817 [ 2861 ]: 1 1088678 : 2 1088679 [ 4868 ]: 0 1093547 : 2 1093548 [ 914 ]: 1 1094462 : 2 1094463 [ 4829 ]: 0 1099292 : 2 1099293 [ 963 ]: 1 1100256 : 2 1100257 [ 4789 ]: 0 1105046 : 2 1105047 [ 966 ]: 1 1106013 : 2 1106014 [ 4800 ]: 0 1110814 : 2 1110815 [ 961 ]: 1 1111776 : 2 1111777 [ 2013 ]: 0 1113790 : 2 1113791 : 0 1113792 : 2 1113793 [ 3768 ]: 1 1117561 : 2 1117562 [ 4832 ]: 0 1122394 : 2 1122395 [ 955 ]: 1 1123350 : 2 1123351 [ 4813 ]: 0 1128164 : 2 1128165 [ 982 ]: 1 1129147 : 2 1129148 : 1 1129149 : 2 1129150 [ 4789 ]: 0 1133939 : 2 1133940 : 0 1133941 : 2 1133942 : 0 1133943 : 2 1133944 [ 969 ]: 1 1134913 : 2 1134914 [ 4841 ]: 0 1139755 : 2 1139756 [ 967 ]: 1 1140723 : 2 1140724 [ 4820 ]: 0 1145544 : 2 1145545 [ 969 ]: 1 1146514 : 2 1146515 [ 5007 ]: 0 1151522 : 2 1151523 [ 3678 ]: 1 1155201 : 2 1155202 [ 4756 ]: 0 1159958 : 2 1159959 [ 978 ]: 1
-------------------------- FAIR LOCKS == FUTEX_UP_FAIR ----------------
<... snip ...>
558617 : 0 558618 : 1 558619 : 2 558620 : 1 558621 : 0 558622 : 1 558623 : 2 558624 : 1 558625 : 0 558626 : 1 558627 : 2 558628 : 1 558629 : 0 558630 : 1 558631 : 2
<... and so on ....>
=================================================================.
/ulockflex -c 3 -a 1 -t 2 -o 5 -m 2 -R 499 -r 0 -x 0 -L f -H 2
===========================================================
(i.e. 0 usecs non-lockholdtime and 0 usec lockhold time)
----------------- UNFAIR LOCKS == FUTEX_UP ------------------
<..snip...>
7682404 [ 4593 ]: 1
7686997 : 2 7686998 [ 16336 ]: 0 7703334 : 2 7703335 [ 23875 ]: 1 7727210 [ 4 ]: 2 7727214 [ 20110 ]: 0 7747324 [ 13298 ]: 1 7760622 [ 11612 ]: 2 7772234 [ 8340 ]: 0 7780574 [ 6732 ]: 1 7787306 [ 13388 ]: 2 7800694 [ 3006 ]: 0 7803700 [ 17121 ]: 1 7820821 [ 6726 ]: 2 7827547 [ 13396 ]: 0 7840943 [ 6760 ]: 1 7847703 [ 13375 ]: 2 7861078 [ 4443 ]: 0 7865521 [ 15566 ]: 1 7881087 [ 6730 ]: 2 7887817 [ 13421 ]: 0 7901238 [ 3013 ]: 1 7904251 [ 16995 ]: 2 7921246 [ 6715 ]: 0 7927961 [ 13397 ]: 1 7941358 [ 6716 ]: 2 7948074 [ 13407 ]: 0 7961481 [ 6743 ]: 1 7968224 [ 13309 ]: 2 7981533 [ 6708 ]: 0 7988241 [ 13374 ]: 1 8001615 [ 3411 ]: 2 8005026 [ 16574 ]: 0 8021600 [ 7016 ]: 1
-------------------------- FAIR LOCKS == FUTEX_UP_FAIR ----------------
<... snip ...> same as for -r 2 -x 1
Signature · 1 line
-- Hubertus Franke ([email protected])
Re: Futexes V :
In reply to Hubertus FrankeQuoted 9 lines · On Wed, 6 Mar 2002 15:36:25 -0500 Hubertus Franke <[email protected]>…
On Wednesday 06 March 2002 11:13 am, Hubertus Franke wrote:
I cut a new version with what I was previously discussing. Now we have two kind of wakeup mechanism
(a) regular wakeup (as was) which basically gives you convoy avoidance
(b) fair wakeup (will first wake a waiting process up .. FIFO)Basically integrated 2 prior patches of Rusty
I like your numbers. Since I think fairness is nice, but lack of starvation is vital, I've been trying to starve a process.
So far, I've not managed it. Please hack on the below code, and see if you can manage it. If not, I think we can say "not a problem in real life", and just stick with the fastest implementation.
Thanks!
Rusty.
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#include <sys/time.h>
#include <time.h>
#include <signal.h>
#include "usersem.h"
#ifndef PROT_SEM
#define PROT_SEM 0x8
#endif
static void spinner(struct futex *sem, int hold)
{
while (1) {
futex_down(sem);
if (hold) sleep(1);
futex_up(sem);
}
}/* Test maximum time to lock given furious spinners. */ int main(int argc, char *argv[])
{
struct futex *sem;
unsigned int i;
unsigned long maxtime = 0;
pid_t children[100];
if (argc != 3) {
fprintf(stderr, "Usage: starve <numspinners> <iterations>\n");
exit(1);
}
sem = malloc(sizeof(*sem));
futex_region(sem, sizeof(*sem));
futex_init(sem);
for (i = 0; i < atoi(argv[1]); i++) {
children[i] = fork();
if (children[i] == 0)
spinner(sem, i < atoi(argv[1])/2);
}
for (i = 0; i < atoi(argv[2]); i++) {
struct timeval start, end, diff;
sleep(1);
gettimeofday(&start, NULL);
futex_down(sem);
gettimeofday(&end, NULL);
futex_up(sem);
timersub(&end, &start, &diff);
printf("Wait time: %lu.%06lu\n", diff.tv_sec, diff.tv_usec);
if (diff.tv_sec * 1000000 + diff.tv_usec > maxtime)
maxtime = diff.tv_sec * 1000000 + diff.tv_usec;
}
/* Kill children */
for (i = 0; i < atoi(argv[1]); i++)
kill(children[i], SIGTERM);
printf("Worst case: %lu\n", maxtime);
exit(0);
}Signature · 1 line
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 1 line · Davide Libenzi
Yes but this is always true alignof >= sizeof
No. m68k sets alignof to 2 for all types with sizeof >= 2.
r~
Quoted 4 lines · Rusty Russell
1)
FUTEX_UPandFUTEX_DOWNdefines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.
This doesn't work on highmem machines - doing the conversion from "<struct page, offset>" to "page_address(page)+offset" is simply not legal (not even for pure hashing purposes - page_address() changes as you kmap it).
You need to keep the <struct page,offset> tuple in that format, and no other. And when you actually touch the page, you need to do the kmap()/kunmap() (and you must not keep it mapped while you sleep, because that might trivially make the kernel run out of virtual mappings).
Linus
Quoted 9 lines · Linus Torvalds
Quoted 3 lines · Rusty Russell
1)
FUTEX_UPandFUTEX_DOWNdefines. (Robert Love)
2) Fix for the "decrement wraparound" problem (Paul Mackerras)
3) x86 fixes: tested on dual x86 box.This doesn't work on highmem machines - doing the conversion from "<struct page, offset>" to "page_address(page)+offset" is simply not legal (not even for pure hashing purposes -
page_address()changes as you kmap it).
Quoted 7 lines
You need to keep the <struct page,offset> tuple in that format, and no other. And when you actually touch the page, you need to do the kmap()/kunmap() (and you must not keep it mapped while you sleep, because that might trivially make the kernel run out of virtual mappings).
Linus
Good point, my package doesn't have that problem, but your suggestion should nicely fit into Rusty's patch, but it seems like increasing in overhead now.
Could you also comment on the functionality that has been discussed.
(I) the fairness issues that have been raised.
do you support two wakeup mechanism: FUTEX_UP and FUTEX_UP_FAIR or you don't care about fairness and starvation
(II) the rwlocks issues,
do you support rich set of functions/ops such as below
(a) writer preference
if any writer is waiting then wake that one up.
(b) reader preference
if any reader is waiting wait up all the readers in the queue
(c) fifo preference
if the first waiter is a writer wait it up, otherwise wake up all
readers
(d) fifo-fair preference
like (c), but only wake up readers until the next writer is
encountered
(a) - (c) can be implemented with Rusty's 2 user-ueue approach as long as the wakeup type is always the same. The last one can't (?).
In the kernel this is easy to implement, but the trouble is the status word in user space, still thinking about it.
It also requires compare and swap.
Thanks for your time and pointing this out.
Quoted 5 lines
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 2 lines · Hubertus Franke
Could you also comment on the functionality that has been discussed.
First off, I have to say that I really like the current patch by Rusty. The hashing approach is very clean, and it all seems quite good. As to specific points:
Quoted 3 lines
(I) the fairness issues that have been raised.
do you support two wakeup mechanism:FUTEX_UPandFUTEX_UP_FAIRor you don't care about fairness and starvation
I don't think fairness and starvation is that big of a deal for semaphores, usually being unfair in these things tends to just improve performance through better cache locality with no real downside. That said, I think the option should be open (which it does seem to be).
For rwlocks, my personal preference is the fifo-fair-preference (unlike semaphore fairness, I have actually seen loads where read- vs write-preference really is unacceptable). This might be a point where we give users the choice.
I do think we should make the lock bigger - I worry that atomic_t simply won't be enough for things like fair rwlocks, which might want a "cmpxchg8b" on x86.
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Linus
Quoted 4 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Can we go to cache line alignment - for an array of locks thats clearly advantageous
Quoted 6 lines · Linus Torvalds
Quoted 1 line · Hubertus Franke
Could you also comment on the functionality that has been discussed.
First off, I have to say that I really like the current patch by Rusty. The hashing approach is very clean, and it all seems quite good. As to
Agreed, Rusty did a marvelous job pulling all the right thinks together and adding the hashing.
Quoted 10 lines
specific points:
Quoted 3 lines
(I) the fairness issues that have been raised.
do you support two wakeup mechanism:FUTEX_UPandFUTEX_UP_FAIRor you don't care about fairness and starvationI don't think fairness and starvation is that big of a deal for semaphores, usually being unfair in these things tends to just improve performance through better cache locality with no real downside. That said, I think the option should be open (which it does seem to be).
Yip, I'd prefer to leave it up to the user on what one exactly wants fair or non-fair wakeup.
Quoted 9 lines
For rwlocks, my personal preference is the fifo-fair-preference (unlike semaphore fairness, I have actually seen loads where read- vs write-preference really is unacceptable). This might be a point where we give users the choice.
I do think we should make the lock bigger - I worry that
atomic_tsimply won't be enough for things like fair rwlocks, which might want a "cmpxchg8b" on x86.
But what about compatibility with i368, no cmpxchg or cmpxchg8b Can't we have to types and infer from the op in the kernel what the correct size in user space is.
Quoted 6 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Linus
Keeping it small, allows for the common case of mutex integration into data objects, though its not a big deal.
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 23 lines · Linus Torvalds
Quoted 2 lines · Hubertus Franke
Could you also comment on the functionality that has been discussed.
First off, I have to say that I really like the current patch by Rusty. The hashing approach is very clean, and it all seems quite good. As to specific points:
Quoted 3 lines
(I) the fairness issues that have been raised.
do you support two wakeup mechanism:FUTEX_UPandFUTEX_UP_FAIRor you don't care about fairness and starvationI don't think fairness and starvation is that big of a deal for semaphores, usually being unfair in these things tends to just improve performance through better cache locality with no real downside. That said, I think the option should be open (which it does seem to be).
For rwlocks, my personal preference is the fifo-fair-preference (unlike semaphore fairness, I have actually seen loads where read- vs write-preference really is unacceptable). This might be a point where we give users the choice.
Quoted 9 lines
I do think we should make the lock bigger - I worry that
atomic_tsimply won't be enough for things like fair rwlocks, which might want a "cmpxchg8b" on x86.So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
I think this is needed if we want to address the "task dies while holding a lock" issue. In this case we need to know who holds the lock.
-g
Quoted 7 lines
Linus
-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/
Signature · 3 lines
George [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Real time sched: http://sourceforge.net/projects/rtsched/
Quoted 7 lines · Hubertus Franke
Quoted 4 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Keeping it small, allows for the common case of mutex integration into data objects, though its not a big deal.
I guess we need to know what the requirements are of the fabled "architectures which need special handling of PROT_SEM" before we can tell whether this is a good idea or not.
Matthew.
Quoted 3 lines · Alan Cox
Can we go to cache line alignment - for an array of locks thats clearly advantageous
I disagree about the "clearly". Firstly, the cacheline alignment is CPU dependent, so on some CPU's it's 32 bytes (or even 16), on others it is 128 bytes.
Secondly, a lot of locking is actually done inside a single thread, and false sharing doesn't happen much - so keeping the locks dense can be quite advantageous.
The cases where false sharing _does_ happen and are a problem should be for the application writer to worry about, not for the kernel to force.
So I think 8 bytes is plenty fine enough - with 16 bytes a remote possibility (I don't think it is needed, but it gives you som epadding for future expansion). And people who have arrays and find false sharing to be a problem can fix it themselves.
I personally don't find arrays of locks very common. It's much more common to have arrays of data structures that _contain_ locks (eg things like having hash tables etc with a per-hashchain lock) and then those container structures may want to be cacheline aligned, but the locks themselves should not need to be.
Linus
Quoted 4 lines · Hubertus Franke
But what about compatibility with i368, no cmpxchg or cmpxchg8b Can't we have to types and infer from the op in the kernel what the correct size in user space is.
I think the next step should be to map in one page of kernel code in a user-readable location, and just do it there.
It's not just 386 vs later due to cmpxchg. It's also the simple issue of UP vs SMP - a UP system still wants to do locking, but it doesn't need the lock prefix. And that lock prefix makes a _huge_ difference performance-wise.
So my suggestion is: ignore i386 for now (no _relevant_ SMP boxes exist anyway), and plan on solving the problem with a separate library page before 2.6.x gets released.
Linus
Quoted 7 lines · Alan Cox
Quoted 4 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Can we go to cache line alignment - for an array of locks thats clearly advantageous
NO and let me explain.
I would to be able to integrate the lock with the data. This is much more cache friendly then putting the lock on a different cacheline.
If you want an array you need to pad each element. That's easy enough to do....
Can't shrink a datastructure on the other hand :-)
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 31 lines · george anzinger
Quoted 21 lines · Linus Torvalds
Quoted 1 line · Hubertus Franke
Could you also comment on the functionality that has been discussed.
First off, I have to say that I really like the current patch by Rusty. The hashing approach is very clean, and it all seems quite good. As to
specific points:
Quoted 3 lines
(I) the fairness issues that have been raised.
do you support two wakeup mechanism:FUTEX_UPandFUTEX_UP_FAIRor you don't care about fairness and starvationI don't think fairness and starvation is that big of a deal for semaphores, usually being unfair in these things tends to just improve performance through better cache locality with no real downside. That said, I think the option should be open (which it does seem to be).
For rwlocks, my personal preference is the fifo-fair-preference (unlike semaphore fairness, I have actually seen loads where read- vs write-preference really is unacceptable). This might be a point where we give users the choice.
Quoted 8 lines
I do think we should make the lock bigger - I worry that
atomic_tsimply won't be enough for things like fair rwlocks, which might want a "cmpxchg8b" on x86.So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Quoted 5 lines
I think this is needed if we want to address the "task dies while holding a lock" issue. In this case we need to know who holds the lock.
-g
Georg, while desirable its very tricky if possible at all.
You need to stick your pid or so into the lock and do it atomically. So let's assume we only stick with architectures that can do cmpxchg-doubleword, still its not fool proof.
First, the app could still corrupt that count or pid field of the lock In that case the whole logic get'ss crewed up.
There is no guarantee that you ever know who holds the locks !!!
Secondly, what guarantee do you have that your data is kosher ? I tend to agree with the masses, that hand waving might be the best first approximation
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 8 lines · Linus Torvalds
Quoted 3 lines · Hubertus Franke
But what about compatibility with i368, no cmpxchg or cmpxchg8b Can't we have to types and infer from the op in the kernel what the correct size in user space is.
I think the next step should be to map in one page of kernel code in a user-readable location, and just do it there.
Your kidding .....
Seriously, how can we guarantee that we correctly determine the lock holder, due to memory corruption problems. If we can't do it correctly all the times, why do it at all ?
Quoted 4 lines
It's not just 386 vs later due to cmpxchg. It's also the simple issue of UP vs SMP - a UP system still wants to do locking, but it doesn't need the lock prefix. And that lock prefix makes a _huge_ difference performance-wise.
Fail to see why that matters. User level locking is mostly beneficial on SMPs. So, you lock the bus for the atomic update. This is UP, nothing's going on on the bus anyway.
In your experience, what is the overhead in cycles for "incl" vs "lock;incl". Even if its a few more cycles, still beats the heck out of using other heavyweight kernel APIs
Quoted 4 lines
So my suggestion is: ignore i386 for now (no _relevant_ SMP boxes exist anyway), and plan on solving the problem with a separate library page before 2.6.x gets released.
Any rough design..
Quoted 1 line
Linus
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 8 lines
Quoted 4 lines
It's not just 386 vs later due to cmpxchg. It's also the simple issue of UP vs SMP - a UP system still wants to do locking, but it doesn't need the lock prefix. And that lock prefix makes a _huge_ difference performance-wise.
Fail to see why that matters. User level locking is mostly beneficial on SMPs. So, you lock the bus for the atomic update. This is UP, nothing's going on on the bus anyway.
Lots of older x86 is too stupid to optimise exclusive cache line locked operations. After all the bus is still shared - PCI bus masters for one
Alan
Quoted 5 lines
NO and let me explain.
I would to be able to integrate the lock with the data. This is much more cache friendly then putting the lock on a different cacheline.
Yep - I agree
Quoted 8 lines · Hubertus Franke
Quoted 3 lines
I think the next step should be to map in one page of kernel code in a user-readable location, and just do it there.
Your kidding .....
Seriously, how can we guarantee that we correctly determine the lock holder, due to memory corruption problems. If we can't do it correctly all the times, why do it at all ?
You don't understand. This has nothing to do with lock holders, or anything else.
I'm saying that we map in a page at a magic offset (just above the stack), and that page contains the locking code.
For 386 CPU's (where only UP matters), we can trivially come up with a lock that doesn't use cmpxchg8b and that isn't SMP-safe. It might even go into the kernel every time if it has to - ie it _works_, it just isn't optimal.
Quoted 1 line
Fail to see why that matters. User level locking is mostly beneficial on SMPs.
That's not the issue AT ALL.
Semaphores are absolutely required on UP too, with threads. There is _zero_ difference between UP and SMP from a locking perspective in user space due to the fact that we can be preempted at any time - except from the cache coherency issue.
Quoted 2 lines
So, you lock the bus for the atomic update. This is UP, nothing's going on on the bus anyway.
That's not the point. Nobody has locked the bus in the last ten years: the cache coherency is done on a cacheline basis, not on the bus.
The point being that the difference between a "decl" and a "lock ; decl" is about 1:12 or so in performance.
Quoted 2 lines
Even if its a few more cycles, still beats the heck out of using other heavyweight kernel APIs
Sure it does. But if the speed of locking matters enough for user-level locks to matter, don't you think the 1:12 difference matters as well?
Linus
Followup to: <[email protected]> By author: Linus Torvalds <[email protected]> In newsgroup: linux.dev.kernel
Quoted 22 lines · Alan Cox
Quoted 3 lines
Can we go to cache line alignment - for an array of locks thats clearly advantageous
I disagree about the "clearly". Firstly, the cacheline alignment is CPU dependent, so on some CPU's it's 32 bytes (or even 16), on others it is 128 bytes.
Secondly, a lot of locking is actually done inside a single thread, and false sharing doesn't happen much - so keeping the locks dense can be quite advantageous.
The cases where false sharing _does_ happen and are a problem should be for the application writer to worry about, not for the kernel to force.
So I think 8 bytes is plenty fine enough - with 16 bytes a remote possibility (I don't think it is needed, but it gives you som epadding for future expansion). And people who have arrays and find false sharing to be a problem can fix it themselves.
Quoted 6 lines
I personally don't find arrays of locks very common. It's much more common to have arrays of data structures that _contain_ locks (eg things like having hash tables etc with a per-hashchain lock) and then those container structures may want to be cacheline aligned, but the locks themselves should not need to be.
Also, on UP this is all a waste.
-hpa
Signature · 3 lines
<[email protected]> at work, <[email protected]> in private! "Unix gives you enough rope to shoot yourself in the foot." http://www.zytor.com/~hpa/puzzle.txt <[email protected]>
Followup to: <[email protected]> By author: Hubertus Franke <[email protected]> In newsgroup: linux.dev.kernel
Quoted 10 lines
Quoted 3 lines
Can we go to cache line alignment - for an array of locks thats clearly advantageous
NO and let me explain.
I would to be able to integrate the lock with the data. This is much more cache friendly then putting the lock on a different cacheline.
Not just cache, but programmer-friendly as well. Data structures containing locks (sometimes multiple and related) are really the common case.
-hpa
Signature · 3 lines
<[email protected]> at work, <[email protected]> in private! "Unix gives you enough rope to shoot yourself in the foot." http://www.zytor.com/~hpa/puzzle.txt <[email protected]>
Quoted 36 lines · Hubertus Franke
Quoted 34 lines · george anzinger
Quoted 21 lines · Linus Torvalds
Quoted 1 line · Hubertus Franke
Could you also comment on the functionality that has been discussed.
First off, I have to say that I really like the current patch by Rusty. The hashing approach is very clean, and it all seems quite good. As to
specific points:
Quoted 3 lines
(I) the fairness issues that have been raised.
do you support two wakeup mechanism:FUTEX_UPandFUTEX_UP_FAIRor you don't care about fairness and starvationI don't think fairness and starvation is that big of a deal for semaphores, usually being unfair in these things tends to just improve performance through better cache locality with no real downside. That said, I think the option should be open (which it does seem to be).
For rwlocks, my personal preference is the fifo-fair-preference (unlike semaphore fairness, I have actually seen loads where read- vs write-preference really is unacceptable). This might be a point where we give users the choice.
Quoted 8 lines
I do think we should make the lock bigger - I worry that
atomic_tsimply won't be enough for things like fair rwlocks, which might want a "cmpxchg8b" on x86.So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
I think this is needed if we want to address the "task dies while holding a lock" issue. In this case we need to know who holds the lock.
Quoted 8 lines
Quoted 2 lines
-g
George, while desirable its very tricky if possible at all.
You need to stick your pid or so into the lock and do it atomically. So let's assume we only stick with architectures that can do cmpxchg-doubleword, still its not fool proof.
Uh, just the pid would do. Maybe reserve a bit to indicate contention, but surly one word would be enough.
Quoted 3 lines
First, the app could still corrupt that count or pid field of the lock In that case the whole logic get'ss crewed up.
There is no guarantee that you ever know who holds the locks !!!
At that point you are most likely down for the count. The most use for this would be development where programs are dying like flies. If the sem area is "registered" with the kernel, it could do the right thing® in the exit code. Except for using the cmpxchg I don't think it adds to the overhead.
Quoted 2 lines
Secondly, what guarantee do you have that your data is kosher ?
Well, none, but you don't in either case. This is a non-argument.
Quoted 2 lines
I tend to agree with the masses, that hand waving might be the best first approximation
That is your privilege :-)
Quoted 3 lines
--
-- Hubertus Franke ([email protected])
Signature · 3 lines
George [email protected] High-res-timers: http://sourceforge.net/projects/high-res-timers/ Real time sched: http://sourceforge.net/projects/rtsched/
Quoted 4 lines · Linus Torvalds
Quoted 3 lines · Hubertus Franke
Quoted 2 lines
I think the next step should be to map in one page of kernel code in a user-readable location, and just do it there.
Quoted 8 lines
Quoted 4 lines
Your kidding .....
Seriously, how can we guarantee that we correctly determine the lock holder, due to memory corruption problems. If we can't do it correctly all the times, why do it at all ?You don't understand. This has nothing to do with lock holders, or anything else.
Sorry misunderstood your answer with respect to another question.
Quoted 8 lines
I'm saying that we map in a page at a magic offset (just above the stack), and that page contains the locking code.
For 386 CPU's (where only UP matters), we can trivially come up with a lock that doesn't use cmpxchg8b and that isn't SMP-safe. It might even go into the kernel every time if it has to - ie it _works_, it just isn't optimal.
Ahhhh, in this context, now "I see the light" (actually its dark at the east coast).
So you envision this to go through some named "section" or do you want to go through the futex_region() library call which identifies whether the code page has been mapped. If not, the kernel then will provide the locking code in that page dependent on the architecture (UP or SMP). Fair enough.
Quoted 10 lines
Quoted 2 lines
Fail to see why that matters. User level locking is mostly beneficial on SMPs.
That's not the issue AT ALL.
Semaphores are absolutely required on UP too, with threads. There is _zero_ difference between UP and SMP from a locking perspective in user space due to the fact that we can be preempted at any time - except from the cache coherency issue.
Agreed, my point was wrt providing the functionality only. Only difference between UP and SMP would be that a spinning version would default to the standard version (no spinning) under UP.
Quoted 9 lines
Quoted 2 lines
So, you lock the bus for the atomic update. This is UP, nothing's going on on the bus anyway.
That's not the point. Nobody has locked the bus in the last ten years: the cache coherency is done on a cacheline basis, not on the bus.
The point being that the difference between a "decl" and a "lock ; decl" is about 1:12 or so in performance.
I am no expert in architecture, but if its done through the cache coherency mechanism, the overhead shouldn't be 12:1. You simply mark the cache line as part of you instruction to avoid a cache line transfer. How can that be 12 times slower. .. Ready to be educated....
Quoted 7 lines
Quoted 2 lines
Even if its a few more cycles, still beats the heck out of using other heavyweight kernel APIs
Sure it does. But if the speed of locking matters enough for user-level locks to matter, don't you think the 1:12 difference matters as well?
Linus
Yipp, I buy that argument.
Overall, it just seems to me the user locking subsystem is becoming quickly again a complicated beast.
Anyway, time to go home and play with the kids :-)
Signature · 1 line
-- Hubertus Franke ([email protected])
Followup to: <[email protected]> By author: Linus Torvalds <[email protected]> In newsgroup: linux.dev.kernel
Quoted 4 lines
You don't understand. This has nothing to do with lock holders, or anything else.
Quoted 8 lines
I'm saying that we map in a page at a magic offset (just above the stack), and that page contains the locking code.
For 386 CPU's (where only UP matters), we can trivially come up with a lock that doesn't use cmpxchg8b and that isn't SMP-safe. It might even go into the kernel every time if it has to - ie it _works_, it just isn't optimal.
Okay, I'll say it and be impopular...
Perhaps it's time to drop i386 support?
It seems to me that the i386 support has been around mostly on a "until we have a reason to do otherwise" basis, but perhaps this is the reason?
There certainly are enough little, nagging reasons... CMPXCHG, BSWAP, and especially WP...
-hpa
Signature · 3 lines
<[email protected]> at work, <[email protected]> in private! "Unix gives you enough rope to shoot yourself in the foot." http://www.zytor.com/~hpa/puzzle.txt <[email protected]>
Quoted 4 lines
Quoted 1 line
cmpxchg-doubleword, still its not fool proof.
Uh, just the pid would do. Maybe reserve a bit to indicate contention, but surly one word would be enough.
Make it a dword, the 16bit pid is beginning to look strained on big boxes
Quoted 6 lines
It seems to me that the i386 support has been around mostly on a "until we have a reason to do otherwise" basis, but perhaps this is the reason?
There certainly are enough little, nagging reasons... CMPXCHG, BSWAP, and especially WP...
They don't really arise in most normal situations. Bswap is trivial to the extreme. Cmpxchg only comes up on SMP boxes. Right now the one big hit is cmpxchg8 if you use direct rendering. And quite frankly if you use the direct rendering infrastructure on a 386 its going to be a teeny bit slow anyway 8)
So if anything its just not worth the effort of breaking the 386 setup either 8). 386 SMP is a different issue but I don't see any lunatics doing a 386 based sequent port thankfully.
Alan
Quoted 3 lines · george anzinger
Uh, just the pid would do. Maybe reserve a bit to indicate contention, but surly one word would be enough.
Not really.
The pid would mean that anybody who gets a lock would have to have its pid available (remember the fast-path is what we really care about), but there's also a fundamental race between getting the lock and writing the pid to the second word of the lock that you just won't avoid.
And that's assuming you only use the semaphores for pure mutual exclusion. That is the normal behaviour, but some people use semaphores for other things (ie "N people can be active inside this region" where N != 1).
And then you have to realize that doing the same for readers in a rwlock is even worse.
In short, it just cannot be done quickly and simply, and for many cases it cannot be done at all.
Linus
Quoted 8 lines · Hubertus Franke
Quoted 3 lines
The point being that the difference between a "decl" and a "lock ; decl" is about 1:12 or so in performance.
I am no expert in architecture, but if its done through the cache coherency mechanism, the overhead shouldn't be 12:1. You simply mark the cache line as part of you instruction to avoid a cache line transfer. How can that be 12 times slower. .. Ready to be educated....
A lock in a SMP system also needs to synchronize the instruction stream, and not let stores move "out" from the locked region.
On a UP system, this all happens automatically (well, getting it to happen right is obviously one of the big issues in an out-of-order CPU core, but it's a very fundamental part of the core, so it's "free" in the sense that if it isn't done, the CPU simply doesn't work).
On SMP, it's a memory barrier. This is why a "lock ; decl" is more expensive than a "decl" - it's the implied memory ordering constraints (on other architectures they are explicit). On an intel CPU, this basically means that the pipeline is drained, so a locked instruction takes roughly 12 cycles on a PPro core (AMD's K7 core seems to be rather more graceful about this one). I haven't timed a P4 lately, I think it's worse.
Other architectures do the memory ordering explicitly, and some are better, some are worse. But it always costs you _something_.
Linus
Quoted 3 lines · On Fri, 8 Mar 2002 11:22:20 -0800 (PST) Linus Torvalds…
First off, I have to say that I really like the current patch by Rusty. The hashing approach is very clean, and it all seems quite good. As to specific points:
Thanks. [Aside: VIRO PLEASE TAKE NOTE.]
Quoted 8 lines
Quoted 3 lines
(I) the fairness issues that have been raised.
do you support two wakeup mechanism:FUTEX_UPandFUTEX_UP_FAIRor you don't care about fairness and starvationI don't think fairness and starvation is that big of a deal for semaphores, usually being unfair in these things tends to just improve performance through better cache locality with no real downside. That said, I think the option should be open (which it does seem to be).
1) Unfairness definitely helps performance (~30% faster on tdbtorture and IIRC on Hubertus' benchmark too).
2) Absolute fairness depends on hardware anyway.
3) I was not able to produce any evidence of STARVATION (which I think we all agree *is* an issue).
So I'd say stick with the minimalistic, fast, unfair solution.
Quoted 4 lines
For rwlocks, my personal preference is the fifo-fair-preference (unlike semaphore fairness, I have actually seen loads where read- vs write-preference really is unacceptable). This might be a point where we give users the choice.
Yes. See post on "furwocks": fair-preference rw locks implemented in userspace on top of the futexes.
Quoted 8 lines
I do think we should make the lock bigger - I worry that
atomic_tsimply won't be enough for things like fair rwlocks, which might want a "cmpxchg8b" on x86.So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Actually, I disagree.
1) We've left wiggle room in the second arg to sys_futex() to add rwsems later if required.
2) Someone needs to implement them and prove they are superior to the pure userspace solution.
The most gain will be from a very briefly held lock that is 99.99% read. But if it's 10%, it's not worth it: we need numbers.
Rusty.
Signature · 1 line
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 3 lines · On Fri, 8 Mar 2002 10:07:51 -0800 (PST) Linus Torvalds…
This doesn't work on highmem machines - doing the conversion from "<struct page, offset>" to "page_address(page)+offset" is simply not legal (not even for pure hashing purposes -
page_address()changes as you kmap it).
Thanks for the catch. Am not blessed (cursed?) with highmem here.
Quoted 4 lines
You need to keep the <struct page,offset> tuple in that format, and no other. And when you actually touch the page, you need to do the kmap()/kunmap() (and you must not keep it mapped while you sleep, because that might trivially make the kernel run out of virtual mappings).
Ick: not allowing for one virtual mapping per process is pretty horrible. Still, pretty easy to fix.
Also updated syscall numbers for 2.5.6, and changed FUTEX_UP/DOWN definitions to be more logical for future expansions (eg. r/w).
Rusty.
Signature · 483 lines
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/arch/i386/kernel/entry.S working-2.5.6-futex-6/arch/i386/kernel/entry.S
--- linux-2.5.6/arch/i386/kernel/entry.S Fri Mar 8 14:49:11 2002
+++ working-2.5.6-futex-6/arch/i386/kernel/entry.S Sat Mar 9 14:09:10 2002
@@ -717,6 +717,7 @@
.long SYMBOL_NAME(sys_fremovexattr)
.long SYMBOL_NAME(sys_tkill)
.long SYMBOL_NAME(sys_sendfile64)
+ .long SYMBOL_NAME(sys_futex) /* 240 */
.rept NR_syscalls-(.-sys_call_table)/4
.long SYMBOL_NAME(sys_ni_syscall)
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/arch/ppc/kernel/misc.S working-2.5.6-futex-6/arch/ppc/kernel/misc.S
--- linux-2.5.6/arch/ppc/kernel/misc.S Fri Mar 8 14:49:11 2002
+++ working-2.5.6-futex-6/arch/ppc/kernel/misc.S Sat Mar 9 14:08:24 2002
@@ -1289,6 +1289,7 @@
.long sys_removexattr
.long sys_lremovexattr
.long sys_fremovexattr /* 220 */
+ .long sys_futex
.rept NR_syscalls-(.-sys_call_table)/4
.long sys_ni_syscall
.endr
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/asm-i386/mman.h working-2.5.6-futex-6/include/asm-i386/mman.h
--- linux-2.5.6/include/asm-i386/mman.h Wed Mar 15 12:45:20 2000
+++ working-2.5.6-futex-6/include/asm-i386/mman.h Sat Mar 9 14:32:43 2002
@@ -4,6 +4,7 @@
#define PROT_READ 0x1 /* page can be read */
#define PROT_WRITE 0x2 /* page can be written */
#define PROT_EXEC 0x4 /* page can be executed */
+#define PROT_SEM 0x8 /* page may be used for atomic ops */
#define PROT_NONE 0x0 /* page can not be accessed */
#define MAP_SHARED 0x01 /* Share changes */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/asm-i386/unistd.h working-2.5.6-futex-6/include/asm-i386/unistd.h
--- linux-2.5.6/include/asm-i386/unistd.h Fri Mar 8 14:49:28 2002
+++ working-2.5.6-futex-6/include/asm-i386/unistd.h Sat Mar 9 14:09:31 2002
@@ -244,6 +244,7 @@
#define __NR_fremovexattr 237
#define __NR_tkill 238
#define __NR_sendfile64 239
+#define __NR_futex 240
/* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/asm-ppc/mman.h working-2.5.6-futex-6/include/asm-ppc/mman.h
--- linux-2.5.6/include/asm-ppc/mman.h Tue May 22 08:02:06 2001
+++ working-2.5.6-futex-6/include/asm-ppc/mman.h Sat Mar 9 14:32:39 2002
@@ -7,6 +7,7 @@
#define PROT_READ 0x1 /* page can be read */
#define PROT_WRITE 0x2 /* page can be written */
#define PROT_EXEC 0x4 /* page can be executed */
+#define PROT_SEM 0x8 /* page may be used for atomic ops */
#define PROT_NONE 0x0 /* page can not be accessed */
#define MAP_SHARED 0x01 /* Share changes */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/asm-ppc/unistd.h working-2.5.6-futex-6/include/asm-ppc/unistd.h
--- linux-2.5.6/include/asm-ppc/unistd.h Wed Feb 20 17:57:18 2002
+++ working-2.5.6-futex-6/include/asm-ppc/unistd.h Sat Mar 9 14:08:27 2002
@@ -228,6 +228,7 @@
#define __NR_removexattr 218
#define __NR_lremovexattr 219
#define __NR_fremovexattr 220
+#define __NR_futex 221
#define __NR(n) #n
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/linux/futex.h working-2.5.6-futex-6/include/linux/futex.h
--- linux-2.5.6/include/linux/futex.h Thu Jan 1 10:00:00 1970
+++ working-2.5.6-futex-6/include/linux/futex.h Sat Mar 9 14:08:24 2002
@@ -0,0 +1,8 @@
+#ifndef _LINUX_FUTEX_H
+#define _LINUX_FUTEX_H
+
+/* Second argument to futex syscall */
+#define FUTEX_UP (0)
+#define FUTEX_DOWN (1)
+
+#endif
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/linux/hash.h working-2.5.6-futex-6/include/linux/hash.h
--- linux-2.5.6/include/linux/hash.h Thu Jan 1 10:00:00 1970
+++ working-2.5.6-futex-6/include/linux/hash.h Sat Mar 9 14:08:27 2002
@@ -0,0 +1,58 @@
+#ifndef _LINUX_HASH_H
+#define _LINUX_HASH_H
+/* Fast hashing routine for a long.
+ (C) 2002 William Lee Irwin III, IBM */
+
+/*
+ * Knuth recommends primes in approximately golden ratio to the maximum
+ * integer representable by a machine word for multiplicative hashing.
+ * Chuck Lever verified the effectiveness of this technique:
+ * http://www.citi.umich.edu/techreports/reports/citi-tr-00-1.pdf
+ *
+ * These primes are chosen to be bit-sparse, that is operations on
+ * them can use shifts and additions instead of multiplications for
+ * machines where multiplications are slow.
+ */
+#if BITS_PER_LONG == 32
+/* 2^31 + 2^29 - 2^25 + 2^22 - 2^19 - 2^16 + 1 */
+#define GOLDEN_RATIO_PRIME 0x9e370001UL
+#elif BITS_PER_LONG == 64
+/* 2^63 + 2^61 - 2^57 + 2^54 - 2^51 - 2^18 + 1 */
+#define GOLDEN_RATIO_PRIME 0x9e37fffffffc0001UL
+#else
+#error Define GOLDEN_RATIO_PRIME for your wordsize.
+#endif
+
+static inline unsigned long hash_long(unsigned long val, unsigned int bits)
+{
+ unsigned long hash = val;
+
+#if BITS_PER_LONG == 64
+ /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
+ unsigned long n = hash;
+ n <<= 18;
+ hash -= n;
+ n <<= 33;
+ hash -= n;
+ n <<= 3;
+ hash += n;
+ n <<= 3;
+ hash -= n;
+ n <<= 4;
+ hash += n;
+ n <<= 2;
+ hash += n;
+#else
+ /* On some cpus multiply is faster, on others gcc will do shifts */
+ hash *= GOLDEN_RATIO_PRIME;
+#endif
+
+ /* High bits are more random, so use them. */
+ return hash >> (BITS_PER_LONG - bits);
+}
+
+static inline unsigned long hash_ptr(void *ptr, unsigned int bits)
+{
+ return hash_long((unsigned long)ptr, bits);
+}
+#endif /* _LINUX_HASH_H */
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/include/linux/mmzone.h working-2.5.6-futex-6/include/linux/mmzone.h
--- linux-2.5.6/include/linux/mmzone.h Fri Mar 1 22:58:34 2002
+++ working-2.5.6-futex-6/include/linux/mmzone.h Sat Mar 9 14:52:15 2002
@@ -51,8 +51,7 @@
/*
* wait_table -- the array holding the hash table
* wait_table_size -- the size of the hash table array
- * wait_table_shift -- wait_table_size
- * == BITS_PER_LONG (1 << wait_table_bits)
+ * wait_table_bits -- wait_table_size == (1 << wait_table_bits)
*
* The purpose of all these is to keep track of the people
* waiting for a page to become available and make them
@@ -75,7 +74,7 @@
*/
wait_queue_head_t * wait_table;
unsigned long wait_table_size;
- unsigned long wait_table_shift;
+ unsigned long wait_table_bits;
/*
* Discontig memory support fields.
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/kernel/Makefile working-2.5.6-futex-6/kernel/Makefile
--- linux-2.5.6/kernel/Makefile Wed Feb 20 17:56:17 2002
+++ working-2.5.6-futex-6/kernel/Makefile Sat Mar 9 14:08:27 2002
@@ -15,7 +15,7 @@
obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \
module.o exit.o itimer.o info.o time.o softirq.o resource.o \
sysctl.o acct.o capability.o ptrace.o timer.o user.o \
- signal.o sys.o kmod.o context.o
+ signal.o sys.o kmod.o context.o futex.o
obj-$(CONFIG_UID16) += uid16.o
obj-$(CONFIG_MODULES) += ksyms.o
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/kernel/futex.c working-2.5.6-futex-6/kernel/futex.c
--- linux-2.5.6/kernel/futex.c Thu Jan 1 10:00:00 1970
+++ working-2.5.6-futex-6/kernel/futex.c Sat Mar 9 15:03:13 2002
@@ -0,0 +1,232 @@
+/*
+ * Fast Userspace Mutexes (which I call "Futexes!").
+ * (C) Rusty Russell, IBM 2002
+ *
+ * Thanks to Ben LaHaise for yelling "hashed waitqueues" loudly
+ * enough at me, Linus for the original (flawed) idea, Matthew
+ * Kirkwood for proof-of-concept implementation.
+ *
+ * "The futexes are also cursed."
+ * "But they come in a choice of three flavours!"
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+ */
+#include <linux/kernel.h>
+#include <linux/spinlock.h>
+#include <linux/sched.h>
+#include <linux/mm.h>
+#include <linux/hash.h>
+#include <linux/init.h>
+#include <linux/fs.h>
+#include <linux/futex.h>
+#include <linux/highmem.h>
+#include <asm/atomic.h>
+
+/* These mutexes are a very simple counter: the winner is the one who
+ decrements from 1 to 0. The counter starts at 1 when the lock is
+ free. A value other than 0 or 1 means someone may be sleeping.
+ This is simple enough to work on all architectures, but has the
+ problem that if we never "up" the semaphore it could eventually
+ wrap around. */
+
+/* FIXME: This may be way too small. --RR */
+#define FUTEX_HASHBITS 6
+
+/* We use this instead of a normal wait_queue_t, so we can wake only
+ the relevent ones (hashed queues may be shared) */
+struct futex_q {
+ struct list_head list;
+ struct task_struct *task;
+ /* Page struct and offset within it. */
+ struct page *page;
+ unsigned int offset;
+};
+
+/* The key for the hash is the address + index + offset within page */
+static struct list_head futex_queues[1<<FUTEX_HASHBITS];
+static spinlock_t futex_lock = SPIN_LOCK_UNLOCKED;
+
+static inline struct list_head *hash_futex(struct page *page,
+ unsigned long offset)
+{
+ unsigned long h;
+
+ /* struct page is shared, so we can hash on its address */
+ h = (unsigned long)page + offset;
+ return &futex_queues[hash_long(h, FUTEX_HASHBITS)];
+}
+
+static inline void wake_one_waiter(struct list_head *head,
+ struct page *page,
+ unsigned int offset)
+{
+ struct list_head *i;
+
+ spin_lock(&futex_lock);
+ list_for_each(i, head) {
+ struct futex_q *this = list_entry(i, struct futex_q, list);
+
+ if (this->page == page && this->offset == offset) {
+ wake_up_process(this->task);
+ break;
+ }
+ }
+ spin_unlock(&futex_lock);
+}
+
+/* Add at end to avoid starvation */
+static inline void queue_me(struct list_head *head,
+ struct futex_q *q,
+ struct page *page,
+ unsigned int offset)
+{
+ q->task = current;
+ q->page = page;
+ q->offset = offset;
+
+ spin_lock(&futex_lock);
+ list_add_tail(&q->list, head);
+ spin_unlock(&futex_lock);
+}
+
+static inline void unqueue_me(struct futex_q *q)
+{
+ spin_lock(&futex_lock);
+ list_del(&q->list);
+ spin_unlock(&futex_lock);
+}
+
+/* Get kernel address of the user page and pin it. */
+static struct page *pin_page(unsigned long page_start)
+{
+ struct mm_struct *mm = current->mm;
+ struct page *page;
+ int err;
+
+ down_read(&mm->mmap_sem);
+ err = get_user_pages(current, current->mm, page_start,
+ 1 /* one page */,
+ 1 /* writable */,
+ 0 /* don't force */,
+ &page,
+ NULL /* don't return vmas */);
+ up_read(&mm->mmap_sem);
+
+ if (err < 0)
+ return ERR_PTR(err);
+ return page;
+}
+
+/* Try to decrement the user count to zero. */
+static int decrement_to_zero(struct page *page, unsigned int offset)
+{
+ atomic_t *count;
+ int ret = 0;
+
+ count = kmap(page) + offset;
+ /* If we take the semaphore from 1 to 0, it's ours. If it's
+ zero, decrement anyway, to indicate we are waiting. If
+ it's negative, don't decrement so we don't wrap... */
+ if (atomic_read(count) >= 0 && atomic_dec_and_test(count))
+ ret = 1;
+ kunmap(page);
+ return ret;
+}
+
+/* Simplified from arch/ppc/kernel/semaphore.c: Paul M. is a genius. */
+static int futex_down(struct list_head *head, struct page *page, int offset)
+{
+ int retval = 0;
+ struct futex_q q;
+
+ current->state = TASK_INTERRUPTIBLE;
+ queue_me(head, &q, page, offset);
+
+ while (!decrement_to_zero(page, offset)) {
+ if (signal_pending(current)) {
+ retval = -EINTR;
+ break;
+ }
+ schedule();
+ current->state = TASK_INTERRUPTIBLE;
+ }
+ current->state = TASK_RUNNING;
+ unqueue_me(&q);
+ /* If we were signalled, we might have just been woken: we
+ must wake another one. Otherwise we need to wake someone
+ else (if they are waiting) so they drop the count below 0,
+ and when we "up" in userspace, we know there is a
+ waiter. */
+ wake_one_waiter(head, page, offset);
+ return retval;
+}
+
+static int futex_up(struct list_head *head, struct page *page, int offset)
+{
+ atomic_t *count;
+
+ count = kmap(page) + offset;
+ atomic_set(count, 1);
+ smp_wmb();
+ kunmap(page);
+ wake_one_waiter(head, page, offset);
+ return 0;
+}
+
+asmlinkage int sys_futex(void *uaddr, int op)
+{
+ int ret;
+ unsigned long pos_in_page;
+ struct list_head *head;
+ struct page *page;
+
+ pos_in_page = ((unsigned long)uaddr) % PAGE_SIZE;
+
+ /* Must be "naturally" aligned, and not on page boundary. */
+ if ((pos_in_page % __alignof__(atomic_t)) != 0
+ || pos_in_page + sizeof(atomic_t) > PAGE_SIZE)
+ return -EINVAL;
+
+ /* Simpler if it doesn't vanish underneath us. */
+ page = pin_page((unsigned long)uaddr - pos_in_page);
+ if (IS_ERR(page))
+ return PTR_ERR(page);
+
+ head = hash_futex(page, pos_in_page);
+ switch (op) {
+ case FUTEX_UP:
+ ret = futex_up(head, page, pos_in_page);
+ break;
+ case FUTEX_DOWN:
+ ret = futex_down(head, page, pos_in_page);
+ break;
+ /* Add other lock types here... */
+ default:
+ ret = -EINVAL;
+ }
+ put_page(page);
+
+ return ret;
+}
+
+static int __init init(void)
+{
+ unsigned int i;
+
+ for (i = 0; i < ARRAY_SIZE(futex_queues); i++)
+ INIT_LIST_HEAD(&futex_queues[i]);
+ return 0;
+}
+__initcall(init);
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/mm/filemap.c working-2.5.6-futex-6/mm/filemap.c
--- linux-2.5.6/mm/filemap.c Fri Mar 8 14:49:30 2002
+++ working-2.5.6-futex-6/mm/filemap.c Sat Mar 9 14:08:27 2002
@@ -25,6 +25,7 @@
#include <linux/iobuf.h>
#include <linux/compiler.h>
#include <linux/fs.h>
+#include <linux/hash.h>
#include <asm/pgalloc.h>
#include <asm/uaccess.h>
@@ -773,32 +774,8 @@
static inline wait_queue_head_t *page_waitqueue(struct page *page)
{
const zone_t *zone = page_zone(page);
- wait_queue_head_t *wait = zone->wait_table;
- unsigned long hash = (unsigned long)page;
-#if BITS_PER_LONG == 64
- /* Sigh, gcc can't optimise this alone like it does for 32 bits. */
- unsigned long n = hash;
- n <<= 18;
- hash -= n;
- n <<= 33;
- hash -= n;
- n <<= 3;
- hash += n;
- n <<= 3;
- hash -= n;
- n <<= 4;
- hash += n;
- n <<= 2;
- hash += n;
-#else
- /* On some cpus multiply is faster, on others gcc will do shifts */
- hash *= GOLDEN_RATIO_PRIME;
-#endif
-
- hash >>= zone->wait_table_shift;
-
- return &wait[hash];
+ return &zone->wait_table[hash_ptr(page, zone->wait_table_bits)];
}
/*
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/mm/mprotect.c working-2.5.6-futex-6/mm/mprotect.c
--- linux-2.5.6/mm/mprotect.c Wed Feb 20 17:57:21 2002
+++ working-2.5.6-futex-6/mm/mprotect.c Sat Mar 9 14:32:32 2002
@@ -280,7 +280,7 @@
end = start + len;
if (end < start)
return -EINVAL;
- if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC))
+ if (prot & ~(PROT_READ | PROT_WRITE | PROT_EXEC | PROT_SEM))
return -EINVAL;
if (end == start)
return 0;
diff -urN -I \$.*\$ --exclude TAGS -X /home/rusty/devel/kernel/kernel-patches/current-dontdiff --minimal linux-2.5.6/mm/page_alloc.c working-2.5.6-futex-6/mm/page_alloc.c
--- linux-2.5.6/mm/page_alloc.c Fri Mar 8 14:49:30 2002
+++ working-2.5.6-futex-6/mm/page_alloc.c Sat Mar 9 14:08:27 2002
@@ -776,8 +776,8 @@
* per zone.
*/
zone->wait_table_size = wait_table_size(size);
- zone->wait_table_shift =
- BITS_PER_LONG - wait_table_bits(zone->wait_table_size);
+ zone->wait_table_bits =
+ wait_table_bits(zone->wait_table_size);
zone->wait_table = (wait_queue_head_t *)
alloc_bootmem_node(pgdat, zone->wait_table_size
* sizeof(wait_queue_head_t));In article <[email protected]>,
Quoted 4 lines · Alan Cox
So if anything its just not worth the effort of breaking the 386 setup either 8). 386 SMP is a different issue but I don't see any lunatics doing a 386 based sequent port thankfully.
Since the only person that comes to mind that would be crazy enough to even _try_ to use Linux on 386-SMP is you, Alan, I'm really relieved to hear you say that ;)
And no, it's not worth discontinuing i386 support. It just isn't painful enough to maintain.
Note that the i386 has _long_ been a "stepchild", though: because of the lack of WP, the kernel simply doesn't do threaded MM correctly on a 386. Never has, and never will.
However, the known "incorrect" case is so obscure that it's not even an issue - although I suspect that it means that you should not let untrusted users run on a i386 server machine that contains any sensitive data. I could cerrtainly come up with exploits that would work at least in theory (whether they are workable in practice I don't know).
Using i386's for network servers is fine, of course. Just don't use them for cpu farms (not that I think anybody is - it takes quite a big farm of i386 machines to equal even _one_ PII ;)
Linus
Quoted 3 lines
So if anything its just not worth the effort of breaking the 386 setup either 8). 386 SMP is a different issue but I don't see any lunatics doing a 386 based sequent port thankfully.
Hey, don't count it out ... someone was emailing me a week or two ago, asking what the internal structure of a Sequent Symmetry was, so that they could get Linux running on it. OK, so they gave up when I gave them an outline of what was in there, but ... ;-)
M.
PS. No I'm not suggesting we should support 386 SMP ;-)
Quoted 3 lines
two ago, asking what the internal structure of a Sequent Symmetry was, so that they could get Linux running on it. OK, so they gave up when I gave them an outline of what was in there, but ... ;-)
I feel safe. A long time ago I looked at a symettry but 3 phase power was harder to arrange 8)
Quoted 6 lines
Quoted 3 lines
two ago, asking what the internal structure of a Sequent Symmetry was, so that they could get Linux running on it. OK, so they gave up when I gave them an outline of what was in there, but ... ;-)
I feel safe. A long time ago I looked at a symettry but 3 phase power was harder to arrange 8)
IIRC the half height cabinets run of single phase, 240V. I'm sure I can arrange to have a 386 half height system shipped to you ;-) ;-)
M.
PS. Have fun with that VME bus ...
Quoted 5 lines
IIRC the half height cabinets run of single phase, 240V. I'm sure I can arrange to have a 386 half height system shipped to you ;-) ;-)
PS. Have fun with that VME bus ...
Actually we have VME code for Linux, including a PCI/VME bridge. Its not in the base distro as the author never needed to tidy it up to make it work for arbitary PCI/VME bridges.
I think you should ship the machine to hpa though, then he can run kernel.org on it 8)
Quoted 24 lines · Linus Torvalds
Quoted 7 lines · Hubertus Franke
Quoted 2 lines
The point being that the difference between a "decl" and a "lock ; decl" is about 1:12 or so in performance.
I am no expert in architecture, but if its done through the cache coherency mechanism, the overhead shouldn't be 12:1. You simply mark the cache line as part of you instruction to avoid a cache line transfer. How can that be 12 times slower. .. Ready to be educated....
A lock in a SMP system also needs to synchronize the instruction stream, and not let stores move "out" from the locked region.
On a UP system, this all happens automatically (well, getting it to happen right is obviously one of the big issues in an out-of-order CPU core, but it's a very fundamental part of the core, so it's "free" in the sense that if it isn't done, the CPU simply doesn't work).
On SMP, it's a memory barrier. This is why a "lock ; decl" is more expensive than a "decl" - it's the implied memory ordering constraints (on other architectures they are explicit). On an intel CPU, this basically means that the pipeline is drained, so a locked instruction takes roughly 12 cycles on a PPro core (AMD's K7 core seems to be rather more graceful about this one). I haven't timed a P4 lately, I think it's worse.
Quoted 4 lines
Other architectures do the memory ordering explicitly, and some are better, some are worse. But it always costs you _something_.
Linus
Sure, not contending that. Right now I think our focus should be to get the right functionality out and address people's concerns. Improvements, as you suggested, are orthogonal and can always be put in later.
Signature · 1 line
-- Hubertus Franke ([email protected])
Linus Torvalds
AppliedQuoted 6 lines · Rusty Russell
Quoted 5 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Quoted 6 lines
Actually, I disagree.
1) We've left wiggle room in the second arg to
sys_futex()to add rwsems later if required.
2) Someone needs to implement them and prove they are superior to the pure userspace solution.
You've convinced me.
Considering how long people argued about dubious cycle measurements on the rwsem implementation, and where the crrent one actually uses a spinlock for exclusion on the fast path, the kernel lock really probably doesn't need to be expanded, and as there is provable overhead to the expansion, I'll just agree with you.
Applied.
Linus
Quoted 21 lines · Linus Torvalds
Quoted 11 lines · Rusty Russell
Quoted 4 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the code sucks right now.
Actually, I disagree.
1) We've left wiggle room in the second arg to
sys_futex()to add rwsems later if required.
2) Someone needs to implement them and prove they are superior to the pure userspace solution.You've convinced me.
Considering how long people argued about dubious cycle measurements on the rwsem implementation, and where the crrent one actually uses a spinlock for exclusion on the fast path, the kernel lock really probably doesn't need to be expanded, and as there is provable overhead to the expansion, I'll just agree with you.
Quoted 3 lines
Applied.
Linus
Great, now, that this is settled, let's go back to the compare and swap issues.
As far as I can tell, we need compare and swap on a single queue implementation for rwsems. Again most architectures provide something like that today. As for those which don't, why not provide either of the following approaches
(a) spinlock around the update code in the kernel. We could provide multiple spinlocks to avoid potential collision.
(b) only provide futex in the kernel and user library approach using 2 queues as shown by Rusty. The lack of cmpxchg support would be exported by the futex_region call.
-
-- Hubertus Franke ([email protected])
Quoted 5 lines · H. Peter Anvin
Okay, I'll say it and be impopular...
Perhaps it's time to drop i386 support?
Wouldn't it be better to just separate it out? I.e. make i386 an arch of its own, while most pc people use a "486 and up" arch?
The few who actually want 386 code won't loose it, and other developers won't have to bother with 386 issues. Then drop the 386 arch when it dies from lack of maintenance...
Helge Hafting
In message <[email protected]> you
write:
Quoted 6 lines · Rusty Russell
Quoted 4 lines
Quoted 4 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the cod
e
Quoted 10 lines
Quoted 8 lines
Quoted 1 line
sucks right now.
Actually, I disagree.
1) We've left wiggle room in the second arg to
sys_futex()to add rwsems later if required.
2) Someone needs to implement them and prove they are superior to the pure userspace solution.You've convinced me.
Damn. Because now I've been playing with a different approach.
If we basically export "add_to_waitqueue", "del_from_waitqueue", "wait_for_waitqueue" and "wakeup_waitqueue" syscalls, we have a more powerful interface: the kernel need not touch userspace addresses at all (no kmap/kunmap, no worried about spinlocks vs. rwlocks).
The problem is that this fundamentally requires at least two syscalls in the slow path (add_to_waitqueue, try for lock, wait_for_waitqueue). My tests here show it's about 6% slower than the solution you accepted for tdbtorture (which means the slow path is significantly slower). I can't imagine shaving that much more off it.
There are variations on this: cookie could be replaces the page struct and the offset, ala futexes.
Thoughts?
Rusty.
PS. Kudos: it was Ben LaHaise's idea to export waitqueues, but I
didn't see how to do it until Paul M made a bad joke about two syscalls....
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
arch/i386/kernel/entry.S +3 -0
@@ -717,6 +717,9 @@ .long SYMBOL_NAME(sys_fremovexattr) .long SYMBOL_NAME(sys_tkill) .long SYMBOL_NAME(sys_sendfile64) + .long SYMBOL_NAME(sys_uwaitq_add) /* 240 */ + .long SYMBOL_NAME(sys_uwaitq_wait) + .long SYMBOL_NAME(sys_uwaitq_wake) .rept NR_syscalls-(.-sys_call_table)/4 .long SYMBOL_NAME(sys_ni_syscall)
include/asm-i386/mman.h +1 -0
@@ -5,6 +5,7 @@#define PROT_WRITE 0x2/* page can be written */ #define PROT_EXEC 0x4/* page can be executed */ #define PROT_NONE 0x0/* page can not be accessed */ +#define PROT_SEM 0x8/* page can contain semaphores */ #define MAP_SHARED 0x01/* Share changes */ #define MAP_PRIVATE 0x02/* Changes are private */
include/asm-i386/unistd.h +3 -0
@@ -244,6 +244,9 @@#define __NR_fremovexattr 237#define __NR_tkill 238#define __NR_sendfile64 239 +#define __NR_uwaitq_add 240 +#define __NR_uwaitq_wait 241 +#define __NR_uwaitq_wake 242/* user-visible error numbers are in the range -1 - -124: see <asm-i386/errno.h> */
include/linux/sched.h +14 -0
@@ -230,6 +230,11 @@typedef struct prio_array prio_array_t ; +struct uwaitq { +struct list_head list; +unsigned long /*long*/ cookie; +}; +struct task_struct {volatile long state;/* -1 unrunnable, 0 runnable, >0 stopped */ struct thread_info *thread_info;
@@ -344,6 +349,8 @@/* journalling filesystem info */ void *journal_info; +/* user wait queue info */ +struct uwaitq uwaitq; };extern void __put_task_struct (struct task_struct *tsk);
@@ -508,6 +515,13 @@extern int kill_proc (pid_t ,int ,int );extern int do_sigaction (int ,const struct k_sigaction *,struct k_sigaction *);extern int do_sigaltstack (const stack_t *,stack_t *,unsigned long ); + +extern void __uwaitq_unqueue (struct uwaitq *q); +static inline void uwaitq_unqueue (struct uwaitq *q) +{ +if (q->cookie) +__uwaitq_unqueue (q); +}/* * Re-calculate pending state from the set of locally pending
kernel/Makefile +1 -1
@@ -15,7 +15,7 @@ obj-y = sched.o dma.o fork.o exec_domain.o panic.o printk.o \ module.o exit.o itimer.o info.o time.o softirq.o resource.o \ sysctl.o acct.o capability.o ptrace.o timer.o user.o \ - signal.o sys.o kmod.o context.o + signal.o sys.o kmod.o context.o uwaitq.o obj-$(CONFIG_UID16) += uid16.o obj-$(CONFIG_MODULES) += ksyms.o
kernel/exit.c +1 -1
@@ -502,7 +502,7 @@acct_process (code);#endif __exit_mm (tsk); - +uwaitq_unqueue (&tsk->uwaitq);lock_kernel ();sem_exit ();__exit_files (tsk);
kernel/fork.c +2 -0
@@ -720,6 +720,8 @@if (retval) goto bad_fork_cleanup_namespace; p->semundo =NULL ; +INIT_LIST_HEAD (&p->uwaitq.list); + p->uwaitq.cookie =0 ;/* Our parent execution domain becomes current domain These must match for thread signalling to apply */
kernel/uwaitq.c +153 -0
153 lines not on this page · raw message
Hi!
Quoted 15 lines
Quoted 3 lines
So if anything its just not worth the effort of breaking the 386 setup either 8). 386 SMP is a different issue but I don't see any lunatics doing a 386 based sequent port thankfully.
Since the only person that comes to mind that would be crazy enough to even _try_ to use Linux on 386-SMP is you, Alan, I'm really relieved to hear you say that ;)
And no, it's not worth discontinuing i386 support. It just isn't painful enough to maintain.
Note that the i386 has _long_ been a "stepchild", though: because of the lack of WP, the kernel simply doesn't do threaded MM correctly on a 386. Never has, and never will.
Quoted 5 lines
However, the known "incorrect" case is so obscure that it's not even an issue - although I suspect that it means that you should not let untrusted users run on a i386 server machine that contains any sensitive data. I could cerrtainly come up with exploits that would work at least in theory (whether they are workable in practice I don't know).
That should mean at least warning during bootup. Checking for WP bit... message does not suggest that WP not present means security hole.
arch/i386/mm/init.c renamed from /arch/i386/mm/init.c +1 -1
@@ -383,7 +383,7 @@local_flush_tlb ();if (!boot_cpu_data.wp_works_ok) { -printk ("No.\n" ); +printk ("No (that's security hole).\n" );#ifdef CONFIG_X86_WP_WORKS_OK panic ("This kernel doesn't support CPU's with broken WP. Recompile it for a 386!" );#endif
Pavel
Signature · 2 lines
(about SSSCA) "I don't say this lightly. However, I really think that the U.S. no longer is classifiable as a democracy, but rather as a plutocracy." --hpa
Quoted 39 lines · Rusty Russell
In message <[email protected]> you
write:
Quoted 5 lines · Rusty Russell
Quoted 4 lines
Quoted 4 lines
So I would suggest making the size (and thus alignment check) of locks at least 8 bytes (and preferably 16). That makes it slightly harder to put locks on the stack, but gcc does support stack alignment, even if the cod
e
Quoted 10 lines
Quoted 8 lines
Quoted 1 line
sucks right now.
Actually, I disagree.
1) We've left wiggle room in the second arg to
sys_futex()to add rwsems later if required.
2) Someone needs to implement them and prove they are superior to the pure userspace solution.You've convinced me.
Damn. Because now I've been playing with a different approach.
If we basically export "
add_to_waitqueue", "del_from_waitqueue", "wait_for_waitqueue" and "wakeup_waitqueue" syscalls, we have a more powerful interface: the kernel need not touch userspace addresses at all (no kmap/kunmap, no worried about spinlocks vs. rwlocks).The problem is that this fundamentally requires at least two syscalls in the slow path (
add_to_waitqueue, try for lock,wait_for_waitqueue). My tests here show it's about 6% slower than the solution you accepted for tdbtorture (which means the slow path is significantly slower). I can't imagine shaving that much more off it.There are variations on this: cookie could be replaces the page struct and the offset, ala futexes.
Quoted 6 lines
Thoughts?
Rusty.PS. Kudos: it was Ben LaHaise's idea to export waitqueues, but I
didn't see how to do it until Paul M made a bad joke about two syscalls....
Rusty, aren't you now going back to the design that I implemented after Ben's comments.
Quoted 1 line
From the get-go, I never touched the user address in the kernel, as I thought
it would require detailed knowledge of the user level locking strategy.
Could you explain, why you need add_to_waitqueue and wait_for_waitqueue as separate calls ? Is it for resolving a race conditions ?
One comment with respect to multiple wait queues and rwsems:
Again it will allow you to do reader-pref and/or writer-pref, but not something like FIFO, i.e. wake up a writer if first waiter or wake up all readers if first ..... and so on.
I don't know whether the latter is terrible important ...
Signature · 1 line
-- Hubertus Franke ([email protected])
Quoted 4 lines · Rusty Russell
Quoted 2 lines
You've convinced me.
Damn. Because now I've been playing with a different approach.
I don't think your current patch is very useful.
It's obviously slower, and while it is an interesting approach for not just lock generation but also for synchronization points, it doesn't seem to actually _buy_ you anything. And since the cookie isn't guaranteed to be unique, you can't actually use it as a synchronization point on its own, but must always still have some shared memory location as a confirmation for whatever the synchronization was.
Finally, waitqueue's (to me) always were about two big points:
- natural race condition avoidance through ordering:
current->state = sleeping; add_wait_queue(); if (test) schedule();
which you basically emulate with the "zero cookie" thing.
- ability to wait on multiple events concurrently
which you don't take any advantage of at all.
So you kind of missed the second big point of waitqueues, so the end result really isn't any more fundamentally powerful than the (faster) specialized semaphore system call as far as I can tell.
In short, I would argue that this approach, while interesting, doesn't actually _buy_ you anything.
Now, if you want to wake on any of N events, then a "add_wait_queue*N + wait" approach actually makes sense. But quite frankly, once you are there you should really instead do full events, and go away and work together with Ben on the aio stuff instead of this.
So: interesting approach, but in its current form pointless as far as I can see.
Linus
In message <[email protected]> you
write:
Quoted 8 lines · Rusty Russell
Quoted 4 lines
Quoted 2 lines
You've convinced me.
Damn. Because now I've been playing with a different approach.
I don't think your current patch is very useful.
I agree. But your "Applied" EMail rushed me into posting it.
Quoted 6 lines
It's obviously slower, and while it is an interesting approach for not just lock generation but also for synchronization points, it doesn't seem to actually _buy_ you anything. And since the cookie isn't guaranteed to be unique, you can't actually use it as a synchronization point on its own, but must always still have some shared memory location as a confirmation for whatever the synchronization was.
My original cookie was 128 bits. ie. unique.
Quoted 2 lines
So: interesting approach, but in its current form pointless as far as I can see.
Yeah, I'm not sure what it's useful for either. But the code is out there if someone gets inspired...
Thanks,
Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
In message <[email protected]> you write:
Quoted 11 lines
Quoted 4 lines
If we basically export "
add_to_waitqueue", "del_from_waitqueue", "wait_for_waitqueue" and "wakeup_waitqueue" syscalls, we have a more powerful interface: the kernel need not touch userspace addresses at all (no kmap/kunmap, no worried about spinlocks vs. rwlocks).Rusty, aren't you now going back to the design that I implemented after Ben's comments.
From the get-go, I never touched the user address in the kernel, as I thought it would require detailed knowledge of the user level locking strategy.
Yes, as with my initial patch (like you, I had a semaphore in the kernel). However, with two separate syscalls, you don't need to allocate anything in the kernel, and still know nothing about the userspace locking.
Quoted 2 lines
Could you explain, why you need
add_to_waitqueueandwait_for_waitqueueas separate calls ? Is it for resolving a race conditions ?
Yep. Exactly analogous to the kernel idiom (add to queue, check, sleep). (Except the "waker dequeues us" microoptimization).
Quoted 5 lines
One comment with respect to multiple wait queues and rwsems:
Again it will allow you to do reader-pref and/or writer-pref, but not something like FIFO, i.e. wake up a writer if first waiter or wake up all readers if first ..... and so on.
I don't know whether the latter is terrible important ...
(Aside: I'm still reluctant to implement strict FIFO locks until someone shows a starvation case in the current locks).
I thought about this a little. If we add a flags arg to the sys_uwaitq_wake() and make sys_uwaitq_wait() return the flags used by the waker, and sys_uwaitq_wake return 1 if it woke someone...
#define UWAITQ_PASSING 1
up:
ret = sys_uwaitq_wake(cookie, UWAITQ_PASSING);
if (ret < 0) return -1;
/* No waiters actually waiting? */
if (ret == 0) {
lock->counter = 1;
sys_uwaitq_wake(cookie, 0);
}
down:
sys_uwaitq_queue(cookie);
if (atomic dec counter to 0) {
sys_uwaitq_queue(0); /* unqueue */
return 0;
}
ret = sys_uwaitq_wait(cookie);
if (ret < 0)
return -1;
if (ret == UWAITQ_PASSING)
return 0;
goto down; /* spin again */Rusty.
--
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 2 lines · On Sun, 10 Mar 2002 19:41:36 +0000 (UTC) [email protected] (Linus…
And no, it's not worth discontinuing i386 support. It just isn't painful enough to maintain.
How about just dropping 386 + SMP support?
Then it would be a nobrainer to have cmpxchg a generic operation, AND export it to userspace in the proposed "kernel routine page".
arch/i386/config.in +3 -1
@@ -177,7 +177,9 @@ bool 'Math emulation' CONFIG_MATH_EMULATION bool 'MTRR (Memory Type Range Register) support' CONFIG_MTRR -bool 'Symmetric multi-processing support' CONFIG_SMP +if [ "$CONFIG_M386" != "y" ]; then + bool 'Symmetric multi-processing support' CONFIG_SMP +fi bool 'Preemptible Kernel' CONFIG_PREEMPT if [ "$CONFIG_SMP" != "y" ]; then bool 'Local APIC support on uniprocessors' CONFIG_X86_UP_APIC
Thanks!
Rusty.
Signature · 1 line
Anyone who quotes me in their sig is an idiot. -- Rusty Russell.
Quoted 4 lines
Quoted 2 lines
And no, it's not worth discontinuing i386 support. It just isn't painful enough to maintain.
How about just dropping 386 + SMP support?
We dont support SMP 386 boxes anyway. Nothing to drop. We've never supported anything earlier than 486 SMP like the early MP1.1 compliant IBM boards, (and briefly the compaq non MP 1.1 compliant stuff (Thomas Radke - the forgotten man in the creation of SMP Linux - did 2/4 way compaq stuff)