From nobody Fri Dec 19 20:14:38 2025 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 A85546F09B; Sun, 18 Feb 2024 19:04:33 +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=1708283075; cv=none; b=ooRR9dVBI0UcwFBp8zIu06DK9/rxerYmFct6AmLUn7jyUakUNgdr7qkCGAvXruL52KFwN0DLJG75dCyunSMNqYOJzTSBNqKvGhzGUUXdA7F2rp/ykjNJx0BPf4un+CWt7y2QXCMAzy1+rWO+AuXWLo6+XmjBqUQ76GvKySzyQog= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708283075; c=relaxed/simple; bh=HasGcnsQPSL+51DxJXDfrG8HXukFZjehWiaU2ph0T/w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=ORgepS+1ySG5mjfXLwPqsP4+P20CTb+DUSFCApEEpIugvnCrltawIBltJR9w9rmcSdgoT2eDZ23cHFunsSosgy4j9FArazqM0fW3uvyMKRFLVtFi9BtHeKzV2AyH0+cRspq6POEtt55K1Ioed6UMqRKiPOa5DGnTGFVpd+Cs+gY= 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=QbZvSBMy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=1z8+tEXm; 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="QbZvSBMy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="1z8+tEXm" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T7h0W3N0f4G0pvkySvFkTqS4E2wnXwr34/jX5hTg2E0=; b=QbZvSBMyDM4LOXmsINAW3w29lQi6+iV6oQkdlkV+GEv4sBYFoXHoVGk4LYf57RGYMZm3Lw Agq6rijvE5UuJlE5kduRrbGQHWNTHZ7CR9JG8p8iIUoQuYgOuogv5b5S2koYAlFnLykNpP BIcvF321vrpUKDgkTI0JnGQe2D69ltp0PKCjHlY/Mio9lGQw5UMcCHy29ZOajsOxjue1cB X4umV+QaQqzODLrSyJc0ZAuZGJSYcA6uKzldZvSMX5nPRvUH8cawUaTYPzzZo2d2OuzuIW R+dkjjwKUNILaScRlyDCvBK91fi0MuotaUffV7vgIsZR6lqLkcsmWhCZvm9PCg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=T7h0W3N0f4G0pvkySvFkTqS4E2wnXwr34/jX5hTg2E0=; b=1z8+tEXmXQ18FV+rBWbYgc0kRFK8G2HiNfmJFiIet43GfimLO57gHMXL7MJAJy1NTAVxVb 5IVL+++Q0XAAiiDA== 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, =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH printk v2 01/26] serial: core: Provide port lock wrappers Date: Sun, 18 Feb 2024 20:03:01 +0106 Message-Id: <20240218185726.1994771-2-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Thomas Gleixner mainline commit: b0af4bcb49464c221ad5f95d40f2b1b252ceedcc When a serial port is used for kernel console output, then all modifications to the UART registers which are done from other contexts, e.g. getty, termios, are interference points for the kernel console. So far this has been ignored and the printk output is based on the principle of hope. The rework of the console infrastructure which aims to support threaded and atomic consoles, requires to mark sections which modify the UART registers as unsafe. This allows the atomic write function to make informed decisions and eventually to restore operational state. It also allows to prevent the regular UART code from modifying UART registers while printk output is in progress. All modifications of UART registers are guarded by the UART port lock, which provides an obvious synchronization point with the console infrastructure. Provide wrapper functions for spin_[un]lock*(port->lock) invocations so that the console mechanics can be applied later on at a single place and does not require to copy the same logic all over the drivers. Signed-off-by: Thomas Gleixner Reviewed-by: Ilpo J=C3=A4rvinen Signed-off-by: John Ogness Link: https://lore.kernel.org/r/20230914183831.587273-2-john.ogness@linutro= nix.de Signed-off-by: Greg Kroah-Hartman --- include/linux/serial_core.h | 79 +++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index bb6f073bc159..f1d5c0d1568c 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -588,6 +588,85 @@ struct uart_port { void *private_data; /* generic platform data pointer */ }; =20 +/** + * uart_port_lock - Lock the UART port + * @up: Pointer to UART port structure + */ +static inline void uart_port_lock(struct uart_port *up) +{ + spin_lock(&up->lock); +} + +/** + * uart_port_lock_irq - Lock the UART port and disable interrupts + * @up: Pointer to UART port structure + */ +static inline void uart_port_lock_irq(struct uart_port *up) +{ + spin_lock_irq(&up->lock); +} + +/** + * uart_port_lock_irqsave - Lock the UART port, save and disable interrupts + * @up: Pointer to UART port structure + * @flags: Pointer to interrupt flags storage + */ +static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned l= ong *flags) +{ + spin_lock_irqsave(&up->lock, *flags); +} + +/** + * uart_port_trylock - Try to lock the UART port + * @up: Pointer to UART port structure + * + * Returns: True if lock was acquired, false otherwise + */ +static inline bool uart_port_trylock(struct uart_port *up) +{ + return spin_trylock(&up->lock); +} + +/** + * uart_port_trylock_irqsave - Try to lock the UART port, save and disable= interrupts + * @up: Pointer to UART port structure + * @flags: Pointer to interrupt flags storage + * + * Returns: True if lock was acquired, false otherwise + */ +static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigne= d long *flags) +{ + return spin_trylock_irqsave(&up->lock, *flags); +} + +/** + * uart_port_unlock - Unlock the UART port + * @up: Pointer to UART port structure + */ +static inline void uart_port_unlock(struct uart_port *up) +{ + spin_unlock(&up->lock); +} + +/** + * uart_port_unlock_irq - Unlock the UART port and re-enable interrupts + * @up: Pointer to UART port structure + */ +static inline void uart_port_unlock_irq(struct uart_port *up) +{ + spin_unlock_irq(&up->lock); +} + +/** + * uart_port_lock_irqrestore - Unlock the UART port, restore interrupts + * @up: Pointer to UART port structure + * @flags: The saved interrupt flags for restore + */ +static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsig= ned long flags) +{ + spin_unlock_irqrestore(&up->lock, flags); +} + static inline int serial_port_in(struct uart_port *up, int offset) { return up->serial_in(up, offset); --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 A862D71B35; Sun, 18 Feb 2024 19:04:33 +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=1708283075; cv=none; b=LzSIsjVjU01EgH+UhpArRIBW+cwraqci/+/xd+3977Ou4bISM/mjZSym3Igv7GurW37Svg9t5GCsr8Q9eOmEW0u+f1HvZblMAxtJiHOA7AVqUOq9gYhXNWnP3mkauYViQpZlbQvyrFh1y4BP9zTdgDcGzjTQ23U4c2ne/1AlUIA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708283075; c=relaxed/simple; bh=MO6Y8tj0NIiymjFHpZwGZycpK1m0Vc+X7D6vRJLm6OQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=HrEYbWttJArkFXIjF+xMLBpbPbfDh/5zC83kX4/AaxPiAyfZCfYDJZoaTZKSn960hiak/SKfzRTNF6nXvzezIDcwxPeaKpYv3UBWl39HByt0fI57OCCuHe+YWHgoU5I7W3oZZ2KZNtood2uRjhGOxySf4M7xlA4jwFqevOQabEU= 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=GM/VNnAa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=j01hjXjY; 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="GM/VNnAa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="j01hjXjY" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7K5mFiabL7N74FJZwx3sG2fqkTYhk/mX6rh+7t2HHdE=; b=GM/VNnAaXsfrk931JW7tOsC8rSnrcoVqetLvg7dHTNoaA0biAHVOo0QDOQZHWoeseZkkwM mRFZ9mcxot4t+TcgT/BPvpiknWQs1OH99Fhd8aPFXFwDfpRwYOpkc7KYUHJfcOa5N4nPDe pwPSogdC84OLic8N43gLNsWNSSyLgBEKssAc9aR2Ts/+DmFWiYxqhtAHK6nP47dn9C2iUv xiRwnf80gRKlr8q8/oWpv3LOCgWxj8qd1UJmWMqWjLMZ0Rb1zHUS2ogv/C4SVqphfIdATV Sd2b+8wJRFJh+jdsehIsvBCLThyeZsFXpZQFuJqj+fdo8czdgU31ep9pzrk0Cg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282668; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=7K5mFiabL7N74FJZwx3sG2fqkTYhk/mX6rh+7t2HHdE=; b=j01hjXjY0lEpNjUkrWN71FG4yDfhUXPiKRay1EPW8PknNnIXHNnQV9wJESzRbUmBKK7NOA lIzvZldl2ebOclBw== 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, =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= Subject: [PATCH printk v2 02/26] serial: core: Use lock wrappers Date: Sun, 18 Feb 2024 20:03:02 +0106 Message-Id: <20240218185726.1994771-3-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable From: Thomas Gleixner mainline commit: c5cbdb76e8e33ce90fec2946e8eee7d71d68e57a When a serial port is used for kernel console output, then all modifications to the UART registers which are done from other contexts, e.g. getty, termios, are interference points for the kernel console. So far this has been ignored and the printk output is based on the principle of hope. The rework of the console infrastructure which aims to support threaded and atomic consoles, requires to mark sections which modify the UART registers as unsafe. This allows the atomic write function to make informed decisions and eventually to restore operational state. It also allows to prevent the regular UART code from modifying UART registers while printk output is in progress. All modifications of UART registers are guarded by the UART port lock, which provides an obvious synchronization point with the console infrastructure. To avoid adding this functionality to all UART drivers, wrap the spin_[un]lock*() invocations for uart_port::lock into helper functions which just contain the spin_[un]lock*() invocations for now. In a subsequent step these helpers will gain the console synchronization mechanisms. Converted with coccinelle. No functional change. Signed-off-by: Thomas Gleixner Reviewed-by: Ilpo J=C3=A4rvinen Signed-off-by: John Ogness Link: https://lore.kernel.org/r/20230914183831.587273-3-john.ogness@linutro= nix.de Signed-off-by: Greg Kroah-Hartman --- include/linux/serial_core.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index f1d5c0d1568c..3091c62ec37b 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -1035,14 +1035,14 @@ static inline void uart_unlock_and_check_sysrq(stru= ct uart_port *port) u8 sysrq_ch; =20 if (!port->has_sysrq) { - spin_unlock(&port->lock); + uart_port_unlock(port); return; } =20 sysrq_ch =3D port->sysrq_ch; port->sysrq_ch =3D 0; =20 - spin_unlock(&port->lock); + uart_port_unlock(port); =20 if (sysrq_ch) handle_sysrq(sysrq_ch); @@ -1054,14 +1054,14 @@ static inline void uart_unlock_and_check_sysrq_irqr= estore(struct uart_port *port u8 sysrq_ch; =20 if (!port->has_sysrq) { - spin_unlock_irqrestore(&port->lock, flags); + uart_port_unlock_irqrestore(port, flags); return; } =20 sysrq_ch =3D port->sysrq_ch; port->sysrq_ch =3D 0; =20 - spin_unlock_irqrestore(&port->lock, flags); + uart_port_unlock_irqrestore(port, flags); =20 if (sysrq_ch) handle_sysrq(sysrq_ch); @@ -1077,12 +1077,12 @@ static inline int uart_prepare_sysrq_char(struct ua= rt_port *port, u8 ch) } static inline void uart_unlock_and_check_sysrq(struct uart_port *port) { - spin_unlock(&port->lock); + uart_port_unlock(port); } static inline void uart_unlock_and_check_sysrq_irqrestore(struct uart_port= *port, unsigned long flags) { - spin_unlock_irqrestore(&port->lock, flags); + uart_port_unlock_irqrestore(port, flags); } #endif /* CONFIG_MAGIC_SYSRQ_SERIAL */ =20 --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 A858E6F507; Sun, 18 Feb 2024 19:04:33 +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=1708283076; cv=none; b=ppMCEYoH98Tb/WHdKsxaa4ATXJb0XjB+OyuMqdyp01u+EigJTtTOqBhAwEjL6uzYiEjF1WlrZYNTBhxWpNZ5EGzzlnMWD2jNpVJ8nyJx0GOIyNW5nnsCMmbtbEKsE9/geATzpf6+2wSNxZSPih7acV6s16pX2MPPuYEuQ3pf2NA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708283076; c=relaxed/simple; bh=keGeNhzs3xp+qeYPU8cNtdBOt0vcNU3zMqIHr/8gsdk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EFNV6inn4TM1o/YYlwAMDMLLhVF2EkQrfd+AWDTiwa7gtIAmMmdnWSaLw7Q4xCTWwO9UsFyLpi4bxBvzQf/PoOSzVxx3sk5wKBsMcSU0QS7htnQuZrI494X/DQh822IoFnt/yNw5zfACaqaYEMypVsiza16W3HiGGCtKZPf7r0U= 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=hzsRMOH6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=aTmN1BPg; 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="hzsRMOH6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="aTmN1BPg" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282669; 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=hl9qbklGnpInKmYvJoa6ZS5PHIy9GOhc0q9FzMa8X2Y=; b=hzsRMOH6xeWxc2o2bRi5V62jaRTvfs7zJ+P0LqBDGw2zp7XE/6L+IQGhAIrtviSmkoco4x 9eIgWPX+MYzeEF+BWttPOsMQfQp637cS3Ljr33qbYM0REFlEz+3GqCfKqDIX0XLYBkbH/K l9JX/WpwrANdf0ctJgkvSnfllg0p3BLmu4mhA7pmccbd/V+FqKDyzsgiq1/Ln/fU3EJuwI EO/SzKzaikWR4aiaskFaHBKwzNUgqzm3topJgtTZTG60Axn1vfYluAEdZA5b5ZMkUQnjcI 8tWHpYiq2ITkc4fiGkjXfwa2ndDHUnwfFQKhqfNEU5OYIfM8xQ9K9p3GtVKisQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282669; 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=hl9qbklGnpInKmYvJoa6ZS5PHIy9GOhc0q9FzMa8X2Y=; b=aTmN1BPgkJfjz5ghzY+8zNVy8iT7ckSrrKhBMJR8BN+z8EP3Hh0PLeU0D6cc7pLTCRm959 PY5rzrq5hPOljlDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Randy Dunlap , Greg Kroah-Hartman , Jiri Slaby , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , linux-serial@vger.kernel.org Subject: [PATCH printk v2 03/26] serial: core: fix kernel-doc for uart_port_unlock_irqrestore() Date: Sun, 18 Feb 2024 20:03:03 +0106 Message-Id: <20240218185726.1994771-4-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" From: Randy Dunlap mainline commit: 29bff582b74ed0bdb7e6986482ad9e6799ea4d2f Fix the function name to avoid a kernel-doc warning: include/linux/serial_core.h:666: warning: expecting prototype for uart_port= _lock_irqrestore(). Prototype was for uart_port_unlock_irqrestore() instead Fixes: b0af4bcb4946 ("serial: core: Provide port lock wrappers") Signed-off-by: Randy Dunlap Cc: Thomas Gleixner Cc: John Ogness Cc: linux-serial@vger.kernel.org Cc: Greg Kroah-Hartman Cc: Jiri Slaby Reviewed-by: John Ogness Link: https://lore.kernel.org/r/20230927044128.4748-1-rdunlap@infradead.org Signed-off-by: Greg Kroah-Hartman --- include/linux/serial_core.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 3091c62ec37b..89f7b6c63598 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -658,7 +658,7 @@ static inline void uart_port_unlock_irq(struct uart_por= t *up) } =20 /** - * uart_port_lock_irqrestore - Unlock the UART port, restore interrupts + * uart_port_unlock_irqrestore - Unlock the UART port, restore interrupts * @up: Pointer to UART port structure * @flags: The saved interrupt flags for restore */ --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 A866E71B36 for ; Sun, 18 Feb 2024 19:04:33 +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=1708283075; cv=none; b=ez9el1Zg1YONAVWSACvMwBGg0BR6uWH9bffocgEsOKNZsLPNAseXWHIhwguGSvYVazN0y0PhbZLlO1R0mPtzrr1myMtMncEuAqEBT0L/H4+Z+ti8UwkZ2VjI6ENbL0Bgw4CXZZs/8MhPNeRIrVis7iJIHGWR9vkwUd88vNDK+Mk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708283075; c=relaxed/simple; bh=UgNQzP2/u6tlCo+4rRFkvhUnGTuONwvzfEkavv5ocsw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=E2zdrv6mPAhcnDrTi8Ivm3sDgEER3FU0Tmep2FTPWpe6dC1wflNHyb+3Ufvl3L+FtL7H2LC+VNEzQ7gbKzISN9wtn45ifF+VVh295jovXXfpLPD4qJ0+Avm1kwpnIrNp2tx5pKAXILW5ybMvn7Q/vPEbZVIETavwZEQW8WlMJHs= 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=V1z5I0ZK; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JU0rh4zw; 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="V1z5I0ZK"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JU0rh4zw" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282669; 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=xxlb5lhcjt/hac5FRx4FUo+WOPkTmWiB9/oYCJlGF7c=; b=V1z5I0ZKF4iHILJ11grW3yUcMsf6dzsgE9WmHuFW6u/mxsPMHOJ0NSsnRbXIC6b8V1T0Z9 wqPspAxBrbJs7PJxTaKHgLbrdQPevNQ5dnXs6vXoyD+hrTN3n1gh+X8inITIaEMq3XNX8l dKXlX7bVC2Ea2M7PeM2Satzn9re/EfxAmImJYno6Owt6YqVpL7yIsWeXlWfziccFOTF7EL 89bInQanbWKAzq0TvEvvLMh0Swdds4mpPwEcCIbCABldoG8S0gVmv9Lm/41vzLqA3jr5bT agzhWDfYaOcXGngoaxe8lTI2CS81GGXp1TVsB+vrx2AI7tD3/tARoptktJJG7Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282669; 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=xxlb5lhcjt/hac5FRx4FUo+WOPkTmWiB9/oYCJlGF7c=; b=JU0rh4zwDuGsnP1WFQPCdy7oRZb7xMVV+ZfKxvMADbaw7dYlMWIc2fH5D4SMeMceDUvmUD nvvtMP+k9d9OBDDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 04/26] printk: Consider nbcon boot consoles on seq init Date: Sun, 18 Feb 2024 20:03:04 +0106 Message-Id: <20240218185726.1994771-5-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" If a non-boot console is registering and boot consoles exist, the consoles are flushed before being unregistered. This allows the non-boot console to continue where the boot console left off. If for whatever reason flushing fails, the lowest seq found from any of the enabled boot consoles is used. Until now con->seq was checked. However, if it is an nbcon boot console, the function nbcon_seq_read() must be used to read seq because con->seq is always 0. Check if it is an nbcon boot console and if so call nbcon_seq_read() to read seq. Signed-off-by: John Ogness --- kernel/printk/printk.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1685a71f3f71..696a9d76c09c 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3391,11 +3391,20 @@ static void console_init_seq(struct console *newcon= , bool bootcon_registered) =20 newcon->seq =3D prb_next_seq(prb); for_each_console(con) { - if ((con->flags & CON_BOOT) && - (con->flags & CON_ENABLED) && - con->seq < newcon->seq) { - newcon->seq =3D con->seq; + u64 seq; + + if (!((con->flags & CON_BOOT) && + (con->flags & CON_ENABLED))) { + continue; } + + if (con->flags & CON_NBCON) + seq =3D nbcon_seq_read(con); + else + seq =3D con->seq; + + if (seq < newcon->seq) + newcon->seq =3D seq; } } =20 --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 376B725772 for ; Sun, 18 Feb 2024 18:57:52 +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=1708282673; cv=none; b=o7HLDI/U5H2AhiZmJpMmM6RDRqPoQIjW169gqqxSuXF2nhbU4X5Leh8SB+bYMCKHfjJGIPzRgo7VCb8VGPx3jGEpdTGHOhcZoj9va+cpbhLXRzoOcTMwGOwCnwShL8iq2IuIxol9erraH5sLYGivT9/5WjZElnz42sc+L91duKI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282673; c=relaxed/simple; bh=szxbYO6I4lhZ0tittilU4W8LlO/EcPP5ZLOScG+pyp4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=f81GMo2NjMSd90QLGQq86KEuLjh2hfMjI5uVaYyQr+YjEG3OxrKmTlVl3X8LnYBtFgj66OYakSNfRYA2Sq/aaQWBjk6nTIWgGN8V0Yx3/iPZCv2OuAvhM9gMWbW0eWNFkD8dWXhmIZd2yBQm+DSnE18WEsTt1gpaDLC7zdwETxI= 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=qrnb2FPQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=TZBO/V1B; 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="qrnb2FPQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="TZBO/V1B" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282669; 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=4Un0rwRwZZ3a2XT9jb+evA7D9ZWLwutkDIt2olFNBSM=; b=qrnb2FPQWgUIoPQJwgH3E7DufsQ8IsmtrQQqmcYm22LHa2RlOcWRaeSgLnTFRkhmI+mqtI LRtd+VTf3IGtSg31pnViD3VR4oQLjkg/aygbOYwww0ug90EJiO7B0J2tW815u+ZjEbJ2L7 aXR2jDLA0+TBCYwEQzB2/sxQTpYFEI8RPyW5XDMBYMHWL1pAxkd9SBV82uAdo7iMHesD38 WRuCisj15Tlbb3l3AEKayPiqVcSXandv/cN4vbqodVlGzOWCbAH+ssr8l8CDm7k3kZuMNt CVTx2lQTuI+sOT63pugYF4DZCMWzYNhlyYfNKl9HVk47k8XSDkbqVUxCyXX39w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282669; 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=4Un0rwRwZZ3a2XT9jb+evA7D9ZWLwutkDIt2olFNBSM=; b=TZBO/V1Bg1bIdmb66zgE0251XZPPIH3YGi9O+tOmQDQ3pr7l4tHkAdFsCwv7sEQgcEqLRv 77OR8OHaSoy8eBCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, "Paul E. McKenney" , Miguel Ojeda , kernel test robot Subject: [PATCH printk v2 05/26] printk: Add notation to console_srcu locking Date: Sun, 18 Feb 2024 20:03:05 +0106 Message-Id: <20240218185726.1994771-6-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" kernel/printk/printk.c:284:5: sparse: sparse: context imbalance in 'console_srcu_read_lock' - wrong count at exit include/linux/srcu.h:301:9: sparse: sparse: context imbalance in 'console_srcu_read_unlock' - unexpected unlock Reported-by: kernel test robot Fixes: 6c4afa79147e ("printk: Prepare for SRCU console list protection") Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 696a9d76c09c..b75ca383683d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -282,6 +282,7 @@ EXPORT_SYMBOL(console_list_unlock); * Return: A cookie to pass to console_srcu_read_unlock(). */ int console_srcu_read_lock(void) + __acquires(&console_srcu) { return srcu_read_lock_nmisafe(&console_srcu); } @@ -295,6 +296,7 @@ EXPORT_SYMBOL(console_srcu_read_lock); * Counterpart to console_srcu_read_lock() */ void console_srcu_read_unlock(int cookie) + __releases(&console_srcu) { srcu_read_unlock_nmisafe(&console_srcu, cookie); } --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 376F76F094 for ; Sun, 18 Feb 2024 18:57:52 +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=1708282673; cv=none; b=Uv8s91tBnvaOp/R2oG0A0MJpYp7Jehn0wwBJfSmSDngELDRkCJ0KIgwiF2pZH77QEin566yf7pMj3zsLBFnzIK1YW+3VN4gO0pG/HeuPmDqVj0VwDL4cQxarPHjZXiumWSFnffxeDeXqfkEvBR+CULeSbwvSHLKjw5M4oU/wlH0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282673; c=relaxed/simple; bh=xRjhM41YiCV0XIFxys+lC68vQSHxZf7wjECrZPYdxII=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Mj0bkQ6pLnjysOZ3uzyjxPZAFa03D9aicEziRr9PWz4W7jTD6dFAWCa/49QmzwDRMHLr5+9JcCcsgNIh4IvrkZ19rN0ANhC/wD9v18xmyEvFQ4inHmvCwgJrm1k34E2y3KKIPC4BTSWVYpMJifewp3iyR6qtk/1zvs3PdHVtC6A= 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=snqAalWA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ZmyFWTbF; 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="snqAalWA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZmyFWTbF" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282670; 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=Kjns8XLkW3UOjxVLP1Xg+LLtdkqweLRjHg511yq2AQM=; b=snqAalWATXYn0aUg65LZvGca4wwwf8i8uVStRHQLraQha7BTOjAHnpHAmXikabBL39RLiE MRmXDgvvG2+RBg0rtq5N4Y099iHJtZtZRA2IM7CyqsvJkv1hL2xszoPrUIuRQu2i7iPNm5 4Ct4gXVxeZ//4jnJt56fzxkAOIuycjU+ZRQseZzN1btEl27/9yjG37VTWInD4oLeSwQB14 42u4kchPISfE4m5ic24bedmSDw3pVg7g13BwY2g5C762aTwkRl/Awgk1+pErAzruPbDe6c eU+3HuZVI71hY0OhWp23G8N/FtzzDJANTPF4+myOwLmf1PAuNXlNo146FHyCKg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282670; 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=Kjns8XLkW3UOjxVLP1Xg+LLtdkqweLRjHg511yq2AQM=; b=ZmyFWTbFlEMBKl9FVuqkxwy1anMpcoUdWmv8BI2orJRjCsQxNuo7BoQOG21L6KMv1rWiVC 1NYdc0VXdvcu3ICg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 06/26] printk: nbcon: Ensure ownership release on failed emit Date: Sun, 18 Feb 2024 20:03:06 +0106 Message-Id: <20240218185726.1994771-7-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Until now it was assumed that ownership has been lost when the write_atomic() callback fails. And nbcon_emit_next_record() directly returned false. However, if nbcon_emit_next_record() returns false, the context must no longer have ownership. The semantics for the callbacks could be specified such that if they return false, they must have released ownership. But in practice those semantics seem odd since the ownership was acquired outside of the callback. Ensure ownership has been released before reporting failure by explicitly attempting a release. If the current context is not the owner, the release has no effect. Signed-off-by: John Ogness --- kernel/printk/nbcon.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index c8093bcc01fe..8ecd76aa22e6 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -852,7 +852,7 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt) unsigned long con_dropped; struct nbcon_state cur; unsigned long dropped; - bool done; + bool done =3D false; =20 /* * The printk buffers are filled within an unsafe section. This @@ -891,17 +891,18 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt) nbcon_state_read(con, &cur); wctxt->unsafe_takeover =3D cur.unsafe_takeover; =20 - if (con->write_atomic) { + if (con->write_atomic) done =3D con->write_atomic(con, wctxt); - } else { - nbcon_context_release(ctxt); - WARN_ON_ONCE(1); - done =3D false; - } =20 - /* If not done, the emit was aborted. */ - if (!done) + if (!done) { + /* + * The emit was aborted, probably due to a loss of ownership. + * Ensure ownership was lost or released before reporting the + * loss. + */ + nbcon_context_release(ctxt); return false; + } =20 /* * Since any dropped message was successfully output, reset the --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 867256F09A for ; Sun, 18 Feb 2024 18:57:52 +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=1708282674; cv=none; b=i17QdP1VY/RTZXJDka9/mDBWz5zavUzQZZ+rs1x6oSy/3UnayIQLh8RkVriMG6LfYg2SeWVeDAts1/qiT+eyS+mRSHqCBTesZ0OOYx2EQ1gLj+pkQImwHhSr5aDnJX3XkJ9KrbDPqYO82m926+lkZMDsNYAp/fJbfJPF9iF5gSs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282674; c=relaxed/simple; bh=QUm3HonFr4bI9kdM8sLrYXTcDYUW2kypDJWENyu683c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WjCMBKPat0XMtGHi0sO76J4FnPHFGwYZ3L7KbJRzLJYMKyeN1PI+eE86YXrSaMlVEVcr0Ovcjj7uxugMK8AvnsjaJCGtXCdJ4K/pscVYgPp9gAzz4KawjsgGTXxJ5ZsAKRPL08YiMbxFaQFW0X5VCqZHCfsIuybl0ofOv4Uex8c= 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=Z/8tYXT2; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=i9pMn/EB; 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="Z/8tYXT2"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="i9pMn/EB" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282670; 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=rhiYAxJ52E/enQP4Ayf/MIif8Xm2i96p2VSKi7x6/pk=; b=Z/8tYXT2WSDAy/oWuJCJWbFDqgYvMv1StysN1afFGWiaizrs1Sey8dbHymursqMg64AOxu 8y3QmJDsjKSQsXAg9Xq7OL35hf0l20OSbsFDRS1bpet71gqBMKUzO23Nft/f2TZkFaCQQJ 1VTKQ4aEuBfNEMI3CmjUMOzSEDx7WFjsaU8jEf4gxH+EKyJcEPpDplzpU3P9MHt2cm5/jO gg4482yTJPtLbcRrLt+gJOCyqWS3gjRtuBMfK1Jr6J3j2lS2//JhLt19l/q3Onn9y6fIzD 6uinp4xNsIoaOBEk34ufZOFS/TEUy33xfgohPM1mPd+IXv8IWmDIPngc13shyg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282670; 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=rhiYAxJ52E/enQP4Ayf/MIif8Xm2i96p2VSKi7x6/pk=; b=i9pMn/EB1LeNHwwoyZzphndAhEQPz7vVuj1gMJibIlEhslNTv+R6kLlQ5h+5I24cS/nFdG v5uWCEVLIW/zfIBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Sebastian Andrzej Siewior Subject: [PATCH printk v2 07/26] printk: Check printk_deferred_enter()/_exit() usage Date: Sun, 18 Feb 2024 20:03:07 +0106 Message-Id: <20240218185726.1994771-8-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" From: Sebastian Andrzej Siewior Add validation that printk_deferred_enter()/_exit() are called in non-migration contexts. Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 7 +++++-- kernel/printk/printk_safe.c | 12 ++++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 955e31860095..8d5c5588eec9 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -159,13 +159,16 @@ __printf(1, 2) __cold int _printk_deferred(const char= *fmt, ...); =20 extern void __printk_safe_enter(void); extern void __printk_safe_exit(void); +extern void __printk_deferred_enter(void); +extern void __printk_deferred_exit(void); + /* * The printk_deferred_enter/exit macros are available only as a hack for * some code paths that need to defer all printk console printing. Interru= pts * must be disabled for the deferred duration. */ -#define printk_deferred_enter __printk_safe_enter -#define printk_deferred_exit __printk_safe_exit +#define printk_deferred_enter() __printk_deferred_enter() +#define printk_deferred_exit() __printk_deferred_exit() =20 /* * Please don't use printk_ratelimit(), because it shares ratelimiting sta= te diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c index 6d10927a07d8..8d9408d653de 100644 --- a/kernel/printk/printk_safe.c +++ b/kernel/printk/printk_safe.c @@ -26,6 +26,18 @@ void __printk_safe_exit(void) this_cpu_dec(printk_context); } =20 +void __printk_deferred_enter(void) +{ + cant_migrate(); + this_cpu_inc(printk_context); +} + +void __printk_deferred_exit(void) +{ + cant_migrate(); + this_cpu_dec(printk_context); +} + asmlinkage int vprintk(const char *fmt, va_list args) { #ifdef CONFIG_KGDB_KDB --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 BEFAD6F09B; Sun, 18 Feb 2024 18:57:52 +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=1708282674; cv=none; b=AhgcbfS4eg6HlKfcNVIRC6xeRye+lEQZXd646F84cvdUimRUvJvStzmRq0oUyMvDp6MlbubOxn7fdaNlgguVjUjMQYBp7uk+gI2lSHAXc9tLzSa44G+hA5VZAX2upyIqDVWJBiHO+T7d/7zEQpI6ORJPY/zTNmmSqtEnGUGCvBA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282674; c=relaxed/simple; bh=Uh1a7N6liYSzDazmzjchFNl1uPkqsuEMN7I6bWMM5lg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=F8VK+cZaI17xAQQkQ98GiMYBrhgiKM39ALgkY1ZNze7Dlyr2qCWltIHKtAYy8v1sad4izTkuDFauF7DQPvwUD2bV35QWVSQVKFuheOVyozVa0R4Ay1EulMEGreVrdJerJZDb1rZn6v1Ho3bDzvBEeovBI7AxVc8jiz9Ja2EuFEw= 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=OxRI2d34; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Rsnkb8W7; 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="OxRI2d34"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Rsnkb8W7" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282671; 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=xrTMG/IO2xEuBdqD5OxQ1/ULXhRtRhpYhyRMl+KnF7w=; b=OxRI2d34d6eZ84FJ7ek1LsuerkJcVIohMxOCIEONANmxjad1y70ujK/AQkOy/143chnRYk axXq9ozPgZaRTxPS2OU7FU/O2GmA4dQqaDAYVnThA0KVqDRntfohKh/LtQmkD6y6C6/PpO w5Mo7Fvy6+2N6uBZjtL8dfWxrur03oO6Riby83YgQu9pWCnLN3iuAnBMI0QZ8lP6gIHFqN wI3gEXimzNOiRYIOXA7IuqqFHlIQPWrFitlcfb249CDX8ATU6Ne+IliEJ+ba6VDCwX+OcB 8aTaUpAxr18hbUGOGraqr3e+Caqa7suhJtJvrivhatX1a6gL7hZM8e7x1aOLqg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282671; 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=xrTMG/IO2xEuBdqD5OxQ1/ULXhRtRhpYhyRMl+KnF7w=; b=Rsnkb8W7+dOJGMPJ0e2uA0Qw+jE3lvQlvn7hjAPcJzTKGdemWwBwLWErNiRpjvS9iouHHa VyIr8Gp7jlbNNSAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Andy Shevchenko , Tony Lindgren , Geert Uytterhoeven , Justin Chen , Jiaqing Zhao , linux-serial@vger.kernel.org Subject: [PATCH printk v2 08/26] printk: nbcon: Implement processing in port->lock wrapper Date: Sun, 18 Feb 2024 20:03:08 +0106 Message-Id: <20240218185726.1994771-9-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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. Add a flag to struct uart_port to track nbcon console ownership. Signed-off-by: John Ogness --- drivers/tty/serial/8250/8250_port.c | 1 + include/linux/printk.h | 13 +++++ include/linux/serial_core.h | 19 ++++++- kernel/printk/nbcon.c | 77 +++++++++++++++++++++++++++++ 4 files changed, 108 insertions(+), 2 deletions(-) diff --git a/drivers/tty/serial/8250/8250_port.c b/drivers/tty/serial/8250/= 8250_port.c index 141627370aab..16e2705b4867 100644 --- a/drivers/tty/serial/8250/8250_port.c +++ b/drivers/tty/serial/8250/8250_port.c @@ -3284,6 +3284,7 @@ void serial8250_init_port(struct uart_8250_port *up) struct uart_port *port =3D &up->port; =20 spin_lock_init(&port->lock); + port->nbcon_locked_port =3D false; port->ctrl_id =3D 0; port->pm =3D NULL; port->ops =3D &serial8250_pops; diff --git a/include/linux/printk.h b/include/linux/printk.h index 8d5c5588eec9..ef57a4d93ae2 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -9,6 +9,8 @@ #include #include =20 +struct uart_port; + extern const char linux_banner[]; extern const char linux_proc_banner[]; =20 @@ -195,6 +197,8 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); +extern void uart_nbcon_acquire(struct uart_port *up); +extern void uart_nbcon_release(struct uart_port *up); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -274,6 +278,15 @@ static inline void dump_stack(void) static inline void printk_trigger_flush(void) { } + +static inline void uart_nbcon_acquire(struct uart_port *up) +{ +} + +static inline void uart_nbcon_release(struct uart_port *up) +{ +} + #endif =20 bool this_cpu_in_panic(void); diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 89f7b6c63598..d4b93d721715 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -488,6 +488,7 @@ struct uart_port { struct uart_icount icount; /* statistics */ =20 struct console *cons; /* struct console, if any */ + bool nbcon_locked_port; /* True, if the port is locked by nbcon */ /* flags must be updated while holding port mutex */ upf_t flags; =20 @@ -595,6 +596,7 @@ struct uart_port { static inline void uart_port_lock(struct uart_port *up) { spin_lock(&up->lock); + uart_nbcon_acquire(up); } =20 /** @@ -604,6 +606,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_nbcon_acquire(up); } =20 /** @@ -614,6 +617,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_nbcon_acquire(up); } =20 /** @@ -624,7 +628,11 @@ 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; + + uart_nbcon_acquire(up); + return true; } =20 /** @@ -636,7 +644,11 @@ 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; + + uart_nbcon_acquire(up); + return true; } =20 /** @@ -645,6 +657,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_nbcon_release(up); spin_unlock(&up->lock); } =20 @@ -654,6 +667,7 @@ static inline void uart_port_unlock(struct uart_port *u= p) */ static inline void uart_port_unlock_irq(struct uart_port *up) { + uart_nbcon_release(up); spin_unlock_irq(&up->lock); } =20 @@ -664,6 +678,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_nbcon_release(up); spin_unlock_irqrestore(&up->lock, flags); } =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 8ecd76aa22e6..02e8fdc1ea43 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -6,6 +6,7 @@ #include #include #include +#include #include "internal.h" /* * Printk console printing implementation for consoles which does not depe= nd @@ -995,3 +996,79 @@ void nbcon_free(struct console *con) =20 con->pbufs =3D NULL; } + +static inline bool uart_is_nbcon(struct uart_port *up) +{ + int cookie; + bool ret; + + if (!uart_console(up)) + return false; + + cookie =3D console_srcu_read_lock(); + ret =3D (console_srcu_read_flags(up->cons) & CON_NBCON); + console_srcu_read_unlock(cookie); + return ret; +} + +/** + * uart_nbcon_acquire - The second half of the port locking wrapper + * @up: The uart port whose @lock was locked + * + * The uart_port_lock() wrappers will first lock the spin_lock @up->lock. + * Then this function is called to implement nbcon-specific processing. + * + * If @up is an nbcon console, this console will be acquired and marked as + * unsafe. Otherwise this function does nothing. + */ +void uart_nbcon_acquire(struct uart_port *up) +{ + struct console *con =3D up->cons; + struct nbcon_context ctxt; + + if (!uart_is_nbcon(up)) + return; + + WARN_ON_ONCE(up->nbcon_locked_port); + + do { + do { + memset(&ctxt, 0, sizeof(ctxt)); + ctxt.console =3D con; + ctxt.prio =3D NBCON_PRIO_NORMAL; + } while (!nbcon_context_try_acquire(&ctxt)); + + } while (!nbcon_context_enter_unsafe(&ctxt)); + + up->nbcon_locked_port =3D true; +} +EXPORT_SYMBOL_GPL(uart_nbcon_acquire); + +/** + * uart_nbcon_release - The first half of the port unlocking wrapper + * @up: The uart port whose @lock is about to be unlocked + * + * The uart_port_unlock() wrappers will first call this function to implem= ent + * nbcon-specific processing. Then afterwards the uart_port_unlock() wrapp= ers + * will unlock the spin_lock @up->lock. + * + * If @up is an nbcon console, the console will be marked as safe and + * released. Otherwise this function does nothing. + */ +void uart_nbcon_release(struct uart_port *up) +{ + struct console *con =3D up->cons; + struct nbcon_context ctxt =3D { + .console =3D con, + .prio =3D NBCON_PRIO_NORMAL, + }; + + if (!up->nbcon_locked_port) + return; + + if (nbcon_context_exit_unsafe(&ctxt)) + nbcon_context_release(&ctxt); + + up->nbcon_locked_port =3D false; +} +EXPORT_SYMBOL_GPL(uart_nbcon_release); --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 DA5766F09C for ; Sun, 18 Feb 2024 18:57:52 +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=1708282674; cv=none; b=MrjfjejcyY12hoSJfICLYJEu1AXyJNGesjG6UCW+xUJTpm4HhSJGEThYqIyUoSlARYtlLqtH4hPldujSqoouE1T9/0M1kkrlxl/+3CzMZQnlDXqKnckVU59I3TFdA3qMiQ31rOSTd+aIQf7PXqW+atlv6YMo70MXs83plAhaxu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282674; c=relaxed/simple; bh=vTgd2itv2WvqWfA2iHd5zEucMOb4GN4MKZQhbctg1CE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=m+GfE/DmQ815XkHAIHAxqPE524y93tsrHQJ8lD8lnjKSwa7+5b6UtBpNCxafjwJf7sjMnOiaT7SE+ZpaWohlM/oK0BR9BSF9dMiOGndbtvLuk2ERdTFnbfks17SOp3hCYAcnk562lnwKIF/c3p6llGoXjy7dDiZ4VFk6KO405sE= 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=bUvH6ld6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mv3bI0iZ; 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="bUvH6ld6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mv3bI0iZ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282671; 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=3I1dHuUwYQfwJlbzcngwYe/b4Lctrl7m0BYdiHEdADM=; b=bUvH6ld6ZR2Lx7Xmdigmcn4lgfr2aDNvw4v/80RT0CXEAcaNRMKHc5jHO6u4d7wjSu/kLh xfbiIRlZLFennUA4anuz4ogw5nxq3uoQNdSr4AS9EpdIyuAyE6bm6xHKI8lP6pZSj0asaV jce8Tfn5LLyr7LDy4vYkcEfHQjv+rYzyi82pYP5oOd880wpNfwKafO0sh/KGE7/4hDp/CQ hAUPlz1qdkP9mi6CtrcL5wxhCsaz97b9WRlh33kGjePhNUgcYigxSKYU/xMeunxgaSt3Xo s3kOkTkSe7ie8C3PBR2rawdtpVOBYFIa9jCMKt4A3p5z5prM65E5vTdN5ubQkQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282671; 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=3I1dHuUwYQfwJlbzcngwYe/b4Lctrl7m0BYdiHEdADM=; b=mv3bI0iZRBTmcWWJ3oT7jB7zGmwIkGzso/mTLeCK3sGCyUsHPvlz6U4660FUqUbnlE82tO 7rxsKqaEcP+w6OCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v2 09/26] printk: nbcon: Add detailed doc for write_atomic() Date: Sun, 18 Feb 2024 20:03:09 +0106 Message-Id: <20240218185726.1994771-10-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" The write_atomic() callback has special requirements and is allowed to use special helper functions. Provide detailed documentation of the callback so that a developer has a chance of implementing it correctly. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 34 ++++++++++++++++++++++++++++++---- 1 file changed, 30 insertions(+), 4 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index e4fc6f7c1496..5c55faa013e8 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -278,7 +278,7 @@ struct nbcon_write_context { /** * struct console - The console descriptor structure * @name: The name of the console driver - * @write: Write callback to output messages (Optional) + * @write: Legacy write callback to output messages (Optional) * @read: Read callback for console input (Optional) * @device: The underlying TTY device driver (Optional) * @unblank: Callback to unblank the console (Optional) @@ -295,7 +295,6 @@ struct nbcon_write_context { * @data: Driver private data * @node: hlist node for the console list * - * @write_atomic: Write callback for atomic context * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @pbufs: Pointer to nbcon private buffer @@ -320,8 +319,35 @@ struct console { struct hlist_node node; =20 /* nbcon console specific members */ - bool (*write_atomic)(struct console *con, - struct nbcon_write_context *wctxt); + + /** + * @write_atomic: + * + * NBCON callback to write out text in any context. (Optional) + * + * This callback is called with the console already acquired. The + * callback can use nbcon_can_proceed() at any time to verify that + * it is still the owner of the console. In the case that it has + * lost ownership, it is no longer allowed to go forward. In this + * case it must back out immediately and carefully. The buffer + * content is also no longer trusted since it no longer belongs to + * the context. + * + * If the callback needs to perform actions where ownership is not + * allowed to be taken over, nbcon_enter_unsafe() and + * nbcon_exit_unsafe() can be used to mark such sections. These + * functions are also points of possible ownership transfer. If + * either function returns false, ownership has been lost. + * + * This callback can be called from any context (including NMI). + * Therefore it must avoid usage of any locking and instead rely + * on the console ownership for synchronization. + * + * Returns true if all text was successfully written out and + * ownership was never lost, otherwise false. + */ + bool (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); + atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct printk_buffers *pbufs; --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 7A6726F09D for ; Sun, 18 Feb 2024 18:57:53 +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=1708282674; cv=none; b=qnyZrIxiFk1Ee5SAEc7/U1gR30f41FRWHrB/Kxi6H6ogBk6GeBW/o++N1ofFENYuKZuXJigqzd5a8gFrbZqpBvE8Hz+aBcAfi0DJkopO0QUsyYg6fOfDOFXN/yLLfGaMrbrVqgrn8WOYNp0Tqa78K5k/+beZmkxr5qGjk/ST9nU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282674; c=relaxed/simple; bh=dox9/szQb+26iWG6sdrMGBj/6N8EK08NBiaQoryJAZY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=osFYRAlJotSEBhvSR6sqKyFBv9FFvxL+CwHA1hAuV9zXpcGOjvyYn8oZ/479QXias97SZW+48reOMEo9PVaD573Emr7jFtwzEQZIpTmevAdsOTU5PBCFJkLLwEWbqHLDXiSM2m4WjaM7t163Nqe6w5+x92hMjHCEDe2mOgq1udw= 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=n0WIo/21; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rxjV0DyE; 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="n0WIo/21"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rxjV0DyE" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282671; 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=Do8ewsprPzBc28AvFoSgrf1uIwAex3sQdWFNrubJEIE=; b=n0WIo/21+og62Xcdgg4mHDaJ9aa+CTHp0s7sApg1zCrn4OCuWxR6QQMEbiCHvbb+dFMm1L V9FnWhjtHIeb1jcwhgPgzjnfISI4Du2Fid4OhSb5q5yRNHbxovscbdrlDAnyqtr5Ot/Dvz NoOrEmSD94iNw1Ij7asEVkJqyzuQoUkXKFubKBHBbASydUM5avhQSJ/lEmOZB48MPTkOww 2fXOOOFQZuvDbsRED3yBvNqNo/77k7k+urNy+DhwMGutAIrdyj5z5wGiJ6uO7NrSrGnbmr hneukamflRKxXUyGNhrOISFNO4NJCHCVL46pWq55uSLBGxplRjEP6lnVlLUs1g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282671; 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=Do8ewsprPzBc28AvFoSgrf1uIwAex3sQdWFNrubJEIE=; b=rxjV0DyEVlmUerI569cgyyqOJTLFr3BJW/IldEcccb31Wr3M+HzMoQYeQ7O44tY+agL9J3 gl8yKmTbAJdauQDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v2 10/26] printk: nbcon: Fix kerneldoc for enums Date: Sun, 18 Feb 2024 20:03:10 +0106 Message-Id: <20240218185726.1994771-11-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Kerneldoc requires enums to be specified as such. Otherwise it is interpreted as function documentation. Signed-off-by: John Ogness Reviewed-by: Petr Mladek Reviewed-by: Randy Dunlap --- include/linux/console.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 5c55faa013e8..d8922282efa1 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -130,7 +130,7 @@ static inline int con_debug_leave(void) */ =20 /** - * cons_flags - General console flags + * enum cons_flags - General console flags * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate * output of messages that were already shown by boot * consoles or read by userspace via syslog() syscall. @@ -211,7 +211,7 @@ struct nbcon_state { static_assert(sizeof(struct nbcon_state) <=3D sizeof(int)); =20 /** - * nbcon_prio - console owner priority for nbcon consoles + * enum nbcon_prio - console owner priority for nbcon consoles * @NBCON_PRIO_NONE: Unused * @NBCON_PRIO_NORMAL: Normal (non-emergency) usage * @NBCON_PRIO_EMERGENCY: Emergency output (WARN/OOPS...) --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 B37606F09F for ; Sun, 18 Feb 2024 18:57:53 +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=1708282675; cv=none; b=dcVoXcTLsiI0pCnIrd1jBsohAEkEfIfI4+L5IZ9PdOpQ+v+XHY2FQbmWdm2PJVPHzzOdfFNBpaFDo85UFWEWQEUJ271725C638SbUrLART7jGaP1syYn58bTBdMgBvbuz/tQjlzs2pitC14QJ4avMMCIJQ+uXo6QKWmk5j2ITmU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282675; c=relaxed/simple; bh=Gn+uo9q8su4/qFeCC4JeId6lrhn6gSNTkrfRmi156Nw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZTySrhhER/jtyP0qo2schzyNrPGMWwf59Lftqb7SJ1vv61+KKTqNemRmGjzXAi8TFjs1lg0f7r9Wx2Fv8WF7ecpil3m4NIYg7QvDxCY5rKMlNqM8vmOGJ8kERzo7UTXVbMpl8j88TLGZ/72Pkqk05b2BB9GM7OBjdsQSVbbhuBg= 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=QMSBU7jD; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Y+6u7iXO; 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="QMSBU7jD"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Y+6u7iXO" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282672; 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=n24MWmzqzOOOOZwEfqW/0eRQt3f3jgwSF64cWTce6J0=; b=QMSBU7jD94Yh/o3DcWTZjh0a8JGG9Yx9WNZg5k4O77UQ4qID8e+4cRj1MXJ3SVvOAmVn2/ h2OKdZDfsKW916dakuJrYuN8Y3JU/PeeaGWdkRV/DNOoyrO1GZcHFOFGjRRPpxmAQtqwG+ Ewg+eeiAXtrsH5wjUJmkovot03PxpfwzMmJaE+Dq99ru3fkcTZgAtlnTbcXrnlgdR2iZ/T JwSdJL8smlJrWZc4SL7jk+c8UTfaTaswB08nQdr5MjdYXm7qxU1ooTR9SchSIR+StZT7xx Pw9TdcOnef41ejCiw9jH+vbIcdc7QkmT6p6JpXFPkBRSFwzjl8EF9vT9UrVZwQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282672; 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=n24MWmzqzOOOOZwEfqW/0eRQt3f3jgwSF64cWTce6J0=; b=Y+6u7iXOgtAwwO99mk362rIMqX3U2eCm3OiQleJuLdrhHEE5dahThYSucQl6FvoXuqDPcY 9Y+H+7HsP4PVEvBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 11/26] printk: Make console_is_usable() available to nbcon Date: Sun, 18 Feb 2024 20:03:11 +0106 Message-Id: <20240218185726.1994771-12-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Move console_is_usable() as-is into internal.h so that it can be used by nbcon printing functions as well. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 32 ++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 30 ------------------------------ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6c2afee5ef62..6e8c1b02adae 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -78,6 +78,36 @@ bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); =20 +/* + * Check if the given console is currently capable and allowed to print + * records. + * + * Requires the console_srcu_read_lock. + */ +static inline bool console_is_usable(struct console *con) +{ + short flags =3D console_srcu_read_flags(con); + + if (!(flags & CON_ENABLED)) + return false; + + if ((flags & CON_SUSPENDED)) + return false; + + if (!con->write) + return false; + + /* + * Console drivers may assume that per-cpu resources have been + * allocated. So unless they're explicitly marked as being able to + * cope (CON_ANYTIME) don't call them until this CPU is officially up. + */ + if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME)) + return false; + + return true; +} + #else =20 #define PRINTK_PREFIX_MAX 0 @@ -99,6 +129,8 @@ static inline bool nbcon_alloc(struct console *con) { re= turn false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } =20 +static inline bool console_is_usable(struct console *con) { return false; } + #endif /* CONFIG_PRINTK */ =20 extern struct printk_buffers printk_shared_pbufs; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index b75ca383683d..ecb50590815c 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2694,36 +2694,6 @@ int is_console_locked(void) } EXPORT_SYMBOL(is_console_locked); =20 -/* - * Check if the given console is currently capable and allowed to print - * records. - * - * Requires the console_srcu_read_lock. - */ -static inline bool console_is_usable(struct console *con) -{ - short flags =3D console_srcu_read_flags(con); - - if (!(flags & CON_ENABLED)) - return false; - - if ((flags & CON_SUSPENDED)) - return false; - - if (!con->write) - return false; - - /* - * Console drivers may assume that per-cpu resources have been - * allocated. So unless they're explicitly marked as being able to - * cope (CON_ANYTIME) don't call them until this CPU is officially up. - */ - if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME)) - return false; - - return true; -} - static void __console_unlock(void) { console_locked =3D 0; --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 0C7A36F504 for ; Sun, 18 Feb 2024 18:57:53 +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=1708282675; cv=none; b=JkReHT7NbwNe9sFEkl7DtcbCCTNfb6qHFXp0bRrNkwqFICsiKv3MRosZqXdcNM/OdpKgUuV7z1SDNb3hHLcVZzSZfVtTbU9sEy+MSPw8Hc2OqjB2jFpJap5Rz9fgCOEDfaLKl5AUwEMRs12H/AEJNvpzVY7Ny6YJZbdsYSa9tnY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282675; c=relaxed/simple; bh=VBHx4Cus2cJidu47KwQl5FHQQheHgkCUVrX8zpd9KsM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pGHncS6O3FLed5WbdgXksC1Ic56ZcbNeWllbMiA67J1I3HGDN+LuFX+j1RuNScjIT4oDQ8MBgrmIU/3uoZ1WpZ6S/0STxcz2nutDb3shyX6lCTp+J6FR5ZZbupCJVKvRFfI+uLT7Z6O8MyE4eJiEY2lwbkhRimXBgnqHDBE584E= 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=t7xu7hfV; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=3WC1PWFT; 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="t7xu7hfV"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="3WC1PWFT" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282672; 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=IUwIeSGZ+StqqGIqBIJs0cGENFsoVWre3Jl13joVS94=; b=t7xu7hfVQXpd79WP0b9+W4yS7IsMad5j3VNVWCe5WHFy2pwwlog2q66avUuTa7EurOMbPd UWvIjARCFpJvv5Jp42GoE7ne8PBdxxT6p2fsZH1ic8ur0e0ZMylY00a6CkpP8bXiwZLNb8 nihLJssxgJ6M8Vb+GyNTdG256q3I0QUsN+q3IT2ZSALYVDLd8S376vkjiitDNaogQ0Eu7O R1hQAsoBHDBDNkzF7CxwHn1Wwul0N52Iz616hQhcLL4m+AFia5+Q8ReUR95m9sQ+EEQG5j KOn9mXZuS9LqHBWEnxMeYNSdAXBAZYW55ZW+7xob6AfnzKAIQDt2WaQQF/DyXw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282672; 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=IUwIeSGZ+StqqGIqBIJs0cGENFsoVWre3Jl13joVS94=; b=3WC1PWFTwRsETdj+pq340CnfZVlD62OQIrZCKwUyr53+TZcJsH3jvnZzhsPN/IiZFOHmHU 7MQyIjPv75805ICw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 12/26] printk: Let console_is_usable() handle nbcon Date: Sun, 18 Feb 2024 20:03:12 +0106 Message-Id: <20240218185726.1994771-13-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" The nbcon consoles use a different printing callback. For nbcon consoles, check for the write_atomic() callback instead of write(). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6e8c1b02adae..69c8861be92c 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -80,6 +80,8 @@ void nbcon_free(struct console *con); =20 /* * Check if the given console is currently capable and allowed to print + * records. Note that this function does not consider the current context, + * which can also play a role in deciding if @con can be used to print * records. * * Requires the console_srcu_read_lock. @@ -94,8 +96,13 @@ static inline bool console_is_usable(struct console *con) if ((flags & CON_SUSPENDED)) return false; =20 - if (!con->write) - return false; + if (flags & CON_NBCON) { + if (!con->write_atomic) + return false; + } else { + if (!con->write) + return false; + } =20 /* * Console drivers may assume that per-cpu resources have been --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 470346F50C for ; Sun, 18 Feb 2024 18:57:54 +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=1708282675; cv=none; b=bTVB4U3gorWFZCM9rTR+7W7fWi/qCziwgEkGLSi5AYqKgs11rFivksI/tcxHDsuGn+xaxthhEnW7F75HSPdKv4zM5IoA6WRYLTTT4nXF+v1NwlEbIQQ0VKSBJ0wg99CWAqiTARwQC86kuhRZYoHGfHQdnJPdzNDUyyk9KCuxyBk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282675; c=relaxed/simple; bh=KWSkXCKQeNrr5eZStT4LqIX1rH8fREQLBiX0c1gHrHM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sy7ozQXYskoiRyAtZtwEyjfCsjAGRiVQKSZ2i3KrMthUfKmJJGr33q28amZr0BpW2oMb+EAu71HkcrglfQvI6DbV20pooeFJo0sBQPMrxLgShppA2dRKU1Ocl0OfE6AyzqcKLqouIXQN/BLP3kxLMyuDKvAsnFebJDNsnv54wfE= 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=UiMlLlop; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=gFUUiIHd; 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="UiMlLlop"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="gFUUiIHd" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282672; 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=0GibUZTFTeq8Azv+JjrVAmh6V7HAl4otIfefb2UG9wk=; b=UiMlLlop/JEpMBas0X+fajoIusei1Be/FpyJcoty5xeAu1KPqr30RJO4Hqf8X8OyGyDfXN nU4N3qCrcwCfyCoU3j+3RzzC96tUZFGkLzMmvIpCbG4POseS/+TEi98WGoMo/Zyz3K/YMe F/dZ2U45Azmi1NXFJX/PuiIfwJVVEGkhkbBKuu5yW2J4OfBtwmceEveWAu9QcNEa+xd6+1 5tjS6hUYg9cj0kG37YdxaCUukxh3+QoatNCcg9S1ejaC+ZS/pbp7beRR+3DY/OKSYzPF4F AfbBSmFwHRjJTseChgbZxHA9q41FgyWWNUd0Z6lBsrjGTPjwE/6NnKv2Z/j22Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282672; 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=0GibUZTFTeq8Azv+JjrVAmh6V7HAl4otIfefb2UG9wk=; b=gFUUiIHdAlO9f732mfLv2nx9vP+orySfBI5//27FsQFvL56KV9Ud3P30BMUojDQAZxZS0G GlaQl5HxOC/sgCDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 13/26] printk: Add @flags argument for console_is_usable() Date: Sun, 18 Feb 2024 20:03:13 +0106 Message-Id: <20240218185726.1994771-14-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" The caller of console_is_usable() usually needs @console->flags for its own checks. Rather than having console_is_usable() read its own copy, make the caller pass in the @flags. This also ensures that the caller saw the same @flags value. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 8 ++------ kernel/printk/printk.c | 5 +++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 69c8861be92c..6780911fa8f2 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -83,13 +83,9 @@ void nbcon_free(struct console *con); * records. Note that this function does not consider the current context, * which can also play a role in deciding if @con can be used to print * records. - * - * Requires the console_srcu_read_lock. */ -static inline bool console_is_usable(struct console *con) +static inline bool console_is_usable(struct console *con, short flags) { - short flags =3D console_srcu_read_flags(con); - if (!(flags & CON_ENABLED)) return false; =20 @@ -136,7 +132,7 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } =20 -static inline bool console_is_usable(struct console *con) { return false; } +static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } =20 #endif /* CONFIG_PRINTK */ =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index ecb50590815c..9d56ce5837f9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2939,9 +2939,10 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove =20 cookie =3D console_srcu_read_lock(); for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); bool progress; =20 - if (!console_is_usable(con)) + if (!console_is_usable(con, flags)) continue; any_usable =3D true; =20 @@ -3783,7 +3784,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre * that they make forward progress, so only increment * @diff for usable consoles. */ - if (!console_is_usable(c)) + if (!console_is_usable(c, flags)) continue; =20 if (flags & CON_NBCON) { --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 B6B956F520 for ; Sun, 18 Feb 2024 18:57:54 +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=1708282677; cv=none; b=OJseM3BEauP2lSo8Ys7Wd/NUTf8IdNHsgmhSGXWJoU97l0iPdBehceeR4CZf45zTzuehCj13B4PNDXTYSakU1VmvFA5Hm3e4/IZudCMeJijxU+Xj132xB0amLBWeKbgSO7ZELZY7LchKLc04aDbrmQXg9rHMHXjgAsd94diuRP4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282677; c=relaxed/simple; bh=aVOu1/bqDAARdrnRU5+DODTSrgXpJ65t9RLdNz7m7TU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZQzItlAGpVE8bUo+Gye7NxtglewX8vomZEQktW7GCjhbY4OZCdG6kP/wNGMnl9LMJy8Tlh45T9wNWcMKsv2TXkWMoQvGfQH+0V6JIDzlgUBntiBm1ACqiTsYnGKG+BonfKuR8Mx82NCCuzgPzu3MWzBwcQEVX4deDQO6RRlj7Mo= 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=QP9FA40Y; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FhOTMESp; 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="QP9FA40Y"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FhOTMESp" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282672; 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=glYrKDUJ7lHx0vPgFl5st9OXHzyL6p/mgGcT2k0j/yc=; b=QP9FA40YAYELeVxUBZi0Zk2entx086hATCdFn7fwEWkwoePQdmQY0Zj0BaqefkCK4vyKhE yRkaQCR/GZDq+3tMY85Aso10KSnz9ewFoQR0uzw5xX7UEx2kQvU6snT7OpbrX6IUFeAuZ8 fOSXwvqW+QtcEVp3Oojb3qIw7rd/+Txlt4JS0gailqcqmpWZNwOF1AWvEl9rg0nLp1KJuz pO222FA/6ucaoV7cZ+z7J+PSOdW/EK/nUEaEJzVNFX95ipdWO4CLM5FJhy2oe4RcPyY6zw JrZHmJEV1me0Qx4e7u6kZE3qICmMgHtfiV/CuPCLnfNVRHS1Rgz5ka538z3gfw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282672; 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=glYrKDUJ7lHx0vPgFl5st9OXHzyL6p/mgGcT2k0j/yc=; b=FhOTMESp09Y0esdlb4LAnktBJn/wIjVMQZnSHidLDRzBe14BT9TOvFuRuyAZ4oZ7T1ZDDm /HeXGmslX87eNKBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 14/26] printk: nbcon: Provide function to flush using write_atomic() Date: Sun, 18 Feb 2024 20:03:14 +0106 Message-Id: <20240218185726.1994771-15-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" From: Thomas Gleixner Provide nbcon_atomic_flush_all() to perform flushing of all registered nbcon consoles using their write_atomic() callback. Like with legacy consoles, the nbcon consoles are flushed one record per console. This allows all nbcon consoles to print lines pseudo-simultaneously, rather than one console waiting for the full ringbuffer to dump to another console before printing anything. Unlike console_flush_all(), nbcon_atomic_flush_all() will only flush up through the newest record at the time of the call. This prevents a CPU from printing unbounded when other CPUs are adding records. Perform nbcon console atomic flushing in console_flush_on_panic(). This function is not only used in panic() but also other locations where there may be stored messages that need to be flushed. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 + kernel/printk/nbcon.c | 100 ++++++++++++++++++++++++++++++++++++++- kernel/printk/printk.c | 2 + 3 files changed, 102 insertions(+), 2 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6780911fa8f2..d9a5205692fc 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -77,6 +77,7 @@ void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); +void nbcon_atomic_flush_all(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -131,6 +132,7 @@ static inline void nbcon_seq_force(struct console *con,= u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } +static inline void nbcon_atomic_flush_all(void) { } =20 static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 02e8fdc1ea43..2eb2929c1027 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -539,7 +539,6 @@ static struct printk_buffers panic_nbcon_pbufs; * in an unsafe state. Otherwise, on success the caller may assume * the console is not in an unsafe state. */ -__maybe_unused static bool nbcon_context_try_acquire(struct nbcon_context *ctxt) { unsigned int cpu =3D smp_processor_id(); @@ -841,7 +840,6 @@ EXPORT_SYMBOL_GPL(nbcon_exit_unsafe); * When true is returned, @wctxt->ctxt.backlog indicates whether there are * still records pending in the ringbuffer, */ -__maybe_unused static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); @@ -930,6 +928,104 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * nbcon_atomic_emit_one - Print one record for an nbcon console using the + * write_atomic() callback + * @wctxt: An initialized write context struct to use + * for this context + * + * Return: False if the given console could not print a record or there + * are no more records to print, otherwise true. + * + * This is an internal helper to handle the locking of the console before + * calling nbcon_emit_next_record(). + */ +static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + + if (!nbcon_context_try_acquire(ctxt)) + return false; + + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + */ + if (!nbcon_emit_next_record(wctxt)) + return false; + + nbcon_context_release(ctxt); + + return ctxt->backlog; +} + +/** + * __nbcon_atomic_flush_all - Flush all nbcon consoles using their + * write_atomic() callback + * @stop_seq: Flush up until this record + */ +static void __nbcon_atomic_flush_all(u64 stop_seq) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + struct console *con; + bool any_progress; + int cookie; + + do { + any_progress =3D false; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + unsigned long irq_flags; + + if (!(flags & CON_NBCON)) + continue; + + if (!console_is_usable(con, flags)) + continue; + + if (nbcon_seq_read(con) >=3D stop_seq) + continue; + + memset(ctxt, 0, sizeof(*ctxt)); + ctxt->console =3D con; + ctxt->spinwait_max_us =3D 2000; + ctxt->prio =3D NBCON_PRIO_NORMAL; + + /* + * Atomic flushing does not use console driver + * synchronization (i.e. it does not hold the port + * lock for uart consoles). Therefore IRQs must be + * disabled to avoid being interrupted and then + * calling into a driver that will deadlock trying + * acquire console ownership. + */ + local_irq_save(irq_flags); + + any_progress |=3D nbcon_atomic_emit_one(&wctxt); + + local_irq_restore(irq_flags); + } + console_srcu_read_unlock(cookie); + } while (any_progress); +} + +/** + * nbcon_atomic_flush_all - Flush all nbcon consoles using their + * write_atomic() callback + * + * Flush the backlog up through the currently newest record. Any new + * records added while flushing will not be flushed. This is to avoid + * one CPU printing unbounded because other CPUs continue to add records. + */ +void nbcon_atomic_flush_all(void) +{ + __nbcon_atomic_flush_all(prb_next_reserve_seq(prb)); +} + /** * nbcon_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 9d56ce5837f9..ea170ade4d42 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3169,6 +3169,8 @@ void console_flush_on_panic(enum con_flush_mode mode) console_srcu_read_unlock(cookie); } =20 + nbcon_atomic_flush_all(); + console_flush_all(false, &next_seq, &handover); } =20 --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 628406FB86 for ; Sun, 18 Feb 2024 18:57:55 +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=1708282677; cv=none; b=miYAzYkJaujzTcoE3WgimXqdloJ2+rC6t5E7/Osr5tjSRXu++APozVRh2SV9trmSb9RNJ/UW6FophEf9rLC3AZpz1f04QrgOJ8zLgv/C0UaAaXkbUN7EbDZNA7G/4FjqNHVGKldYwmkVOi2WjQWAwzXAbjmrvWep4TxFfPtZy/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282677; c=relaxed/simple; bh=44GMokOuhofcbVPi1PR9Ui7+7904b0FBavmkwZ/WX3M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QUBl1w3al/mN1IT1Bi2CHZxDEnuuTrCip1GDUaPAWYNmQQP1sEB5qFoet0vUoJQ70fkew6aCl8vQ0Kh15cobEhijMrcowoUpdzQ1OgghTXPLuv3Aj4u072eECXOAeNJleCoTGV9/sGcOwVSLQm7ESoENFXUX3jfMe0HCHt3+Dt0= 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=rJ1/Wdf4; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=9QfHj5ew; 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="rJ1/Wdf4"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="9QfHj5ew" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282673; 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=QA/99sNBe74Zpg0DWyhl5wv3RvxapTRr2AVbgdHNKI4=; b=rJ1/Wdf4YXXUQmodc7+URL0ljrOU8z6ZTYWtKB2i5KhN1BD61MJz2T+1+1cvhlgpLJwpQO /gbCtKVFxov4Q2DsM1zr+Me+kviqt+CDoGYOgzwqqZ4Ikf9ZRJVIf6E5btxSqpX7cAZzy1 YUZy+4lQK0+3DNveay7/3XOWXQu4DgJH1QDUlDn/RoCiorZULjjdyofJq6xsjOPs3ek93L 7679/EUmaZYUedbO9bv9jXkxFlBf4yL+8OrB1fzlv8hQa+D4Pnr1jGgci8IOfHhJjA0BP7 P/uJAfRzY/GoJovcz8Umt2cb91rwFg9Kw3848oKAb+h+XsWENrq49de0SqTqsQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282673; 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=QA/99sNBe74Zpg0DWyhl5wv3RvxapTRr2AVbgdHNKI4=; b=9QfHj5ew/k0LsjwUvp0IoPme/gb2gFwSzUmf3FCspKkLClGC+ux0FPNIurkWNS3ZRu814F ocrdcs+7MlSWVvAw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 15/26] printk: Track registered boot consoles Date: Sun, 18 Feb 2024 20:03:15 +0106 Message-Id: <20240218185726.1994771-16-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Unfortunately it is not known if a boot console and a regular (legacy or nbcon) console use the same hardware. For this reason they must not be allowed to print simultaneously. For legacy consoles this is not an issue because they are already synchronized with the boot consoles using the console lock. However nbcon consoles can be triggered separately. Add a global flag @have_boot_console to identify if any boot consoles are registered. This will be used in follow-up commits to ensure that boot consoles and nbcon consoles cannot print simultaneously. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index ea170ade4d42..1b14159990ba 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -463,6 +463,14 @@ static int console_msg_format =3D MSG_FORMAT_DEFAULT; /* syslog_lock protects syslog_* variables and write access to clear_seq. = */ static DEFINE_MUTEX(syslog_lock); =20 +/* + * Specifies if a boot console is registered. If boot consoles are present, + * nbcon consoles cannot print simultaneously and must be synchronized by + * the console lock. This is because boot consoles and nbcon consoles may + * have mapped the same hardware. + */ +bool have_boot_console; + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -3499,6 +3507,9 @@ void register_console(struct console *newcon) if (newcon->flags & CON_NBCON) nbcon_init(newcon); =20 + if (newcon->flags & CON_BOOT) + have_boot_console =3D true; + /* * Put this console in the list - keep the * preferred driver at the head of the list. @@ -3551,6 +3562,8 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + bool found_boot_con =3D false; + struct console *c; int res; =20 lockdep_assert_console_list_lock_held(); @@ -3598,6 +3611,17 @@ static int unregister_console_locked(struct console = *console) if (console->exit) res =3D console->exit(console); =20 + /* + * With this console gone, the global flags tracking registered + * console types may have changed. Update them. + */ + for_each_console(c) { + if (c->flags & CON_BOOT) + found_boot_con =3D true; + } + if (!found_boot_con) + have_boot_console =3D false; + return res; } =20 --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 627F86FB84 for ; Sun, 18 Feb 2024 18:57:55 +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=1708282677; cv=none; b=DEGltN6YM9rIBkIlNY64Fek8hAZlWb29rTBsdvYT6UmFMGp+vOAjV/SaHd81kjJFbcQvwK5YScO+n/q15vMZeBIMSoxcPvg+6Pdvm3ScLuzSZOXA8IqyFHXkd5PYGQaogFIiDgPlDDEgNmr34AsJa87vnxlRV/JDR5yB5EsD/7M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282677; c=relaxed/simple; bh=XNAFOxzMyR2DiCX23eg6uCGQcbosdfuXh1/QvXBV+3g=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JRqiAAdJ7N95ew4LdmxCwK69cSShxTUxsdM9AoCw26HOqT+aYJG2BLX/BwWRWuGZpY7HasekPRSzU4Ax7ftWlpmPgq4rMwnJqqbPjVv2/kDKZ8Y1tcVNinwke1bk8HBpSvDHjuw2yDSZCR8+Y9sK7HVRpvllKD68AbV8/xojtDc= 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=X1pn8gUl; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=uRlGqUPm; 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="X1pn8gUl"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="uRlGqUPm" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282673; 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=2WpRiuHUchJ8PUhBQPppc9wlrZ+BaiMWpumcr51nxKc=; b=X1pn8gUlKUoMEiWMnaptlfxcDZ/fGoV6vTRapTZO3rzlah99nQ7MUPx+Ha+jrZN1cvTGZK JrPWwd+KnMJUZjxwwHxTkgZVzelM+KIUL80N/0gM6KtqB55uszHNyVCyXY/pQk0wvOMwkK N6sB+qCR2oVPZG6QvDCPvmLZ57GaYjSgXdCKjpF4NqvFXXlZsZqnR2BmuZf9AIg8n8iGUl S8LlAegzwnddOuEvKG4KlklwB/76QjbzJ5Wyth4ktGSyyjuLFureb94qasBM6jQBt0Zwl7 aOLDzHlfdM2jKBlHAaa6J73DIx02d5aN2C4UQXkBVqFDNc0DtFwIBf7GnVdWRw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282673; 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=2WpRiuHUchJ8PUhBQPppc9wlrZ+BaiMWpumcr51nxKc=; b=uRlGqUPmvzNMGbcXT8nTGeSPZO0Nk1Wof15ZY2YyL6MAW1gTwVA2JE+R49msvY6HL7XMAh Ch1jTVfhrX2veLBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 16/26] printk: nbcon: Use nbcon consoles in console_flush_all() Date: Sun, 18 Feb 2024 20:03:16 +0106 Message-Id: <20240218185726.1994771-17-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Allow nbcon consoles to print messages in the legacy printk() caller context (printing via unlock) by integrating them into console_flush_all(). The write_atomic() callback is used for printing. Provide nbcon_legacy_emit_next_record(), which acts as the nbcon variant of console_emit_next_record(). Call this variant within console_flush_all() for nbcon consoles. Since nbcon consoles use their own @nbcon_seq variable to track the next record to print, this also must be appropriately handled. Signed-off-by: John Ogness --- kernel/printk/internal.h | 6 ++++++ kernel/printk/nbcon.c | 45 ++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 19 ++++++++++++----- 3 files changed, 65 insertions(+), 5 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index d9a5205692fc..b34847ec6b0d 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -71,6 +71,8 @@ void defer_console_output(void); =20 u16 printk_parse_prefix(const char *text, int *level, enum printk_info_flags *flags); +void console_lock_spinning_enable(void); +int console_lock_spinning_disable_and_check(int cookie); =20 u64 nbcon_seq_read(struct console *con); void nbcon_seq_force(struct console *con, u64 seq); @@ -78,6 +80,8 @@ bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); void nbcon_atomic_flush_all(void); +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie); =20 /* * Check if the given console is currently capable and allowed to print @@ -133,6 +137,8 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } static inline void nbcon_atomic_flush_all(void) { } +static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, + int cookie) { return false; } =20 static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 2eb2929c1027..747f5cbfe5ee 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -532,6 +532,7 @@ static struct printk_buffers panic_nbcon_pbufs; * nbcon_context_try_acquire - Try to acquire nbcon console * @ctxt: The context of the caller * + * Context: Any context which could not be migrated to another CPU. * Return: True if the console was acquired. False otherwise. * * If the caller allowed an unsafe hostile takeover, on success the @@ -960,6 +961,50 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_c= ontext *wctxt) return ctxt->backlog; } =20 +/** + * nbcon_legacy_emit_next_record - Print one record for an nbcon console + * in legacy contexts + * @con: The console to print on + * @handover: Will be set to true if a printk waiter has taken over the + * console_lock, in which case the caller is no longer holding + * both the console_lock and the SRCU read lock. Otherwise it + * is set to false. + * @cookie: The cookie from the SRCU read lock. + * + * Context: Any context which could not be migrated to another CPU. + * Return: True if a record could be printed, otherwise false. + * + * This function is meant to be called by console_flush_all() to print rec= ords + * on nbcon consoles from legacy context (printing via console unlocking). + * Essentially it is the nbcon version of console_emit_next_record(). + */ +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + bool progress =3D false; + unsigned long flags; + + *handover =3D false; + + /* Use the same procedure as console_emit_next_record(). */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + stop_critical_timings(); + + ctxt->console =3D con; + ctxt->prio =3D NBCON_PRIO_NORMAL; + + progress =3D nbcon_atomic_emit_one(&wctxt); + + start_critical_timings(); + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + + return progress; +} + /** * __nbcon_atomic_flush_all - Flush all nbcon consoles using their * write_atomic() callback diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1b14159990ba..d91771fb306e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1871,7 +1871,7 @@ static bool console_waiter; * there may be a waiter spinning (like a spinlock). Also it must be * ready to hand over the lock at the end of the section. */ -static void console_lock_spinning_enable(void) +void console_lock_spinning_enable(void) { /* * Do not use spinning in panic(). The panic CPU wants to keep the lock. @@ -1910,7 +1910,7 @@ static void console_lock_spinning_enable(void) * * Return: 1 if the lock rights were passed, 0 otherwise. */ -static int console_lock_spinning_disable_and_check(int cookie) +int console_lock_spinning_disable_and_check(int cookie) { int waiter; =20 @@ -2948,13 +2948,22 @@ static bool console_flush_all(bool do_cond_resched,= u64 *next_seq, bool *handove cookie =3D console_srcu_read_lock(); for_each_console_srcu(con) { short flags =3D console_srcu_read_flags(con); + u64 printk_seq; bool progress; =20 if (!console_is_usable(con, flags)) continue; any_usable =3D true; =20 - progress =3D console_emit_next_record(con, handover, cookie); + if (flags & CON_NBCON) { + progress =3D nbcon_legacy_emit_next_record(con, handover, cookie); + + printk_seq =3D nbcon_seq_read(con); + } else { + progress =3D console_emit_next_record(con, handover, cookie); + + printk_seq =3D con->seq; + } =20 /* * If a handover has occurred, the SRCU read lock @@ -2964,8 +2973,8 @@ static bool console_flush_all(bool do_cond_resched, u= 64 *next_seq, bool *handove return false; =20 /* Track the next of the highest seq flushed. */ - if (con->seq > *next_seq) - *next_seq =3D con->seq; + if (printk_seq > *next_seq) + *next_seq =3D printk_seq; =20 if (!progress) continue; --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 CEB9371B2F for ; Sun, 18 Feb 2024 18:57:55 +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=1708282677; cv=none; b=Y9mYu8xAWujFVHJL4YN45dcPm66F9YSqrRq/0uknJZazR7BDCC3xLdcyfXesUt5MLf7OlClVDuW+3bsgathwn/sq+uhKXsautDhq8kOOGvO3HCr9R113agEFc0npg1GsJuZtXmKpj2Po27X5qcQw15/BNB7++7fRr6vBojzCwK0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282677; c=relaxed/simple; bh=cFmrlN6+bjkYvopWH2Q6A0YP81LxOASzKK6Wrrcc1LQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=GZqveQ+1skRgw8If4+KeMTOF0wIiYzgaUwwN/GTTR8EbWpcSaUPL1R2zUdCrj7y83FFyVW5AZPO05buv+BK3g8X4qcJ9fSiOX5W/03DFVpNf/2Sq0MiOwmsKpqeQXBS+ICdv3kp6T3eAaOuByrW353Mj4JLIHoH4AyEYymul3mI= 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=ct5W9yD3; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bUykeJ6U; 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="ct5W9yD3"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bUykeJ6U" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282673; 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=U8tKxZn6xYxnbLSfcQWQs6Fl73klYk0DkiEPSmtK48M=; b=ct5W9yD3PDE9b+waw7Odwy0FtNYmwplTQMHj7vKc38guEZ6Q2ZlG5ci0CehFfccgGPgjg1 xZqnIMdEJjovy2Okdt4hNLdtviWsJkQMQN0n2nbQhErdFGOYg9ZzTMrM0pi4amHME0hrye OH2RgT8hoFRLlY/Y8Z0zhTm4rclmFiZfaEFNwdyqZlXrtPDR3/GYFGD/x5ITcjV5KRtdfo vOiXdA4PZ4vk9m9eKs6Ttp/l3XIPTrOwAH5o3V5aZ/fZia2chddSqPEWmtjRNtdHyudDrf 4p+ewt98SR4bpsirn1cFSPTvkL3eaCPv3WXTHg2oz33OW7u5fbqc+gYE7z2jag== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282673; 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=U8tKxZn6xYxnbLSfcQWQs6Fl73klYk0DkiEPSmtK48M=; b=bUykeJ6UlUC/VGxARJe/pHOyyjNV24ASEP11nA7i+rlF5J42Ds+TgwQLCsZcgLTvvgadip yi/pkG1662XnHSDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 17/26] printk: nbcon: Assign priority based on CPU state Date: Sun, 18 Feb 2024 20:03:17 +0106 Message-Id: <20240218185726.1994771-18-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Use the current state of the CPU to determine which priority to assign to the printing context. Note: The uart_port wrapper, which is responsible for non-console- printing activities, will always use NORMAL priority. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 ++ kernel/printk/nbcon.c | 30 ++++++++++++++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index b34847ec6b0d..70e9a1ea75be 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -79,6 +79,7 @@ void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); +enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_all(void); bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, int cookie); @@ -136,6 +137,7 @@ static inline void nbcon_seq_force(struct console *con,= u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } +static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } static inline void nbcon_atomic_flush_all(void) { } static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, int cookie) { return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 747f5cbfe5ee..1a18549e43b8 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -961,6 +961,22 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_c= ontext *wctxt) return ctxt->backlog; } =20 +/** + * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon + * printing on the current CPU + * + * Context: Any context which could not be migrated to another CPU. + * Return: The nbcon_prio to use for acquiring an nbcon console in this + * context for printing. + */ +enum nbcon_prio nbcon_get_default_prio(void) +{ + if (this_cpu_in_panic()) + return NBCON_PRIO_PANIC; + + return NBCON_PRIO_NORMAL; +} + /** * nbcon_legacy_emit_next_record - Print one record for an nbcon console * in legacy contexts @@ -994,7 +1010,7 @@ bool nbcon_legacy_emit_next_record(struct console *con= , bool *handover, stop_critical_timings(); =20 ctxt->console =3D con; - ctxt->prio =3D NBCON_PRIO_NORMAL; + ctxt->prio =3D nbcon_get_default_prio(); =20 progress =3D nbcon_atomic_emit_one(&wctxt); =20 @@ -1038,7 +1054,6 @@ static void __nbcon_atomic_flush_all(u64 stop_seq) memset(ctxt, 0, sizeof(*ctxt)); ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; - ctxt->prio =3D NBCON_PRIO_NORMAL; =20 /* * Atomic flushing does not use console driver @@ -1047,9 +1062,14 @@ static void __nbcon_atomic_flush_all(u64 stop_seq) * disabled to avoid being interrupted and then * calling into a driver that will deadlock trying * acquire console ownership. + * + * This also disables migration in order to get the + * current CPU priority. */ local_irq_save(irq_flags); =20 + ctxt->prio =3D nbcon_get_default_prio(); + any_progress |=3D nbcon_atomic_emit_one(&wctxt); =20 local_irq_restore(irq_flags); @@ -1161,6 +1181,9 @@ static inline bool uart_is_nbcon(struct uart_port *up) * * If @up is an nbcon console, this console will be acquired and marked as * unsafe. Otherwise this function does nothing. + * + * nbcon consoles acquired via the port lock wrapper always use priority + * NBCON_PRIO_NORMAL. */ void uart_nbcon_acquire(struct uart_port *up) { @@ -1195,6 +1218,9 @@ EXPORT_SYMBOL_GPL(uart_nbcon_acquire); * * If @up is an nbcon console, the console will be marked as safe and * released. Otherwise this function does nothing. + * + * nbcon consoles acquired via the port lock wrapper always use priority + * NBCON_PRIO_NORMAL. */ void uart_nbcon_release(struct uart_port *up) { --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 4ECC36F091 for ; Sun, 18 Feb 2024 18:57:55 +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=1708282678; cv=none; b=Miht3BAc8cu6YvwqSrL0YndnZdiGwNoGLPB1GaxXKhKuGhTjxKHFOWDrtkoaivOGyxinWNxSzi2Z4/x8g+8I2ojsNHFeVDQpJBkge4/1Mi9WR5YRqU0hlnrwe49bSyyzhwvgJF5dDEXgxA7Abi38d7FYOG6Ajv+724agfYPInzg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282678; c=relaxed/simple; bh=81eO5T4mRHN1U1zZes/KHjNFY37KXcnTqwj3ihqj47c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=O5KVFe1WHb+Y25CNsKZTtUSHpVBNMOugyJV+9Yw2K4BV6QAVnwvHaQYNzJnujrN4tFAGlQnCv5JuXrT06m3sH4vFBZ3O2QDOzeQ4BBH12W3gyUzu6GPJWMVWU2p33eGpUy7UbJKiFAfqRwRB6/6W1ep2Orj8npOkjZwXXCumeDc= 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=FXZwC3OF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Xzyu0Cxe; 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="FXZwC3OF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Xzyu0Cxe" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282674; 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=Bzed56TBLrwPGrN35dvYqd3cTuWFkQirwReZT+CtxsI=; b=FXZwC3OFc25rzIrHikF4WtdBv2APWCF5VEmZ2scnH8HpzoR0dg4QM35v9Ua/AvOJKwkSPg THLEn5wdfOtxt0QIcDei+R59stZHcg5rB2mVzBiyQ5VtElXAAqf7EtAEJkdF8EziTDXF8I DNcTA52EkfWu8F29D2aq6RdaSElN8M+S6rjvlLTkhZ9XDBCQ/xspUVVPGh0Z/u7dCwNCEK HDo3YielB5lvVcH5N0jUymejn9XbGl3M4ne16/6+5y34jYUIDaO4Hm1lI/q8yuBzObKYLE BbO2oNQA1RVGAC33fMqMSK28dm6w5Oax8qBX3bPFiL28ehk7ZjMEd4t7kxCIzQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282674; 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=Bzed56TBLrwPGrN35dvYqd3cTuWFkQirwReZT+CtxsI=; b=Xzyu0CxexcMZiEDy4gQzCjfvKEd0rZp1n39XpIQTaMfLxZmfj0urMk/8z9PuagwSCHywl6 WDAJh5fw296pSIBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Uros Bizjak , "Guilherme G. Piccoli" , Lukas Wunner , Arnd Bergmann Subject: [PATCH printk v2 18/26] printk: nbcon: Add unsafe flushing on panic Date: Sun, 18 Feb 2024 20:03:18 +0106 Message-Id: <20240218185726.1994771-19-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Add nbcon_atomic_flush_unsafe() to flush all nbcon consoles using the write_atomic() callback and allowing unsafe hostile takeovers. Call this at the end of panic() as a final attempt to flush any pending messages. Note that legacy consoles use unsafe methods for flushing from the beginning of panic (see bust_spinlocks()). Therefore, systems using both legacy and nbcon consoles may still fail to see panic messages due to unsafe legacy console usage. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 5 +++++ kernel/panic.c | 1 + kernel/printk/nbcon.c | 18 ++++++++++++++++-- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index ef57a4d93ae2..fe37395af9e2 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -199,6 +199,7 @@ extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); extern void uart_nbcon_acquire(struct uart_port *up); extern void uart_nbcon_release(struct uart_port *up); +void nbcon_atomic_flush_unsafe(void); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -287,6 +288,10 @@ static inline void uart_nbcon_release(struct uart_port= *up) { } =20 +static inline void nbcon_atomic_flush_unsafe(void) +{ +} + #endif =20 bool this_cpu_in_panic(void); diff --git a/kernel/panic.c b/kernel/panic.c index f22d8f33ea14..c039f8e1ddae 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -453,6 +453,7 @@ void panic(const char *fmt, ...) * Explicitly flush the kernel log buffer one last time. */ console_flush_on_panic(CONSOLE_FLUSH_PENDING); + nbcon_atomic_flush_unsafe(); =20 local_irq_enable(); for (i =3D 0; ; i +=3D PANIC_TIMER_STEP) { diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 1a18549e43b8..64dedd79e880 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1025,8 +1025,9 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * __nbcon_atomic_flush_all - Flush all nbcon consoles using their * write_atomic() callback * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers */ -static void __nbcon_atomic_flush_all(u64 stop_seq) +static void __nbcon_atomic_flush_all(u64 stop_seq, bool allow_unsafe_takeo= ver) { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); @@ -1054,6 +1055,7 @@ static void __nbcon_atomic_flush_all(u64 stop_seq) memset(ctxt, 0, sizeof(*ctxt)); ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; + ctxt->allow_unsafe_takeover =3D allow_unsafe_takeover; =20 /* * Atomic flushing does not use console driver @@ -1088,7 +1090,19 @@ static void __nbcon_atomic_flush_all(u64 stop_seq) */ void nbcon_atomic_flush_all(void) { - __nbcon_atomic_flush_all(prb_next_reserve_seq(prb)); + __nbcon_atomic_flush_all(prb_next_reserve_seq(prb), false); +} + +/** + * nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their + * write_atomic() callback and allowing unsafe hostile takeovers + * + * Flush the backlog up through the currently newest record. Unsafe hostile + * takeovers will be performed, if necessary. + */ +void nbcon_atomic_flush_unsafe(void) +{ + __nbcon_atomic_flush_all(prb_next_reserve_seq(prb), true); } =20 /** --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 1572B71B38 for ; Sun, 18 Feb 2024 18:57:56 +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=1708282678; cv=none; b=bLKuVVeqPeH+SyLFZ7iixn1Fi2aSJjOnfDDReA+am4t7ENjV4mhTjLludCnCQ2bz9N2F6JZVUVwG1pa9X0B7m1WgxyrW0f+UGOGr8kvwJO3ENmZyKtnczHzmu7SuCyUoArtba77KsPCmrmhhNamYu8yh7hSN7cFkQmFaoAltAZo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282678; c=relaxed/simple; bh=MSC0yqFA6MdNJ32n25Ewi0S5rdt3EruLjFiW5QHefOM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EsAIrrXpXwMaGkEG76BtnDf5qim/DgOUyUuzOgMbuLTgH12frpHMMka0k4cdjKWjHYsU3UvNux9M07m+A1Wx6i+d0v8fPyuzC3Q2/uamQWzVMuW94hFOcGr7pfH+z4+tOqw6XcRKBLOF6E8w/guSyVUuq/WoGEwbUVodo3aUZaI= 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=mjJqDNK5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=G5yGs3GU; 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="mjJqDNK5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="G5yGs3GU" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282674; 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=OotBUrgxQCK8IqOMKIc8OhTnB4TB5YG6MBjdk6yC3WQ=; b=mjJqDNK5XCubYFdP056JtAZ04KsiV7Wk86CvvOfY+crckzRYKpiugcbGviQBzTNGVrijWt 19sbo16TQj7aQihMxIbDCyl7D5xYL8/MREaX5AQ/KkizNb+2e/SGoxVXzV/jVRxgR6prmc VbT+4J8w5mbBKFXAKVV69TvNrwhJb1gSVjxXZDEvV7UyOXWEHm55fkL5FUGeJVCbzxmNpV r25ozsl7yIb3rqJdDcsxfjNgfW755XFsGsDjwBoppaz2+gOVFzGOkB51UVA3pAzjBvfXFv 6o2jgsAtAAi+6uhHzM5W5s5TCWcl5FmLtd1KzyM488ITX4sN92pcHgGXDW908g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282674; 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=OotBUrgxQCK8IqOMKIc8OhTnB4TB5YG6MBjdk6yC3WQ=; b=G5yGs3GUU0mAY87B/KytgBTfHgHysjBH+QIsv42gny1YAb8pS7ureudbJ1tyjF4DHEH3xi e6D3hDWWrHtK5iCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 19/26] printk: Avoid console_lock dance if no legacy or boot consoles Date: Sun, 18 Feb 2024 20:03:19 +0106 Message-Id: <20240218185726.1994771-20-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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 console lock is used to attempt legacy-type printing even if there are no legacy or boot consoles registered. If no such consoles are registered, the console lock does not need to be taken. Add tracking of legacy console registration and use it with boot console tracking to avoid unnecessary code paths, i.e. do not use the console lock if there are no boot consoles and no legacy consoles. Signed-off-by: John Ogness --- kernel/printk/internal.h | 12 ++++++++ kernel/printk/printk.c | 59 ++++++++++++++++++++++++++++++---------- 2 files changed, 56 insertions(+), 15 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 70e9a1ea75be..74181c71776f 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -44,6 +44,16 @@ enum printk_info_flags { }; =20 extern struct printk_ringbuffer *prb; +extern bool have_legacy_console; +extern bool have_boot_console; + +/* + * Specifies if the console lock/unlock dance is needed for console + * printing. If @have_boot_console is true, the nbcon consoles will + * be printed serially along with the legacy consoles because nbcon + * consoles cannot print simultaneously with boot consoles. + */ +#define printing_via_unlock (have_legacy_console || have_boot_console) =20 __printf(4, 0) int vprintk_store(int facility, int level, @@ -123,6 +133,8 @@ static inline bool console_is_usable(struct console *co= n, short flags) #define PRINTK_MESSAGE_MAX 0 #define PRINTKRB_RECORD_MAX 0 =20 +#define printing_via_unlock (false) + /* * In !PRINTK builds we still export console_sem * semaphore and some of console functions (console_unlock()/etc.), so diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d91771fb306e..336dad179b99 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -463,6 +463,13 @@ static int console_msg_format =3D MSG_FORMAT_DEFAULT; /* syslog_lock protects syslog_* variables and write access to clear_seq. = */ static DEFINE_MUTEX(syslog_lock); =20 +/* + * Specifies if a legacy console is registered. If legacy consoles are + * present, it is necessary to perform the console_lock/console_unlock dan= ce + * whenever console flushing should occur. + */ +bool have_legacy_console; + /* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by @@ -2344,7 +2351,7 @@ asmlinkage int vprintk_emit(int facility, int level, printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 /* If called from the scheduler, we can not call up(). */ - if (!in_sched) { + if (!in_sched && printing_via_unlock) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2645,7 +2652,7 @@ void resume_console(void) */ static int console_cpu_notify(unsigned int cpu) { - if (!cpuhp_tasks_frozen) { + if (!cpuhp_tasks_frozen && printing_via_unlock) { /* If trylock fails, someone else is doing the printing */ if (console_trylock()) console_unlock(); @@ -3188,7 +3195,8 @@ void console_flush_on_panic(enum con_flush_mode mode) =20 nbcon_atomic_flush_all(); =20 - console_flush_all(false, &next_seq, &handover); + if (printing_via_unlock) + console_flush_all(false, &next_seq, &handover); } =20 /* @@ -3513,8 +3521,11 @@ void register_console(struct console *newcon) newcon->dropped =3D 0; console_init_seq(newcon, bootcon_registered); =20 - if (newcon->flags & CON_NBCON) + if (newcon->flags & CON_NBCON) { nbcon_init(newcon); + } else { + have_legacy_console =3D true; + } =20 if (newcon->flags & CON_BOOT) have_boot_console =3D true; @@ -3571,6 +3582,7 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + bool found_legacy_con =3D false; bool found_boot_con =3D false; struct console *c; int res; @@ -3627,9 +3639,13 @@ static int unregister_console_locked(struct console = *console) for_each_console(c) { if (c->flags & CON_BOOT) found_boot_con =3D true; + if (!(c->flags & CON_NBCON)) + found_legacy_con =3D true; } if (!found_boot_con) have_boot_console =3D false; + if (!found_legacy_con) + have_legacy_console =3D false; =20 return res; } @@ -3781,6 +3797,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre u64 last_diff =3D 0; u64 printk_seq; short flags; + bool locked; int cookie; u64 diff; u64 seq; @@ -3790,22 +3807,28 @@ static bool __pr_flush(struct console *con, int tim= eout_ms, bool reset_on_progre seq =3D prb_next_reserve_seq(prb); =20 /* Flush the consoles so that records up to @seq are printed. */ - console_lock(); - console_unlock(); + if (printing_via_unlock) { + console_lock(); + console_unlock(); + } =20 for (;;) { unsigned long begin_jiffies; unsigned long slept_jiffies; =20 + locked =3D false; diff =3D 0; =20 - /* - * Hold the console_lock to guarantee safe access to - * console->seq. Releasing console_lock flushes more - * records in case @seq is still not printed on all - * usable consoles. - */ - console_lock(); + if (printing_via_unlock) { + /* + * Hold the console_lock to guarantee safe access to + * console->seq. Releasing console_lock flushes more + * records in case @seq is still not printed on all + * usable consoles. + */ + console_lock(); + locked =3D true; + } =20 cookie =3D console_srcu_read_lock(); for_each_console_srcu(c) { @@ -3825,6 +3848,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre if (flags & CON_NBCON) { printk_seq =3D nbcon_seq_read(c); } else { + WARN_ON_ONCE(!locked); printk_seq =3D c->seq; } =20 @@ -3836,7 +3860,8 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre if (diff !=3D last_diff && reset_on_progress) remaining_jiffies =3D timeout_jiffies; =20 - console_unlock(); + if (locked) + console_unlock(); =20 /* Note: @diff is 0 if there are no usable consoles. */ if (diff =3D=3D 0 || remaining_jiffies =3D=3D 0) @@ -3958,7 +3983,11 @@ void defer_console_output(void) * New messages may have been added directly to the ringbuffer * using vprintk_store(), so wake any waiters as well. */ - __wake_up_klogd(PRINTK_PENDING_WAKEUP | PRINTK_PENDING_OUTPUT); + int val =3D PRINTK_PENDING_WAKEUP; + + if (printing_via_unlock) + val |=3D PRINTK_PENDING_OUTPUT; + __wake_up_klogd(val); } =20 void printk_trigger_flush(void) --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 53A3371B45 for ; Sun, 18 Feb 2024 18:57:56 +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=1708282678; cv=none; b=Bg/vGaOrKCNhSMJ2HEMBNBcVoKU6OpMxpmcc+diADPFoN/4om3Wbh7NhiJsd/n50Kmgrkb0gEL5uUWCP021BM9kY0QoJO/gqwIhIrk+ldpMVPB7TzQTGA3b4tyYPEwVDdvbCynKj5/wctxvUXAJZ/66z0uTGEHn+ZojTKFwEKIc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282678; c=relaxed/simple; bh=QdXwnt6+PhX0aD+AlOZp4u+/7nviebzBO5H0/vlodlw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Oslzbae/biHzS/wfmzj74Fj8vOEaCia7Thx97rh2mEVhNKp3y1gdo7c7/hVAKF5zT47thNzWKYKIXAYQcSycRp34BNIVaykEBp//W0l2IcfY1TCJW1/wH66ZmmP1fwwy/YZ4dyNjmZ52IwNGglkEz4cQTNJZOQI+xqVPydW4cMk= 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=kipOVu39; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zcuQRKgf; 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="kipOVu39"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zcuQRKgf" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282674; 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=5fJ6yqWdSDvmc7d434BHASreaek3tLpIi1BkLTUqxi8=; b=kipOVu39ofseUoqPTQoCnM23+6kBri5ZY2eBYwv/o+FNAIZUzfw4DtLwp8jDymOfwfi2Pe 4hma6CyBwf2rY4nROH7z1tKEDIcig9XMyk0V+3kCWjCIev9z7q94K6jKZEwgEmANmkgtQ0 bS/5Lwdwmv2ns+NDsy19ux3NkvtqWruYfwTMeQjERAbsIcJigtdYrR0Y4hkaiA3YjLIUsO DUJlz+LqYzQ/w3nKk53Hx1EoymeoDaYPowaeN/AJn1sJwzWPYuKlX5k+OChheaPDPp6VLV 3SHQCtyqROdOxNvNM5LBsbIUXqd54/bZq1/bqjvyzP+MWwhrSn66ePh15T16OA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282674; 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=5fJ6yqWdSDvmc7d434BHASreaek3tLpIi1BkLTUqxi8=; b=zcuQRKgfxpNNB2OuJJiAlo1opLefEjxr6MexZJu4AsBTFQoMR2c3u1HnqC6qtG9R/LOa+M kOMUfgdZJyWjKTAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 20/26] printk: Track nbcon consoles Date: Sun, 18 Feb 2024 20:03:20 +0106 Message-Id: <20240218185726.1994771-21-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Add a global flag @have_nbcon_console to identify if any nbcon consoles are registered. This will be used in follow-up commits to preserve legacy behavior when no nbcon consoles are registered. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 336dad179b99..2c4ab717c110 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -470,6 +470,13 @@ static DEFINE_MUTEX(syslog_lock); */ bool have_legacy_console; =20 +/* + * Specifies if an nbcon console is registered. If nbcon consoles are pres= ent, + * synchronous printing of legacy consoles will not occur during panic unt= il + * the backtrace has been stored to the ringbuffer. + */ +static bool have_nbcon_console; + /* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by @@ -3522,6 +3529,7 @@ void register_console(struct console *newcon) console_init_seq(newcon, bootcon_registered); =20 if (newcon->flags & CON_NBCON) { + have_nbcon_console =3D true; nbcon_init(newcon); } else { have_legacy_console =3D true; @@ -3583,6 +3591,7 @@ EXPORT_SYMBOL(register_console); static int unregister_console_locked(struct console *console) { bool found_legacy_con =3D false; + bool found_nbcon_con =3D false; bool found_boot_con =3D false; struct console *c; int res; @@ -3639,13 +3648,18 @@ static int unregister_console_locked(struct console= *console) for_each_console(c) { if (c->flags & CON_BOOT) found_boot_con =3D true; - if (!(c->flags & CON_NBCON)) + + if (c->flags & CON_NBCON) + found_nbcon_con =3D true; + else found_legacy_con =3D true; } if (!found_boot_con) have_boot_console =3D false; if (!found_legacy_con) have_legacy_console =3D false; + if (!found_nbcon_con) + have_nbcon_console =3D false; =20 return res; } --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 38E0D73168 for ; Sun, 18 Feb 2024 18:57:56 +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=1708282680; cv=none; b=PcMyacDuDXGNX3b/yePeBt4YduGU8jL9B5PEKlrlTdL9/Jld22cEESEg+25QeZOmE7zHR3EKtUSzB7wwple5E0J3Bp3lGAnqaTDeHNC9Q48lMwDULzCsMSd0K85gPafeGSVj0CS/jrugISUzWUHxvW5j0ghOYceOFms+gN68fa8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282680; c=relaxed/simple; bh=pfK9qq9Tq8FSQB42rxPrvRkgcIJuLdvvsicEAYeKWHY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=owuA/VnO8X1PE/Qtt9I38T0bSGEzxI1lhFJU8ejO7uRmDRqbJhJYYLvtjlT7WM9TC/RcaPAyoQV/OMv+xEw/A457hnLggxwoQavOCXLwWYBDiHqeLjRwZmkEhHzWcagu3Pzdeh85cX4dakcmGJ8eto3rM/Ka1OqHY9zWNz860Dk= 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=Lh2fhBGX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JfHPw2o3; 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="Lh2fhBGX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JfHPw2o3" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282675; 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=gPQBcPF1TwjA3+5rOjGCeC1igurDrs1roxu0N8F4XyM=; b=Lh2fhBGXPEgFw0mxMWnVOm8TVECrZ8oSZeW1DqPuvyLg1K6EOxJ1YltQR3GFeVffPmLeEz UJsPBSJjeoYDTAOwY3CAQ7VT1dppthLr3OfLwstrC22Wk7QYtL5fulgWq93PIyyJSdvocr vNG6MNRG8TjgMqNiVuVLYAc4uHTirPbRvwCF/Kep93cVgxH5x62PiwBzGqV+KsZJFr/F1t tTOn2DCCr0uVyohozvfp7oV9IiHgSKuPM/t4FEkO0HORUloe3kWwEo5814T2Z5xIN4QFeK k85YZxNRhU5XYFWCWBavSCSwyz9TBgoy77P/3Htcopis1b4HlDOiG9TEhpLe+Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282675; 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=gPQBcPF1TwjA3+5rOjGCeC1igurDrs1roxu0N8F4XyM=; b=JfHPw2o3dPQ75TjOWAnNmUeoHWZOYo4xQfB2ra+PsaPjxjR4/cXbQzuej3vXFNHdr0KSDB ciyVw65Fm4ESxhBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Uros Bizjak , "Guilherme G. Piccoli" , Kefeng Wang , Arnd Bergmann Subject: [PATCH printk v2 21/26] printk: Coordinate direct printing in panic Date: Sun, 18 Feb 2024 20:03:21 +0106 Message-Id: <20240218185726.1994771-22-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Perform printing by nbcon consoles on the panic CPU from the printk() caller context in order to get panic messages printed as soon as possible. If legacy and nbcon consoles are registered, the legacy consoles will no longer perform direct printing on the panic CPU until after the backtrace has been stored. This will give the safe nbcon consoles a chance to print the panic messages before allowing the unsafe legacy consoles to print. If no nbcon consoles are registered, there is no change in behavior (i.e. legacy consoles will always attempt to print from the printk() caller context). Signed-off-by: John Ogness --- include/linux/printk.h | 5 ++++ kernel/panic.c | 2 ++ kernel/printk/printk.c | 53 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index fe37395af9e2..a2d40a637226 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -197,6 +197,7 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); +void printk_legacy_allow_panic_sync(void); extern void uart_nbcon_acquire(struct uart_port *up); extern void uart_nbcon_release(struct uart_port *up); void nbcon_atomic_flush_unsafe(void); @@ -280,6 +281,10 @@ static inline void printk_trigger_flush(void) { } =20 +static inline void printk_legacy_allow_panic_sync(void) +{ +} + static inline void uart_nbcon_acquire(struct uart_port *up) { } diff --git a/kernel/panic.c b/kernel/panic.c index c039f8e1ddae..86813305510f 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -370,6 +370,8 @@ void panic(const char *fmt, ...) */ atomic_notifier_call_chain(&panic_notifier_list, 0, buf); =20 + printk_legacy_allow_panic_sync(); + panic_print_sys_info(false); =20 kmsg_dump(KMSG_DUMP_PANIC); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 2c4ab717c110..109cfdd988aa 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2329,12 +2329,23 @@ int vprintk_store(int facility, int level, return ret; } =20 +static bool legacy_allow_panic_sync; + +/* + * This acts as a one-way switch to allow legacy consoles to print from + * the printk() caller context on a panic CPU. + */ +void printk_legacy_allow_panic_sync(void) +{ + legacy_allow_panic_sync =3D true; +} + asmlinkage int vprintk_emit(int facility, int level, const struct dev_printk_info *dev_info, const char *fmt, va_list args) { + bool do_trylock_unlock =3D printing_via_unlock; int printed_len; - bool in_sched =3D false; =20 /* Suppress unimportant messages after panic happens */ if (unlikely(suppress_printk)) @@ -2350,15 +2361,43 @@ asmlinkage int vprintk_emit(int facility, int level, =20 if (level =3D=3D LOGLEVEL_SCHED) { level =3D LOGLEVEL_DEFAULT; - in_sched =3D true; + /* If called from the scheduler, we can not call up(). */ + do_trylock_unlock =3D false; } =20 printk_delay(level); =20 printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 - /* If called from the scheduler, we can not call up(). */ - if (!in_sched && printing_via_unlock) { + if (!have_boot_console && have_nbcon_console) { + bool is_panic_context =3D this_cpu_in_panic(); + + /* + * In panic, the legacy consoles are not allowed to print from + * the printk calling context unless explicitly allowed. This + * gives the safe nbcon consoles a chance to print out all the + * panic messages first. This restriction only applies if + * there are nbcon consoles registered. + */ + if (is_panic_context) + do_trylock_unlock &=3D legacy_allow_panic_sync; + + /* + * There are situations where nbcon atomic printing should + * happen in the printk() caller context: + * + * - When this CPU is in panic. + * + * Note that if boot consoles are registered, the + * console_lock/console_unlock dance must be relied upon + * instead because nbcon consoles cannot print simultaneously + * with boot consoles. + */ + if (is_panic_context) + nbcon_atomic_flush_all(); + } + + if (do_trylock_unlock) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2378,10 +2417,10 @@ asmlinkage int vprintk_emit(int facility, int level, preempt_enable(); } =20 - if (in_sched) - defer_console_output(); - else + if (do_trylock_unlock) wake_up_klogd(); + else + defer_console_output(); =20 return printed_len; } --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 1A51A73163 for ; Sun, 18 Feb 2024 18:57:57 +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=1708282679; cv=none; b=hOM2HIW6AwfQBPw6d3+4vJffe1XqCRfUeiwlcpN6L+9c5jFg1jaHqOKhsDVIp+OatbV6Z9R/1FBb+qR1pGfIX9gHZO/xBFE+n2uCNGpcd7ui6Q+gmwFKPcLMkNgIoJK/k/dE9Gm5WPIt5DYLvySPJMrbPMr+/qOXA4+PWZP/zu0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282679; c=relaxed/simple; bh=e7GbIKGogj5AP6ontVh7TIYkL2C0F+okj8XJlpL+1uE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NijXI4qtPuYeZIgLtkCbodndp3LIf1bf34QvzTLzpC6a6jKqyiTXOASDnLlp/zaI1s3B995pMw8dfPCpIPBPmP1qgNMYGVtL1+mh04n7bIJnIu5L1mPZXIdvFgs0p7HcE65W5xqSA9zsI64LSVJXH+FXDrR2ERoat13voRWP0tk= 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=Xc6CwIuS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=6uoS2Ttx; 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="Xc6CwIuS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="6uoS2Ttx" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282675; 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=chNxmh0c/kNKxyDg+4LUMVdYUn+cMPiAIaQ2mbvqe3U=; b=Xc6CwIuSRTv33zta/xRJXtT995IlWZr+PW3IHK3NmPPkzQRlqeczv9xIUVQHzHdt6zWkck j+eSKHmhgiC3U2ixwJV/dsAGrvFq3Y61E+pqtbjUdQ/o6PKcyKo8034xNb7Ws8U0oqBitM BTfQnrNH4ZsVwVAJcyBuQsjk5TPpVz4UtVIv6r237wSB4l9sLvm/ojK414w0fk7vShQIho l3+O+D7tg7DgL7lZrpNwq24wY64WYt8NLSFYADeurj8xqiI281FQBmJn8KooIFhn3f1vX3 Undq8eHZ62YqCnIWv1lwALk/wrFHR0ci5e6ZNSiNwie7h9hzHAeidLYfk9jrCg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282675; 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=chNxmh0c/kNKxyDg+4LUMVdYUn+cMPiAIaQ2mbvqe3U=; b=6uoS2TtxoVJ82xFQcHORU233rpeZS6laROejmkz/b3nXsD/uFUQPj7lJrTjRfxK5DSLrtW ULed0kxJ4VUf8XDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v2 22/26] printk: nbcon: Implement emergency sections Date: Sun, 18 Feb 2024 20:03:22 +0106 Message-Id: <20240218185726.1994771-23-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" From: Thomas Gleixner In emergency situations (something has gone wrong but the system continues to operate), usually important information (such as a backtrace) is generated via printk(). Each individual printk record has little meaning. It is the collection of printk messages that is most often needed by developers and users. In order to help ensure that the collection of printk messages in an emergency situation are all stored to the ringbuffer as quickly as possible, disable console output for that CPU while it is in the emergency situation. When exiting the emergency situation, trigger the consoles to be flushed. Add per-CPU emergency nesting tracking because an emergency can arise while in an emergency situation. Add functions to mark the beginning and end of emergency sections where the urgent messages are generated. Do not print if the current CPU is in an emergency state. Trigger console flushing when exiting all emergency nesting. Note that the emergency state is not system-wide. While one CPU is in an emergency state, another CPU may continue to print console messages. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- include/linux/console.h | 4 ++ kernel/printk/nbcon.c | 81 +++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 25 ++++++++++--- 3 files changed, 104 insertions(+), 6 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index d8922282efa1..b1c870898181 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -478,10 +478,14 @@ static inline bool console_is_registered(const struct= console *con) hlist_for_each_entry(con, &console_list, node) =20 #ifdef CONFIG_PRINTK +extern void nbcon_cpu_emergency_enter(void); +extern void nbcon_cpu_emergency_exit(void); extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); #else +static inline void nbcon_cpu_emergency_enter(void) { } +static inline void nbcon_cpu_emergency_exit(void) { } static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { = return false; } static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) {= return false; } static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { = return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 64dedd79e880..ce556c1f4dc5 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -929,6 +929,29 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/* Track the nbcon emergency nesting per CPU. */ +static DEFINE_PER_CPU(unsigned int, nbcon_pcpu_emergency_nesting); +static unsigned int early_nbcon_pcpu_emergency_nesting __initdata; + +/** + * nbcon_get_cpu_emergency_nesting - Get the per CPU emergency nesting poi= nter + * + * Return: Either a pointer to the per CPU emergency nesting counter of + * the current CPU or to the init data during early boot. + */ +static __ref unsigned int *nbcon_get_cpu_emergency_nesting(void) +{ + /* + * The value of __printk_percpu_data_ready gets set in normal + * context and before SMP initialization. As a result it could + * never change while inside an nbcon emergency section. + */ + if (!printk_percpu_data_ready()) + return &early_nbcon_pcpu_emergency_nesting; + + return this_cpu_ptr(&nbcon_pcpu_emergency_nesting); +} + /** * nbcon_atomic_emit_one - Print one record for an nbcon console using the * write_atomic() callback @@ -971,9 +994,15 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_c= ontext *wctxt) */ enum nbcon_prio nbcon_get_default_prio(void) { + unsigned int *cpu_emergency_nesting; + if (this_cpu_in_panic()) return NBCON_PRIO_PANIC; =20 + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + if (*cpu_emergency_nesting) + return NBCON_PRIO_EMERGENCY; + return NBCON_PRIO_NORMAL; } =20 @@ -1105,6 +1134,58 @@ void nbcon_atomic_flush_unsafe(void) __nbcon_atomic_flush_all(prb_next_reserve_seq(prb), true); } =20 +/** + * nbcon_cpu_emergency_enter - Enter an emergency section where printk() + * messages for that CPU are only stored + * + * Upon exiting the emergency section, all stored messages are flushed. + * + * Context: Any context. Disables preemption. + * + * When within an emergency section, no printing occurs on that CPU. This + * is to allow all emergency messages to be dumped into the ringbuffer bef= ore + * flushing the ringbuffer. The actual printing occurs when exiting the + * outermost emergency section. + */ +void nbcon_cpu_emergency_enter(void) +{ + unsigned int *cpu_emergency_nesting; + + preempt_disable(); + + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + (*cpu_emergency_nesting)++; +} + +/** + * nbcon_cpu_emergency_exit - Exit an emergency section and flush the + * stored messages + * + * Flushing only occurs when exiting all nesting for the CPU. + * + * Context: Any context. Enables preemption. + */ +void nbcon_cpu_emergency_exit(void) +{ + unsigned int *cpu_emergency_nesting; + bool do_trigger_flush =3D false; + + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + + WARN_ON_ONCE(*cpu_emergency_nesting =3D=3D 0); + + if (*cpu_emergency_nesting =3D=3D 1) + do_trigger_flush =3D true; + + /* Undo the nesting count of nbcon_cpu_emergency_enter(). */ + (*cpu_emergency_nesting)--; + + preempt_enable(); + + if (do_trigger_flush) + printk_trigger_flush(); +} + /** * nbcon_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 109cfdd988aa..c8ad2b6ffe63 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2404,16 +2404,29 @@ asmlinkage int vprintk_emit(int facility, int level, * printing of all remaining records to all consoles so that * this context can return as soon as possible. Hopefully * another printk() caller will take over the printing. + * + * Also, nbcon_get_default_prio() requires migration disabled. */ preempt_disable(); + /* - * Try to acquire and then immediately release the console - * semaphore. The release will print out buffers. With the - * spinning variant, this context tries to take over the - * printing from another printing context. + * Do not emit for EMERGENCY priority. The console will be + * explicitly flushed when exiting the emergency section. */ - if (console_trylock_spinning()) - console_unlock(); + if (nbcon_get_default_prio() =3D=3D NBCON_PRIO_EMERGENCY) { + do_trylock_unlock =3D false; + } else { + /* + * Try to acquire and then immediately release the + * console semaphore. The release will print out + * buffers. With the spinning variant, this context + * tries to take over the printing from another + * printing context. + */ + if (console_trylock_spinning()) + console_unlock(); + } + preempt_enable(); } =20 --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 A292D7317A for ; Sun, 18 Feb 2024 18:57:57 +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=1708282679; cv=none; b=nPpM93eBy+4c56RG3dRpRKHaeKRaNf/2YxTC4zoABYBM+UdoBdn1veetzkAhV4YhLPrRX7mLRvR3CzqcVmHuHsgGESvJDu6ubKl2iCAf0oUCppHh6Es48s/nL0VuAL3oTk00GboafuvpyKdiH1BsNpj2VlIXJUZyEWa+xSQcGBo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282679; c=relaxed/simple; bh=9MS2P3GionMP7c/6VZMtfIJf7yMWxofhT265EbuKiDI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=L5vW3rahW7wFn1bkbFGNkQ07MvFg9JSsoUWBYhIY7BefMZM+xCqcxUaN96gTGuym/OGCXJ9FlfU/3nWSaLcwIhP+meX8wQimcmQWS45MTMlHor8ACdZhfxqEYpukdpwkf8DbRLlV37zSFCR3KahtbErh2kmifDwebBTNLpPc65k= 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=kxf6wz3G; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=5wm9tfqa; 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="kxf6wz3G"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="5wm9tfqa" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282676; 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=GUbvpoY1vkrIaf4t1+bOO9YGAlSrhJitnWRmUehFKmc=; b=kxf6wz3GUu4qdeFhzXpICe2cGfNBJSkLoT82KX/lAcZbK1dpaDV/emR2wz9uRLKOm+7pfV X+65xFM6mk8goNdIDmv2Jm7NzIGQE0DHhH/fVd+3jNKDsTSF/cfMY8YcxIz3z8RKWnF4lH 4yAjPwxF7ORRa1JtkbmQdQLOx04iIefHtB3a34F0yRu19Yl7rKB17/M3lnBFsI52GYqL3m kGPiT6n+5j5nKi6vPBIyJ0LRhiqo9I9qM6neXMzdICMbTtpcsLiU2b47FmUcOkziXOeCyY BmiguoKf+t3UIdk5pyMCAUtol6CnxZM50YfQ0OiQvu68h708WfRRiJUnaOqDbw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282676; 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=GUbvpoY1vkrIaf4t1+bOO9YGAlSrhJitnWRmUehFKmc=; b=5wm9tfqaTzyXeL2PKtyecqBe9en8uVauawNKYe+bAlM4JD8JFvwFjfMjY+AJsxeI8QH6y9 U5DlklXhgWwT55Aw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Kefeng Wang , "Guilherme G. Piccoli" , Uros Bizjak , Arnd Bergmann Subject: [PATCH printk v2 23/26] panic: Mark emergency section in warn Date: Sun, 18 Feb 2024 20:03:23 +0106 Message-Id: <20240218185726.1994771-24-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" From: Thomas Gleixner Mark the full contents of __warn() as an emergency section. In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) Reviewed-by: Petr Mladek --- kernel/panic.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/panic.c b/kernel/panic.c index 86813305510f..d30d261f9246 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -667,6 +667,8 @@ struct warn_args { void __warn(const char *file, int line, void *caller, unsigned taint, struct pt_regs *regs, struct warn_args *args) { + nbcon_cpu_emergency_enter(); + disable_trace_on_warning(); =20 if (file) @@ -697,6 +699,8 @@ void __warn(const char *file, int line, void *caller, u= nsigned taint, =20 /* Just a warning, don't kill lockdep. */ add_taint(taint, LOCKDEP_STILL_OK); + + nbcon_cpu_emergency_exit(); } =20 #ifdef CONFIG_BUG --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 534B973198 for ; Sun, 18 Feb 2024 18:57:58 +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=1708282680; cv=none; b=FRfx2MhUkqDiKpd1FenA3lmhxgcxpSHOr6pmymypQNz1K531ATUuYHQI4fd3cZqekERkSNSmG6+yD0XnxaABv2x3V22oq/IyHUFZ3k5t0zU2CERFkO2AZOqqcnOQofpAoBTFwvnw1szfILnoKVtR8oAQFUfxqA/U5QADqiDArG4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282680; c=relaxed/simple; bh=KzZOuY5lO3xJJiP/W1QN+WoXmg5dBEU4teyAhbH6PyM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QhGxNcUIbkS9OP1rqDZcWodK6ojIBKL0YCafEtygoppKL6NOT6yQaELhUzbhjWoIeq8yngbOZNeQgWo0x04U/opmwg/ABi3cABNKy9hgS+NWyNq4e6N1Ittjwf1I81PbIb4mJoytunPeK+iyuVDZ6aC8QmiXQjzy+/Qi2+G13tQ= 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=ctirOIaN; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RqqpxluB; 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="ctirOIaN"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RqqpxluB" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282676; 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=vuY4H6DNfOvqdhuJgmJkGITg1xVtZYUjr6s1BV/VGVM=; b=ctirOIaNRIvgvx3YlEoT7eH4LHmxuqM1FokSoyH0oWhdq5jTYW+lYL/44OKj6rsAY5RpHU frOqOjhxm1qw2g0xiZ155kVd/OxpvloShfzpNUx01Sk4PcAmjHDs5XFq5SY3zojwP44Niq kzuFFtUdhdYhkhJLWdxnh7H0aJNevyyCAHMDzAD1jkPltPwhfq/vDUtYAATl5c/c/QkGSh YhQOiylqDCC57M3b5lC4o3b+XocaT2fVAfMSuZLrpWxyx0vIeYKO/wyA9D9YvImqOK+KfG 4ygRkaJdPAqFfgSlKNV+NsklnZIxpcjlLBCaLg1NSY7N9DGFfkGz0zQmyiqZ5w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282676; 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=vuY4H6DNfOvqdhuJgmJkGITg1xVtZYUjr6s1BV/VGVM=; b=RqqpxluB27iUY1upXjEPYku+xInDFnSzwmsETUKpBmIETtkeLCPFtzkFtxr0vooC1/lybD z01lu3AituTxVGBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , "Peter Zijlstra (Intel)" , Josh Poimboeuf , "Guilherme G. Piccoli" , Arnd Bergmann , Kefeng Wang , Uros Bizjak Subject: [PATCH printk v2 24/26] panic: Mark emergency section in oops Date: Sun, 18 Feb 2024 20:03:24 +0106 Message-Id: <20240218185726.1994771-25-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Mark an emergency section beginning with oops_enter() until the end of oops_exit(). In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. The very end of oops_exit() performs a kmsg_dump(). This is not included in the emergency section because it is another flushing mechanism that should occur after the consoles have been triggered to flush. Signed-off-by: John Ogness --- kernel/panic.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/panic.c b/kernel/panic.c index d30d261f9246..9fa44bc38f46 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -634,6 +634,7 @@ bool oops_may_print(void) */ void oops_enter(void) { + nbcon_cpu_emergency_enter(); tracing_off(); /* can't trust the integrity of the kernel anymore: */ debug_locks_off(); @@ -656,6 +657,7 @@ void oops_exit(void) { do_oops_enter_exit(); print_oops_end_marker(); + nbcon_cpu_emergency_exit(); kmsg_dump(KMSG_DUMP_OOPS); } =20 --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 CC39B745C5; Sun, 18 Feb 2024 18:57:58 +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=1708282680; cv=none; b=cg5yrMfKClgdurSo4WFQTfcirGT3AQiGDLONqxSOq23DY4mientcKtVdD/4EAX3CT1hcZWcG93J0NipwSgha8NC94mojwLs01mq16OF8odxNRinVkkGN3tVnZjgKU/9u7OaanBwOvXeGmtyGua6OvWTF4ZTFUeo4a4M1J2j9PLI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282680; c=relaxed/simple; bh=OqtVB7F0XU7K6r/XkEXRYuJkg6/1ejUUWR2/2NldzE0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DUx7BLSjnbQTproe28tJ6wVRqI/B5GCY9pbQ531R28jJF/SVNgTcyYkJIPweF4Cz/OYTLOKo6ejcTr8JN5t7VcajPfnBFdl3KXwpFs+ZZYzVmESXish6/uskqh542ASHDegfgjMw/g0J5kX7bXAOBRVVBgK7WXSUwWXYYdhJiiQ= 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=NvT0XLwH; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=x2/uK7ua; 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="NvT0XLwH"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="x2/uK7ua" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282677; 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=ou6pqnQIQ61CFk5TWsTCxvXU+rgsnFekoY9DjGSMLxI=; b=NvT0XLwHYOh9+AxQfVwE3XtEQHME+T7jeyFjb+mH0NRGGQKCbh5EmRxPxr3TogqegumTxl DDyXyi39jg6SLdi/ZLLl2VJ++kV4V4Lz/rHgAjGEip9fKs7uqjgCN6IvPD+SAu54hjehKE /CVFyAJfTYhPJxBXicT0qE+9Cyw4/gJjnqsIh5QHCjZIARUcGp4Q1rminYa7+rRP9eMt8h JuZeaQCUDPY+POfG/+INo0985kWx6JRQpeJPA6LR8sIWTp0IY+ey6lprOPEmSZXBzWY0Y1 KnQoaFyYnphPLhcxg2bAOsdfSkm+OMMq40yl4e8g9g/OthkEZ/0mjKZWOZlSzw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282677; 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=ou6pqnQIQ61CFk5TWsTCxvXU+rgsnFekoY9DjGSMLxI=; b=x2/uK7uacN1qu3ts3Df1coB1t8Xae85UpjQ3ZkPOYdrYjk/NQ0AEn05N8/xeRAvDI8AjVt OKnGkqYsCYaKYSDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , rcu@vger.kernel.org Subject: [PATCH printk v2 25/26] rcu: Mark emergency section in rcu stalls Date: Sun, 18 Feb 2024 20:03:25 +0106 Message-Id: <20240218185726.1994771-26-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Mark an emergency section within print_other_cpu_stall(), where RCU stall information is printed. In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/rcu/tree_stall.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h index ac8e86babe44..efb2be8939a2 100644 --- a/kernel/rcu/tree_stall.h +++ b/kernel/rcu/tree_stall.h @@ -9,6 +9,7 @@ =20 #include #include +#include =20 //////////////////////////////////////////////////////////////////////////= //// // @@ -604,6 +605,8 @@ static void print_other_cpu_stall(unsigned long gp_seq,= unsigned long gps) if (rcu_stall_is_suppressed()) return; =20 + nbcon_cpu_emergency_enter(); + /* * OK, time to rat on our buddy... * See Documentation/RCU/stallwarn.rst for info on how to debug @@ -658,6 +661,8 @@ static void print_other_cpu_stall(unsigned long gp_seq,= unsigned long gps) panic_on_rcu_stall(); =20 rcu_force_quiescent_state(); /* Kick them all. */ + + nbcon_cpu_emergency_exit(); } =20 static void print_cpu_stall(unsigned long gps) --=20 2.39.2 From nobody Fri Dec 19 20:14:38 2025 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 1473B745D7 for ; Sun, 18 Feb 2024 18:57:59 +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=1708282680; cv=none; b=WFMpaxFt1JabkoBdfexDfTXiArKZQHl59pkHzwhBZJuB96EYGwDNFgcJRLXgxZjsWiNLHP9+goifnC71nJegZ36nMjSO0tEaKIwbmOOprF6poOx5iUWHRU2pg6fMJGPil3hXZLwPB0EKKh09aqFsqARmjIpj+nV/qL6BrViKzbo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282680; c=relaxed/simple; bh=OagJvzD8CITXGM05wrJdLFbnK8R3FnQZlKkDZ7ArXY0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tl+0ejlRmOjX/bpbMvekIMokL61zLSwCbpniIimvTW1ZpBQOai65e4a3Gu35+4SJZLoJjEVOOAiG7OHIHvoV08BhGtJGsr/2hLL7DuC+4+qLZZ6xCMh6Qmjo9PvWk0tES2fUVRTeNtv9IwIUrmSrrgCnyZbjYtwk9SVWJ0OIwwg= 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=16iOEDIe; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=iHynVoQd; 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="16iOEDIe"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="iHynVoQd" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282677; 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=0lPBki68mVE7PCw7W4jrgk0lCFhQvBUvfvhYmuWjIeY=; b=16iOEDIef9bfi8hm5YKwdXMo0KA3BGTQYHPoFj515WihwX2bbE+vwFUVFaE5Du1RD7Y5W2 RPwwTqoL6dA2GLDE1UyNa3DkBrV1y2AT9dUZM10gr2IQ6QTaEIKAOA9R1vYmwRH7v5i/LW hBXv3pVUwSCOxaDFMsCTnMSJrRE+ezEIBTczVi/gszaGneoixpcpGkWyW361N2KLds8b9E T1VuopLugS5WomPG7SWkvrlSVJ/kiI4l4ux3mRbLXZo7iqt/NUSd2lWWwWiVtaTjb6MluE tD99NTIR2S1ylI9Jywrwdx9LaxGtztzqu6YDkz54DL0f/6KRyFckh8LM/JeqyQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282677; 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=0lPBki68mVE7PCw7W4jrgk0lCFhQvBUvfvhYmuWjIeY=; b=iHynVoQdMqQvQ5UY9zDTHbYAg0fKIK48nfTCXAbx3zmnVvmiXaZF3CIrbiq4IENzpgm/Yp wGM0dChF7tn7OlDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng Subject: [PATCH printk v2 26/26] lockdep: Mark emergency section in lockdep splats Date: Sun, 18 Feb 2024 20:03:26 +0106 Message-Id: <20240218185726.1994771-27-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-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" Mark an emergency section within print_usage_bug(), where lockdep bugs are printed. In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. Signed-off-by: John Ogness --- kernel/locking/lockdep.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index e85b5ad3e206..00465373d358 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -56,6 +56,7 @@ #include #include #include +#include =20 #include =20 @@ -3970,6 +3971,8 @@ print_usage_bug(struct task_struct *curr, struct held= _lock *this, if (!debug_locks_off() || debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: inconsistent lock state\n"); @@ -3998,6 +4001,8 @@ print_usage_bug(struct task_struct *curr, struct held= _lock *this, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 /* --=20 2.39.2