patches for test-and-set without ll/sc (Re: thread-ready ABIs)
Hi, all.
Please review an attached patch set and if it is ok, please megre into the cvs trees.
Kevin, please let us know about "k1 semaphore" you said. I want to know we can merge those functions or not.
Technical discussions are welcome.
------------
From: Machida Hiroyuki <[email protected]>
To: [email protected], [email protected], [email protected],
[email protected], [email protected]
Date: Tue, 22 Jan 2002 15:27:44 +0900
X-Mailer: Mew version 1.94.2 on Emacs 19.28 / Mule 2.3 (SUETSUMUHANA)
Hi, all.
As I said at 1/20, I'll post the short descriptions about our test-and-set implementation and patches for linux-2.4.17 and glibc-2.2.3.
=====================================================================
We implemented the fast and safe user level test and set function for single MIPS CPUs. You don't need to use LL/SC and sysmips() with this method. (excatly say, sysmips() is needed for initializing, but once initialized, we don't use it any more).
NOTE: We assume the single processor to use this method, You can not use our method for SMP.
WHAT'S CHANGED:
* kernel side change #1
Set specific constant (we call this value "_TST_ACCESS_MAGIC") to K1 on every transition from kernel mode to user mode. This means you can use k1 in any exception handler as same as before our method introduced, except that you have to do
"li k1, _TST_ACCESS_MAGIC"
at the very previous of
"eret"
or
"j k0;
"rfe"
.
We choose the value of _TST_ACCESS_MAGIC, to cause SEGV fault when you use this value as address.
* kernel side change #2
On memory fault hander, kernel check write-access to _TST_ACCESS_MAGIC from fixed address range of user process. (EPC is in _TST_START_MAGIC to _TST_START_MAGIC+PAGE_SIZE) If the condtion is met, kernel restart user process from _TST_START_MAGIC.
* kernel side change #3
We add pseudo device driver "/dev/tst" to provide
test_and_set procedure at the same virtual address
(_TST_START_MAGIC) to any user process.
_TST_START_MAGIC:
.set noreorder
0:
move k1, a0
lw v0, 0(a0)
nop
bnez v0, 1f
nop
bne k1, a0, 0b
nop ....<point A>
sw a1, 0(k1)
1:
jr ra
nop* glibc change:
We implement test_and_set(addr, val) as follows,
Do mmap /dev/tst to _TST_START_MAGIC, if not yet mapped. call _TST_START_MAGIC(addr, val)
If we can't open /dev/tst then, use sysmips() as final resort.
HOW TO WORK:
If no context-switch is occured in _TST_START_MAGIC() procedure, nobody changes the mutex var. It's no problem. So you can do _TST_START_MAGIC() porcedure as you see.
But, if some context-swtich is occured in _TST_START_MAGIC() somebody chages the mutex var. It's a problem.
We must not store to the mutex var, if context-swtich is occured at <point A>.
In our method, kernel sets k1 as _TST_ACCESS_MAGIC on
transition to user mode. "sw a1, 0(k1)" causes
SEGV-fault if context-swtich is occured at <point A>. The SEGV-fault hander catch this situation, restart user process from top of _TST_START_MAGIC().
PATCHES:
I attached three patches;
1. patch for linux kernel 2.4.17 (SourceForge tree)
2. patch for glibc 2.2.3 (of HHL 2.0)
3. patch for linuxthread 2.2.3 (of HHL 2.0)
To test those patches; you must
turn on CONFIG_MIPS_TST_DEV on config kernel, have working version of sysmips(MIPS_ATOMIC_SET), update kernel headers before building glibc and make /dev/tst device ("mknod c /dev/tst 123 0", 123 is a tempoary major number for this device)
I'v tested at ITE board. On testing, I'v made lettle changes into "drivers/char/Config.in" and "arch/mips/kernel/sysmip.c" to enable CONFIG_MIPS_TST_DEV and to work sysmips() at ITE board. Those chages are not included in the patch set.
===================================================================
You can find the paper about it in
http://lc.linux.or.jp/lc2001/papers/tas-ps2-paper.pdf
(sorry in japanese only)
The abstract of the paper is following;
The Implementation of user level test-and-set on PS2 Linux In the multi-thread environment like Linux, a fast user-level mutual exclusion mechanism is strongly required. But MIPS chips designed for embedded and single processor, like the Emotion Engine, have no atomic test-and-set instruction. We implemented the fast user-level mutual exclusion without invoking system-call and its costs, on the PS2 Linux. This method utilizes the memory protection facility of Operating System, to detect preemption and nullify the operation. In this paper, we present the method and its evaluation.
---
Hiroyuki Machida
Sony Corp.
Quoted 2 lines
Kevin, please let us know about "k1 semaphore" you said. I want to know we can merge those functions or not.
My proposal is for a very simple emulation of ll/sc for CPUs that implement the MIPS II "branch likely" instructions but which do not have a functioning LL/SC. That means that it is *not* a solution for R3000 processor such as those found in old DEC and SGI platforms. LL/SC is also part of MIPS II, but several manufacturers seem to have done MIPS II/III processors where LL/SC
is not fully functional. This hack could thus be workable both for the TX59 family and the Vr4100 family.
The idea leverages off the fact that a branch likely instruction performs a kind of conditional execution. The instruction in the delay slot is executed only if the branch is taken. This can be used to synthesize a conditional store. The user level code for a simple atomic increment, for example, would look something like this:
_atomic_inc_nollsc:
.set noreorder
li t0,MAGIC_COOKIE
Retry:
mov k1,t0
lw t1,0(a0)
addiu t1,t1,1
BEQL k1,t0,done
sw t1,0(a0)
b Retry
nopdone
jr ra
nop
If - and this is an important consideration - IF the value of MAGIC_COOKIE can be determined such that
it can *never* be the residue value left in k1 by a kernel exception handler, *no* kernel modifications are required for this to work identically to LL/SC! If, for example, k1 always ends up containing either a Status, EPC, or EntryLo value after an ERET, something like 0xffdadaff could be used as a MAGIC_COOKIE value, as it is not a sane value for any of the above.
You will get some number of needless retries in this scheme to the extent that there are TLB misses on the load and on the instructions themselves if the routine crosses a page boundary. You would get those same retries using LL/SC.
You could eliminate those retries by preserving the appropriate k-register in the TLB refill handler, but I really doubt that it would be worthwhile.
If there is any doubt about the possibility of the MAGIC_COOKIE value being left in k1 (or
k0, which could also be used as the "LL flop" if its behavior is more easily constrained), an explicit operation at the end of the fault handlers could be used to clear the register. That would still be far less complex and intrusive than the mods that you suggest below.
I have not implemented this scheme under Linux, but we have tried it with success under other OSes. It should in principle be SMP safe.
Quoted 1 line
Technical discussions are welcome.
Well, there you have one!
Regards,
Kevin K.
Quoted 7 lines · Kevin D. Kissell
The idea leverages off the fact that a branch likely instruction performs a kind of conditional execution. The instruction in the delay slot is executed only if the branch is taken. This can be used to synthesize a conditional store. The user level code for a simple atomic increment, for example, would look something like this:
Hmm, could you use this to take the race out of the kernel wait loop too? Ie use current->need_resched as the test and 'wait' as the conditional operation.
Jason
Quoted 10 lines · Kevin D. Kissell
Quoted 8 lines
The idea leverages off the fact that a branch likely instruction performs a kind of conditional execution. The instruction in the delay slot is executed only if the branch is taken. This can be used to synthesize a conditional store. The user level code for a simple atomic increment, for example, would look something like this:
Quoted 3 lines
Hmm, could you use this to take the race out of the kernel wait loop too? Ie use
current->need_reschedas the test and 'wait' as the conditional operation.
It's quite possible. But remember that it won't work on an R3000. R39xx yes, but not an R3K "classic".
Kevin K.
Hi Kevin,
As you said, following codes can run fine if CPU has brach-likely.
From: "Kevin D. Kissell" <[email protected]>
Subject: Re: patches for test-and-set without ll/sc (Re: thread-ready ABIs)
Date: Tue, 22 Jan 2002 18:16:25 +0100
Quoted 11 lines
_atomic_inc_nollsc:
.set noreorder
li t0,MAGIC_COOKIE
Retry:mov k1,t0 lw t1,0(a0) addiu t1,t1,1 BEQL k1,t0,done sw t1,0(a0) b Retry nop
Quoted 4 lines
done
jr ra
nop
<snip>
Quoted 8 lines
If there is any doubt about the possibility of the
MAGIC_COOKIEvalue being left in k1 (or
k0, which could also be used as the "LL flop" if its behavior is more easily constrained), an explicit operation at the end of the fault handlers could be used to clear the register. That would still be far less complex and intrusive than the mods that you suggest below.
I think we should always "clear" k1 at the end of exception handler. (Above "clear" means "set !MAGIC_COOKIE"). It's conaervative way, but robust aginst future changes in exception handler.
Quoted 1 line
It should in principle be SMP safe.
I don't think so.
Suppose that
THREAD A is bound to CPU A and THREAD B is bound to CPU B. THREAD A and THREAD B are running on_atomic_inc_nollsc().
Two threads are really running at the same time, without context-switch. In this case nobody clear k1.
Anyway, I will merge your brach-likely method and make some changes.
This change will provide signle interface of user level test-and-set(), without LL/SC instruction emulation nor system-call. So everyone will be able to run single user program binary to on following three types of CPUs;
CPU has LL/SC,
CPU has no LL/SC, but has branch-likely and CPU has neither LL/SC nor branch-likely.
---
Hiroyuki Machida
Sony Corp.
Quoted 3 lines
Quoted 1 line
It should in principle be SMP safe.
I don't think so.
You are quite right. It was an offhand remark
that should have been better qualified. The basic
mechanism requires no global memory resource and will detect discontinuitues in scheduling on several CPUs at once, but but it is certainly not SMP safe in the sense of the code sample providing atomic increment on a classical SMP hardware platform.
Regards,
Kevin K.
Quoted 9 lines · Machida Hiroyuki
Quoted 1 line
It should in principle be SMP safe.
I don't think so.
Suppose that
THREAD A is bound to CPU A and THREAD B is bound to CPU B. THREAD A and THREAD B are runningon_atomic_inc_nollsc().
Two threads are really running at the same time, without context-switch. In this case nobody clear k1.
There is a method for mutual exclusion called Dekker's Algorithem (sp?) which only requires just atomic stores and can be implemented in plain C. Downside is it's weak performance that renders it pretty much a CS only thing.
Ralf
From: Ralf Baechle <[email protected]>
Subject: Re: patches for test-and-set without ll/sc (Re: thread-ready ABIs)
Date: Thu, 24 Jan 2002 10:59:15 -0800
Quoted 4 lines
There is a method for mutual exclusion called Dekker's Algorithem (sp?) which only requires just atomic stores and can be implemented in plain C. Downside is it's weak performance that renders it pretty much a CS only thing.
Dekker's Algorithem and Lamport's Algorithm are not match for test_and_set() interface in glibc.
In test_and_set(), mutex var is INT, but those algorimths need to share addtional variables.
---
Hiroyuki Machida
Sony Corp.
Quoted 10 lines
Quoted 9 lines
Quoted 1 line
It should in principle be SMP safe.
I don't think so.
Suppose that
THREAD A is bound to CPU A and THREAD B is bound to CPU B. THREAD A and THREAD B are runningon_atomic_inc_nollsc(). Two threads are really running at the same time, without context-switch. In this case nobody clear k1.
Quoted 4 lines
There is a method for mutual exclusion called Dekker's Algorithem (sp?) which only requires just atomic stores and can be implemented in plain C. Downside is it's weak performance that renders it pretty much a CS only thing.
Having actually ised Dekker's algorithm once in an industrial application (2 Z80's with a shared buffer) some 20-odd years ago, I can say that it does work, but caution that, while in theory one can scale it to arbitrary number of CPUs, the time of the operation expands by something like the square of the number of CPUs involved. It's minimally acceptable for 2 CPUs. More than that...
Kevin K.