Jordan Crouse | 29f66af | 2011-11-17 13:39:20 -0700 | [diff] [blame^] | 1 | /* Copyright (c) 2011, Code Aurora Forum. All rights reserved. |
| 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 | |
| 34 | struct genlock { |
| 35 | struct list_head active; /* List of handles holding lock */ |
| 36 | spinlock_t lock; /* Spinlock to protect the lock internals */ |
| 37 | wait_queue_head_t queue; /* Holding pen for processes pending lock */ |
| 38 | struct file *file; /* File structure for exported lock */ |
| 39 | int state; /* Current state of the lock */ |
| 40 | }; |
| 41 | |
| 42 | struct genlock_handle { |
| 43 | struct genlock *lock; /* Lock currently attached to the handle */ |
| 44 | struct list_head entry; /* List node for attaching to a lock */ |
| 45 | struct file *file; /* File structure associated with handle */ |
| 46 | int active; /* Number of times the active lock has been |
| 47 | taken */ |
| 48 | }; |
| 49 | |
| 50 | /* |
| 51 | * Release the genlock object. Called when all the references to |
| 52 | * the genlock file descriptor are released |
| 53 | */ |
| 54 | |
| 55 | static int genlock_release(struct inode *inodep, struct file *file) |
| 56 | { |
| 57 | kfree(file->private_data); |
| 58 | return 0; |
| 59 | } |
| 60 | |
| 61 | static const struct file_operations genlock_fops = { |
| 62 | .release = genlock_release, |
| 63 | }; |
| 64 | |
| 65 | /** |
| 66 | * genlock_create_lock - Create a new lock |
| 67 | * @handle - genlock handle to attach the lock to |
| 68 | * |
| 69 | * Returns: a pointer to the genlock |
| 70 | */ |
| 71 | |
| 72 | struct genlock *genlock_create_lock(struct genlock_handle *handle) |
| 73 | { |
| 74 | struct genlock *lock; |
| 75 | |
| 76 | if (handle->lock != NULL) |
| 77 | return ERR_PTR(-EINVAL); |
| 78 | |
| 79 | lock = kzalloc(sizeof(*lock), GFP_KERNEL); |
| 80 | if (lock == NULL) |
| 81 | return ERR_PTR(-ENOMEM); |
| 82 | |
| 83 | INIT_LIST_HEAD(&lock->active); |
| 84 | init_waitqueue_head(&lock->queue); |
| 85 | spin_lock_init(&lock->lock); |
| 86 | |
| 87 | lock->state = _UNLOCKED; |
| 88 | |
| 89 | /* |
| 90 | * Create an anonyonmous inode for the object that can exported to |
| 91 | * other processes |
| 92 | */ |
| 93 | |
| 94 | lock->file = anon_inode_getfile("genlock", &genlock_fops, |
| 95 | lock, O_RDWR); |
| 96 | |
| 97 | /* Attach the new lock to the handle */ |
| 98 | handle->lock = lock; |
| 99 | |
| 100 | return lock; |
| 101 | } |
| 102 | EXPORT_SYMBOL(genlock_create_lock); |
| 103 | |
| 104 | /* |
| 105 | * Get a file descriptor reference to a lock suitable for sharing with |
| 106 | * other processes |
| 107 | */ |
| 108 | |
| 109 | static int genlock_get_fd(struct genlock *lock) |
| 110 | { |
| 111 | int ret; |
| 112 | |
| 113 | if (!lock->file) |
| 114 | return -EINVAL; |
| 115 | |
| 116 | ret = get_unused_fd_flags(0); |
| 117 | if (ret < 0) |
| 118 | return ret; |
| 119 | fd_install(ret, lock->file); |
| 120 | return ret; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * genlock_attach_lock - Attach an existing lock to a handle |
| 125 | * @handle - Pointer to a genlock handle to attach the lock to |
| 126 | * @fd - file descriptor for the exported lock |
| 127 | * |
| 128 | * Returns: A pointer to the attached lock structure |
| 129 | */ |
| 130 | |
| 131 | struct genlock *genlock_attach_lock(struct genlock_handle *handle, int fd) |
| 132 | { |
| 133 | struct file *file; |
| 134 | |
| 135 | if (handle->lock != NULL) |
| 136 | return ERR_PTR(-EINVAL); |
| 137 | |
| 138 | file = fget(fd); |
| 139 | if (file == NULL) |
| 140 | return ERR_PTR(-EBADF); |
| 141 | |
| 142 | handle->lock = file->private_data; |
| 143 | |
| 144 | return handle->lock; |
| 145 | } |
| 146 | EXPORT_SYMBOL(genlock_attach_lock); |
| 147 | |
| 148 | /* Helper function that returns 1 if the specified handle holds the lock */ |
| 149 | |
| 150 | static int handle_has_lock(struct genlock *lock, struct genlock_handle *handle) |
| 151 | { |
| 152 | struct genlock_handle *h; |
| 153 | |
| 154 | list_for_each_entry(h, &lock->active, entry) { |
| 155 | if (h == handle) |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | return 0; |
| 160 | } |
| 161 | |
| 162 | /* If the lock just became available, signal the next entity waiting for it */ |
| 163 | |
| 164 | static void _genlock_signal(struct genlock *lock) |
| 165 | { |
| 166 | if (list_empty(&lock->active)) { |
| 167 | /* If the list is empty, then the lock is free */ |
| 168 | lock->state = _UNLOCKED; |
| 169 | /* Wake up the first process sitting in the queue */ |
| 170 | wake_up(&lock->queue); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | /* Attempt to release the handle's ownership of the lock */ |
| 175 | |
| 176 | static int _genlock_unlock(struct genlock *lock, struct genlock_handle *handle) |
| 177 | { |
| 178 | int ret = -EINVAL; |
| 179 | unsigned long irqflags; |
| 180 | |
| 181 | spin_lock_irqsave(&lock->lock, irqflags); |
| 182 | |
| 183 | if (lock->state == _UNLOCKED) |
| 184 | goto done; |
| 185 | |
| 186 | /* Make sure this handle is an owner of the lock */ |
| 187 | if (!handle_has_lock(lock, handle)) |
| 188 | goto done; |
| 189 | |
| 190 | /* If the handle holds no more references to the lock then |
| 191 | release it (maybe) */ |
| 192 | |
| 193 | if (--handle->active == 0) { |
| 194 | list_del(&handle->entry); |
| 195 | _genlock_signal(lock); |
| 196 | } |
| 197 | |
| 198 | ret = 0; |
| 199 | |
| 200 | done: |
| 201 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 202 | return ret; |
| 203 | } |
| 204 | |
| 205 | /* Attempt to acquire the lock for the handle */ |
| 206 | |
| 207 | static int _genlock_lock(struct genlock *lock, struct genlock_handle *handle, |
| 208 | int op, int flags, uint32_t timeout) |
| 209 | { |
| 210 | unsigned long irqflags; |
| 211 | int ret = 0; |
| 212 | unsigned int ticks = msecs_to_jiffies(timeout); |
| 213 | |
| 214 | spin_lock_irqsave(&lock->lock, irqflags); |
| 215 | |
| 216 | /* Sanity check - no blocking locks in a debug context. Even if it |
| 217 | * succeed to not block, the mere idea is too dangerous to continue |
| 218 | */ |
| 219 | |
| 220 | if (in_interrupt() && !(flags & GENLOCK_NOBLOCK)) |
| 221 | BUG(); |
| 222 | |
| 223 | /* Fast path - the lock is unlocked, so go do the needful */ |
| 224 | |
| 225 | if (lock->state == _UNLOCKED) |
| 226 | goto dolock; |
| 227 | |
| 228 | if (handle_has_lock(lock, handle)) { |
| 229 | |
| 230 | /* |
| 231 | * If the handle already holds the lock and the type matches, |
| 232 | * then just increment the active pointer. This allows the |
| 233 | * handle to do recursive locks |
| 234 | */ |
| 235 | |
| 236 | if (lock->state == op) { |
| 237 | handle->active++; |
| 238 | goto done; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * If the handle holds a write lock then the owner can switch |
| 243 | * to a read lock if they want. Do the transition atomically |
| 244 | * then wake up any pending waiters in case they want a read |
| 245 | * lock too. |
| 246 | */ |
| 247 | |
| 248 | if (op == _RDLOCK && handle->active == 1) { |
| 249 | lock->state = _RDLOCK; |
| 250 | wake_up(&lock->queue); |
| 251 | goto done; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * Otherwise the user tried to turn a read into a write, and we |
| 256 | * don't allow that. |
| 257 | */ |
| 258 | |
| 259 | ret = -EINVAL; |
| 260 | goto done; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * If we request a read and the lock is held by a read, then go |
| 265 | * ahead and share the lock |
| 266 | */ |
| 267 | |
| 268 | if (op == GENLOCK_RDLOCK && lock->state == _RDLOCK) |
| 269 | goto dolock; |
| 270 | |
| 271 | /* Treat timeout 0 just like a NOBLOCK flag and return if the |
| 272 | lock cannot be aquired without blocking */ |
| 273 | |
| 274 | if (flags & GENLOCK_NOBLOCK || timeout == 0) { |
| 275 | ret = -EAGAIN; |
| 276 | goto done; |
| 277 | } |
| 278 | |
| 279 | /* Wait while the lock remains in an incompatible state */ |
| 280 | |
| 281 | while (lock->state != _UNLOCKED) { |
| 282 | unsigned int elapsed; |
| 283 | |
| 284 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 285 | |
| 286 | elapsed = wait_event_interruptible_timeout(lock->queue, |
| 287 | lock->state == _UNLOCKED, ticks); |
| 288 | |
| 289 | spin_lock_irqsave(&lock->lock, irqflags); |
| 290 | |
| 291 | if (elapsed <= 0) { |
| 292 | ret = (elapsed < 0) ? elapsed : -ETIMEDOUT; |
| 293 | goto done; |
| 294 | } |
| 295 | |
| 296 | ticks = elapsed; |
| 297 | } |
| 298 | |
| 299 | dolock: |
| 300 | /* We can now get the lock, add ourselves to the list of owners */ |
| 301 | |
| 302 | list_add_tail(&handle->entry, &lock->active); |
| 303 | lock->state = op; |
| 304 | handle->active = 1; |
| 305 | |
| 306 | done: |
| 307 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 308 | return ret; |
| 309 | |
| 310 | } |
| 311 | |
| 312 | /** |
| 313 | * genlock_lock - Acquire or release a lock |
| 314 | * @handle - pointer to the genlock handle that is requesting the lock |
| 315 | * @op - the operation to perform (RDLOCK, WRLOCK, UNLOCK) |
| 316 | * @flags - flags to control the operation |
| 317 | * @timeout - optional timeout to wait for the lock to come free |
| 318 | * |
| 319 | * Returns: 0 on success or error code on failure |
| 320 | */ |
| 321 | |
| 322 | int genlock_lock(struct genlock_handle *handle, int op, int flags, |
| 323 | uint32_t timeout) |
| 324 | { |
| 325 | struct genlock *lock = handle->lock; |
| 326 | int ret = 0; |
| 327 | |
| 328 | if (lock == NULL) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | switch (op) { |
| 332 | case GENLOCK_UNLOCK: |
| 333 | ret = _genlock_unlock(lock, handle); |
| 334 | break; |
| 335 | case GENLOCK_RDLOCK: |
| 336 | case GENLOCK_WRLOCK: |
| 337 | ret = _genlock_lock(lock, handle, op, flags, timeout); |
| 338 | break; |
| 339 | default: |
| 340 | ret = -EINVAL; |
| 341 | break; |
| 342 | } |
| 343 | |
| 344 | return ret; |
| 345 | } |
| 346 | EXPORT_SYMBOL(genlock_lock); |
| 347 | |
| 348 | /** |
| 349 | * genlock_wait - Wait for the lock to be released |
| 350 | * @handle - pointer to the genlock handle that is waiting for the lock |
| 351 | * @timeout - optional timeout to wait for the lock to get released |
| 352 | */ |
| 353 | |
| 354 | int genlock_wait(struct genlock_handle *handle, uint32_t timeout) |
| 355 | { |
| 356 | struct genlock *lock = handle->lock; |
| 357 | unsigned long irqflags; |
| 358 | int ret = 0; |
| 359 | unsigned int ticks = msecs_to_jiffies(timeout); |
| 360 | |
| 361 | if (lock == NULL) |
| 362 | return -EINVAL; |
| 363 | |
| 364 | spin_lock_irqsave(&lock->lock, irqflags); |
| 365 | |
| 366 | /* |
| 367 | * if timeout is 0 and the lock is already unlocked, then success |
| 368 | * otherwise return -EAGAIN |
| 369 | */ |
| 370 | |
| 371 | if (timeout == 0) { |
| 372 | ret = (lock->state == _UNLOCKED) ? 0 : -EAGAIN; |
| 373 | goto done; |
| 374 | } |
| 375 | |
| 376 | while (lock->state != _UNLOCKED) { |
| 377 | unsigned int elapsed; |
| 378 | |
| 379 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 380 | |
| 381 | elapsed = wait_event_interruptible_timeout(lock->queue, |
| 382 | lock->state == _UNLOCKED, ticks); |
| 383 | |
| 384 | spin_lock_irqsave(&lock->lock, irqflags); |
| 385 | |
| 386 | if (elapsed <= 0) { |
| 387 | ret = (elapsed < 0) ? elapsed : -ETIMEDOUT; |
| 388 | break; |
| 389 | } |
| 390 | |
| 391 | ticks = elapsed; |
| 392 | } |
| 393 | |
| 394 | done: |
| 395 | spin_unlock_irqrestore(&lock->lock, irqflags); |
| 396 | return ret; |
| 397 | } |
| 398 | |
| 399 | /** |
| 400 | * genlock_release_lock - Release a lock attached to a handle |
| 401 | * @handle - Pointer to the handle holding the lock |
| 402 | */ |
| 403 | |
| 404 | void genlock_release_lock(struct genlock_handle *handle) |
| 405 | { |
| 406 | unsigned long flags; |
| 407 | |
| 408 | if (handle == NULL || handle->lock == NULL) |
| 409 | return; |
| 410 | |
| 411 | spin_lock_irqsave(&handle->lock->lock, flags); |
| 412 | |
| 413 | /* If the handle is holding the lock, then force it closed */ |
| 414 | |
| 415 | if (handle_has_lock(handle->lock, handle)) { |
| 416 | list_del(&handle->entry); |
| 417 | _genlock_signal(handle->lock); |
| 418 | } |
| 419 | spin_unlock_irqrestore(&handle->lock->lock, flags); |
| 420 | |
| 421 | fput(handle->lock->file); |
| 422 | handle->lock = NULL; |
| 423 | handle->active = 0; |
| 424 | } |
| 425 | EXPORT_SYMBOL(genlock_release_lock); |
| 426 | |
| 427 | /* |
| 428 | * Release function called when all references to a handle are released |
| 429 | */ |
| 430 | |
| 431 | static int genlock_handle_release(struct inode *inodep, struct file *file) |
| 432 | { |
| 433 | struct genlock_handle *handle = file->private_data; |
| 434 | |
| 435 | genlock_release_lock(handle); |
| 436 | kfree(handle); |
| 437 | |
| 438 | return 0; |
| 439 | } |
| 440 | |
| 441 | static const struct file_operations genlock_handle_fops = { |
| 442 | .release = genlock_handle_release |
| 443 | }; |
| 444 | |
| 445 | /* |
| 446 | * Allocate a new genlock handle |
| 447 | */ |
| 448 | |
| 449 | static struct genlock_handle *_genlock_get_handle(void) |
| 450 | { |
| 451 | struct genlock_handle *handle = kzalloc(sizeof(*handle), GFP_KERNEL); |
| 452 | if (handle == NULL) |
| 453 | return ERR_PTR(-ENOMEM); |
| 454 | |
| 455 | return handle; |
| 456 | } |
| 457 | |
| 458 | /** |
| 459 | * genlock_get_handle - Create a new genlock handle |
| 460 | * |
| 461 | * Returns: A pointer to a new genlock handle |
| 462 | */ |
| 463 | |
| 464 | struct genlock_handle *genlock_get_handle(void) |
| 465 | { |
| 466 | struct genlock_handle *handle = _genlock_get_handle(); |
| 467 | if (IS_ERR(handle)) |
| 468 | return handle; |
| 469 | |
| 470 | handle->file = anon_inode_getfile("genlock-handle", |
| 471 | &genlock_handle_fops, handle, O_RDWR); |
| 472 | |
| 473 | return handle; |
| 474 | } |
| 475 | EXPORT_SYMBOL(genlock_get_handle); |
| 476 | |
| 477 | /** |
| 478 | * genlock_put_handle - release a reference to a genlock handle |
| 479 | * @handle - A pointer to the handle to release |
| 480 | */ |
| 481 | |
| 482 | void genlock_put_handle(struct genlock_handle *handle) |
| 483 | { |
| 484 | if (handle) |
| 485 | fput(handle->file); |
| 486 | } |
| 487 | EXPORT_SYMBOL(genlock_put_handle); |
| 488 | |
| 489 | /** |
| 490 | * genlock_get_handle_fd - Get a handle reference from a file descriptor |
| 491 | * @fd - The file descriptor for a genlock handle |
| 492 | */ |
| 493 | |
| 494 | struct genlock_handle *genlock_get_handle_fd(int fd) |
| 495 | { |
| 496 | struct file *file = fget(fd); |
| 497 | |
| 498 | if (file == NULL) |
| 499 | return ERR_PTR(-EINVAL); |
| 500 | |
| 501 | return file->private_data; |
| 502 | } |
| 503 | EXPORT_SYMBOL(genlock_get_handle_fd); |
| 504 | |
| 505 | #ifdef CONFIG_GENLOCK_MISCDEVICE |
| 506 | |
| 507 | static long genlock_dev_ioctl(struct file *filep, unsigned int cmd, |
| 508 | unsigned long arg) |
| 509 | { |
| 510 | struct genlock_lock param; |
| 511 | struct genlock_handle *handle = filep->private_data; |
| 512 | struct genlock *lock; |
| 513 | int ret; |
| 514 | |
| 515 | switch (cmd) { |
| 516 | case GENLOCK_IOC_NEW: { |
| 517 | lock = genlock_create_lock(handle); |
| 518 | if (IS_ERR(lock)) |
| 519 | return PTR_ERR(lock); |
| 520 | |
| 521 | return 0; |
| 522 | } |
| 523 | case GENLOCK_IOC_EXPORT: { |
| 524 | if (handle->lock == NULL) |
| 525 | return -EINVAL; |
| 526 | |
| 527 | ret = genlock_get_fd(handle->lock); |
| 528 | if (ret < 0) |
| 529 | return ret; |
| 530 | |
| 531 | param.fd = ret; |
| 532 | |
| 533 | if (copy_to_user((void __user *) arg, ¶m, |
| 534 | sizeof(param))) |
| 535 | return -EFAULT; |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | case GENLOCK_IOC_ATTACH: { |
| 540 | if (copy_from_user(¶m, (void __user *) arg, |
| 541 | sizeof(param))) |
| 542 | return -EFAULT; |
| 543 | |
| 544 | lock = genlock_attach_lock(handle, param.fd); |
| 545 | if (IS_ERR(lock)) |
| 546 | return PTR_ERR(lock); |
| 547 | |
| 548 | return 0; |
| 549 | } |
| 550 | case GENLOCK_IOC_LOCK: { |
| 551 | if (copy_from_user(¶m, (void __user *) arg, |
| 552 | sizeof(param))) |
| 553 | return -EFAULT; |
| 554 | |
| 555 | return genlock_lock(handle, param.op, param.flags, |
| 556 | param.timeout); |
| 557 | } |
| 558 | case GENLOCK_IOC_WAIT: { |
| 559 | if (copy_from_user(¶m, (void __user *) arg, |
| 560 | sizeof(param))) |
| 561 | return -EFAULT; |
| 562 | |
| 563 | return genlock_wait(handle, param.timeout); |
| 564 | } |
| 565 | case GENLOCK_IOC_RELEASE: { |
| 566 | genlock_release_lock(handle); |
| 567 | return 0; |
| 568 | } |
| 569 | default: |
| 570 | return -EINVAL; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | static int genlock_dev_release(struct inode *inodep, struct file *file) |
| 575 | { |
| 576 | struct genlock_handle *handle = file->private_data; |
| 577 | |
| 578 | genlock_put_handle(handle); |
| 579 | |
| 580 | return 0; |
| 581 | } |
| 582 | |
| 583 | static int genlock_dev_open(struct inode *inodep, struct file *file) |
| 584 | { |
| 585 | struct genlock_handle *handle = _genlock_get_handle(); |
| 586 | if (IS_ERR(handle)) |
| 587 | return PTR_ERR(handle); |
| 588 | |
| 589 | handle->file = file; |
| 590 | file->private_data = handle; |
| 591 | return 0; |
| 592 | } |
| 593 | |
| 594 | static const struct file_operations genlock_dev_fops = { |
| 595 | .open = genlock_dev_open, |
| 596 | .release = genlock_dev_release, |
| 597 | .unlocked_ioctl = genlock_dev_ioctl, |
| 598 | }; |
| 599 | |
| 600 | static struct miscdevice genlock_dev; |
| 601 | |
| 602 | static int genlock_dev_init(void) |
| 603 | { |
| 604 | genlock_dev.minor = MISC_DYNAMIC_MINOR; |
| 605 | genlock_dev.name = "genlock"; |
| 606 | genlock_dev.fops = &genlock_dev_fops; |
| 607 | genlock_dev.parent = NULL; |
| 608 | |
| 609 | return misc_register(&genlock_dev); |
| 610 | } |
| 611 | |
| 612 | static void genlock_dev_close(void) |
| 613 | { |
| 614 | misc_deregister(&genlock_dev); |
| 615 | } |
| 616 | |
| 617 | module_init(genlock_dev_init); |
| 618 | module_exit(genlock_dev_close); |
| 619 | |
| 620 | #endif |