Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2008 Intel Corporation |
| 3 | * Author: Matthew Wilcox <willy@linux.intel.com> |
| 4 | * |
| 5 | * Distributed under the terms of the GNU GPL, version 2 |
| 6 | */ |
| 7 | |
| 8 | #include <linux/compiler.h> |
| 9 | #include <linux/kernel.h> |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/sched.h> |
| 12 | #include <linux/semaphore.h> |
| 13 | #include <linux/spinlock.h> |
| 14 | |
| 15 | /* |
| 16 | * Some notes on the implementation: |
| 17 | * |
| 18 | * down_trylock() and up() can be called from interrupt context. |
| 19 | * So we have to disable interrupts when taking the lock. |
| 20 | * |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 21 | * The ->count variable defines how many more tasks can acquire the |
| 22 | * semaphore. If it's zero, there may be tasks waiting on the list. |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 23 | */ |
| 24 | |
| 25 | static noinline void __down(struct semaphore *sem); |
| 26 | static noinline int __down_interruptible(struct semaphore *sem); |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 27 | static noinline int __down_killable(struct semaphore *sem); |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 28 | static noinline int __down_timeout(struct semaphore *sem, long jiffies); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 29 | static noinline void __up(struct semaphore *sem); |
| 30 | |
| 31 | void down(struct semaphore *sem) |
| 32 | { |
| 33 | unsigned long flags; |
| 34 | |
| 35 | spin_lock_irqsave(&sem->lock, flags); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 36 | if (likely(sem->count > 0)) |
| 37 | sem->count--; |
| 38 | else |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 39 | __down(sem); |
| 40 | spin_unlock_irqrestore(&sem->lock, flags); |
| 41 | } |
| 42 | EXPORT_SYMBOL(down); |
| 43 | |
| 44 | int down_interruptible(struct semaphore *sem) |
| 45 | { |
| 46 | unsigned long flags; |
| 47 | int result = 0; |
| 48 | |
| 49 | spin_lock_irqsave(&sem->lock, flags); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 50 | if (likely(sem->count > 0)) |
| 51 | sem->count--; |
| 52 | else |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 53 | result = __down_interruptible(sem); |
| 54 | spin_unlock_irqrestore(&sem->lock, flags); |
| 55 | |
| 56 | return result; |
| 57 | } |
| 58 | EXPORT_SYMBOL(down_interruptible); |
| 59 | |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 60 | int down_killable(struct semaphore *sem) |
| 61 | { |
| 62 | unsigned long flags; |
| 63 | int result = 0; |
| 64 | |
| 65 | spin_lock_irqsave(&sem->lock, flags); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 66 | if (likely(sem->count > 0)) |
| 67 | sem->count--; |
| 68 | else |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 69 | result = __down_killable(sem); |
| 70 | spin_unlock_irqrestore(&sem->lock, flags); |
| 71 | |
| 72 | return result; |
| 73 | } |
| 74 | EXPORT_SYMBOL(down_killable); |
| 75 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 76 | /** |
| 77 | * down_trylock - try to acquire the semaphore, without waiting |
| 78 | * @sem: the semaphore to be acquired |
| 79 | * |
| 80 | * Try to acquire the semaphore atomically. Returns 0 if the mutex has |
| 81 | * been acquired successfully and 1 if it is contended. |
| 82 | * |
| 83 | * NOTE: This return value is inverted from both spin_trylock and |
| 84 | * mutex_trylock! Be careful about this when converting code. |
| 85 | * |
| 86 | * Unlike mutex_trylock, this function can be used from interrupt context, |
| 87 | * and the semaphore can be released by any task or interrupt. |
| 88 | */ |
| 89 | int down_trylock(struct semaphore *sem) |
| 90 | { |
| 91 | unsigned long flags; |
| 92 | int count; |
| 93 | |
| 94 | spin_lock_irqsave(&sem->lock, flags); |
| 95 | count = sem->count - 1; |
| 96 | if (likely(count >= 0)) |
| 97 | sem->count = count; |
| 98 | spin_unlock_irqrestore(&sem->lock, flags); |
| 99 | |
| 100 | return (count < 0); |
| 101 | } |
| 102 | EXPORT_SYMBOL(down_trylock); |
| 103 | |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 104 | int down_timeout(struct semaphore *sem, long jiffies) |
| 105 | { |
| 106 | unsigned long flags; |
| 107 | int result = 0; |
| 108 | |
| 109 | spin_lock_irqsave(&sem->lock, flags); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 110 | if (likely(sem->count > 0)) |
| 111 | sem->count--; |
| 112 | else |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 113 | result = __down_timeout(sem, jiffies); |
| 114 | spin_unlock_irqrestore(&sem->lock, flags); |
| 115 | |
| 116 | return result; |
| 117 | } |
| 118 | EXPORT_SYMBOL(down_timeout); |
| 119 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 120 | void up(struct semaphore *sem) |
| 121 | { |
| 122 | unsigned long flags; |
| 123 | |
| 124 | spin_lock_irqsave(&sem->lock, flags); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 125 | if (likely(list_empty(&sem->wait_list))) |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 126 | sem->count++; |
| 127 | else |
| 128 | __up(sem); |
| 129 | spin_unlock_irqrestore(&sem->lock, flags); |
| 130 | } |
| 131 | EXPORT_SYMBOL(up); |
| 132 | |
| 133 | /* Functions for the contended case */ |
| 134 | |
| 135 | struct semaphore_waiter { |
| 136 | struct list_head list; |
| 137 | struct task_struct *task; |
| 138 | int up; |
| 139 | }; |
| 140 | |
| 141 | /* |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 142 | * Because this function is inlined, the 'state' parameter will be |
| 143 | * constant, and thus optimised away by the compiler. Likewise the |
| 144 | * 'timeout' parameter for the cases without timeouts. |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 145 | */ |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 146 | static inline int __sched __down_common(struct semaphore *sem, long state, |
| 147 | long timeout) |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 148 | { |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 149 | struct task_struct *task = current; |
| 150 | struct semaphore_waiter waiter; |
| 151 | |
| 152 | list_add_tail(&waiter.list, &sem->wait_list); |
| 153 | waiter.task = task; |
| 154 | waiter.up = 0; |
| 155 | |
| 156 | for (;;) { |
| 157 | if (state == TASK_INTERRUPTIBLE && signal_pending(task)) |
| 158 | goto interrupted; |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 159 | if (state == TASK_KILLABLE && fatal_signal_pending(task)) |
| 160 | goto interrupted; |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 161 | if (timeout <= 0) |
| 162 | goto timed_out; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 163 | __set_task_state(task, state); |
| 164 | spin_unlock_irq(&sem->lock); |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 165 | timeout = schedule_timeout(timeout); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 166 | spin_lock_irq(&sem->lock); |
| 167 | if (waiter.up) |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 168 | return 0; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 169 | } |
| 170 | |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 171 | timed_out: |
| 172 | list_del(&waiter.list); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 173 | return -ETIME; |
| 174 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 175 | interrupted: |
| 176 | list_del(&waiter.list); |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 177 | return -EINTR; |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static noinline void __sched __down(struct semaphore *sem) |
| 181 | { |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 182 | __down_common(sem, TASK_UNINTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | static noinline int __sched __down_interruptible(struct semaphore *sem) |
| 186 | { |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 187 | return __down_common(sem, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 188 | } |
| 189 | |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 190 | static noinline int __sched __down_killable(struct semaphore *sem) |
| 191 | { |
Matthew Wilcox | f1241c8 | 2008-03-14 13:43:13 -0400 | [diff] [blame] | 192 | return __down_common(sem, TASK_KILLABLE, MAX_SCHEDULE_TIMEOUT); |
| 193 | } |
| 194 | |
| 195 | static noinline int __sched __down_timeout(struct semaphore *sem, long jiffies) |
| 196 | { |
| 197 | return __down_common(sem, TASK_UNINTERRUPTIBLE, jiffies); |
Matthew Wilcox | f06d968 | 2008-03-14 13:19:33 -0400 | [diff] [blame] | 198 | } |
| 199 | |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 200 | static noinline void __sched __up(struct semaphore *sem) |
| 201 | { |
Matthew Wilcox | b17170b | 2008-03-14 14:35:22 -0400 | [diff] [blame^] | 202 | struct semaphore_waiter *waiter = list_first_entry(&sem->wait_list, |
| 203 | struct semaphore_waiter, list); |
| 204 | list_del(&waiter->list); |
| 205 | waiter->up = 1; |
| 206 | wake_up_process(waiter->task); |
Matthew Wilcox | 64ac24e | 2008-03-07 21:55:58 -0500 | [diff] [blame] | 207 | } |