^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) #include <linux/tty.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <linux/kallsyms.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <linux/semaphore.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) /* Legacy tty mutex glue */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * Getting the big tty mutex.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) void tty_lock(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) tty_kref_get(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) mutex_lock(&tty->legacy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) EXPORT_SYMBOL(tty_lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) int tty_lock_interruptible(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) if (WARN(tty->magic != TTY_MAGIC, "L Bad %p\n", tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) tty_kref_get(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) ret = mutex_lock_interruptible(&tty->legacy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) void tty_unlock(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) if (WARN(tty->magic != TTY_MAGIC, "U Bad %p\n", tty))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) mutex_unlock(&tty->legacy_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) tty_kref_put(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) EXPORT_SYMBOL(tty_unlock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) void tty_lock_slave(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) if (tty && tty != tty->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) tty_lock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) void tty_unlock_slave(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) if (tty && tty != tty->link)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) tty_unlock(tty);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) void tty_set_lock_subclass(struct tty_struct *tty)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) lockdep_set_subclass(&tty->legacy_mutex, TTY_LOCK_SLAVE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) }