From nobody Fri Feb 13 15:10:18 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C9B5828379; Mon, 27 May 2024 06:38:22 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716791904; cv=none; b=p6i5SPYZhAQN/+zJA1W+c84ewQYTwWt6+uwOB//YeNcH3wuF/87g4jB4cMQr6qUrdorJyiF7nETwSt+i4Vt7rWmJU4uMBkPYzF6TjtvEOSerivXEPe4dteAvEt6F6epRzlatpHqUpfHGIYTrxNIrTXPP7VINgo/Xlmt5wC0xyVM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716791904; c=relaxed/simple; bh=xfgoJJ7KYGaA0v4WSAop29ufu9YiwR5LBEtcGuCDyLc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tDje0KQRkJEnmDq8m2nzbJ8CRwrQxqNVJcnW4gkVUbs3WVP9IK4R5cVGW4fln95JeCtOIf7EEw9XeDCZMfdsYtPY8vOSj5q//2BTezdlxV1VzkrzevMZxOMjT0bipo0C9Bm4669rlBTAmTd69/UbXE+elz0F4OalxquCFWf8Q/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ds8wXT7+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dLwu4cib; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ds8wXT7+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dLwu4cib" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1716791901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vj8sIG4y4mzFTOtfWKSK0xfleOpjXWWwflzt4OFWNas=; b=ds8wXT7+BHCRbh6sXCSzlY+F4nTB+P8TDXEFFTpJf0ghwePsljsa9VpIRWgQPP0Fe9VH0W rDEruO7zFdApsMEX7C+EbFy/6RdZcJv4lONTg+qlK9CH+6EO1o2ifSTNjIeJZujDAmPsQF ZGeinMHcfZferb5P0jlPKWJg2UvAUs2cfUlL3203RnyrK2FLPtMGsepWM2QuMmQ80H6k0B Sl0ZMBhXt+mDhl4pLXKJ7Au1cnBL/h6E9ZU5oTSqwkaDaKpxt9cuUSzPC1ymGavxS2Tr9S rYJ2O1m5glnjAoRQlr7jUDBWikUh9LtHOpsQgMiQey6yIW+ip36pWcOg90kQ2w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1716791901; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=vj8sIG4y4mzFTOtfWKSK0xfleOpjXWWwflzt4OFWNas=; b=dLwu4cibfzNTYdijo75FkvCEfkAbFquOBa9+4oPuBtzzhpQU6Ww1jvrN+l66ZAUOhn50M6 cfqhtWFjUZYwd2Cg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org Subject: [PATCH printk v6 12/30] serial: core: Implement processing in port->lock wrapper Date: Mon, 27 May 2024 08:43:31 +0206 Message-Id: <20240527063749.391035-13-john.ogness@linutronix.de> In-Reply-To: <20240527063749.391035-1-john.ogness@linutronix.de> References: <20240527063749.391035-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently the port->lock wrappers uart_port_lock(), uart_port_unlock() (and their variants) only lock/unlock the spin_lock. If the port is an nbcon console, the wrappers must also acquire/release the console and mark the region as unsafe. This allows general port->lock synchronization to be synchronized with the nbcon console ownership. Note that __uart_port_using_nbcon() relies on the port->lock being held while a console is added and removed from the console list (i.e. all uart nbcon drivers *must* take the port->lock in their device_lock() callbacks). Signed-off-by: John Ogness Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek --- include/linux/serial_core.h | 82 ++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 038693774f21..14fb5458dafa 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -623,6 +625,60 @@ static inline void uart_port_set_cons(struct uart_port= *up, struct console *con) up->cons =3D con; __uart_port_unlock_irqrestore(up, flags); } + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_using_nbcon(struct uart_port *up) +{ + lockdep_assert_held_once(&up->lock); + + if (likely(!uart_console(up))) + return false; + + /* + * @up->cons is only modified under the port lock. Therefore it is + * certain that it cannot disappear here. + * + * @up->cons->node is added/removed from the console list under the + * port lock. Therefore it is certain that the registration status + * cannot change here, thus @up->cons->flags can be read directly. + */ + if (hlist_unhashed_lockless(&up->cons->node) || + !(up->cons->flags & CON_NBCON) || + !up->cons->write_atomic) { + return false; + } + + return true; +} + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return true; + + return nbcon_device_try_acquire(up->cons); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_acquire(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return; + + while (!nbcon_device_try_acquire(up->cons)) + cpu_relax(); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_release(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return; + + nbcon_device_release(up->cons); +} + /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure @@ -630,6 +686,7 @@ static inline void uart_port_set_cons(struct uart_port = *up, struct console *con) static inline void uart_port_lock(struct uart_port *up) { spin_lock(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -639,6 +696,7 @@ static inline void uart_port_lock(struct uart_port *up) static inline void uart_port_lock_irq(struct uart_port *up) { spin_lock_irq(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -649,6 +707,7 @@ static inline void uart_port_lock_irq(struct uart_port = *up) static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned l= ong *flags) { spin_lock_irqsave(&up->lock, *flags); + __uart_port_nbcon_acquire(up); } =20 /** @@ -659,7 +718,15 @@ static inline void uart_port_lock_irqsave(struct uart_= port *up, unsigned long *f */ static inline bool uart_port_trylock(struct uart_port *up) { - return spin_trylock(&up->lock); + if (!spin_trylock(&up->lock)) + return false; + + if (!__uart_port_nbcon_try_acquire(up)) { + spin_unlock(&up->lock); + return false; + } + + return true; } =20 /** @@ -671,7 +738,15 @@ static inline bool uart_port_trylock(struct uart_port = *up) */ static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigne= d long *flags) { - return spin_trylock_irqsave(&up->lock, *flags); + if (!spin_trylock_irqsave(&up->lock, *flags)) + return false; + + if (!__uart_port_nbcon_try_acquire(up)) { + spin_unlock_irqrestore(&up->lock, *flags); + return false; + } + + return true; } =20 /** @@ -680,6 +755,7 @@ static inline bool uart_port_trylock_irqsave(struct uar= t_port *up, unsigned long */ static inline void uart_port_unlock(struct uart_port *up) { + __uart_port_nbcon_release(up); spin_unlock(&up->lock); } =20 @@ -689,6 +765,7 @@ static inline void uart_port_unlock(struct uart_port *u= p) */ static inline void uart_port_unlock_irq(struct uart_port *up) { + __uart_port_nbcon_release(up); spin_unlock_irq(&up->lock); } =20 @@ -699,6 +776,7 @@ static inline void uart_port_unlock_irq(struct uart_por= t *up) */ static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsig= ned long flags) { + __uart_port_nbcon_release(up); spin_unlock_irqrestore(&up->lock, flags); } =20 --=20 2.39.2