diff --git a/drivers/tty/n_tty.c b/drivers/tty/n_tty.c
index 2fb7043..9b332e0 100644
--- a/drivers/tty/n_tty.c
+++ b/drivers/tty/n_tty.c
@@ -1997,10 +1997,17 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
 			if (tty->ops->flush_chars)
 				tty->ops->flush_chars(tty);
 		} else {
+			bool lock;
+
+			lock = L_ECHO(tty) || (tty->icanon & L_ECHONL(tty));
+			if (lock)
+				mutex_lock(&tty->output_lock);
 			while (nr > 0) {
 				c = tty->ops->write(tty, b, nr);
 				if (c < 0) {
 					retval = c;
+					if (lock)
+						mutex_unlock(&tty->output_lock);
 					goto break_out;
 				}
 				if (!c)
@@ -2008,6 +2015,8 @@ static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
 				b += c;
 				nr -= c;
 			}
+			if (lock)
+				mutex_unlock(&tty->output_lock);
 		}
 		if (!nr)
 			break;
