Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 1 | /* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved. |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 2 | * |
| 3 | * This program is free software; you can redistribute it and/or modify |
| 4 | * it under the terms of the GNU General Public License version 2 and |
| 5 | * only version 2 as published by the Free Software Foundation. |
| 6 | * |
| 7 | * This program is distributed in the hope that it will be useful, |
| 8 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 9 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 10 | * GNU General Public License for more details. |
| 11 | */ |
| 12 | |
| 13 | #include <linux/fb.h> |
| 14 | #include <linux/slab.h> |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/list.h> |
| 17 | #include <linux/file.h> |
| 18 | #include <linux/sched.h> |
| 19 | #include <linux/fs.h> |
| 20 | #include <linux/wait.h> |
| 21 | #include <linux/uaccess.h> |
| 22 | #include <linux/anon_inodes.h> |
| 23 | #include <linux/miscdevice.h> |
| 24 | #include <linux/genlock.h> |
| 25 | |
| 26 | /* Lock states - can either be unlocked, held as an exclusive write lock or a |
| 27 | * shared read lock |
| 28 | */ |
| 29 | |
| 30 | #define _UNLOCKED 0 |
| 31 | #define _RDLOCK GENLOCK_RDLOCK |
| 32 | #define _WRLOCK GENLOCK_WRLOCK |
| 33 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 34 | #define GENLOCK_LOG_ERR(fmt, args...) \ |
| 35 | pr_err("genlock: %s: " fmt, __func__, ##args) |
| 36 | |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 37 | struct genlock { |
| 38 | struct list_head active; /* List of handles holding lock */ |
| 39 | spinlock_t lock; /* Spinlock to protect the lock internals */ |
| 40 | wait_queue_head_t queue; /* Holding pen for processes pending lock */ |
| 41 | struct file *file; /* File structure for exported lock */ |
| 42 | int state; /* Current state of the lock */ |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 43 | struct kref refcount; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | struct genlock_handle { |
| 47 | struct genlock *lock; /* Lock currently attached to the handle */ |
| 48 | struct list_head entry; /* List node for attaching to a lock */ |
| 49 | struct file *file; /* File structure associated with handle */ |
| 50 | int active; /* Number of times the active lock has been |
| 51 | taken */ |
| 52 | }; |
| 53 | |
Jordan Crouse | 03fbfbc | 2011-12-16 14:28:28 -0700 | [diff] [blame] | 54 | /* |
| 55 | * Create a spinlock to protect against a race condition when a lock gets |
| 56 | * released while another process tries to attach it |
| 57 | */ |
| 58 | |
| 59 | static DEFINE_SPINLOCK(genlock_file_lock); |
| 60 | |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 61 | static void genlock_destroy(struct kref *kref) |
| 62 | { |
| 63 | struct genlock *lock = container_of(kref, struct genlock, |
| 64 | refcount); |
| 65 | |
Jordan Crouse | 03fbfbc | 2011-12-16 14:28:28 -0700 | [diff] [blame] | 66 | /* |
| 67 | * Clear the private data for the file descriptor in case the fd is |
| 68 | * still active after the lock gets released |
| 69 | */ |
| 70 | |
| 71 | spin_lock(&genlock_file_lock); |
| 72 | if (lock->file) |
| 73 | lock->file->private_data = NULL; |
| 74 | spin_unlock(&genlock_file_lock); |
| 75 | |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 76 | kfree(lock); |
| 77 | } |
| 78 | |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 79 | /* |
| 80 | * Release the genlock object. Called when all the references to |
| 81 | * the genlock file descriptor are released |
| 82 | */ |
| 83 | |
| 84 | static int genlock_release(struct inode *inodep, struct file *file) |
| 85 | { |
Jordan Crouse | 03fbfbc | 2011-12-16 14:28:28 -0700 | [diff] [blame] | 86 | struct genlock *lock = file->private_data; |
| 87 | /* |
| 88 | * Clear the refrence back to this file structure to avoid |
| 89 | * somehow reusing the lock after the file has been destroyed |
| 90 | */ |
| 91 | |
| 92 | if (lock) |
| 93 | lock->file = NULL; |
| 94 | |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static const struct file_operations genlock_fops = { |
| 99 | .release = genlock_release, |
| 100 | }; |
| 101 | |
| 102 | /** |
| 103 | * genlock_create_lock - Create a new lock |
| 104 | * @handle - genlock handle to attach the lock to |
| 105 | * |
| 106 | * Returns: a pointer to the genlock |
| 107 | */ |
| 108 | |
| 109 | struct genlock *genlock_create_lock(struct genlock_handle *handle) |
| 110 | { |
| 111 | struct genlock *lock; |
| 112 | |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 113 | if (IS_ERR_OR_NULL(handle)) { |
| 114 | GENLOCK_LOG_ERR("Invalid handle\n"); |
| 115 | return ERR_PTR(-EINVAL); |
| 116 | } |
| 117 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 118 | if (handle->lock != NULL) { |
| 119 | GENLOCK_LOG_ERR("Handle already has a lock attached\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 120 | return ERR_PTR(-EINVAL); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 121 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 122 | |
| 123 | lock = kzalloc(sizeof(*lock), GFP_KERNEL); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 124 | if (lock == NULL) { |
| 125 | GENLOCK_LOG_ERR("Unable to allocate memory for a lock\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 126 | return ERR_PTR(-ENOMEM); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 127 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 128 | |
| 129 | INIT_LIST_HEAD(&lock->active); |
| 130 | init_waitqueue_head(&lock->queue); |
| 131 | spin_lock_init(&lock->lock); |
| 132 | |
| 133 | lock->state = _UNLOCKED; |
| 134 | |
| 135 | /* |
| 136 | * Create an anonyonmous inode for the object that can exported to |
| 137 | * other processes |
| 138 | */ |
| 139 | |
| 140 | lock->file = anon_inode_getfile("genlock", &genlock_fops, |
| 141 | lock, O_RDWR); |
| 142 | |
| 143 | /* Attach the new lock to the handle */ |
| 144 | handle->lock = lock; |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 145 | kref_init(&lock->refcount); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 146 | |
| 147 | return lock; |
| 148 | } |
| 149 | EXPORT_SYMBOL(genlock_create_lock); |
| 150 | |
| 151 | /* |
| 152 | * Get a file descriptor reference to a lock suitable for sharing with |
| 153 | * other processes |
| 154 | */ |
| 155 | |
| 156 | static int genlock_get_fd(struct genlock *lock) |
| 157 | { |
| 158 | int ret; |
| 159 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 160 | if (!lock->file) { |
| 161 | GENLOCK_LOG_ERR("No file attached to the lock\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 162 | return -EINVAL; |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 163 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 164 | |
| 165 | ret = get_unused_fd_flags(0); |
| 166 | if (ret < 0) |
| 167 | return ret; |
| 168 | fd_install(ret, lock->file); |
| 169 | return ret; |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * genlock_attach_lock - Attach an existing lock to a handle |
| 174 | * @handle - Pointer to a genlock handle to attach the lock to |
| 175 | * @fd - file descriptor for the exported lock |
| 176 | * |
| 177 | * Returns: A pointer to the attached lock structure |
| 178 | */ |
| 179 | |
| 180 | struct genlock *genlock_attach_lock(struct genlock_handle *handle, int fd) |
| 181 | { |
| 182 | struct file *file; |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 183 | struct genlock *lock; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 184 | |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 185 | if (IS_ERR_OR_NULL(handle)) { |
| 186 | GENLOCK_LOG_ERR("Invalid handle\n"); |
| 187 | return ERR_PTR(-EINVAL); |
| 188 | } |
| 189 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 190 | if (handle->lock != NULL) { |
| 191 | GENLOCK_LOG_ERR("Handle already has a lock attached\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 192 | return ERR_PTR(-EINVAL); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 193 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 194 | |
| 195 | file = fget(fd); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 196 | if (file == NULL) { |
| 197 | GENLOCK_LOG_ERR("Bad file descriptor\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 198 | return ERR_PTR(-EBADF); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 199 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 200 | |
Jordan Crouse | 03fbfbc | 2011-12-16 14:28:28 -0700 | [diff] [blame] | 201 | /* |
| 202 | * take a spinlock to avoid a race condition if the lock is |
| 203 | * released and then attached |
| 204 | */ |
| 205 | |
| 206 | spin_lock(&genlock_file_lock); |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 207 | lock = file->private_data; |
Jordan Crouse | 03fbfbc | 2011-12-16 14:28:28 -0700 | [diff] [blame] | 208 | spin_unlock(&genlock_file_lock); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 209 | |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 210 | fput(file); |
| 211 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 212 | if (lock == NULL) { |
| 213 | GENLOCK_LOG_ERR("File descriptor is invalid\n"); |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 214 | return ERR_PTR(-EINVAL); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 215 | } |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 216 | |
| 217 | handle->lock = lock; |
| 218 | kref_get(&lock->refcount); |
| 219 | |
| 220 | return lock; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 221 | } |
| 222 | EXPORT_SYMBOL(genlock_attach_lock); |
| 223 | |
| 224 | /* Helper function that returns 1 if the specified handle holds the lock */ |
| 225 | |
| 226 | static int handle_has_lock(struct genlock *lock, struct genlock_handle *handle) |
| 227 | { |
| 228 | struct genlock_handle *h; |
| 229 | |
| 230 | list_for_each_entry(h, &lock->active, entry) { |
| 231 | if (h == handle) |
| 232 | return 1; |
| 233 | } |
| 234 | |
| 235 | return 0; |
| 236 | } |
| 237 | |
| 238 | /* If the lock just became available, signal the next entity waiting for it */ |
| 239 | |
| 240 | static void _genlock_signal(struct genlock *lock) |
| 241 | { |
| 242 | if (list_empty(&lock->active)) { |
| 243 | /* If the list is empty, then the lock is free */ |
| 244 | lock->state = _UNLOCKED; |
| 245 | /* Wake up the first process sitting in the queue */ |
| 246 | wake_up(&lock->queue); |
| 247 | } |
| 248 | } |
| 249 | |
| 250 | /* Attempt to release the handle's ownership of the lock */ |
| 251 | |
| 252 | static int _genlock_unlock(struct genlock *lock, struct genlock_handle *handle) |
| 253 | { |
| 254 | int ret = -EINVAL; |
| 255 | unsigned long irqflags; |
| 256 | |
| 257 | spin_lock_irqsave(&lock->lock, irqflags); |
| 258 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 259 | if (lock->state == _UNLOCKED) { |
| 260 | GENLOCK_LOG_ERR("Trying to unlock an unlocked handle\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 261 | goto done; |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 262 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 263 | |
| 264 | /* Make sure this handle is an owner of the lock */ |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 265 | if (!handle_has_lock(lock, handle)) { |
| 266 | GENLOCK_LOG_ERR("handle does not have lock attached to it\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 267 | goto done; |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 268 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 269 | /* If the handle holds no more references to the lock then |
| 270 | release it (maybe) */ |
| 271 | |
| 272 | if (--handle->active == 0) { |
| 273 | list_del(&handle->entry); |
| 274 | _genlock_signal(lock); |
| 275 | } |
| 276 | |
| 277 | ret = 0; |
| 278 | |
| 279 | done: |
| 280 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | /* Attempt to acquire the lock for the handle */ |
| 285 | |
| 286 | static int _genlock_lock(struct genlock *lock, struct genlock_handle *handle, |
| 287 | int op, int flags, uint32_t timeout) |
| 288 | { |
| 289 | unsigned long irqflags; |
| 290 | int ret = 0; |
Jordan Crouse | be669bc | 2012-03-20 14:09:29 -0600 | [diff] [blame^] | 291 | unsigned long ticks = msecs_to_jiffies(timeout); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 292 | |
| 293 | spin_lock_irqsave(&lock->lock, irqflags); |
| 294 | |
| 295 | /* Sanity check - no blocking locks in a debug context. Even if it |
| 296 | * succeed to not block, the mere idea is too dangerous to continue |
| 297 | */ |
| 298 | |
| 299 | if (in_interrupt() && !(flags & GENLOCK_NOBLOCK)) |
| 300 | BUG(); |
| 301 | |
| 302 | /* Fast path - the lock is unlocked, so go do the needful */ |
| 303 | |
| 304 | if (lock->state == _UNLOCKED) |
| 305 | goto dolock; |
| 306 | |
| 307 | if (handle_has_lock(lock, handle)) { |
| 308 | |
| 309 | /* |
| 310 | * If the handle already holds the lock and the type matches, |
| 311 | * then just increment the active pointer. This allows the |
| 312 | * handle to do recursive locks |
| 313 | */ |
| 314 | |
| 315 | if (lock->state == op) { |
| 316 | handle->active++; |
| 317 | goto done; |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * If the handle holds a write lock then the owner can switch |
| 322 | * to a read lock if they want. Do the transition atomically |
| 323 | * then wake up any pending waiters in case they want a read |
| 324 | * lock too. |
| 325 | */ |
| 326 | |
| 327 | if (op == _RDLOCK && handle->active == 1) { |
| 328 | lock->state = _RDLOCK; |
| 329 | wake_up(&lock->queue); |
| 330 | goto done; |
| 331 | } |
| 332 | |
| 333 | /* |
| 334 | * Otherwise the user tried to turn a read into a write, and we |
| 335 | * don't allow that. |
| 336 | */ |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 337 | GENLOCK_LOG_ERR("Trying to upgrade a read lock to a write" |
| 338 | "lock\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 339 | ret = -EINVAL; |
| 340 | goto done; |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * If we request a read and the lock is held by a read, then go |
| 345 | * ahead and share the lock |
| 346 | */ |
| 347 | |
| 348 | if (op == GENLOCK_RDLOCK && lock->state == _RDLOCK) |
| 349 | goto dolock; |
| 350 | |
| 351 | /* Treat timeout 0 just like a NOBLOCK flag and return if the |
| 352 | lock cannot be aquired without blocking */ |
| 353 | |
| 354 | if (flags & GENLOCK_NOBLOCK || timeout == 0) { |
| 355 | ret = -EAGAIN; |
| 356 | goto done; |
| 357 | } |
| 358 | |
| 359 | /* Wait while the lock remains in an incompatible state */ |
| 360 | |
| 361 | while (lock->state != _UNLOCKED) { |
Jordan Crouse | be669bc | 2012-03-20 14:09:29 -0600 | [diff] [blame^] | 362 | signed long elapsed; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 363 | |
| 364 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 365 | |
| 366 | elapsed = wait_event_interruptible_timeout(lock->queue, |
| 367 | lock->state == _UNLOCKED, ticks); |
| 368 | |
| 369 | spin_lock_irqsave(&lock->lock, irqflags); |
| 370 | |
| 371 | if (elapsed <= 0) { |
| 372 | ret = (elapsed < 0) ? elapsed : -ETIMEDOUT; |
| 373 | goto done; |
| 374 | } |
| 375 | |
Jordan Crouse | be669bc | 2012-03-20 14:09:29 -0600 | [diff] [blame^] | 376 | ticks = (unsigned long) elapsed; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 377 | } |
| 378 | |
| 379 | dolock: |
| 380 | /* We can now get the lock, add ourselves to the list of owners */ |
| 381 | |
| 382 | list_add_tail(&handle->entry, &lock->active); |
| 383 | lock->state = op; |
| 384 | handle->active = 1; |
| 385 | |
| 386 | done: |
| 387 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 388 | return ret; |
| 389 | |
| 390 | } |
| 391 | |
| 392 | /** |
| 393 | * genlock_lock - Acquire or release a lock |
| 394 | * @handle - pointer to the genlock handle that is requesting the lock |
| 395 | * @op - the operation to perform (RDLOCK, WRLOCK, UNLOCK) |
| 396 | * @flags - flags to control the operation |
| 397 | * @timeout - optional timeout to wait for the lock to come free |
| 398 | * |
| 399 | * Returns: 0 on success or error code on failure |
| 400 | */ |
| 401 | |
| 402 | int genlock_lock(struct genlock_handle *handle, int op, int flags, |
| 403 | uint32_t timeout) |
| 404 | { |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 405 | struct genlock *lock; |
| 406 | |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 407 | int ret = 0; |
| 408 | |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 409 | if (IS_ERR_OR_NULL(handle)) { |
| 410 | GENLOCK_LOG_ERR("Invalid handle\n"); |
| 411 | return -EINVAL; |
| 412 | } |
| 413 | |
| 414 | lock = handle->lock; |
| 415 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 416 | if (lock == NULL) { |
| 417 | GENLOCK_LOG_ERR("Handle does not have a lock attached\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 418 | return -EINVAL; |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 419 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 420 | |
| 421 | switch (op) { |
| 422 | case GENLOCK_UNLOCK: |
| 423 | ret = _genlock_unlock(lock, handle); |
| 424 | break; |
| 425 | case GENLOCK_RDLOCK: |
| 426 | case GENLOCK_WRLOCK: |
| 427 | ret = _genlock_lock(lock, handle, op, flags, timeout); |
| 428 | break; |
| 429 | default: |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 430 | GENLOCK_LOG_ERR("Invalid lock operation\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 431 | ret = -EINVAL; |
| 432 | break; |
| 433 | } |
| 434 | |
| 435 | return ret; |
| 436 | } |
| 437 | EXPORT_SYMBOL(genlock_lock); |
| 438 | |
| 439 | /** |
| 440 | * genlock_wait - Wait for the lock to be released |
| 441 | * @handle - pointer to the genlock handle that is waiting for the lock |
| 442 | * @timeout - optional timeout to wait for the lock to get released |
| 443 | */ |
| 444 | |
| 445 | int genlock_wait(struct genlock_handle *handle, uint32_t timeout) |
| 446 | { |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 447 | struct genlock *lock; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 448 | unsigned long irqflags; |
| 449 | int ret = 0; |
Jordan Crouse | be669bc | 2012-03-20 14:09:29 -0600 | [diff] [blame^] | 450 | unsigned long ticks = msecs_to_jiffies(timeout); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 451 | |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 452 | if (IS_ERR_OR_NULL(handle)) { |
| 453 | GENLOCK_LOG_ERR("Invalid handle\n"); |
| 454 | return -EINVAL; |
| 455 | } |
| 456 | |
| 457 | lock = handle->lock; |
| 458 | |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 459 | if (lock == NULL) { |
| 460 | GENLOCK_LOG_ERR("Handle does not have a lock attached\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 461 | return -EINVAL; |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 462 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 463 | |
| 464 | spin_lock_irqsave(&lock->lock, irqflags); |
| 465 | |
| 466 | /* |
| 467 | * if timeout is 0 and the lock is already unlocked, then success |
| 468 | * otherwise return -EAGAIN |
| 469 | */ |
| 470 | |
| 471 | if (timeout == 0) { |
| 472 | ret = (lock->state == _UNLOCKED) ? 0 : -EAGAIN; |
| 473 | goto done; |
| 474 | } |
| 475 | |
| 476 | while (lock->state != _UNLOCKED) { |
Jordan Crouse | be669bc | 2012-03-20 14:09:29 -0600 | [diff] [blame^] | 477 | signed long elapsed; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 478 | |
| 479 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 480 | |
| 481 | elapsed = wait_event_interruptible_timeout(lock->queue, |
| 482 | lock->state == _UNLOCKED, ticks); |
| 483 | |
| 484 | spin_lock_irqsave(&lock->lock, irqflags); |
| 485 | |
| 486 | if (elapsed <= 0) { |
| 487 | ret = (elapsed < 0) ? elapsed : -ETIMEDOUT; |
| 488 | break; |
| 489 | } |
| 490 | |
Jordan Crouse | be669bc | 2012-03-20 14:09:29 -0600 | [diff] [blame^] | 491 | ticks = (unsigned long) elapsed; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 492 | } |
| 493 | |
| 494 | done: |
| 495 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 496 | return ret; |
| 497 | } |
| 498 | |
Jordan Crouse | 4df70a2 | 2012-01-25 14:40:51 -0700 | [diff] [blame] | 499 | static void genlock_release_lock(struct genlock_handle *handle) |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 500 | { |
| 501 | unsigned long flags; |
| 502 | |
| 503 | if (handle == NULL || handle->lock == NULL) |
| 504 | return; |
| 505 | |
| 506 | spin_lock_irqsave(&handle->lock->lock, flags); |
| 507 | |
| 508 | /* If the handle is holding the lock, then force it closed */ |
| 509 | |
| 510 | if (handle_has_lock(handle->lock, handle)) { |
| 511 | list_del(&handle->entry); |
| 512 | _genlock_signal(handle->lock); |
| 513 | } |
| 514 | spin_unlock_irqrestore(&handle->lock->lock, flags); |
| 515 | |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 516 | kref_put(&handle->lock->refcount, genlock_destroy); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 517 | handle->lock = NULL; |
| 518 | handle->active = 0; |
| 519 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 520 | |
| 521 | /* |
| 522 | * Release function called when all references to a handle are released |
| 523 | */ |
| 524 | |
| 525 | static int genlock_handle_release(struct inode *inodep, struct file *file) |
| 526 | { |
| 527 | struct genlock_handle *handle = file->private_data; |
| 528 | |
| 529 | genlock_release_lock(handle); |
| 530 | kfree(handle); |
| 531 | |
| 532 | return 0; |
| 533 | } |
| 534 | |
| 535 | static const struct file_operations genlock_handle_fops = { |
| 536 | .release = genlock_handle_release |
| 537 | }; |
| 538 | |
| 539 | /* |
| 540 | * Allocate a new genlock handle |
| 541 | */ |
| 542 | |
| 543 | static struct genlock_handle *_genlock_get_handle(void) |
| 544 | { |
| 545 | struct genlock_handle *handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 546 | if (handle == NULL) { |
| 547 | GENLOCK_LOG_ERR("Unable to allocate memory for the handle\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 548 | return ERR_PTR(-ENOMEM); |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 549 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 550 | |
| 551 | return handle; |
| 552 | } |
| 553 | |
| 554 | /** |
| 555 | * genlock_get_handle - Create a new genlock handle |
| 556 | * |
| 557 | * Returns: A pointer to a new genlock handle |
| 558 | */ |
| 559 | |
| 560 | struct genlock_handle *genlock_get_handle(void) |
| 561 | { |
| 562 | struct genlock_handle *handle = _genlock_get_handle(); |
| 563 | if (IS_ERR(handle)) |
| 564 | return handle; |
| 565 | |
| 566 | handle->file = anon_inode_getfile("genlock-handle", |
| 567 | &genlock_handle_fops, handle, O_RDWR); |
| 568 | |
| 569 | return handle; |
| 570 | } |
| 571 | EXPORT_SYMBOL(genlock_get_handle); |
| 572 | |
| 573 | /** |
| 574 | * genlock_put_handle - release a reference to a genlock handle |
| 575 | * @handle - A pointer to the handle to release |
| 576 | */ |
| 577 | |
| 578 | void genlock_put_handle(struct genlock_handle *handle) |
| 579 | { |
| 580 | if (handle) |
| 581 | fput(handle->file); |
| 582 | } |
| 583 | EXPORT_SYMBOL(genlock_put_handle); |
| 584 | |
| 585 | /** |
| 586 | * genlock_get_handle_fd - Get a handle reference from a file descriptor |
| 587 | * @fd - The file descriptor for a genlock handle |
| 588 | */ |
| 589 | |
| 590 | struct genlock_handle *genlock_get_handle_fd(int fd) |
| 591 | { |
| 592 | struct file *file = fget(fd); |
| 593 | |
| 594 | if (file == NULL) |
| 595 | return ERR_PTR(-EINVAL); |
| 596 | |
| 597 | return file->private_data; |
| 598 | } |
| 599 | EXPORT_SYMBOL(genlock_get_handle_fd); |
| 600 | |
| 601 | #ifdef CONFIG_GENLOCK_MISCDEVICE |
| 602 | |
| 603 | static long genlock_dev_ioctl(struct file *filep, unsigned int cmd, |
| 604 | unsigned long arg) |
| 605 | { |
| 606 | struct genlock_lock param; |
| 607 | struct genlock_handle *handle = filep->private_data; |
| 608 | struct genlock *lock; |
| 609 | int ret; |
| 610 | |
Jordan Crouse | a73ed09 | 2012-01-24 15:40:22 -0700 | [diff] [blame] | 611 | if (IS_ERR_OR_NULL(handle)) |
| 612 | return -EINVAL; |
| 613 | |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 614 | switch (cmd) { |
| 615 | case GENLOCK_IOC_NEW: { |
| 616 | lock = genlock_create_lock(handle); |
| 617 | if (IS_ERR(lock)) |
| 618 | return PTR_ERR(lock); |
| 619 | |
| 620 | return 0; |
| 621 | } |
| 622 | case GENLOCK_IOC_EXPORT: { |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 623 | if (handle->lock == NULL) { |
| 624 | GENLOCK_LOG_ERR("Handle does not have a lock" |
| 625 | "attached\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 626 | return -EINVAL; |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 627 | } |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 628 | |
| 629 | ret = genlock_get_fd(handle->lock); |
| 630 | if (ret < 0) |
| 631 | return ret; |
| 632 | |
| 633 | param.fd = ret; |
| 634 | |
| 635 | if (copy_to_user((void __user *) arg, ¶m, |
| 636 | sizeof(param))) |
| 637 | return -EFAULT; |
| 638 | |
| 639 | return 0; |
| 640 | } |
| 641 | case GENLOCK_IOC_ATTACH: { |
| 642 | if (copy_from_user(¶m, (void __user *) arg, |
| 643 | sizeof(param))) |
| 644 | return -EFAULT; |
| 645 | |
| 646 | lock = genlock_attach_lock(handle, param.fd); |
| 647 | if (IS_ERR(lock)) |
| 648 | return PTR_ERR(lock); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | case GENLOCK_IOC_LOCK: { |
| 653 | if (copy_from_user(¶m, (void __user *) arg, |
| 654 | sizeof(param))) |
| 655 | return -EFAULT; |
| 656 | |
| 657 | return genlock_lock(handle, param.op, param.flags, |
| 658 | param.timeout); |
| 659 | } |
| 660 | case GENLOCK_IOC_WAIT: { |
| 661 | if (copy_from_user(¶m, (void __user *) arg, |
| 662 | sizeof(param))) |
| 663 | return -EFAULT; |
| 664 | |
| 665 | return genlock_wait(handle, param.timeout); |
| 666 | } |
| 667 | case GENLOCK_IOC_RELEASE: { |
Jordan Crouse | 4df70a2 | 2012-01-25 14:40:51 -0700 | [diff] [blame] | 668 | /* |
| 669 | * Return error - this ioctl has been deprecated. |
| 670 | * Locks should only be released when the handle is |
| 671 | * destroyed |
| 672 | */ |
| 673 | GENLOCK_LOG_ERR("Deprecated RELEASE ioctl called\n"); |
| 674 | return -EINVAL; |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 675 | } |
| 676 | default: |
Naomi Luis | b558aed | 2011-12-12 12:07:48 -0800 | [diff] [blame] | 677 | GENLOCK_LOG_ERR("Invalid ioctl\n"); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 678 | return -EINVAL; |
| 679 | } |
| 680 | } |
| 681 | |
| 682 | static int genlock_dev_release(struct inode *inodep, struct file *file) |
| 683 | { |
| 684 | struct genlock_handle *handle = file->private_data; |
| 685 | |
Jordan Crouse | 4a2879b | 2011-12-01 11:52:59 -0700 | [diff] [blame] | 686 | genlock_release_lock(handle); |
| 687 | kfree(handle); |
Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame] | 688 | |
| 689 | return 0; |
| 690 | } |
| 691 | |
| 692 | static int genlock_dev_open(struct inode *inodep, struct file *file) |
| 693 | { |
| 694 | struct genlock_handle *handle = _genlock_get_handle(); |
| 695 | if (IS_ERR(handle)) |
| 696 | return PTR_ERR(handle); |
| 697 | |
| 698 | handle->file = file; |
| 699 | file->private_data = handle; |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static const struct file_operations genlock_dev_fops = { |
| 704 | .open = genlock_dev_open, |
| 705 | .release = genlock_dev_release, |
| 706 | .unlocked_ioctl = genlock_dev_ioctl, |
| 707 | }; |
| 708 | |
| 709 | static struct miscdevice genlock_dev; |
| 710 | |
| 711 | static int genlock_dev_init(void) |
| 712 | { |
| 713 | genlock_dev.minor = MISC_DYNAMIC_MINOR; |
| 714 | genlock_dev.name = "genlock"; |
| 715 | genlock_dev.fops = &genlock_dev_fops; |
| 716 | genlock_dev.parent = NULL; |
| 717 | |
| 718 | return misc_register(&genlock_dev); |
| 719 | } |
| 720 | |
| 721 | static void genlock_dev_close(void) |
| 722 | { |
| 723 | misc_deregister(&genlock_dev); |
| 724 | } |
| 725 | |
| 726 | module_init(genlock_dev_init); |
| 727 | module_exit(genlock_dev_close); |
| 728 | |
| 729 | #endif |