f43e995c7a0fc8a64ae1ee41eb2c7afa418df5b4
[deliverable/linux.git] / drivers / tty / tty_mutex.c
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_NESTED,
17 };
18
19 /*
20 * Getting the big tty mutex.
21 */
22
23 static void __lockfunc tty_lock_nested(struct tty_struct *tty,
24 unsigned int subclass)
25 {
26 if (tty->magic != TTY_MAGIC) {
27 pr_err("L Bad %p\n", tty);
28 WARN_ON(1);
29 return;
30 }
31 tty_kref_get(tty);
32 mutex_lock_nested(&tty->legacy_mutex, subclass);
33 }
34
35 void __lockfunc tty_lock(struct tty_struct *tty)
36 {
37 return tty_lock_nested(tty, TTY_MUTEX_NORMAL);
38 }
39 EXPORT_SYMBOL(tty_lock);
40
41 void __lockfunc tty_unlock(struct tty_struct *tty)
42 {
43 if (tty->magic != TTY_MAGIC) {
44 pr_err("U Bad %p\n", tty);
45 WARN_ON(1);
46 return;
47 }
48 mutex_unlock(&tty->legacy_mutex);
49 tty_kref_put(tty);
50 }
51 EXPORT_SYMBOL(tty_unlock);
52
53 void __lockfunc tty_lock_slave(struct tty_struct *tty)
54 {
55 if (tty && tty != tty->link) {
56 WARN_ON(!mutex_is_locked(&tty->link->legacy_mutex) ||
57 !tty->driver->type == TTY_DRIVER_TYPE_PTY ||
58 !tty->driver->type == PTY_TYPE_SLAVE);
59 tty_lock_nested(tty, TTY_MUTEX_NESTED);
60 }
61 }
62
63 void __lockfunc tty_unlock_slave(struct tty_struct *tty)
64 {
65 if (tty && tty != tty->link)
66 tty_unlock(tty);
67 }
This page took 0.036882 seconds and 4 git commands to generate.