tty_mutex.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. /* Legacy tty mutex glue */
  7. enum {
  8. TTY_MUTEX_NORMAL,
  9. TTY_MUTEX_NESTED,
  10. };
  11. /*
  12. * Getting the big tty mutex.
  13. */
  14. static void __lockfunc tty_lock_nested(struct tty_struct *tty,
  15. unsigned int subclass)
  16. {
  17. if (tty->magic != TTY_MAGIC) {
  18. pr_err("L Bad %p\n", tty);
  19. WARN_ON(1);
  20. return;
  21. }
  22. tty_kref_get(tty);
  23. mutex_lock_nested(&tty->legacy_mutex, subclass);
  24. }
  25. void __lockfunc tty_lock(struct tty_struct *tty)
  26. {
  27. return tty_lock_nested(tty, TTY_MUTEX_NORMAL);
  28. }
  29. EXPORT_SYMBOL(tty_lock);
  30. void __lockfunc tty_unlock(struct tty_struct *tty)
  31. {
  32. if (tty->magic != TTY_MAGIC) {
  33. pr_err("U Bad %p\n", tty);
  34. WARN_ON(1);
  35. return;
  36. }
  37. mutex_unlock(&tty->legacy_mutex);
  38. tty_kref_put(tty);
  39. }
  40. EXPORT_SYMBOL(tty_unlock);
  41. /*
  42. * Getting the big tty mutex for a pair of ttys with lock ordering
  43. * On a non pty/tty pair tty2 can be NULL which is just fine.
  44. */
  45. void __lockfunc tty_lock_pair(struct tty_struct *tty,
  46. struct tty_struct *tty2)
  47. {
  48. if (tty < tty2) {
  49. tty_lock(tty);
  50. tty_lock_nested(tty2, TTY_MUTEX_NESTED);
  51. } else {
  52. if (tty2 && tty2 != tty)
  53. tty_lock(tty2);
  54. tty_lock_nested(tty, TTY_MUTEX_NESTED);
  55. }
  56. }
  57. EXPORT_SYMBOL(tty_lock_pair);
  58. void __lockfunc tty_unlock_pair(struct tty_struct *tty,
  59. struct tty_struct *tty2)
  60. {
  61. tty_unlock(tty);
  62. if (tty2 && tty2 != tty)
  63. tty_unlock(tty2);
  64. }
  65. EXPORT_SYMBOL(tty_unlock_pair);