blob: f43e995c7a0fc8a64ae1ee41eb2c7afa418df5b4 [file] [log] [blame]
Arnd Bergmannb07471f2010-08-06 21:40:30 +02001#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
Peter Hurley2aff5e22014-11-05 12:13:01 -05007/*
8 * Nested tty locks are necessary for releasing pty pairs.
9 * The stable lock order is master pty first, then slave pty.
10 */
11
Alan Cox89c8d912012-08-08 16:30:13 +010012/* Legacy tty mutex glue */
13
14enum {
15 TTY_MUTEX_NORMAL,
16 TTY_MUTEX_NESTED,
17};
Eric Dumazetfde86d32012-05-31 11:35:18 +020018
Arnd Bergmannb07471f2010-08-06 21:40:30 +020019/*
20 * Getting the big tty mutex.
21 */
Alan Cox89c8d912012-08-08 16:30:13 +010022
23static void __lockfunc tty_lock_nested(struct tty_struct *tty,
24 unsigned int subclass)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020025{
Alan Cox89c8d912012-08-08 16:30:13 +010026 if (tty->magic != TTY_MAGIC) {
Sangho Yi7a0c4ed2012-10-18 00:15:13 +090027 pr_err("L Bad %p\n", tty);
Alan Cox89c8d912012-08-08 16:30:13 +010028 WARN_ON(1);
29 return;
30 }
31 tty_kref_get(tty);
32 mutex_lock_nested(&tty->legacy_mutex, subclass);
33}
34
35void __lockfunc tty_lock(struct tty_struct *tty)
36{
37 return tty_lock_nested(tty, TTY_MUTEX_NORMAL);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020038}
39EXPORT_SYMBOL(tty_lock);
40
Alan Cox89c8d912012-08-08 16:30:13 +010041void __lockfunc tty_unlock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020042{
Alan Cox89c8d912012-08-08 16:30:13 +010043 if (tty->magic != TTY_MAGIC) {
Sangho Yi7a0c4ed2012-10-18 00:15:13 +090044 pr_err("U Bad %p\n", tty);
Alan Cox89c8d912012-08-08 16:30:13 +010045 WARN_ON(1);
46 return;
47 }
48 mutex_unlock(&tty->legacy_mutex);
49 tty_kref_put(tty);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020050}
51EXPORT_SYMBOL(tty_unlock);
Alan Cox89c8d912012-08-08 16:30:13 +010052
Peter Hurley2aff5e22014-11-05 12:13:01 -050053void __lockfunc tty_lock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010054{
Peter Hurley2aff5e22014-11-05 12:13:01 -050055 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);
Alan Cox89c8d912012-08-08 16:30:13 +010059 tty_lock_nested(tty, TTY_MUTEX_NESTED);
60 }
61}
Alan Cox89c8d912012-08-08 16:30:13 +010062
Peter Hurley2aff5e22014-11-05 12:13:01 -050063void __lockfunc tty_unlock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010064{
Peter Hurley2aff5e22014-11-05 12:13:01 -050065 if (tty && tty != tty->link)
66 tty_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010067}