tty_mutex.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include <linux/tty.h>
  2. #include <linux/module.h>
  3. #include <linux/kallsyms.h>
  4. #include <linux/semaphore.h>
  5. #include <linux/sched.h>
  6. /*
  7. * Nested tty locks are necessary for releasing pty pairs.
  8. * The stable lock order is master pty first, then slave pty.
  9. */
  10. /* Legacy tty mutex glue */
  11. enum {
  12. TTY_MUTEX_NORMAL,
  13. TTY_MUTEX_SLAVE,
  14. };
  15. /*
  16. * Getting the big tty mutex.
  17. */
  18. void __lockfunc tty_lock(struct tty_struct *tty)
  19. {
  20. if (tty->magic != TTY_MAGIC) {
  21. pr_err("L Bad %p\n", tty);
  22. WARN_ON(1);
  23. return;
  24. }
  25. tty_kref_get(tty);
  26. mutex_lock(&tty->legacy_mutex);
  27. }
  28. EXPORT_SYMBOL(tty_lock);
  29. void __lockfunc tty_unlock(struct tty_struct *tty)
  30. {
  31. if (tty->magic != TTY_MAGIC) {
  32. pr_err("U Bad %p\n", tty);
  33. WARN_ON(1);
  34. return;
  35. }
  36. mutex_unlock(&tty->legacy_mutex);
  37. tty_kref_put(tty);
  38. }
  39. EXPORT_SYMBOL(tty_unlock);
  40. void __lockfunc tty_lock_slave(struct tty_struct *tty)
  41. {
  42. if (tty && tty != tty->link) {
  43. WARN_ON(!mutex_is_locked(&tty->link->legacy_mutex) ||
  44. !tty->driver->type == TTY_DRIVER_TYPE_PTY ||
  45. !tty->driver->type == PTY_TYPE_SLAVE);
  46. tty_lock(tty);
  47. }
  48. }
  49. void __lockfunc tty_unlock_slave(struct tty_struct *tty)
  50. {
  51. if (tty && tty != tty->link)
  52. tty_unlock(tty);
  53. }
  54. void tty_set_lock_subclass(struct tty_struct *tty)
  55. {
  56. lockdep_set_subclass(&tty->legacy_mutex, TTY_MUTEX_SLAVE);
  57. }