From nobody Wed May 01 15:33:34 2024 X-Original-To: dev-commits-src-all@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VV1LQ36Znz5JZb7; Wed, 1 May 2024 15:33:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VV1LQ2R7xz4D5h; Wed, 1 May 2024 15:33:34 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714577614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=THg3h3W8wPVWTUjc9bFZOlsItLsRSm9h7FRoCsBFyxI=; b=YyrCO5hniR5foQrBpXtL6QiSHgDkKX3S3RcxagEsxfXvZ9RPtWbAH1lMNREFz3J5hzwsgK B7s+NYPESsHfyKRBavZyL5WS8dM9lLrq875XuhvWFHUl9sXKjLnoA9sgskLcz2aRoL0sSR YCvyQ7Z9usVij8xdco0k+lVa3n3XgLlCzX1edOKbekj9wgFk3zGajgXVxSP/XY+t0kguQK NaJo9QUAi2Go/+/gcgIv/dUY3URNKUE9JzKaNNRFgdV4J4Ky/zqfw1KR8v/9jtvtRJ0SRr FD+FLcRErsnh3sop1pTtuCOmgg9mzMrxre9Pr3PQ1UeuJEQOWPW3Ew8QZ7Vntg== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1714577614; a=rsa-sha256; cv=none; b=o7naXwVQtuJKDfo2f7T+e4kBiXmAs9Wzdg6mnSG2hv3iDl7YcEbfjM3BiU0bC5NEKuXOZY LNmleroPfR5gM0YIsXx4KSYXFWGDrRt5XECfRPrmBVKVQG7qKFNYY0hF/fHe0CKYDUI9IJ 0lL1S/Pyl7deY21KyrTKFS/3JUsNmuiNfv+Ik/QQ2+X73bRDeqKCBj2nZV5sgs3nT1Nn3P d6vPYqCDtvvLjF5hbOEvUJ1/9OuOpDHocqVv+3SPX3Xgj2LvtGRXASpdg7YM8+V39wZwNw Aa0mrLEdXCVG+zXFGEPmJ3qITR4SwtUFMk2zTue2T8LzzPPD4fIYPgPc3483iw== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1714577614; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=THg3h3W8wPVWTUjc9bFZOlsItLsRSm9h7FRoCsBFyxI=; b=t2eZ9TXqeiiGoMfjY5+Lt6ymVFlYl5jHorEUY5GaEv0MmBSbwI3hQ6/DrJCvlwpmNh/XnL rTQqouqqAep8nfEmQYym314Y2JP9NPtJ7GWhD/jg78QN3W0RivHlJlQv4Kd5RBT+ILZk6t 1xXpPV/fM5mZ4MiRE+jJrZoM5xG8enGuyFJ7NjeY+7so2cE/iy95oozZ1Vkp2OLhsgiyMc QYU66Z0JnVaXfyoL4PoiluOT+JtogcBzMV06YrFS3lU4rWxRvpx5JzvnFfJQL6zZyJIyGU X4LOTws1PMQOJHEiubAMHOBLSONtaoMM1kfK9hgNylwLT/ra/Uwvfxms1IP3UA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VV1LQ22L6zPXq; Wed, 1 May 2024 15:33:34 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 441FXYns058451; Wed, 1 May 2024 15:33:34 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 441FXYGU058448; Wed, 1 May 2024 15:33:34 GMT (envelope-from git) Date: Wed, 1 May 2024 15:33:34 GMT Message-Id: <202405011533.441FXYGU058448@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Mark Johnston Subject: git: e10b9d6602df - main - bhyve: Move lock of uart frontend to uart backend List-Id: Commit messages for all branches of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-all List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-all@freebsd.org Sender: owner-dev-commits-src-all@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: markj X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: e10b9d6602dfd876a47f85d8866cabb5a26b8482 Auto-Submitted: auto-generated The branch main has been updated by markj: URL: https://cgit.FreeBSD.org/src/commit/?id=e10b9d6602dfd876a47f85d8866cabb5a26b8482 commit e10b9d6602dfd876a47f85d8866cabb5a26b8482 Author: SHENG-YI HONG AuthorDate: 2024-05-01 15:09:31 +0000 Commit: Mark Johnston CommitDate: 2024-05-01 15:09:31 +0000 bhyve: Move lock of uart frontend to uart backend Currently, lock of uart in bhyve is placed in frontend. There are some problems about it: 1. If every frontend should has a lock, why not move it inside backend as they all have same uart_softc. 2. If backend needs to modify the information of uart after initialize, it will be impossible as backend cannot use lock. For example, if we want implement a telnet support for uart in backend, It should wait for connection when initialize. After some remote process connect it, it needs to modify rfd and wfd in backend. So I decide to move it to backend. Reviewed by: corvink, jhb, markj Differential Revision: https://reviews.freebsd.org/D44947 --- usr.sbin/bhyve/uart_backend.c | 22 +++++++++++++++++++++- usr.sbin/bhyve/uart_backend.h | 3 ++- usr.sbin/bhyve/uart_emul.c | 15 ++++++--------- usr.sbin/bhyve/uart_pl011.c | 16 ++++++---------- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/usr.sbin/bhyve/uart_backend.c b/usr.sbin/bhyve/uart_backend.c index 8d91f4f671e1..51844cbf1170 100644 --- a/usr.sbin/bhyve/uart_backend.c +++ b/usr.sbin/bhyve/uart_backend.c @@ -35,6 +35,7 @@ #include #include #include +#include #include #include #include @@ -66,6 +67,7 @@ struct uart_softc { struct ttyfd tty; struct fifo rxfifo; struct mevent *mev; + pthread_mutex_t mtx; }; static bool uart_stdio; /* stdio in use for i/o */ @@ -325,7 +327,13 @@ uart_tty_backend(struct uart_softc *sc, const char *path) struct uart_softc * uart_init(void) { - return (calloc(1, sizeof(struct uart_softc))); + struct uart_softc *sc = calloc(1, sizeof(struct uart_softc)); + if (sc == NULL) + return (NULL); + + pthread_mutex_init(&sc->mtx, NULL); + + return (sc); } int @@ -346,3 +354,15 @@ uart_tty_open(struct uart_softc *sc, const char *path, return (retval); } + +void +uart_softc_lock(struct uart_softc *sc) +{ + pthread_mutex_lock(&sc->mtx); +} + +void +uart_softc_unlock(struct uart_softc *sc) +{ + pthread_mutex_unlock(&sc->mtx); +} diff --git a/usr.sbin/bhyve/uart_backend.h b/usr.sbin/bhyve/uart_backend.h index fa7949ad6d1c..ecf785ffd893 100644 --- a/usr.sbin/bhyve/uart_backend.h +++ b/usr.sbin/bhyve/uart_backend.h @@ -51,5 +51,6 @@ int uart_rxfifo_snapshot(struct uart_softc *sc, struct uart_softc *uart_init(void); int uart_tty_open(struct uart_softc *sc, const char *path, void (*drain)(int, enum ev_type, void *), void *arg); - +void uart_softc_lock(struct uart_softc *sc); +void uart_softc_unlock(struct uart_softc *sc); #endif /* _UART_BACKEND_H_ */ diff --git a/usr.sbin/bhyve/uart_emul.c b/usr.sbin/bhyve/uart_emul.c index 58d6697e4fea..bc71d4760634 100644 --- a/usr.sbin/bhyve/uart_emul.c +++ b/usr.sbin/bhyve/uart_emul.c @@ -83,7 +83,6 @@ static struct { struct uart_ns16550_softc { struct uart_softc *backend; - pthread_mutex_t mtx; /* protects all softc elements */ uint8_t data; /* Data register (R/W) */ uint8_t ier; /* Interrupt enable register (R/W) */ uint8_t lcr; /* Line control register (R/W) */ @@ -204,14 +203,14 @@ uart_drain(int fd __unused, enum ev_type ev, void *arg) * to take out the softc lock to protect against concurrent * access from a vCPU i/o exit */ - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); loopback = (sc->mcr & MCR_LOOPBACK) != 0; uart_rxfifo_drain(sc->backend, loopback); if (!loopback) uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } void @@ -220,7 +219,7 @@ uart_ns16550_write(struct uart_ns16550_softc *sc, int offset, uint8_t value) int fifosz; uint8_t msr; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); /* * Take care of the special case DLAB accesses first @@ -329,7 +328,7 @@ uart_ns16550_write(struct uart_ns16550_softc *sc, int offset, uint8_t value) done: uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } uint8_t @@ -337,7 +336,7 @@ uart_ns16550_read(struct uart_ns16550_softc *sc, int offset) { uint8_t iir, intr_reason, reg; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); /* * Take care of the special case DLAB accesses first @@ -414,7 +413,7 @@ uart_ns16550_read(struct uart_ns16550_softc *sc, int offset) done: uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); return (reg); } @@ -446,8 +445,6 @@ uart_ns16550_init(uart_intr_func_t intr_assert, uart_intr_func_t intr_deassert, sc->intr_deassert = intr_deassert; sc->backend = uart_init(); - pthread_mutex_init(&sc->mtx, NULL); - uart_reset(sc); return (sc); diff --git a/usr.sbin/bhyve/uart_pl011.c b/usr.sbin/bhyve/uart_pl011.c index e2d5c8bf5657..fb576451c544 100644 --- a/usr.sbin/bhyve/uart_pl011.c +++ b/usr.sbin/bhyve/uart_pl011.c @@ -31,7 +31,6 @@ #include #include -#include #include #include #include @@ -106,7 +105,6 @@ struct uart_pl011_softc { struct uart_softc *backend; - pthread_mutex_t mtx; /* protects all softc elements */ uint16_t irq_state; @@ -184,7 +182,7 @@ uart_drain(int fd __unused, enum ev_type ev, void *arg) * to take out the softc lock to protect against concurrent * access from a vCPU i/o exit */ - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); old_size = uart_rxfifo_numchars(sc->backend); @@ -202,7 +200,7 @@ uart_drain(int fd __unused, enum ev_type ev, void *arg) if (!loopback) uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } void @@ -210,7 +208,7 @@ uart_pl011_write(struct uart_pl011_softc *sc, int offset, uint32_t value) { bool loopback; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); switch (offset) { case UARTDR: loopback = (sc->cr & UARTCR_LBE) != 0; @@ -266,7 +264,7 @@ uart_pl011_write(struct uart_pl011_softc *sc, int offset, uint32_t value) break; } uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); } uint32_t @@ -276,7 +274,7 @@ uart_pl011_read(struct uart_pl011_softc *sc, int offset) int fifo_sz; reg = 0; - pthread_mutex_lock(&sc->mtx); + uart_softc_lock(sc->backend); switch (offset) { case UARTDR: reg = uart_rxfifo_getchar(sc->backend); @@ -362,7 +360,7 @@ uart_pl011_read(struct uart_pl011_softc *sc, int offset) break; } uart_toggle_intr(sc); - pthread_mutex_unlock(&sc->mtx); + uart_softc_unlock(sc->backend); return (reg); } @@ -380,8 +378,6 @@ uart_pl011_init(uart_intr_func_t intr_assert, uart_intr_func_t intr_deassert, sc->intr_deassert = intr_deassert; sc->backend = uart_init(); - pthread_mutex_init(&sc->mtx, NULL); - uart_reset(sc); return (sc);