Revert "semaphore: fix"

This reverts commit bf726eab3711cf192405d21688a4b21e07b6188a, as it has
been reported to cause a regression with processes stuck in __down(),
apparently because some missing wakeup.

Quoth Sven Wegener:
"I'm currently investigating a regression that has showed up with my
last git pull yesterday. Bisecting the commits showed bf726e
"semaphore: fix" to be the culprit, reverting it fixed the issue.

Symptoms: During heavy filesystem usage (e.g. a kernel compile) I get
several compiler processes in uninterruptible sleep, blocking all i/o
on the filesystem. System is an Intel Core 2 Quad running a 64bit
kernel and userspace. Filesystem is xfs on top of lvm. See below for
the output of sysrq-w."

See

http://lkml.org/lkml/2008/5/10/45

for full report.

In the meantime, we can just fix the BKL performance regression by
reverting back to the good old BKL spinlock implementation instead,
since any sleeping lock will generally perform badly, especially if it
tries to be fair.

Reported-by: Sven Wegener <sven.wegener@stealer.net>
Cc: Andrew Morton <akpm@linux-foundation.org>
Cc: Ingo Molnar <mingo@elte.hu>
Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>

+34 -30
+34 -30
kernel/semaphore.c
··· 54 54 unsigned long flags; 55 55 56 56 spin_lock_irqsave(&sem->lock, flags); 57 - if (unlikely(!sem->count)) 57 + if (likely(sem->count > 0)) 58 + sem->count--; 59 + else 58 60 __down(sem); 59 - sem->count--; 60 61 spin_unlock_irqrestore(&sem->lock, flags); 61 62 } 62 63 EXPORT_SYMBOL(down); ··· 77 76 int result = 0; 78 77 79 78 spin_lock_irqsave(&sem->lock, flags); 80 - if (unlikely(!sem->count)) 81 - result = __down_interruptible(sem); 82 - if (!result) 79 + if (likely(sem->count > 0)) 83 80 sem->count--; 81 + else 82 + result = __down_interruptible(sem); 84 83 spin_unlock_irqrestore(&sem->lock, flags); 85 84 86 85 return result; ··· 103 102 int result = 0; 104 103 105 104 spin_lock_irqsave(&sem->lock, flags); 106 - if (unlikely(!sem->count)) 107 - result = __down_killable(sem); 108 - if (!result) 105 + if (likely(sem->count > 0)) 109 106 sem->count--; 107 + else 108 + result = __down_killable(sem); 110 109 spin_unlock_irqrestore(&sem->lock, flags); 111 110 112 111 return result; ··· 157 156 int result = 0; 158 157 159 158 spin_lock_irqsave(&sem->lock, flags); 160 - if (unlikely(!sem->count)) 161 - result = __down_timeout(sem, jiffies); 162 - if (!result) 159 + if (likely(sem->count > 0)) 163 160 sem->count--; 161 + else 162 + result = __down_timeout(sem, jiffies); 164 163 spin_unlock_irqrestore(&sem->lock, flags); 165 164 166 165 return result; ··· 179 178 unsigned long flags; 180 179 181 180 spin_lock_irqsave(&sem->lock, flags); 182 - sem->count++; 183 - if (unlikely(!list_empty(&sem->wait_list))) 181 + if (likely(list_empty(&sem->wait_list))) 182 + sem->count++; 183 + else 184 184 __up(sem); 185 185 spin_unlock_irqrestore(&sem->lock, flags); 186 186 } ··· 192 190 struct semaphore_waiter { 193 191 struct list_head list; 194 192 struct task_struct *task; 193 + int up; 195 194 }; 196 195 197 196 /* ··· 205 202 { 206 203 struct task_struct *task = current; 207 204 struct semaphore_waiter waiter; 208 - int ret = 0; 209 205 210 - waiter.task = task; 211 206 list_add_tail(&waiter.list, &sem->wait_list); 207 + waiter.task = task; 208 + waiter.up = 0; 212 209 213 210 for (;;) { 214 - if (state == TASK_INTERRUPTIBLE && signal_pending(task)) { 215 - ret = -EINTR; 216 - break; 217 - } 218 - if (state == TASK_KILLABLE && fatal_signal_pending(task)) { 219 - ret = -EINTR; 220 - break; 221 - } 222 - if (timeout <= 0) { 223 - ret = -ETIME; 224 - break; 225 - } 211 + if (state == TASK_INTERRUPTIBLE && signal_pending(task)) 212 + goto interrupted; 213 + if (state == TASK_KILLABLE && fatal_signal_pending(task)) 214 + goto interrupted; 215 + if (timeout <= 0) 216 + goto timed_out; 226 217 __set_task_state(task, state); 227 218 spin_unlock_irq(&sem->lock); 228 219 timeout = schedule_timeout(timeout); 229 220 spin_lock_irq(&sem->lock); 230 - if (sem->count > 0) 231 - break; 221 + if (waiter.up) 222 + return 0; 232 223 } 233 224 225 + timed_out: 234 226 list_del(&waiter.list); 235 - return ret; 227 + return -ETIME; 228 + 229 + interrupted: 230 + list_del(&waiter.list); 231 + return -EINTR; 236 232 } 237 233 238 234 static noinline void __sched __down(struct semaphore *sem) ··· 258 256 { 259 257 struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list, 260 258 struct semaphore_waiter, list); 259 + list_del(&waiter->list); 260 + waiter->up = 1; 261 261 wake_up_process(waiter->task); 262 262 }