aboutsummaryrefslogtreecommitdiffstats
path: root/arch
diff options
context:
space:
mode:
authorBenjamin LaHaise <bcrl@kvack.org>2005-09-03 18:56:52 -0400
committerLinus Torvalds <torvalds@evo.osdl.org>2005-09-05 03:06:14 -0400
commit52fdd08903a1d1162e184114837e232640191627 (patch)
tree9469f521b7ba12ac48337155bc5a65049d361229 /arch
parent4ad8d38342430f8b52f7a8458dce90caf8c8ca64 (diff)
[PATCH] unify x86/x86-64 semaphore code
This patch moves the common code in x86 and x86-64's semaphore.c into a single file in lib/semaphore-sleepers.c. The arch specific asm stubs are left in the arch tree (in semaphore.c for i386 and in the asm for x86-64). There should be no changes in code/functionality with this patch. Signed-off-by: Benjamin LaHaise <benjamin.c.lahaise@intel.com> Cc: Andi Kleen <ak@muc.de> Signed-off-by: Jeff Dike <jdike@addtoit.com> Signed-off-by: Andrew Morton <akpm@osdl.org> Signed-off-by: Linus Torvalds <torvalds@osdl.org>
Diffstat (limited to 'arch')
-rw-r--r--arch/i386/Kconfig4
-rw-r--r--arch/i386/kernel/semaphore.c162
-rw-r--r--arch/um/Kconfig_i3864
-rw-r--r--arch/um/Kconfig_x86_644
-rw-r--r--arch/um/sys-x86_64/Makefile5
-rw-r--r--arch/x86_64/Kconfig4
-rw-r--r--arch/x86_64/kernel/Makefile2
-rw-r--r--arch/x86_64/kernel/semaphore.c180
8 files changed, 19 insertions, 346 deletions
diff --git a/arch/i386/Kconfig b/arch/i386/Kconfig
index dcb0ad098c60..3b3b017e1c15 100644
--- a/arch/i386/Kconfig
+++ b/arch/i386/Kconfig
@@ -14,6 +14,10 @@ config X86
14 486, 586, Pentiums, and various instruction-set-compatible chips by 14 486, 586, Pentiums, and various instruction-set-compatible chips by
15 AMD, Cyrix, and others. 15 AMD, Cyrix, and others.
16 16
17config SEMAPHORE_SLEEPERS
18 bool
19 default y
20
17config MMU 21config MMU
18 bool 22 bool
19 default y 23 default y
diff --git a/arch/i386/kernel/semaphore.c b/arch/i386/kernel/semaphore.c
index 469f496e55c0..7455ab643943 100644
--- a/arch/i386/kernel/semaphore.c
+++ b/arch/i386/kernel/semaphore.c
@@ -13,171 +13,9 @@
13 * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org> 13 * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
14 */ 14 */
15#include <linux/config.h> 15#include <linux/config.h>
16#include <linux/sched.h>
17#include <linux/err.h>
18#include <linux/init.h>
19#include <asm/semaphore.h> 16#include <asm/semaphore.h>
20 17
21/* 18/*
22 * Semaphores are implemented using a two-way counter:
23 * The "count" variable is decremented for each process
24 * that tries to acquire the semaphore, while the "sleeping"
25 * variable is a count of such acquires.
26 *
27 * Notably, the inline "up()" and "down()" functions can
28 * efficiently test if they need to do any extra work (up
29 * needs to do something only if count was negative before
30 * the increment operation.
31 *
32 * "sleeping" and the contention routine ordering is protected
33 * by the spinlock in the semaphore's waitqueue head.
34 *
35 * Note that these functions are only called when there is
36 * contention on the lock, and as such all this is the
37 * "non-critical" part of the whole semaphore business. The
38 * critical part is the inline stuff in <asm/semaphore.h>
39 * where we want to avoid any extra jumps and calls.
40 */
41
42/*
43 * Logic:
44 * - only on a boundary condition do we need to care. When we go
45 * from a negative count to a non-negative, we wake people up.
46 * - when we go from a non-negative count to a negative do we
47 * (a) synchronize with the "sleeper" count and (b) make sure
48 * that we're on the wakeup list before we synchronize so that
49 * we cannot lose wakeup events.
50 */
51
52static fastcall void __attribute_used__ __up(struct semaphore *sem)
53{
54 wake_up(&sem->wait);
55}
56
57static fastcall void __attribute_used__ __sched __down(struct semaphore * sem)
58{
59 struct task_struct *tsk = current;
60 DECLARE_WAITQUEUE(wait, tsk);
61 unsigned long flags;
62
63 tsk->state = TASK_UNINTERRUPTIBLE;
64 spin_lock_irqsave(&sem->wait.lock, flags);
65 add_wait_queue_exclusive_locked(&sem->wait, &wait);
66
67 sem->sleepers++;
68 for (;;) {
69 int sleepers = sem->sleepers;
70
71 /*
72 * Add "everybody else" into it. They aren't
73 * playing, because we own the spinlock in
74 * the wait_queue_head.
75 */
76 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
77 sem->sleepers = 0;
78 break;
79 }
80 sem->sleepers = 1; /* us - see -1 above */
81 spin_unlock_irqrestore(&sem->wait.lock, flags);
82
83 schedule();
84
85 spin_lock_irqsave(&sem->wait.lock, flags);
86 tsk->state = TASK_UNINTERRUPTIBLE;
87 }
88 remove_wait_queue_locked(&sem->wait, &wait);
89 wake_up_locked(&sem->wait);
90 spin_unlock_irqrestore(&sem->wait.lock, flags);
91 tsk->state = TASK_RUNNING;
92}
93
94static fastcall int __attribute_used__ __sched __down_interruptible(struct semaphore * sem)
95{
96 int retval = 0;
97 struct task_struct *tsk = current;
98 DECLARE_WAITQUEUE(wait, tsk);
99 unsigned long flags;
100
101 tsk->state = TASK_INTERRUPTIBLE;
102 spin_lock_irqsave(&sem->wait.lock, flags);
103 add_wait_queue_exclusive_locked(&sem->wait, &wait);
104
105 sem->sleepers++;
106 for (;;) {
107 int sleepers = sem->sleepers;
108
109 /*
110 * With signals pending, this turns into
111 * the trylock failure case - we won't be
112 * sleeping, and we* can't get the lock as
113 * it has contention. Just correct the count
114 * and exit.
115 */
116 if (signal_pending(current)) {
117 retval = -EINTR;
118 sem->sleepers = 0;
119 atomic_add(sleepers, &sem->count);
120 break;
121 }
122
123 /*
124 * Add "everybody else" into it. They aren't
125 * playing, because we own the spinlock in
126 * wait_queue_head. The "-1" is because we're
127 * still hoping to get the semaphore.
128 */
129 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
130 sem->sleepers = 0;
131 break;
132 }
133 sem->sleepers = 1; /* us - see -1 above */
134 spin_unlock_irqrestore(&sem->wait.lock, flags);
135
136 schedule();
137
138 spin_lock_irqsave(&sem->wait.lock, flags);
139 tsk->state = TASK_INTERRUPTIBLE;
140 }
141 remove_wait_queue_locked(&sem->wait, &wait);
142 wake_up_locked(&sem->wait);
143 spin_unlock_irqrestore(&sem->wait.lock, flags);
144
145 tsk->state = TASK_RUNNING;
146 return retval;
147}
148
149/*
150 * Trylock failed - make sure we correct for
151 * having decremented the count.
152 *
153 * We could have done the trylock with a
154 * single "cmpxchg" without failure cases,
155 * but then it wouldn't work on a 386.
156 */
157static fastcall int __attribute_used__ __down_trylock(struct semaphore * sem)
158{
159 int sleepers;
160 unsigned long flags;
161
162 spin_lock_irqsave(&sem->wait.lock, flags);
163 sleepers = sem->sleepers + 1;
164 sem->sleepers = 0;
165
166 /*
167 * Add "everybody else" and us into it. They aren't
168 * playing, because we own the spinlock in the
169 * wait_queue_head.
170 */
171 if (!atomic_add_negative(sleepers, &sem->count)) {
172 wake_up_locked(&sem->wait);
173 }
174
175 spin_unlock_irqrestore(&sem->wait.lock, flags);
176 return 1;
177}
178
179
180/*
181 * The semaphore operations have a special calling sequence that 19 * The semaphore operations have a special calling sequence that
182 * allow us to do a simpler in-line version of them. These routines 20 * allow us to do a simpler in-line version of them. These routines
183 * need to convert that sequence back into the C sequence when 21 * need to convert that sequence back into the C sequence when
diff --git a/arch/um/Kconfig_i386 b/arch/um/Kconfig_i386
index 27c18a8d9d17..8ad156a00499 100644
--- a/arch/um/Kconfig_i386
+++ b/arch/um/Kconfig_i386
@@ -6,6 +6,10 @@ config 64BIT
6 bool 6 bool
7 default n 7 default n
8 8
9config SEMAPHORE_SLEEPERS
10 bool
11 default y
12
9config TOP_ADDR 13config TOP_ADDR
10 hex 14 hex
11 default 0xc0000000 if !HOST_2G_2G 15 default 0xc0000000 if !HOST_2G_2G
diff --git a/arch/um/Kconfig_x86_64 b/arch/um/Kconfig_x86_64
index 735a047c890c..6e5357c5bcfb 100644
--- a/arch/um/Kconfig_x86_64
+++ b/arch/um/Kconfig_x86_64
@@ -6,6 +6,10 @@ config 64BIT
6 bool 6 bool
7 default y 7 default y
8 8
9config SEMAPHORE_SLEEPERS
10 bool
11 default y
12
9config TOP_ADDR 13config TOP_ADDR
10 hex 14 hex
11 default 0x80000000 15 default 0x80000000
diff --git a/arch/um/sys-x86_64/Makefile b/arch/um/sys-x86_64/Makefile
index 7488206ce6f4..736d3edec581 100644
--- a/arch/um/sys-x86_64/Makefile
+++ b/arch/um/sys-x86_64/Makefile
@@ -6,7 +6,7 @@
6 6
7#XXX: why into lib-y? 7#XXX: why into lib-y?
8lib-y = bitops.o bugs.o csum-partial.o delay.o fault.o mem.o memcpy.o \ 8lib-y = bitops.o bugs.o csum-partial.o delay.o fault.o mem.o memcpy.o \
9 ptrace.o ptrace_user.o semaphore.o sigcontext.o signal.o stub.o \ 9 ptrace.o ptrace_user.o sigcontext.o signal.o stub.o \
10 stub_segv.o syscalls.o syscall_table.o sysrq.o thunk.o 10 stub_segv.o syscalls.o syscall_table.o sysrq.o thunk.o
11 11
12obj-y := ksyms.o 12obj-y := ksyms.o
@@ -15,7 +15,7 @@ obj-$(CONFIG_MODULES) += module.o um_module.o
15USER_OBJS := ptrace_user.o sigcontext.o 15USER_OBJS := ptrace_user.o sigcontext.o
16 16
17SYMLINKS = bitops.c csum-copy.S csum-partial.c csum-wrappers.c memcpy.S \ 17SYMLINKS = bitops.c csum-copy.S csum-partial.c csum-wrappers.c memcpy.S \
18 semaphore.c thunk.S module.c 18 thunk.S module.c
19 19
20include arch/um/scripts/Makefile.rules 20include arch/um/scripts/Makefile.rules
21 21
@@ -24,7 +24,6 @@ csum-copy.S-dir = lib
24csum-partial.c-dir = lib 24csum-partial.c-dir = lib
25csum-wrappers.c-dir = lib 25csum-wrappers.c-dir = lib
26memcpy.S-dir = lib 26memcpy.S-dir = lib
27semaphore.c-dir = kernel
28thunk.S-dir = lib 27thunk.S-dir = lib
29module.c-dir = kernel 28module.c-dir = kernel
30 29
diff --git a/arch/x86_64/Kconfig b/arch/x86_64/Kconfig
index 660a03a89e66..75e52c57f19c 100644
--- a/arch/x86_64/Kconfig
+++ b/arch/x86_64/Kconfig
@@ -24,6 +24,10 @@ config X86
24 bool 24 bool
25 default y 25 default y
26 26
27config SEMAPHORE_SLEEPERS
28 bool
29 default y
30
27config MMU 31config MMU
28 bool 32 bool
29 default y 33 default y
diff --git a/arch/x86_64/kernel/Makefile b/arch/x86_64/kernel/Makefile
index 48f9e2c19cd6..427501a336c4 100644
--- a/arch/x86_64/kernel/Makefile
+++ b/arch/x86_64/kernel/Makefile
@@ -4,7 +4,7 @@
4 4
5extra-y := head.o head64.o init_task.o vmlinux.lds 5extra-y := head.o head64.o init_task.o vmlinux.lds
6EXTRA_AFLAGS := -traditional 6EXTRA_AFLAGS := -traditional
7obj-y := process.o semaphore.o signal.o entry.o traps.o irq.o \ 7obj-y := process.o signal.o entry.o traps.o irq.o \
8 ptrace.o time.o ioport.o ldt.o setup.o i8259.o sys_x86_64.o \ 8 ptrace.o time.o ioport.o ldt.o setup.o i8259.o sys_x86_64.o \
9 x8664_ksyms.o i387.o syscall.o vsyscall.o \ 9 x8664_ksyms.o i387.o syscall.o vsyscall.o \
10 setup64.o bootflag.o e820.o reboot.o quirks.o 10 setup64.o bootflag.o e820.o reboot.o quirks.o
diff --git a/arch/x86_64/kernel/semaphore.c b/arch/x86_64/kernel/semaphore.c
deleted file mode 100644
index 48f7c18172b9..000000000000
--- a/arch/x86_64/kernel/semaphore.c
+++ /dev/null
@@ -1,180 +0,0 @@
1/*
2 * x86_64 semaphore implementation.
3 *
4 * (C) Copyright 1999 Linus Torvalds
5 *
6 * Portions Copyright 1999 Red Hat, Inc.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version
11 * 2 of the License, or (at your option) any later version.
12 *
13 * rw semaphores implemented November 1999 by Benjamin LaHaise <bcrl@kvack.org>
14 */
15#include <linux/config.h>
16#include <linux/sched.h>
17#include <linux/init.h>
18#include <asm/errno.h>
19
20#include <asm/semaphore.h>
21
22/*
23 * Semaphores are implemented using a two-way counter:
24 * The "count" variable is decremented for each process
25 * that tries to acquire the semaphore, while the "sleeping"
26 * variable is a count of such acquires.
27 *
28 * Notably, the inline "up()" and "down()" functions can
29 * efficiently test if they need to do any extra work (up
30 * needs to do something only if count was negative before
31 * the increment operation.
32 *
33 * "sleeping" and the contention routine ordering is protected
34 * by the spinlock in the semaphore's waitqueue head.
35 *
36 * Note that these functions are only called when there is
37 * contention on the lock, and as such all this is the
38 * "non-critical" part of the whole semaphore business. The
39 * critical part is the inline stuff in <asm/semaphore.h>
40 * where we want to avoid any extra jumps and calls.
41 */
42
43/*
44 * Logic:
45 * - only on a boundary condition do we need to care. When we go
46 * from a negative count to a non-negative, we wake people up.
47 * - when we go from a non-negative count to a negative do we
48 * (a) synchronize with the "sleeper" count and (b) make sure
49 * that we're on the wakeup list before we synchronize so that
50 * we cannot lose wakeup events.
51 */
52
53void __up(struct semaphore *sem)
54{
55 wake_up(&sem->wait);
56}
57
58void __sched __down(struct semaphore * sem)
59{
60 struct task_struct *tsk = current;
61 DECLARE_WAITQUEUE(wait, tsk);
62 unsigned long flags;
63
64 tsk->state = TASK_UNINTERRUPTIBLE;
65 spin_lock_irqsave(&sem->wait.lock, flags);
66 add_wait_queue_exclusive_locked(&sem->wait, &wait);
67
68 sem->sleepers++;
69 for (;;) {
70 int sleepers = sem->sleepers;
71
72 /*
73 * Add "everybody else" into it. They aren't
74 * playing, because we own the spinlock in
75 * the wait_queue_head.
76 */
77 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
78 sem->sleepers = 0;
79 break;
80 }
81 sem->sleepers = 1; /* us - see -1 above */
82 spin_unlock_irqrestore(&sem->wait.lock, flags);
83
84 schedule();
85
86 spin_lock_irqsave(&sem->wait.lock, flags);
87 tsk->state = TASK_UNINTERRUPTIBLE;
88 }
89 remove_wait_queue_locked(&sem->wait, &wait);
90 wake_up_locked(&sem->wait);
91 spin_unlock_irqrestore(&sem->wait.lock, flags);
92 tsk->state = TASK_RUNNING;
93}
94
95int __sched __down_interruptible(struct semaphore * sem)
96{
97 int retval = 0;
98 struct task_struct *tsk = current;
99 DECLARE_WAITQUEUE(wait, tsk);
100 unsigned long flags;
101
102 tsk->state = TASK_INTERRUPTIBLE;
103 spin_lock_irqsave(&sem->wait.lock, flags);
104 add_wait_queue_exclusive_locked(&sem->wait, &wait);
105
106 sem->sleepers++;
107 for (;;) {
108 int sleepers = sem->sleepers;
109
110 /*
111 * With signals pending, this turns into
112 * the trylock failure case - we won't be
113 * sleeping, and we* can't get the lock as
114 * it has contention. Just correct the count
115 * and exit.
116 */
117 if (signal_pending(current)) {
118 retval = -EINTR;
119 sem->sleepers = 0;
120 atomic_add(sleepers, &sem->count);
121 break;
122 }
123
124 /*
125 * Add "everybody else" into it. They aren't
126 * playing, because we own the spinlock in
127 * wait_queue_head. The "-1" is because we're
128 * still hoping to get the semaphore.
129 */
130 if (!atomic_add_negative(sleepers - 1, &sem->count)) {
131 sem->sleepers = 0;
132 break;
133 }
134 sem->sleepers = 1; /* us - see -1 above */
135 spin_unlock_irqrestore(&sem->wait.lock, flags);
136
137 schedule();
138
139 spin_lock_irqsave(&sem->wait.lock, flags);
140 tsk->state = TASK_INTERRUPTIBLE;
141 }
142 remove_wait_queue_locked(&sem->wait, &wait);
143 wake_up_locked(&sem->wait);
144 spin_unlock_irqrestore(&sem->wait.lock, flags);
145
146 tsk->state = TASK_RUNNING;
147 return retval;
148}
149
150/*
151 * Trylock failed - make sure we correct for
152 * having decremented the count.
153 *
154 * We could have done the trylock with a
155 * single "cmpxchg" without failure cases,
156 * but then it wouldn't work on a 386.
157 */
158int __down_trylock(struct semaphore * sem)
159{
160 int sleepers;
161 unsigned long flags;
162
163 spin_lock_irqsave(&sem->wait.lock, flags);
164 sleepers = sem->sleepers + 1;
165 sem->sleepers = 0;
166
167 /*
168 * Add "everybody else" and us into it. They aren't
169 * playing, because we own the spinlock in the
170 * wait_queue_head.
171 */
172 if (!atomic_add_negative(sleepers, &sem->count)) {
173 wake_up_locked(&sem->wait);
174 }
175
176 spin_unlock_irqrestore(&sem->wait.lock, flags);
177 return 1;
178}
179
180