blob: 4486741190c47d52d8079f360081d99085ce82c4 [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,
Peter Hurley2febdb62014-11-05 12:13:02 -050016 TTY_MUTEX_SLAVE,
Alan Cox89c8d912012-08-08 16:30:13 +010017};
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
Peter Hurley2febdb62014-11-05 12:13:02 -050023void __lockfunc tty_lock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020024{
Alan Cox89c8d912012-08-08 16:30:13 +010025 if (tty->magic != TTY_MAGIC) {
Sangho Yi7a0c4ed2012-10-18 00:15:13 +090026 pr_err("L Bad %p\n", tty);
Alan Cox89c8d912012-08-08 16:30:13 +010027 WARN_ON(1);
28 return;
29 }
30 tty_kref_get(tty);
Peter Hurley2febdb62014-11-05 12:13:02 -050031 mutex_lock(&tty->legacy_mutex);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020032}
33EXPORT_SYMBOL(tty_lock);
34
Alan Cox89c8d912012-08-08 16:30:13 +010035void __lockfunc tty_unlock(struct tty_struct *tty)
Arnd Bergmannb07471f2010-08-06 21:40:30 +020036{
Alan Cox89c8d912012-08-08 16:30:13 +010037 if (tty->magic != TTY_MAGIC) {
Sangho Yi7a0c4ed2012-10-18 00:15:13 +090038 pr_err("U Bad %p\n", tty);
Alan Cox89c8d912012-08-08 16:30:13 +010039 WARN_ON(1);
40 return;
41 }
42 mutex_unlock(&tty->legacy_mutex);
43 tty_kref_put(tty);
Arnd Bergmannb07471f2010-08-06 21:40:30 +020044}
45EXPORT_SYMBOL(tty_unlock);
Alan Cox89c8d912012-08-08 16:30:13 +010046
Peter Hurley2aff5e22014-11-05 12:13:01 -050047void __lockfunc tty_lock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010048{
Peter Hurley2aff5e22014-11-05 12:13:01 -050049 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);
Peter Hurley2febdb62014-11-05 12:13:02 -050053 tty_lock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010054 }
55}
Alan Cox89c8d912012-08-08 16:30:13 +010056
Peter Hurley2aff5e22014-11-05 12:13:01 -050057void __lockfunc tty_unlock_slave(struct tty_struct *tty)
Alan Cox89c8d912012-08-08 16:30:13 +010058{
Peter Hurley2aff5e22014-11-05 12:13:01 -050059 if (tty && tty != tty->link)
60 tty_unlock(tty);
Alan Cox89c8d912012-08-08 16:30:13 +010061}
Peter Hurley2febdb62014-11-05 12:13:02 -050062
63void tty_set_lock_subclass(struct tty_struct *tty)
64{
65 lockdep_set_subclass(&tty->legacy_mutex, TTY_MUTEX_SLAVE);
66}