Skip site navigation (1)Skip section navigation (2)
Date:      Sat, 4 Sep 2021 02:21:11 GMT
From:      Alexander Motin <mav@FreeBSD.org>
To:        src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org
Subject:   git: bd6085c6ae28 - main - Re-implement virtual console (constty).
Message-ID:  <202109040221.1842LBsq030088@gitrepo.freebsd.org>

next in thread | raw e-mail | index | archive | help
The branch main has been updated by mav:

URL: https://cgit.FreeBSD.org/src/commit/?id=bd6085c6ae28fb1cc81cca325656332fbd2cebd8

commit bd6085c6ae28fb1cc81cca325656332fbd2cebd8
Author:     Alexander Motin <mav@FreeBSD.org>
AuthorDate: 2021-09-04 02:18:51 +0000
Commit:     Alexander Motin <mav@FreeBSD.org>
CommitDate: 2021-09-04 02:18:51 +0000

    Re-implement virtual console (constty).
    
    Protect conscallout with tty lock instead of Giant.  In addition to
    Giant removal it also closes race on console unset.
    
    Introduce additional lock to protect against concurrent console sets.
    
    Remove consbuf free on console unset as unsafe, making impossible to
    change buffer size after first allocation.  Instead increase default
    buffer size from 8KB to 64KB and processing rate from 5Hz to 10-15Hz
    to make the output more smooth.
    
    MFC after:      1 month
---
 sys/kern/kern_cons.c | 105 +++++++++++++++++++++++++++------------------------
 sys/kern/tty.c       |  24 +++---------
 sys/sys/cons.h       |   4 +-
 3 files changed, 62 insertions(+), 71 deletions(-)

diff --git a/sys/kern/kern_cons.c b/sys/kern/kern_cons.c
index 5f4bf63bed0f..780fce00387d 100644
--- a/sys/kern/kern_cons.c
+++ b/sys/kern/kern_cons.c
@@ -105,8 +105,10 @@ static u_char console_pausing;		/* pause after each line during probe */
 static const char console_pausestr[] =
 "<pause; press any key to proceed to next line or '.' to end pause mode>";
 struct tty *constty;			/* pointer to console "window" tty */
+static struct mtx constty_mtx;		/* Mutex for constty assignment. */
+MTX_SYSINIT(constty_mtx, &constty_mtx, "constty_mtx", MTX_DEF);
 static struct mtx cnputs_mtx;		/* Mutex for cnputs(). */
-static int use_cnputs_mtx = 0;		/* != 0 if cnputs_mtx locking reqd. */
+MTX_SYSINIT(cnputs_mtx, &cnputs_mtx, "cnputs_mtx", MTX_SPIN | MTX_NOWITNESS);
 
 static void constty_timeout(void *arg);
 
@@ -538,8 +540,8 @@ cnputsn(const char *p, size_t n)
 	size_t i;
 	int unlock_reqd = 0;
 
-	if (use_cnputs_mtx) {
-	  	/*
+	if (mtx_initialized(&cnputs_mtx)) {
+		/*
 		 * NOTE: Debug prints and/or witness printouts in
 		 * console driver clients can cause the "cnputs_mtx"
 		 * mutex to recurse. Simply return if that happens.
@@ -563,49 +565,72 @@ cnputs(const char *p)
 	cnputsn(p, strlen(p));
 }
 
-static int consmsgbuf_size = 8192;
-SYSCTL_INT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RW, &consmsgbuf_size, 0,
-    "Console tty buffer size");
+static unsigned int consmsgbuf_size = 65536;
+SYSCTL_UINT(_kern, OID_AUTO, consmsgbuf_size, CTLFLAG_RWTUN, &consmsgbuf_size,
+    0, "Console tty buffer size");
 
 /*
  * Redirect console output to a tty.
  */
-void
+int
 constty_set(struct tty *tp)
 {
-	int size;
+	int size = consmsgbuf_size;
+	void *buf = NULL;
+
+	tty_assert_locked(tp);
+	if (constty == tp)
+		return (0);
+	if (constty != NULL)
+		return (EBUSY);
 
-	KASSERT(tp != NULL, ("constty_set: NULL tp"));
 	if (consbuf == NULL) {
-		size = consmsgbuf_size;
-		consbuf = malloc(size, M_TTYCONS, M_WAITOK);
-		msgbuf_init(&consmsgbuf, consbuf, size);
-		callout_init(&conscallout, 0);
+		tty_unlock(tp);
+		buf = malloc(size, M_TTYCONS, M_WAITOK);
+		tty_lock(tp);
 	}
+	mtx_lock(&constty_mtx);
+	if (constty != NULL) {
+		mtx_unlock(&constty_mtx);
+		free(buf, M_TTYCONS);
+		return (EBUSY);
+	}
+	if (consbuf == NULL) {
+		consbuf = buf;
+		msgbuf_init(&consmsgbuf, buf, size);
+	} else
+		free(buf, M_TTYCONS);
 	constty = tp;
-	constty_timeout(NULL);
+	mtx_unlock(&constty_mtx);
+
+	callout_init_mtx(&conscallout, tty_getlock(tp), 0);
+	constty_timeout(tp);
+	return (0);
 }
 
 /*
  * Disable console redirection to a tty.
  */
-void
-constty_clear(void)
+int
+constty_clear(struct tty *tp)
 {
 	int c;
 
-	constty = NULL;
-	if (consbuf == NULL)
-		return;
+	tty_assert_locked(tp);
+	if (constty != tp)
+		return (ENXIO);
 	callout_stop(&conscallout);
+	mtx_lock(&constty_mtx);
+	constty = NULL;
+	mtx_unlock(&constty_mtx);
 	while ((c = msgbuf_getchar(&consmsgbuf)) != -1)
 		cnputc(c);
-	free(consbuf, M_TTYCONS);
-	consbuf = NULL;
+	/* We never free consbuf because it can still be in use. */
+	return (0);
 }
 
 /* Times per second to check for pending console tty messages. */
-static int constty_wakeups_per_second = 5;
+static int constty_wakeups_per_second = 15;
 SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
     &constty_wakeups_per_second, 0,
     "Times per second to check for pending console tty messages");
@@ -613,40 +638,20 @@ SYSCTL_INT(_kern, OID_AUTO, constty_wakeups_per_second, CTLFLAG_RW,
 static void
 constty_timeout(void *arg)
 {
+	struct tty *tp = arg;
 	int c;
 
-	if (constty != NULL) {
-		tty_lock(constty);
-		while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
-			if (tty_putchar(constty, c) < 0) {
-				tty_unlock(constty);
-				constty = NULL;
-				break;
-			}
+	tty_assert_locked(tp);
+	while ((c = msgbuf_getchar(&consmsgbuf)) != -1) {
+		if (tty_putchar(tp, c) < 0) {
+			constty_clear(tp);
+			return;
 		}
-
-		if (constty != NULL)
-			tty_unlock(constty);
-	}
-	if (constty != NULL) {
-		callout_reset(&conscallout, hz / constty_wakeups_per_second,
-		    constty_timeout, NULL);
-	} else {
-		/* Deallocate the constty buffer memory. */
-		constty_clear();
 	}
+	callout_reset_sbt(&conscallout, SBT_1S / constty_wakeups_per_second,
+	    0, constty_timeout, tp, C_PREL(1));
 }
 
-static void
-cn_drvinit(void *unused)
-{
-
-	mtx_init(&cnputs_mtx, "cnputs_mtx", NULL, MTX_SPIN | MTX_NOWITNESS);
-	use_cnputs_mtx = 1;
-}
-
-SYSINIT(cndev, SI_SUB_DRIVERS, SI_ORDER_MIDDLE, cn_drvinit, NULL);
-
 /*
  * Sysbeep(), if we have hardware for it
  */
diff --git a/sys/kern/tty.c b/sys/kern/tty.c
index 8700eb8f9ef1..8dfe5e93780f 100644
--- a/sys/kern/tty.c
+++ b/sys/kern/tty.c
@@ -241,8 +241,7 @@ ttydev_leave(struct tty *tp)
 	tp->t_flags |= TF_OPENCLOSE;
 
 	/* Remove console TTY. */
-	if (constty == tp)
-		constty_clear();
+	constty_clear(tp);
 
 	/* Drain any output. */
 	if (!tty_gone(tp))
@@ -1920,24 +1919,11 @@ tty_generic_ioctl(struct tty *tp, u_long cmd, void *data, int fflag,
 			error = priv_check(td, PRIV_TTY_CONSOLE);
 			if (error)
 				return (error);
-
-			/*
-			 * XXX: constty should really need to be locked!
-			 * XXX: allow disconnected constty's to be stolen!
-			 */
-
-			if (constty == tp)
-				return (0);
-			if (constty != NULL)
-				return (EBUSY);
-
-			tty_unlock(tp);
-			constty_set(tp);
-			tty_lock(tp);
-		} else if (constty == tp) {
-			constty_clear();
+			error = constty_set(tp);
+		} else {
+			error = constty_clear(tp);
 		}
-		return (0);
+		return (error);
 	case TIOCGWINSZ:
 		/* Obtain window size. */
 		*(struct winsize*)data = tp->t_winsize;
diff --git a/sys/sys/cons.h b/sys/sys/cons.h
index 779f365f6e99..ad73b258df6d 100644
--- a/sys/sys/cons.h
+++ b/sys/sys/cons.h
@@ -139,8 +139,8 @@ void	cnputc(int);
 void	cnputs(const char *);
 void	cnputsn(const char *, size_t);
 int	cnunavailable(void);
-void	constty_set(struct tty *tp);
-void	constty_clear(void);
+int	constty_set(struct tty *tp);
+int	constty_clear(struct tty *tp);
 
 /* sc(4) / vt(4) coexistence shim */
 #define	VTY_SC 0x01



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?202109040221.1842LBsq030088>