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