From nobody Fri Dec 19 20:34:49 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 69EF8ED8 for ; Sun, 4 Aug 2024 00:51:48 +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=1722732709; cv=none; b=Bg6SzhO+VNUxMYTgFxkWyOIw0/qPmseGPqN9i8WG3NlKxOOkTG04zwncqpycm84hRYV2diLjG47q9PiAaB+5mVQzabSlrGHlzz6X6i5kjnUlzzaadmwlvgFNWouNfHopVCk3DDEbDY6r29xOjqQ2UOoPQYIf0LY5jN7GBaqzW0Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732709; c=relaxed/simple; bh=H69UsiUEtXuKfzx02fwPVHJdeRXFRg3g9GG5RzLVsHQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FoHiJqEvygGqs7oWXklnGZkP0J7g9aeljyVPORvJY4unHgK1vBZqcj1mWrS7O7IJ28u8jaeTkpWSsxwuJk9LCN78N1M6mssfy6dojsEBK5bs7xLNCPLrLf98zYFobn2O4O9cQLh0IY/me1sbEqhbkhaUXqhXdTMEt1DAxgS2b3w= 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=mgCkTVh4; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=djcNaRff; 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="mgCkTVh4"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="djcNaRff" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732700; 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=i3vsoG744Bgw2LY+AobLGckO/izDyansqZqQKin4u6w=; b=mgCkTVh4J4rt8N4iYtRzCM5K/+gMB6l2E+k45g5esGHGVlSkPYBsWEkWwlIx+NYBU6KKph ZtL0aneL5BIW5a4xGaYr0lcfTu18HzHBhtmT3EUSNkZVn+yesXc8MGV/G8oyD1XS5ZZGr5 xlxtrTE+oxdPQtQJN0waBX1pX0Pdzr/kdkbux6oPC9ICwnVoqs/MKxa9gssYPezzSNZW6A D4De3mEY0nWMccNmHAvu/+9ryG5ge/TxmtcRMcVdDb43ZEEWGZVoWIGMgeh2u21Yta8T5W 9Mi232MFGkioPXMQXZ/b68/FKZL+KtqxpVdmf+D66ogSUNWHL6YMUhJNpVu9Aw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732700; 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=i3vsoG744Bgw2LY+AobLGckO/izDyansqZqQKin4u6w=; b=djcNaRffnbrRQ4EgDfQOb3HESfCXalyw4U+tZPllzPfP5zBGLrPcsv5WJTZ2AUQJo43Nw3 /hLnktjhzA3BNlAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Miguel Ojeda , "Paul E. McKenney" Subject: [PATCH printk v7 01/35] printk: Add notation to console_srcu locking Date: Sun, 4 Aug 2024 02:57:04 +0206 Message-Id: <20240804005138.3722656-2-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 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 054c0e7784fd..b3a4c0363a96 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:34:49 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 69EC3A29 for ; Sun, 4 Aug 2024 00:51:48 +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=1722732710; cv=none; b=VaMlMzLqXFf4UFrqyTf7sENleo+u5y9o0BwTVRSlj4UvBoIVjqBgXpbIVrBprp41EIz1fJKh35pvRaIKp7uvuvAI08TyYSiZVZkgaT3hCn3iVgzbHeXFF0ibZE82sPJ8Q2C+7b13+0XTfasee/q0HhxoMwdOC+SkQYvGtPl1Dk8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732710; c=relaxed/simple; bh=cQAt27LoW1Jl/nTzOAQ/jpBaNyuFpePcVMMoNmBrqfo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HtKuqtkMzjKz/+EO3PCWelQ/N4cDlp5eCn6iAdiBmhv7D1xrhM+G4j20YCF43u7IVnTbOIyDbLosuUQudMbvzXujJnKvWX6VFT6l/ICZrYr2K+qUYYsNP6zKlFBRmJt6yJjMrbm42drBOAu5giu+HD+B0QqP+DPwd3HgnOxC9ZY= 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=XjQKhG4R; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7WQECTGe; 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="XjQKhG4R"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7WQECTGe" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732701; 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=5eNYoyN0NDncqy4Z7XwPBDqj7+9dnH3bkoXiqAH1g9s=; b=XjQKhG4RAu4nrYJ6tqVN7Ps396l8c0kMjqyLsDiV1v/Bdet/Dm+ex8K2UZr04P8Ei2LHs8 hT3SvpRDmUfTVaq95Ra7AhHGQqCc3e2OsQaimoWoC5fQIUSOvP8gfdE6m/TF3rPzdGZhS+ +PnS3Ai/EsnWCxvA2jDpgaiCp7aKAKjRHQ2GbYTwKu9M9/xXR3gz3+SS3qJskjbtIuxZJX R5RpghgXLsl+jdBptBI8EVYaTaQFWsZBsFOe3rQErI54wcnV53KPSujf+w6YgiGeOurgiu 3Nsqkm5ZcOSf/I6SkQC84oPS/BOxxVKCDnDrjwUo7PSVKBlx5fVDk+U07v/6Lw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732701; 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=5eNYoyN0NDncqy4Z7XwPBDqj7+9dnH3bkoXiqAH1g9s=; b=7WQECTGeJQVpzxY+vj3vgvOVfDGTJlzW+WEBLIqc7rom4KjIVpYD65eURq3mY4GzL1PNPn FkSu8xygsEc7J0BA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 02/35] printk: nbcon: Consolidate alloc() and init() Date: Sun, 4 Aug 2024 02:57:05 +0206 Message-Id: <20240804005138.3722656-3-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" Rather than splitting the nbcon allocation and initialization into two pieces, perform all initialization in nbcon_alloc(). Later, the initial sequence is calculated and can be explicitly set using nbcon_seq_force(). This removes the need for the strong rules of nbcon_init() that even included a BUG_ON(). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 -- kernel/printk/nbcon.c | 37 +++++++++++-------------------------- kernel/printk/printk.c | 2 +- 3 files changed, 12 insertions(+), 29 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 19dcc5832651..398ecb40d279 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -75,7 +75,6 @@ u16 printk_parse_prefix(const char *text, int *level, u64 nbcon_seq_read(struct console *con); 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); =20 #else @@ -96,7 +95,6 @@ static inline bool printk_percpu_data_ready(void) { retur= n false; } static inline u64 nbcon_seq_read(struct console *con) { return 0; } 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) { } =20 #endif /* CONFIG_PRINTK */ diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index c8093bcc01fe..670692dc9b10 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -929,17 +929,22 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt) } =20 /** - * nbcon_alloc - Allocate buffers needed by the nbcon console - * @con: Console to allocate buffers for + * nbcon_alloc - Allocate and init the nbcon console specific data + * @con: Console to initialize * - * Return: True on success. False otherwise and the console cannot - * be used. + * Return: True if the console was fully allocated and initialized. + * Otherwise @con must not be registered. * - * This is not part of nbcon_init() because buffer allocation must - * be performed earlier in the console registration process. + * When allocation and init was successful, the console must be properly + * freed using nbcon_free() once it is no longer needed. */ bool nbcon_alloc(struct console *con) { + struct nbcon_state state =3D { }; + + nbcon_state_set(con, &state); + atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), 0); + if (con->flags & CON_BOOT) { /* * Boot console printing is synchronized with legacy console @@ -958,26 +963,6 @@ bool nbcon_alloc(struct console *con) return true; } =20 -/** - * nbcon_init - Initialize the nbcon console specific data - * @con: Console to initialize - * - * nbcon_alloc() *must* be called and succeed before this function - * is called. - * - * This function expects that the legacy @con->seq has been set. - */ -void nbcon_init(struct console *con) -{ - struct nbcon_state state =3D { }; - - /* nbcon_alloc() must have been called and successful! */ - BUG_ON(!con->pbufs); - - nbcon_seq_force(con, con->seq); - nbcon_state_set(con, &state); -} - /** * nbcon_free - Free and cleanup the nbcon console specific data * @con: Console to free/cleanup nbcon data diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index b3a4c0363a96..15c61c9ea87e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3618,7 +3618,7 @@ void register_console(struct console *newcon) console_init_seq(newcon, bootcon_registered); =20 if (newcon->flags & CON_NBCON) - nbcon_init(newcon); + nbcon_seq_force(newcon, newcon->seq); =20 /* * Put this console in the list - keep the --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 41F42EDF for ; Sun, 4 Aug 2024 00:51:49 +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=1722732710; cv=none; b=cKNs8psNpy0F5pXwalA9kRO37VEC+EIH5EnvoENBs0ZjW9GRhyCjVVAE9krMTsiaGGaravZKPxXohbwcrfZ4zucV+71RsXvwSRpvTWwOpo15SGxJ1AzGjQSST0KzD+ktu4EM+sjrCmE8qPsAfWT6xtVVEJAgUf2dHTsznf95QnM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732710; c=relaxed/simple; bh=MmS9l1LDp+ZDFdiI+E3/QxHSD/My+JgIhfsgei30hKQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iFaC0wEVFO5Yj6S9VltULWFulh+dvGkE3sniApWCqX8Fj4CrMFrL25lCAmw6r7ANsPAaKA5GkDv2jbp8TDQ9YzOG78Owg+jW6Snw1P095qg+hCG78zFOwdxXjJs2c93iOLzhLqUJ8UkhnYARA+k/G/TFWNocJ+lNYHdQpuMPXYE= 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=rJ53UuNi; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hsLl1jXS; 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="rJ53UuNi"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hsLl1jXS" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732701; 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=3KyagiId8+wbYT3p1n8TW2nOqh9sHrCzp4zum5DTowE=; b=rJ53UuNi6Br+wwaO+mJzTv19Z4lmb27+t0mRQNHO38H6VmBrNfbntOHnuFlbqF4PGUGgP4 4sqAoi0boHLesIEP7M08453xBO8Z++MSbXj7XpSRFRIsOju7OmZKjM7kNUFYYIlrIU3WL6 lXaSYIldKNfv7DVJ5efdrh9wp8473AN/yiDZ5k+fRQso2KVHEQb6fcjqPB9ZPy9hCkbfzp /vVS0Kk+6+WOkaqokvxox50AQcJpVwqEfEf2XYrj5H2Ft+GPQBpCMfaTdpj2XYohYDeu9e S2i27a/hVkYwN62WgY/uVfxkP/y7iIYWszdmps72HQoccZt7N6QvCqa8NtPXhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732701; 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=3KyagiId8+wbYT3p1n8TW2nOqh9sHrCzp4zum5DTowE=; b=hsLl1jXSY6BUTAkpTiWqTzm+hUbFvhLWEKKTka2mprGDLal3FqxEGkbb7SFz7sguumgCL0 JStR1GpL+fDGayCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 03/35] printk: Properly deal with nbcon consoles on seq init Date: Sun, 4 Aug 2024 02:57:06 +0206 Message-Id: <20240804005138.3722656-4-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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: Petr Mladek 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 not updated for nbcon consoles. Check if it is an nbcon boot console and if so call nbcon_seq_read() to read seq. Also, avoid usage of con->seq as temporary storage of the starting record. Instead, rename console_init_seq() to get_init_console_seq() and just return the value. For nbcon consoles set the sequence via nbcon_seq_force(), for legacy consoles set con->seq. The cleaned design should make sure that the value stays and is set before the console is added to the console list. It also unifies the sequence number initialization for legacy and nbcon consoles. Signed-off-by: Petr Mladek Reviewed-by: John Ogness --- kernel/printk/nbcon.c | 3 --- kernel/printk/printk.c | 41 +++++++++++++++++++++++++++++------------ 2 files changed, 29 insertions(+), 15 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 670692dc9b10..776746d20fc0 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -172,9 +172,6 @@ void nbcon_seq_force(struct console *con, u64 seq) u64 valid_seq =3D max_t(u64, seq, prb_first_valid_seq(prb)); =20 atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __u64seq_to_ulseq(valid_= seq)); - - /* Clear con->seq since nbcon consoles use con->nbcon_seq instead. */ - con->seq =3D 0; } =20 /** diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 15c61c9ea87e..6ac333938ffc 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3448,19 +3448,21 @@ static void try_enable_default_console(struct conso= le *newcon) newcon->flags |=3D CON_CONSDEV; } =20 -static void console_init_seq(struct console *newcon, bool bootcon_register= ed) +/* Return the starting sequence number for a newly registered console. */ +static u64 get_init_console_seq(struct console *newcon, bool bootcon_regis= tered) { struct console *con; bool handover; + u64 init_seq; =20 if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) { /* Get a consistent copy of @syslog_seq. */ mutex_lock(&syslog_lock); - newcon->seq =3D syslog_seq; + init_seq =3D syslog_seq; mutex_unlock(&syslog_lock); } else { /* Begin with next message added to ringbuffer. */ - newcon->seq =3D prb_next_seq(prb); + init_seq =3D prb_next_seq(prb); =20 /* * If any enabled boot consoles are due to be unregistered @@ -3481,7 +3483,7 @@ static void console_init_seq(struct console *newcon, = bool bootcon_registered) * Flush all consoles and set the console to start at * the next unprinted sequence number. */ - if (!console_flush_all(true, &newcon->seq, &handover)) { + if (!console_flush_all(true, &init_seq, &handover)) { /* * Flushing failed. Just choose the lowest * sequence of the enabled boot consoles. @@ -3494,19 +3496,30 @@ static void console_init_seq(struct console *newcon= , bool bootcon_registered) if (handover) console_lock(); =20 - newcon->seq =3D prb_next_seq(prb); + init_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 < init_seq) + init_seq =3D seq; } } =20 console_unlock(); } } + + return init_seq; } =20 #define console_first() \ @@ -3538,6 +3551,7 @@ void register_console(struct console *newcon) struct console *con; bool bootcon_registered =3D false; bool realcon_registered =3D false; + u64 init_seq; int err; =20 console_list_lock(); @@ -3615,10 +3629,13 @@ void register_console(struct console *newcon) } =20 newcon->dropped =3D 0; - console_init_seq(newcon, bootcon_registered); + init_seq =3D get_init_console_seq(newcon, bootcon_registered); =20 - if (newcon->flags & CON_NBCON) - nbcon_seq_force(newcon, newcon->seq); + if (newcon->flags & CON_NBCON) { + nbcon_seq_force(newcon, init_seq); + } else { + newcon->seq =3D init_seq; + } =20 /* * Put this console in the list - keep the --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 408B3EDB for ; Sun, 4 Aug 2024 00:51:49 +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=1722732710; cv=none; b=pkrR/dVjKble229PaEtqpi5fhhwgKOF3LioJaZ7QooG3/0QAHjdbyl+S/bEKnvcvmzzxS0QFPkNrOQm1T2HspRsTJDA102/lb8UcH4ESFy/dadDJE7vXq+sEd/DGm0twBWoKQNvI+bqS/v/XQgWRDYW4SKseZ/qQou7oEv2EiXU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732710; c=relaxed/simple; bh=UQEDErLI4jcCnZVu0rwmOPRYJ2gt07XxfjlThqg5euI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QEdRA7XLBHQapZdGDC3oEYoNeKtutPy38z5F7vi1bu90sXAgoc9AZwFUqakEXsI9AGt1ioI741yHe5Y3+QEhmlJqZE68PAzrrIETJMHDm80CpdvPqQ2HYrfFD6rU0C0YUoUip6J0OW6YymAz6dJr8oFTtfi56nCVgYSRZYpW4MI= 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=EO3EMh9Y; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=A3O34kiQ; 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="EO3EMh9Y"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="A3O34kiQ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732702; 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=ayf4o19E2VctcrSLOsJe8upAlNvXm11HIiyOTvRvMPY=; b=EO3EMh9Y1vpWMEeVBSKR2VNzkTLDLp0wgRoQyQ8fLhC2JgG3pw/6nkAwuApzT95e8Da4RL PZGPcQAQa5Wc227NyqY05gOgEwH3PBC9jY51r8phR+Yv6HTleP/34mzwgpYD7AcY3SC4ma FaDRLITxUnotQwH+zxFCFYThOIFrXlAnVIKlNxK/4xYaTnBYeuRpSAeq2p5qcGrccUQDzJ s2MoT2wjsPAXr6+C7tmvYEojdlwjoVPVp0x+lml9wxNTr4sR0Wj+189zCxOshrNxuSRU53 EYHxhzw6uQi2m6vGZaj+mTtkWJdRrBf5aU5cO1x+gt66Ovn88yRz8nQp+h6pjg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732702; 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=ayf4o19E2VctcrSLOsJe8upAlNvXm11HIiyOTvRvMPY=; b=A3O34kiQr+VCGjDVHap+so5q9joWvJLcMNlg5Z8JYlxUODEOfTkUht18GdEBl7C1rDpB4z Pze3XAHFLvRYSDAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Sebastian Andrzej Siewior Subject: [PATCH printk v7 04/35] printk: Check printk_deferred_enter()/_exit() usage Date: Sun, 4 Aug 2024 02:57:07 +0206 Message-Id: <20240804005138.3722656-5-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 | 9 +++++---- kernel/printk/internal.h | 3 +++ kernel/printk/printk_safe.c | 12 ++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index b937cefcb31c..eee8e97da681 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -161,15 +161,16 @@ int _printk(const char *fmt, ...); */ __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/internal.h b/kernel/printk/internal.h index 398ecb40d279..dc8bc0890fd2 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -53,6 +53,9 @@ int vprintk_store(int facility, int level, __printf(1, 0) int vprintk_default(const char *fmt, va_list args); __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args); =20 +void __printk_safe_enter(void); +void __printk_safe_exit(void); + bool printk_percpu_data_ready(void); =20 #define printk_safe_enter_irqsave(flags) \ diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c index 6d10927a07d8..4421ccac3113 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(); + __printk_safe_enter(); +} + +void __printk_deferred_exit(void) +{ + cant_migrate(); + __printk_safe_exit(); +} + asmlinkage int vprintk(const char *fmt, va_list args) { #ifdef CONFIG_KGDB_KDB --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 B39226FB9 for ; Sun, 4 Aug 2024 00:51:50 +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=1722732712; cv=none; b=tgRbIPxBgBcGNg4Eg2RZpEiKJh7hZHVeEdgmYPcxdQjUOncZ/JjLNkjTzPI8MqPWcrsZin9I3IbeNwCCB4/kTwo+WvtRcH59oyiSYBXoiM9NhxZe4zp1M5kuoHf9e5zUG/lWJNMJlvjoBrGgnKvH/74dke/0/bW2Zu2cvQKq5G4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732712; c=relaxed/simple; bh=w/0A+I2P64NXX1ai/t0T7MSBWwFyXQZvqP6UKKp/3BQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MqXrprfim1v+by54P4xSHbJE7lJXB9yxo5Hra+IZA4EaKzv9AlkD2oeDnd1745ij+ySId+lwoLMp84tjvXiNRqsf4Eq5lXrlD05aQhU2KpyHXMPIiOw6jnWGTnFcVFg6vq5JKHgmPpejmiADqb28s5XNyTJruJWq1Ooh6p+p9aE= 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=lZmh4Ksy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=eLA7SE2N; 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="lZmh4Ksy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="eLA7SE2N" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732702; 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=OV1vJv860Ihcd3WShe7ztkuucPJuixldc8WjZwKIQZw=; b=lZmh4KsyBhPRmc79yjDMtze2HZ8clN8AHcrxgF/djrZvsyJ1s3RgivQQ0V5dafITU3issb pQe3/PZyi59YmcREXgu/eF++gOjIY2/sl8MqHJDdZT3bnQ/SLXjeS6Ysx4KFqT5B9X8CID uV6TgzpgB4rhDGWXt4KrCCMu4HUCa7in0W1Q9c+7tG/pvKlpCsIX+nlJZ+6vz/zc/rtzxM l9Nz8QrnNYUr7PZ45iJJeiFgxApAkRzNP2W+ai7AuHBnEF2dH6wgzIu5+xT7/Xh7igIrXi 35MPZ18nYdZp1XlUB+lCF+2B4Kp5rlCzEEY0U4w5z+/YzI2ThzKJfy8dyD3BxQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732702; 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=OV1vJv860Ihcd3WShe7ztkuucPJuixldc8WjZwKIQZw=; b=eLA7SE2NzqKPVxR6Kakqrrm1Vhc+KeJ2aTdZNsRnWZiULZ/xqXtTimCJgENmI4ikvZO3To 6L0FTTRQjOqWMADg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 05/35] printk: nbcon: Clarify rules of the owner/waiter matching Date: Sun, 4 Aug 2024 02:57:08 +0206 Message-Id: <20240804005138.3722656-6-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 functions nbcon_owner_matches() and nbcon_waiter_matches() use a minimal set of data to determine if a context matches. The existing kerneldoc and comments were not clear enough and caused the printk folks to re-prove that the functions are indeed reliable in all cases. Update and expand the explanations so that it is clear that the implementations are sufficient for all cases. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/nbcon.c | 56 +++++++++++++++++++++++++++++++++++-------- 1 file changed, 46 insertions(+), 10 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 776746d20fc0..931b8f086902 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -228,6 +228,13 @@ static int nbcon_context_try_acquire_direct(struct nbc= on_context *ctxt, struct nbcon_state new; =20 do { + /* + * Panic does not imply that the console is owned. However, it + * is critical that non-panic CPUs during panic are unable to + * acquire ownership in order to satisfy the assumptions of + * nbcon_waiter_matches(). In particular, the assumption that + * lower priorities are ignored during panic. + */ if (other_cpu_in_panic()) return -EPERM; =20 @@ -259,18 +266,29 @@ static bool nbcon_waiter_matches(struct nbcon_state *= cur, int expected_prio) /* * The request context is well defined by the @req_prio because: * - * - Only a context with a higher priority can take over the request. + * - Only a context with a priority higher than the owner can become + * a waiter. + * - Only a context with a priority higher than the waiter can + * directly take over the request. * - There are only three priorities. * - Only one CPU is allowed to request PANIC priority. * - Lower priorities are ignored during panic() until reboot. * * As a result, the following scenario is *not* possible: * - * 1. Another context with a higher priority directly takes ownership. - * 2. The higher priority context releases the ownership. - * 3. A lower priority context takes the ownership. - * 4. Another context with the same priority as this context + * 1. This context is currently a waiter. + * 2. Another context with a higher priority than this context + * directly takes ownership. + * 3. The higher priority context releases the ownership. + * 4. Another lower priority context takes the ownership. + * 5. Another context with the same priority as this context * creates a request and starts waiting. + * + * Event #1 implies this context is EMERGENCY. + * Event #2 implies the new context is PANIC. + * Event #3 occurs when panic() has flushed the console. + * Events #4 and #5 are not possible due to the other_cpu_in_panic() + * check in nbcon_context_try_acquire_direct(). */ =20 return (cur->req_prio =3D=3D expected_prio); @@ -578,11 +596,29 @@ static bool nbcon_owner_matches(struct nbcon_state *c= ur, int expected_cpu, int expected_prio) { /* - * Since consoles can only be acquired by higher priorities, - * owning contexts are uniquely identified by @prio. However, - * since contexts can unexpectedly lose ownership, it is - * possible that later another owner appears with the same - * priority. For this reason @cpu is also needed. + * A similar function, nbcon_waiter_matches(), only deals with + * EMERGENCY and PANIC priorities. However, this function must also + * deal with the NORMAL priority, which requires additional checks + * and constraints. + * + * For the case where preemption and interrupts are disabled, it is + * enough to also verify that the owning CPU has not changed. + * + * For the case where preemption or interrupts are enabled, an + * external synchronization method *must* be used. In particular, + * the driver-specific locking mechanism used in device_lock() + * (including disabling migration) should be used. It prevents + * scenarios such as: + * + * 1. [Task A] owns a context with NBCON_PRIO_NORMAL on [CPU X] and + * is scheduled out. + * 2. Another context takes over the lock with NBCON_PRIO_EMERGENCY + * and releases it. + * 3. [Task B] acquires a context with NBCON_PRIO_NORMAL on [CPU X] + * and is scheduled out. + * 4. [Task A] gets running on [CPU X] and sees that the console is + * still owned by a task on [CPU X] with NBON_PRIO_NORMAL. Thus + * [Task A] thinks it is the owner when it is not. */ =20 if (cur->prio !=3D expected_prio) --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 05C408468 for ; Sun, 4 Aug 2024 00:51:50 +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=1722732712; cv=none; b=IwHCXWRImoY2qj2V4E/nKOUZM7fKCWF9oL9LucL8jEoC/NTQWVchmks/jGAEuVS4rG2u6LTYSvHnNuxmBNoAXwJFD8AedyQfVQAVXzQ9VfTydZGznag0LSKB7nNJAVi69UV1n7SnYNeDkxbzCIbdtG5AEuVCQXzdDfuCghxd7Tc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732712; c=relaxed/simple; bh=FFxAFuODw+P+vrod+fHArz08EUy3UXy/c6t6YmdCMUg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ADl4TIKEdvvhNjlnMe4/9WQCglGS1VzKSGB4NbM0GA2R+AF18bWbez4xqPC9rCiwoUi8mDeAD1rMmN01O22/6xx8m5ddEIQ/LTe+qdEio33rH/u21L2DA94de7z4HuWc9EwGRV+y7mZZMopaoWhi9h27NJgycoKGoLOigoM7Kc8= 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=RgwL9/6z; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=v7nDEcLf; 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="RgwL9/6z"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v7nDEcLf" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732702; 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=j+BHmAGsXXsVX201InINqOhrOXKGiIRskQiXRQc9sOc=; b=RgwL9/6z7cCHBxyrKRS2f+q/jfH5uptCubY7v3aNvmmMfL+KxXyh0B3IEtuF2mk9g267Vu KFgPBYwlbyJHFHC61NyLVH/5kZW0kpyL8YtYqPESZZsRGafW1ef63zfnqWXPj6qfpLp4HO okXGyhnaI0p9vQvmJw+Hh9nHawa/p9XbQAgqGLshhijEzCgo/wyaeoonoVbGnEBvkKiKC1 VuzmUgLUyMa4uTOrApW4eYThvUlFan2BlI6y6LCsyndyabE+3ncwgh+g3Tf/JCMUeWUyHy nc2NRKLSRTgcCfgvFdN0qzsHhY7/DR03pqdohBOEgXMt/66VwmbrpkGmWU0Tyg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732702; 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=j+BHmAGsXXsVX201InINqOhrOXKGiIRskQiXRQc9sOc=; b=v7nDEcLfDI+K1ORUBfBf136sOdXfAtLZxVt0NSQKMjBE/Asi7z6nWxUo+oZDpw5Z5jOZ0l bh/cm66n3SWTVKAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v7 06/35] printk: nbcon: Remove return value for write_atomic() Date: Sun, 4 Aug 2024 02:57:09 +0206 Message-Id: <20240804005138.3722656-7-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 return value of write_atomic() does not provide any useful information. On the contrary, it makes things more complicated for the caller to appropriately deal with the information. Change write_atomic() to not have a return value. If the message did not get printed due to loss of ownership, the caller will notice this on its own. If ownership was not lost, it will be assumed that the driver successfully printed the message and the sequence number for that console will be incremented. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 2 +- kernel/printk/nbcon.c | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 31a8f5b85f5d..577b157fe4fb 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -345,7 +345,7 @@ struct console { struct hlist_node node; =20 /* nbcon console specific members */ - bool (*write_atomic)(struct console *con, + void (*write_atomic)(struct console *con, struct nbcon_write_context *wctxt); atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 931b8f086902..f279f839741a 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -885,7 +885,6 @@ 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; =20 /* * The printk buffers are filled within an unsafe section. This @@ -925,16 +924,16 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt) wctxt->unsafe_takeover =3D cur.unsafe_takeover; =20 if (con->write_atomic) { - done =3D con->write_atomic(con, wctxt); + con->write_atomic(con, wctxt); } else { - nbcon_context_release(ctxt); + /* + * This function should never be called for legacy consoles. + * Handle it as if ownership was lost and try to continue. + */ WARN_ON_ONCE(1); - done =3D false; - } - - /* If not done, the emit was aborted. */ - if (!done) + 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:34:49 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 57BC6A945 for ; Sun, 4 Aug 2024 00:51:51 +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=1722732713; cv=none; b=WAZCCm53JSDNmkYUujP3klPKkfwUfr20o8bWcJlKlfacntiT4shwtQafvhG8wHr4c7r4+GdDbh7KZxRFgaxiRR6w1gxluJyzvhg/KfDHEWndS9bfH8ODxYQQokxtzVttQ8C1TjAD+YOVgVVptOAHjHv8fdmZNwv8xqnnsY1/0WI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732713; c=relaxed/simple; bh=yKnV19hWrNK0IoLPABXMEmiKhLFF62eNhWtKhGQNusQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fxr2pPoQcW05r/wcM/FldLut44if4Phx/d2X1dA53+otO3IqIx4yZz6PAakWBjK/8rDKNpRYaGj3iE/BqvPEWoNT24RZerZ3DFmfNnlR194mY9nbszdZLfy7ieEkl+869ptukxW7FmY6xnqJ3GimdJl8PVWejLTUdQSRjxmKUgY= 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=Og0oJwZY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=b4Peb0Nb; 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="Og0oJwZY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="b4Peb0Nb" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732703; 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=qPd6uvFKDkpD+NeIatYE77JXVT2eZZGIALZo/fK24kQ=; b=Og0oJwZY+/flKszHsaLxWZaLm830NyeVlhL5LGnLgopzinmcRJqr16ROg7Rw88c0E62ByH OrVSJqpNXN8xFz/0rWsB5Bk0XSZdwYUVZFEXcmCeTv7isHBbip056jIWmpjtBEGTdCfmeO XvpUuH7xDQCtAijBz2wWggPgcG2MTECtIduk7Z+jzL4phrIQ9c0H32JzDXRx4oWz5bEADu 7Z/uTW4AAcrnXsZDEgaKFnejOMfN0yvJGQt8W+qZkLu+6eSouxNQM/xz3HCM67KxIcFf1I fFnzMi4cu/eghv6ILTeIjEO3SBmm9Xj9J6kkPYJUR9gSrW2dku0S49l3znVzxw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732703; 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=qPd6uvFKDkpD+NeIatYE77JXVT2eZZGIALZo/fK24kQ=; b=b4Peb0Nb1MuH3i2HxwW0oXnkYZ4vm2xny95eEi3ro/UtwIZGH8HG2KNOPKY5ISmB2xZmCw vVZhugfIUCCL45DQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v7 07/35] printk: nbcon: Add detailed doc for write_atomic() Date: Sun, 4 Aug 2024 02:57:10 +0206 Message-Id: <20240804005138.3722656-8-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 577b157fe4fb..35c64ee3827b 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -303,7 +303,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) @@ -320,7 +320,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 @@ -345,8 +344,34 @@ struct console { struct hlist_node node; =20 /* nbcon console specific members */ - void (*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. However, + * a higher priority context is allowed to take it over by default. + * + * The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe() + * around any code where the takeover is not safe, for example, when + * manipulating the serial port registers. + * + * nbcon_enter_unsafe() will fail if the context has lost the console + * ownership in the meantime. In this case, the callback is no longer + * allowed to go forward. It must back out immediately and carefully. + * The buffer content is also no longer trusted since it no longer + * belongs to the context. + * + * The callback should allow the takeover whenever it is safe. It + * increases the chance to see messages when the system is in trouble. + * + * The 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. + */ + void (*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:34:49 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 5626FA934 for ; Sun, 4 Aug 2024 00:51:51 +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=1722732713; cv=none; b=iI6ft6hguQiolEd96pSJTmSSRe46j1VXA4hxl7uNYFT6wF1dqVpzvhJBmbLWohpwVPVKUyFMWvnBqsUpJ52DKLtgrcvmq2F1FoPFLh7sl9i9Q7uIXoFJYFVq3mH9LbU3z4e7efmIk3i/ntRKizYgX4C3rznBnfRlu1IEnkg7WMU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732713; c=relaxed/simple; bh=EDZTvXwEbVltvzWiR3w06E4l6cPYTqndaJgbM1Hw2PU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RPh7z3UFmol28WFdjvOFb0MFgSq8rrz/2MObprnYkofheqHbJ3nv1xwdiBxwHEWpr5rclsHVcwS4okdHONninpOeYNw9BiuIMEjSeUKL+0+OjPQyPqFx+dFYlVGxTiR0A/9iJN1CzZUDInenlZtyg89gqN9IOpVqnP+DOZnGC08= 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=e5FZU/rK; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ufHgUxJR; 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="e5FZU/rK"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ufHgUxJR" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732703; 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=bDb4TIFgrH7FWlQUHsoHZtB4NW70NgpQfMLaM/Fw3fU=; b=e5FZU/rKw4bBz4ULC3rs2vlUh/rGiHk2tIue1r0DiayL7rEvy1coLd1i9z5a5HDV1DYF8S E5xnPZLPNX32VbGtXDxy2b2V0M07Tb3spAF4JF3jwXj6/ZOKqsv8soUpivgYZxNSbz1NQh v4T8RS13VomlgSbJQOpNuDvo6125MqBIyullrXlpawfny4SoysZZeavtYQ6EFrFBzjptK8 8/H3PPs5Eaj9DTD630mpzM6whBn1hvK+FwYFiodIR/syyIEVoCYDeJny33O7Fm75guZDi5 sY7jjeMxqC+O6Qr0CEFjo6MS5eY7Hco/3loeyeUvnU6VK1o3zHIendz7SN0Tqw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732703; 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=bDb4TIFgrH7FWlQUHsoHZtB4NW70NgpQfMLaM/Fw3fU=; b=ufHgUxJRXd5Z5ta6RubNG4noKQNGRlzljQD+2RlX9XVmx4vm5NbSHx5kFxI01MPekPSYc6 PnfC5iWBy6jCscDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v7 08/35] printk: nbcon: Add callbacks to synchronize with driver Date: Sun, 4 Aug 2024 02:57:11 +0206 Message-Id: <20240804005138.3722656-9-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" Console drivers typically must deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls. To provide the necessary synchronization, usually some driver-specific locking mechanism is used (for example, the port spinlock for uart serial consoles). Until now, usage of this driver-specific locking has been hidden from the printk-subsystem and implemented within the various console callbacks. However, nbcon consoles would need to use it even in the generic code. Add device_lock() and device_unlock() callback which will need to get implemented by nbcon consoles. The callbacks will use whatever synchronization mechanism the driver is using for itself. The minimum requirement is to prevent CPU migration. It would allow a context friendly acquiring of nbcon console ownership in non-emergency and non-panic context. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index 35c64ee3827b..46b3c210b931 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -372,6 +372,49 @@ struct console { */ void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); =20 + /** + * @device_lock: + * + * NBCON callback to begin synchronization with driver code. + * + * Console drivers typically must deal with access to the hardware + * via user input/output (such as an interactive login shell) and + * output of kernel messages via printk() calls. This callback is + * called by the printk-subsystem whenever it needs to synchronize + * with hardware access by the driver. It should be implemented to + * use whatever synchronization mechanism the driver is using for + * itself (for example, the port lock for uart serial consoles). + * + * The callback is always called from task context. It may use any + * synchronization method required by the driver. + * + * IMPORTANT: The callback MUST disable migration. The console driver + * may be using a synchronization mechanism that already takes + * care of this (such as spinlocks). Otherwise this function must + * explicitly call migrate_disable(). + * + * The flags argument is provided as a convenience to the driver. It + * will be passed again to device_unlock(). It can be ignored if the + * driver does not need it. + */ + void (*device_lock)(struct console *con, unsigned long *flags); + + /** + * @device_unlock: + * + * NBCON callback to finish synchronization with driver code. + * + * It is the counterpart to device_lock(). + * + * This callback is always called from task context. It must + * appropriately re-enable migration (depending on how device_lock() + * disabled migration). + * + * The flags argument is the value of the same variable that was + * passed to device_lock(). + */ + void (*device_unlock)(struct console *con, unsigned long flags); + 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:34:49 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 49D44A930 for ; Sun, 4 Aug 2024 00:51:51 +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=1722732713; cv=none; b=JyGUN57n31HHPvAc7r9daJ5UcI0kiEDUv6GF3dl4tYnIps8w73fPhzOX7jELo9ke0Up27oXH8kMU35zJUNBf5HvWqB8zu5cVMIGVfr9aSRiFobLsL97Fds+00gsNQVGha+Wi1YBiaDKUQ2P/VUOVyH09y+PsBt1i6w9tTh9KfZM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732713; c=relaxed/simple; bh=EE0q3hdpnMtsse57YJgp4zis2zjcR6z0zeU0j659B84=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mK9ZfOGU1gTyL92SLRNYjO1KigeW8SSnfkDnQYK4f4pV7ZMBWkJUvYgssuWFDQ9UoRUK9PLrnAa7+++228vj6Vpt32n0tTF17/oxnYXNdtxT9p5oCt30a9ynL7CeKNE/kqcP2VsVQ7oOc5cyXKEqJI5HPhbkevhIEqdr5x/5oG0= 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=jQNbmIkU; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DCyfti99; 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="jQNbmIkU"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DCyfti99" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732704; 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=tk3o2TzJsqHT2V8VmhzoN1L6WaQ5SSmi/WNwun+0Gss=; b=jQNbmIkU2VQS4oUQUmd3IuYRYJ4dx5rt8gWGlhP7JfX1+S8PH+wxm+YZpkoaXvFBikcC2i o5Ci+srLl0//3Vs1RG72lDBpfHBeT2oBUziQUFfidbIZHS/EHcPYrnwkPA7Wc5PWf6CAqr 83ja8qRGECpiUG96MhYOa981mp8Nz1o41d8jl7RChMW/f0cSWI4oagYbp6io7sIDhMgRQh gu4/XdRcopwf1CW2H5DOM+4qb77OTFoKYZBR/1dZf+psOMRDHYs0l9+tIDWiP+MwBzBGIu DTe9EFt8yhei7GunKKNUWRX0NuWndOUVeRLzcBRQbcrbR+FWYgHYgf2C+6gruw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732704; 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=tk3o2TzJsqHT2V8VmhzoN1L6WaQ5SSmi/WNwun+0Gss=; b=DCyfti99YHcIKVVzy+RqUXbFZY9zAAH05CHg5HtOcJPVJsLPnYIUg6/jQmfjZSwEf+jw4o a24pRIyshAEVIqCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 09/35] printk: nbcon: Use driver synchronization while (un)registering Date: Sun, 4 Aug 2024 02:57:12 +0206 Message-Id: <20240804005138.3722656-10-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" Console drivers typically have to deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls. They use some classic driver-specific locking mechanism in most situations. But console->write_atomic() callbacks, used by nbcon consoles, are synchronized only by acquiring the console context. The synchronization via the console context ownership is possible only when the console driver is registered. It is when a particular device driver is connected with a particular console driver. The two synchronization mechanisms must be synchronized between each other. It is tricky because the console context ownership is quite special. It might be taken over by a higher priority context. Also CPU migration must be disabled. The most tricky part is to (dis)connect these two mechanisms during the console (un)registration. Use the driver-specific locking callbacks: device_lock(), device_unlock(). They allow taking the device-specific lock while the device is being (un)registered by the related console driver. For example, these callbacks lock/unlock the port lock for serial port drivers. Note that the driver-specific locking is only needed during (un)register if it is an nbcon console with the write_atomic() callback implemented. If write_atomic() is not implemented, the driver should never attempt to access the hardware without first acquiring its driver-specific lock. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 33 ++++++++++++++++++++++++++++++++- 1 file changed, 32 insertions(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6ac333938ffc..7027f0868ef4 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3548,9 +3548,11 @@ static int unregister_console_locked(struct console = *console); */ void register_console(struct console *newcon) { - struct console *con; + bool use_device_lock =3D (newcon->flags & CON_NBCON) && newcon->write_ato= mic; bool bootcon_registered =3D false; bool realcon_registered =3D false; + struct console *con; + unsigned long flags; u64 init_seq; int err; =20 @@ -3637,6 +3639,19 @@ void register_console(struct console *newcon) newcon->seq =3D init_seq; } =20 + /* + * If another context is actively using the hardware of this new + * console, it will not be aware of the nbcon synchronization. This + * is a risk that two contexts could access the hardware + * simultaneously if this new console is used for atomic printing + * and the other context is still using the hardware. + * + * Use the driver synchronization to ensure that the hardware is not + * in use while this new console transitions to being registered. + */ + if (use_device_lock) + newcon->device_lock(newcon, &flags); + /* * Put this console in the list - keep the * preferred driver at the head of the list. @@ -3661,6 +3676,10 @@ void register_console(struct console *newcon) * register_console() completes. */ =20 + /* This new console is now registered. */ + if (use_device_lock) + newcon->device_unlock(newcon, flags); + console_sysfs_notify(); =20 /* @@ -3689,6 +3708,8 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + bool use_device_lock =3D (console->flags & CON_NBCON) && console->write_a= tomic; + unsigned long flags; int res; =20 lockdep_assert_console_list_lock_held(); @@ -3707,8 +3728,18 @@ static int unregister_console_locked(struct console = *console) if (!console_is_registered_locked(console)) return -ENODEV; =20 + /* + * Use the driver synchronization to ensure that the hardware is not + * in use while this console transitions to being unregistered. + */ + if (use_device_lock) + console->device_lock(console, &flags); + hlist_del_init_rcu(&console->node); =20 + if (use_device_lock) + console->device_unlock(console, flags); + /* * * If this isn't the last console and it has CON_CONSDEV set, we --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 AC192B673; Sun, 4 Aug 2024 00:51:51 +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=1722732713; cv=none; b=iKQ8dL6Ogh064QE3/Vjv47pqCYLVBX9/C9lwqIaPc7+acxq6EoswOOx0Lqau1IfkT5fLkzx+2eKLz7x+p6Mk+n3NLUnJnYel2L6izceYLpYN7E50WmGlxv7J0avkEht98sy8ADvmlApdhGuPgkKfkP80XNWNWpEztUxrV/8v2Gw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732713; c=relaxed/simple; bh=i7g9Rk6E1rRqftOtHZzjKlxcVFM+YP6E/P/94xSlZdE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WFnmpDorX0pqKr7CKin1CWm8K4XawszJ6WKifxaWv8C/O/2yoL3r7MCRZznioCLIz2pHj5nZ/uAdX4P8lbbCI+mga1W/mJptqkz8fkBrNkaL2v3EMx2EqHvWyQT6Xom50TF0k7nPzUJIKTesRvF3d3qujv60HWrmKTOwXEWkABk= 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=iFutFd+n; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=SBE5EMqj; 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="iFutFd+n"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="SBE5EMqj" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732704; 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=EwPBydyTGTYXegM2JVbnpVHJON//wTjw/xvV7q8bAP0=; b=iFutFd+nbFnB13SQ9hIqx4evQV3KuPsbFZrZfQj0rSBNxSr0adh4NRrPSHbTRuLjNRBU4n bGa0pU2JCzxzlMMqqHOvtdshDf05EzUgG+8t1Sd0gMI/m9TI5eyILXqQIjiHgFy6Dh/G2/ dEFqu2+gtSSQxi6kyjVz8512VzhdHyjX6IkYS/VeZtOumHWGoqS0fOL+1QHw+iKj0DwSbz t2Jpv+bslMfpOVzT9fgHSnOfgdPiDP48M/iI1a+lXU4adfTEQom6gncWaFGeBDbbMCf9ND i0jbbY1IEcUmvJv2eSGKQY888apUuKoBSbhx/UNlp0fmxINM8JZoipcQ6ADFLg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732704; 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=EwPBydyTGTYXegM2JVbnpVHJON//wTjw/xvV7q8bAP0=; b=SBE5EMqjeqcj2iTPMMaONIt+erFukqHHiehMt+23DxJToxZWAiKjamvhCzEEAw8tRiPo63 KdVZe0XST2lXL5Bg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org Subject: [PATCH printk v7 10/35] serial: core: Provide low-level functions to lock port Date: Sun, 4 Aug 2024 02:57:13 +0206 Message-Id: <20240804005138.3722656-11-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" It will be necessary at times for the uart nbcon console drivers to acquire the port lock directly (without the additional nbcon functionality of the port lock wrappers). These are special cases such as the implementation of the device_lock()/device_unlock() callbacks or for internal port lock wrapper synchronization. Provide low-level variants __uart_port_lock_irqsave() and __uart_port_unlock_irqrestore() for this purpose. Signed-off-by: John Ogness Reviewed-by: Petr Mladek Acked-by: Greg Kroah-Hartman --- include/linux/serial_core.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index aea25eef9a1a..8872cd21e70a 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -590,6 +590,24 @@ struct uart_port { void *private_data; /* generic platform data pointer */ }; =20 +/* + * Only for console->device_lock()/_unlock() callbacks and internal + * port lock wrapper synchronization. + */ +static inline void __uart_port_lock_irqsave(struct uart_port *up, unsigned= long *flags) +{ + spin_lock_irqsave(&up->lock, *flags); +} + +/* + * Only for console->device_lock()/_unlock() callbacks and internal + * port lock wrapper synchronization. + */ +static inline void __uart_port_unlock_irqrestore(struct uart_port *up, uns= igned long flags) +{ + spin_unlock_irqrestore(&up->lock, flags); +} + /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 1DD76DDC7; Sun, 4 Aug 2024 00:51: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=1722732714; cv=none; b=krYK5vxWCY95DyhoDCv5msKQLIddkad0e3XOFPYnHhDlea7rp1+7PWpD/S5T1sk+/6q9XNT3bJFOhc1fIzVVvdSSsmi6CTfSLVM5zor+NlLwioer8YSHFBKBV2u38FISssVAIR0dnFvsSxuoS2lnfuEW6Tti6gCwM3rqaP8dUwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732714; c=relaxed/simple; bh=qu0GAhjXm3Rql3Yn+yPrWxwrSP6aPTYXniSO6W8yPIo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=gvx2SR6XzikNy8eF+7YevaEgGX2azgssGA/5brE2RvnQjgQT/tn7qFMp9p346IkbRVJ9k0QxBLNxCzpkquNQ/kPKsiTUybtWQj47L3VImBPknrXfHmN9BoQ2KFpuBaoq21qIgJvK7eBW19xwE2hBQQJuwO+6sQ21Y39ZnzmIQgs= 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=ZxpUXUrY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=FY1+qHmM; 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="ZxpUXUrY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="FY1+qHmM" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732705; 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=72KWg1lC7QoA8wkEDKOEAHKAbxE9DDTme6Zqb5h0emg=; b=ZxpUXUrY8CaW5ti+wfDb/ZbpzvhG9JEidFErBsSqKt7OKJ9RHamynwW8Sx2VkU2/QnxNmi NcimOs99tJm5nzs5MFDcyX6A4Bj0qbHUOVEUnKA0zjar2ePuYs+DBVCn8tnmWC1bivmjg3 OhDnFUCeA5kppytVwvL0EqzLFt4v/ABUwZJY32cFBvUoY2r0BM+zceq3jWStzNj5pHlDNj SyX3GxBWKnmXEYFdekpVpTt3DJGnI5XrX+Ovz4AwwGcaT7d66oJb7dLqif9bBigXU86CfA xDmfJzviXSvG+tOD3SDiIksAGrRJZ6OEbkmY+OA3XBvdoiopwI0bX4QoBJ+/kg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732705; 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=72KWg1lC7QoA8wkEDKOEAHKAbxE9DDTme6Zqb5h0emg=; b=FY1+qHmMD6qFcoGjMuZzNHFuL/0ggxtLianNieQkJcCcdZ40Vn48fvRqWqPfUqoja3aF2z bjHpKe5ZAugFmQDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , Russell King , Tony Lindgren , Andy Shevchenko , Arnd Bergmann , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , =?UTF-8?q?Th=C3=A9o=20Lebrun?= , Linus Walleij , Lino Sanfilippo , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Konrad Dybcio , Sebastian Andrzej Siewior , linux-serial@vger.kernel.org Subject: [PATCH printk v7 11/35] serial: core: Introduce wrapper to set @uart_port->cons Date: Sun, 4 Aug 2024 02:57:14 +0206 Message-Id: <20240804005138.3722656-12-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 Introduce uart_port_set_cons() as a wrapper to set @cons of a uart_port. The wrapper sets @cons under the port lock in order to prevent @cons from disappearing while another context is holding the port lock. This is necessary for a follow-up commit relating to the port lock wrappers, which rely on @cons not changing between lock and unlock. Signed-off-by: John Ogness Tested-by: Th=C3=A9o Lebrun # EyeQ5, AMBA-PL011 Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek --- drivers/tty/serial/8250/8250_core.c | 6 +++--- drivers/tty/serial/amba-pl011.c | 2 +- drivers/tty/serial/serial_core.c | 16 ++++++++-------- include/linux/serial_core.h | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/= 8250_core.c index 29e4b83e0376..5f9f06911795 100644 --- a/drivers/tty/serial/8250/8250_core.c +++ b/drivers/tty/serial/8250/8250_core.c @@ -423,11 +423,11 @@ static int univ8250_console_setup(struct console *co,= char *options) =20 port =3D &serial8250_ports[co->index].port; /* link port to console */ - port->cons =3D co; + uart_port_set_cons(port, co); =20 retval =3D serial8250_console_setup(port, options, false); if (retval !=3D 0) - port->cons =3D NULL; + uart_port_set_cons(port, NULL); return retval; } =20 @@ -485,7 +485,7 @@ static int univ8250_console_match(struct console *co, c= har *name, int idx, continue; =20 co->index =3D i; - port->cons =3D co; + uart_port_set_cons(port, co); return serial8250_console_setup(port, options, true); } =20 diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl01= 1.c index 8b1644f5411e..7d0134ecd82f 100644 --- a/drivers/tty/serial/amba-pl011.c +++ b/drivers/tty/serial/amba-pl011.c @@ -2480,7 +2480,7 @@ static int pl011_console_match(struct console *co, ch= ar *name, int idx, continue; =20 co->index =3D i; - port->cons =3D co; + uart_port_set_cons(port, co); return pl011_console_setup(co, options); } =20 diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_c= ore.c index 9a18d0b95a41..61c7e1268957 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -3168,8 +3168,15 @@ static int serial_core_add_one_port(struct uart_driv= er *drv, struct uart_port *u state->uart_port =3D uport; uport->state =3D state; =20 + /* + * If this port is in use as a console then the spinlock is already + * initialised. + */ + if (!uart_console_registered(uport)) + uart_port_spin_lock_init(uport); + state->pm_state =3D UART_PM_STATE_UNDEFINED; - uport->cons =3D drv->cons; + uart_port_set_cons(uport, drv->cons); uport->minor =3D drv->tty_driver->minor_start + uport->line; uport->name =3D kasprintf(GFP_KERNEL, "%s%d", drv->dev_name, drv->tty_driver->name_base + uport->line); @@ -3178,13 +3185,6 @@ static int serial_core_add_one_port(struct uart_driv= er *drv, struct uart_port *u goto out; } =20 - /* - * If this port is in use as a console then the spinlock is already - * initialised. - */ - if (!uart_console_registered(uport)) - uart_port_spin_lock_init(uport); - if (uport->cons && uport->dev) of_console_check(uport->dev->of_node, uport->cons->name, uport->line); =20 diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 8872cd21e70a..2cf03ff2056a 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -608,6 +608,23 @@ static inline void __uart_port_unlock_irqrestore(struc= t uart_port *up, unsigned spin_unlock_irqrestore(&up->lock, flags); } =20 +/** + * uart_port_set_cons - Safely set the @cons field for a uart + * @up: The uart port to set + * @con: The new console to set to + * + * This function must be used to set @up->cons. It uses the port lock to + * synchronize with the port lock wrappers in order to ensure that the con= sole + * cannot change or disappear while another context is holding the port lo= ck. + */ +static inline void uart_port_set_cons(struct uart_port *up, struct console= *con) +{ + unsigned long flags; + + __uart_port_lock_irqsave(up, &flags); + up->cons =3D con; + __uart_port_unlock_irqrestore(up, flags); +} /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 69F55101E2 for ; Sun, 4 Aug 2024 00:51: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=1722732714; cv=none; b=ZNExkmwzP39qHrHGkZrdn/fiT9UBgPpY+o5LEskFx+G8yrkPDV2XJzRSJ3ht85pRQroidQfCCeN4YwiT42+DLlrIOya9Bs3GIonAQwlvJSj5y5Za3B2MAbvDTHl1TCBIAmlLqgBxvNrOCfssVxZNwwZFkzSlyywc6j0CBl5Bbw0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732714; c=relaxed/simple; bh=xskXJgvQkL9boiz0qHQhuIq5j2Sph2J69H53TzZn0Ko=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PjvI1QQGRMeyoHqyNoTW1OmrZy2JV/dzz54xEtNg+NTIM+LkUg99UrPYDODDFo2GgK/xNEZQVAA3yD9ZwifCQyrV9XZwHOqIlnl53JTs66hz61C/KiKy23mJpoPfWdqPudMobBCesS6R0XFBUyUL6Zf0YtF00IFpFlRPU16xqws= 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=o2knPv2O; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jwGce3Bp; 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="o2knPv2O"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jwGce3Bp" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732705; 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=VwbQtuej4PvTHWMkC5NdE4fWdsHI6OVBAXHJhUAhxgw=; b=o2knPv2ODupxt0w6IPv+C41rDoRXAO3+WRgx/cnXiSuk+cELO9IQDSdechtQtxwPpczxUQ r2dVkD9E6+IDMMronxrNkLFtpvmUiaiTbx8KlkA7S7x69dJgnnuMPjt62kI5MfNBJt+geO Nyc8BXAHDLhNi+kasfYaM9+5AbxoYNqfqGOmbbmzb49dbhJDSPe7aoklCPQaKvQi7fvVTe ZXQDJVSK2NUX3yaxOlOKILtg2fIAS2BYwI+JGPyICO3P2Z3luy4HJ11nyiJwlLZc/1NNvF 7Vptc18LL10mrPaxZPQ2xbZKOSLqrPHICLlr3bZPI1hcm+eXasdTsEBphVQVxg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732705; 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=VwbQtuej4PvTHWMkC5NdE4fWdsHI6OVBAXHJhUAhxgw=; b=jwGce3BpNzv/1rm5BzNr9zVaV66kAqtGXYZR346KRHP11dTKIblZbbGSUjMgg1Yj2C1WRD 9cy2ehZwi/w05kCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v7 12/35] console: Improve console_srcu_read_flags() comments Date: Sun, 4 Aug 2024 02:57:15 +0206 Message-Id: <20240804005138.3722656-13-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" It was not clear when exactly console_srcu_read_flags() must be used vs. directly reading @console->flags. Refactor and clarify that console_srcu_read_flags() is only needed if the console is registered or the caller is in a context where the registration status of the console may change (due to another context). The function requires the caller holds @console_srcu, which will ensure that the caller sees an appropriate @flags value for the registered console and that exit/cleanup routines will not run if the console is in the process of unregistration. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 46b3c210b931..aafe3121b74e 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -446,28 +446,34 @@ extern void console_list_unlock(void) __releases(cons= ole_mutex); extern struct hlist_head console_list; =20 /** - * console_srcu_read_flags - Locklessly read the console flags + * console_srcu_read_flags - Locklessly read flags of a possibly registered + * console * @con: struct console pointer of console to read flags from * - * This function provides the necessary READ_ONCE() and data_race() - * notation for locklessly reading the console flags. The READ_ONCE() - * in this function matches the WRITE_ONCE() when @flags are modified - * for registered consoles with console_srcu_write_flags(). + * Locklessly reading @con->flags provides a consistent read value because + * there is at most one CPU modifying @con->flags and that CPU is using on= ly + * read-modify-write operations to do so. * - * Only use this function to read console flags when locklessly - * iterating the console list via srcu. + * Requires console_srcu_read_lock to be held, which implies that @con mig= ht + * be a registered console. The purpose of holding console_srcu_read_lock = is + * to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED) + * and that no exit/cleanup routines will run if the console is currently + * undergoing unregistration. + * + * If the caller is holding the console_list_lock or it is _certain_ that + * @con is not and will not become registered, the caller may read + * @con->flags directly instead. * * Context: Any context. + * Return: The current value of the @con->flags field. */ static inline short console_srcu_read_flags(const struct console *con) { WARN_ON_ONCE(!console_srcu_read_lock_is_held()); =20 /* - * Locklessly reading console->flags provides a consistent - * read value because there is at most one CPU modifying - * console->flags and that CPU is using only read-modify-write - * operations to do so. + * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified + * for registered consoles with console_srcu_write_flags(). */ return data_race(READ_ONCE(con->flags)); } --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 68D04101C8 for ; Sun, 4 Aug 2024 00:51: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=1722732714; cv=none; b=qvTGt8S7XLtNcfgtm/GMVH+NmbrwkoB0UxCUgQtvP8E9iX14BysmqHTJfZ10cis9Sh7B3QIY45P0yAPWCRE+OhOSShAcruC0bimgkae1vQ3F0ij+pOe8+eaS16y84lGHUQjJ9W7E6TBAXXY98v/f+MhbvoUr2BqWrZLLxl6VbYE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732714; c=relaxed/simple; bh=QrBIkTvCOFhbSOQNQYQPWRvBsOkZIQIhjIsflOZGoeA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iXjLgL593PCtu/ra/4eeLUHupGPXQoTvNoRWYZR96diajnNYsLJaXizNkWSmJuv7wuoP+ULe40b7Lm5N7ReXuievcO+5Btut1fLOnL3TVv3X8/pHuaL52XVZAmDCjf/hxzeezRtJ9c8Keih3vNOCUFvRlrHPN0XUkzBwlAvYqnU= 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=B12dAJtI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zqvkA/IZ; 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="B12dAJtI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zqvkA/IZ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732706; 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=KrenqVQnJa79mERVlAxgaQwwNijaZMJDRB7NmktjWzw=; b=B12dAJtIege/FsYTo9ibHKAB2hnNjQR+u8KGH7TREm3OPYQa9CGn9cxC6J5jpo262N1Svr VSoXjpzk0Gmx1R5kM9DM3zzZAyyILuBduwMVvF1dEXD1LAgxXdBJg90wBoZbSBIkvXxdZy zXGt5sBcRE8/WiGOp5A9UgVrL4yE96aF8tXT3z1xvF8kZNMWYizTjkwyL0mIGVzmCIKnBv VKC084v2apD7WKDSwxlGPenejrQ3QNgPB6zGM33srUC2Kg5Ywnr7OyDa6Zz9/0zj8bmSEo j2kARhSUaTWnLPy73HgHJfWb9YwZ2oP1t1vhZF/QmUO3CXMfdTn3N0DkpEo8xw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732706; 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=KrenqVQnJa79mERVlAxgaQwwNijaZMJDRB7NmktjWzw=; b=zqvkA/IZoiZeTR5tMPltcEV7p6KTPv9wq/vJ0hmwZvt+8Qlo5Sp6WV6QhzzwWtiyoN5EVr NPI30NP0gVSm2hBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v7 13/35] nbcon: Add API to acquire context for non-printing operations Date: Sun, 4 Aug 2024 02:57:16 +0206 Message-Id: <20240804005138.3722656-14-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" Provide functions nbcon_device_try_acquire() and nbcon_device_release() which will try to acquire the nbcon console ownership with NBCON_PRIO_NORMAL and mark it unsafe for handover/takeover. These functions are to be used together with the device-specific locking when performing non-printing activities on the console device. They will allow synchronization against the atomic_write() callback which will be serialized, for higher priority contexts, only by acquiring the console context ownership. Pitfalls: The API requires to be called in a context with migration disabled because it uses per-CPU variables internally. The context is set unsafe for a takeover all the time. It guarantees full serialization against any atomic_write() caller except for the final flush in panic() which might try an unsafe takeover. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 2 ++ include/linux/printk.h | 14 ++++++++++ kernel/printk/nbcon.c | 58 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/include/linux/console.h b/include/linux/console.h index aafe3121b74e..3706f944de46 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -322,6 +322,7 @@ struct nbcon_write_context { * * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print + * @nbcon_device_ctxt: Context available for non-printing operations * @pbufs: Pointer to nbcon private buffer */ struct console { @@ -417,6 +418,7 @@ struct console { =20 atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; + struct nbcon_context __private nbcon_device_ctxt; struct printk_buffers *pbufs; }; =20 diff --git a/include/linux/printk.h b/include/linux/printk.h index eee8e97da681..9687089f5ace 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -9,6 +9,8 @@ #include #include =20 +struct console; + extern const char linux_banner[]; extern const char linux_proc_banner[]; =20 @@ -198,6 +200,8 @@ extern asmlinkage void dump_stack_lvl(const char *log_l= vl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); void console_try_replay_all(void); +extern bool nbcon_device_try_acquire(struct console *con); +extern void nbcon_device_release(struct console *con); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -280,6 +284,16 @@ static inline void printk_trigger_flush(void) static inline void console_try_replay_all(void) { } + +static inline bool nbcon_device_try_acquire(struct console *con) +{ + return false; +} + +static inline void nbcon_device_release(struct console *con) +{ +} + #endif =20 bool this_cpu_in_panic(void); diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index f279f839741a..61f0ae6a4809 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include "internal.h" /* * Printk console printing implementation for consoles which does not depe= nd @@ -546,6 +548,7 @@ static struct printk_buffers panic_nbcon_pbufs; * nbcon_context_try_acquire - Try to acquire nbcon console * @ctxt: The context of the caller * + * Context: Under @ctxt->con->device_lock() or local_irq_save(). * Return: True if the console was acquired. False otherwise. * * If the caller allowed an unsafe hostile takeover, on success the @@ -553,7 +556,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(); @@ -1011,3 +1013,57 @@ void nbcon_free(struct console *con) =20 con->pbufs =3D NULL; } + +/** + * nbcon_device_try_acquire - Try to acquire nbcon console and enter unsafe + * section + * @con: The nbcon console to acquire + * + * Context: Under the locking mechanism implemented in + * @con->device_lock() including disabling migration. + * Return: True if the console was acquired. False otherwise. + * + * Console drivers will usually use their own internal synchronization + * mechasism to synchronize between console printing and non-printing + * activities (such as setting baud rates). However, nbcon console drivers + * supporting atomic consoles may also want to mark unsafe sections when + * performing non-printing activities in order to synchronize against their + * atomic_write() callback. + * + * This function acquires the nbcon console using priority NBCON_PRIO_NORM= AL + * and marks it unsafe for handover/takeover. + */ +bool nbcon_device_try_acquire(struct console *con) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_device_ctxt); + + cant_migrate(); + + memset(ctxt, 0, sizeof(*ctxt)); + ctxt->console =3D con; + ctxt->prio =3D NBCON_PRIO_NORMAL; + + if (!nbcon_context_try_acquire(ctxt)) + return false; + + if (!nbcon_context_enter_unsafe(ctxt)) + return false; + + return true; +} +EXPORT_SYMBOL_GPL(nbcon_device_try_acquire); + +/** + * nbcon_device_release - Exit unsafe section and release the nbcon console + * @con: The nbcon console acquired in nbcon_device_try_acquire() + */ +void nbcon_device_release(struct console *con) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_device_ctxt); + + if (!nbcon_context_exit_unsafe(ctxt)) + return; + + nbcon_context_release(ctxt); +} +EXPORT_SYMBOL_GPL(nbcon_device_release); --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 6B51F101F7; Sun, 4 Aug 2024 00:51: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=1722732715; cv=none; b=TslB5ML/WtKwAEkxV+8LCXomE+vMH6bYRCr9USOHlT+GOvkElHoHU98Kt2l0ApY3XKWefDt7raGhT5722Ci+LdEURE1U50FA5GG7tcXnGNf0WE797fRQ3mpVRoQ3HA8Ht3KpTYyEGPqTb7Z3pLBmUYuLnd+tZo9gIIpUm8L8Q7o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732715; c=relaxed/simple; bh=R4TOefOxndRBCPUXtpdDEsXMvKoXT/g+AiM/wcdCOiI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rYIwxX2Vo1cUd8SVjf2eR9ytMxSeR7SrdSbmBwW8ZcUIKgwuDSL1mYkmMhhoTYjorOg70vwSjuTWwQ+o07LDpkJHRWceDxmB5U6FBDgric0nD0qsD80vh/uwPEZ65WxL0rmhJBaBhXwXuDQGuvaX+oBMcDi+244NTg6E0gX2LuE= 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=l/fXZS+x; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dv56lOLv; 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="l/fXZS+x"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dv56lOLv" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732706; 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=ElxI59csI9kczE5ClB/S01RcILMnwACUfY2TZOkMQ4U=; b=l/fXZS+x0lNZdVdNF3mHafrcm28dLMexDUad/up+cz844KzgH63zSE6hJuDJrvCGszuSsN VRNUu9HYhxKaDGg+NfOniOXuDuPpQzeoYU3gUiF5mwkdzSJVE9Hd6zyRpQ3jotctYefglB WcX+1xAH6Csh6CqIsNUVuXsdiXfY8R8lvEc/+CIbXtj8BRwYQ6R8n9PbB7RYQUYAhVr3io pb+P12zdxhOW8fNwhlLt/s978Gi3GTj3d4a5n0zoIwXaP0Ptx+ITelip1P0s6/3UtASiaH zbGUam3BqCPHU1ul6uL+rRJSBFnC2+wsAmXtr2BmQ/EAX5b00OMJsRZquTqyYQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732706; 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=ElxI59csI9kczE5ClB/S01RcILMnwACUfY2TZOkMQ4U=; b=dv56lOLvq0mQUvKTp9eZ0uGVOjGWIr1SLxu8RmgIkfxkfSmG3mC7Lm/3+X/odBARzrpDqB YTNN12tEI1Fw5OCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , linux-serial@vger.kernel.org Subject: [PATCH printk v7 14/35] serial: core: Acquire nbcon context in port->lock wrapper Date: Sun, 4 Aug 2024 02:57:17 +0206 Message-Id: <20240804005138.3722656-15-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 that has implemented the write_atomic() callback, the wrappers must also acquire/release the console context and mark the region as unsafe. This allows general port->lock synchronization to be synchronized against the nbcon write_atomic() callback. Note that __uart_port_using_nbcon() relies on the port->lock being held while a console is added and removed from the console list (i.e. all uart nbcon drivers *must* take the port->lock in their device_lock() callbacks). Signed-off-by: John Ogness Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek --- include/linux/serial_core.h | 82 ++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 2cf03ff2056a..4ab65874a850 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -11,6 +11,8 @@ #include #include #include +#include +#include #include #include #include @@ -625,6 +627,60 @@ static inline void uart_port_set_cons(struct uart_port= *up, struct console *con) up->cons =3D con; __uart_port_unlock_irqrestore(up, flags); } + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_using_nbcon(struct uart_port *up) +{ + lockdep_assert_held_once(&up->lock); + + if (likely(!uart_console(up))) + return false; + + /* + * @up->cons is only modified under the port lock. Therefore it is + * certain that it cannot disappear here. + * + * @up->cons->node is added/removed from the console list under the + * port lock. Therefore it is certain that the registration status + * cannot change here, thus @up->cons->flags can be read directly. + */ + if (hlist_unhashed_lockless(&up->cons->node) || + !(up->cons->flags & CON_NBCON) || + !up->cons->write_atomic) { + return false; + } + + return true; +} + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return true; + + return nbcon_device_try_acquire(up->cons); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_acquire(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return; + + while (!nbcon_device_try_acquire(up->cons)) + cpu_relax(); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_release(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return; + + nbcon_device_release(up->cons); +} + /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure @@ -632,6 +688,7 @@ static inline void uart_port_set_cons(struct uart_port = *up, struct console *con) static inline void uart_port_lock(struct uart_port *up) { spin_lock(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -641,6 +698,7 @@ static inline void uart_port_lock(struct uart_port *up) static inline void uart_port_lock_irq(struct uart_port *up) { spin_lock_irq(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -651,6 +709,7 @@ static inline void uart_port_lock_irq(struct uart_port = *up) static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned l= ong *flags) { spin_lock_irqsave(&up->lock, *flags); + __uart_port_nbcon_acquire(up); } =20 /** @@ -661,7 +720,15 @@ static inline void uart_port_lock_irqsave(struct uart_= port *up, unsigned long *f */ static inline bool uart_port_trylock(struct uart_port *up) { - return spin_trylock(&up->lock); + if (!spin_trylock(&up->lock)) + return false; + + if (!__uart_port_nbcon_try_acquire(up)) { + spin_unlock(&up->lock); + return false; + } + + return true; } =20 /** @@ -673,7 +740,15 @@ static inline bool uart_port_trylock(struct uart_port = *up) */ static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigne= d long *flags) { - return spin_trylock_irqsave(&up->lock, *flags); + if (!spin_trylock_irqsave(&up->lock, *flags)) + return false; + + if (!__uart_port_nbcon_try_acquire(up)) { + spin_unlock_irqrestore(&up->lock, *flags); + return false; + } + + return true; } =20 /** @@ -682,6 +757,7 @@ static inline bool uart_port_trylock_irqsave(struct uar= t_port *up, unsigned long */ static inline void uart_port_unlock(struct uart_port *up) { + __uart_port_nbcon_release(up); spin_unlock(&up->lock); } =20 @@ -691,6 +767,7 @@ static inline void uart_port_unlock(struct uart_port *u= p) */ static inline void uart_port_unlock_irq(struct uart_port *up) { + __uart_port_nbcon_release(up); spin_unlock_irq(&up->lock); } =20 @@ -701,6 +778,7 @@ static inline void uart_port_unlock_irq(struct uart_por= t *up) */ static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsig= ned long flags) { + __uart_port_nbcon_release(up); spin_unlock_irqrestore(&up->lock, flags); } =20 --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 D789D1862A for ; Sun, 4 Aug 2024 00:51: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=1722732715; cv=none; b=maZL6YZjqAdKHAqM2wulTvSIKYom8HVVQD+azjJx/zT5URqwvDNWoOwEFD81Blofa1REyXDwNP/ET9d6rPLlbswa2SeZI8kQJMWTmKdQ7VVoRJKhMfXKScAT0tLTKJATL+1Oso4ZO9tIkdWT3yDiocXBo66lhwAHyDf8djCWXAw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732715; c=relaxed/simple; bh=1c1EohtOFTkv/3gZxIc0vQk/ntHg84KrPkKocOc813o=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tiZ31UoQcNpKfsqDXdY6VQd7T3lB61/tfESxAlKJ8zwXlDDT12H3BGI4mEasXvuWBltlrgMe+Qh9PCDMbhGXqVN5G7yVehuQxVWNPkF3XiFYz6xsQt5xcO5PhGSQCQSZ1EdICo/fKXctGQlWQ16qg2mG6159J57w/3Xbro8ssa4= 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=YlY2gWbe; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=KTyzwXs6; 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="YlY2gWbe"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="KTyzwXs6" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732706; 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=aNeK2H/ne/glar5xPRr97ZgYSDkKLjqfRfr1orQbwpk=; b=YlY2gWbenfVBqR1ClmwH2828t/lo56tbswviCp2EfvKtorVNMpEYA2nBiOm3Z3NAvryT9K 1wwv3dBxQL1zsQXIBd7mYPp8NRa3McZ9wF5mV6bbp1fYvHxM+oUMqs0qyQGQCH8KyAqRkS ECDv7yNKNL1vG+XAiJlAnKbZsLS02AGTX3D0SstxoVu+i4yk7odA0dXinNaWB7YwW5s3Tl HC/6ZrreFgJpwrY7GG67/IJnxgBpozPg6XuonV8bfqSv66bVuX/m0SwPFWcJLq4BkkN3bT 7lhd/xaHMqAYDqwfGz6H1ZMQwKGdiUf4aTGH/iW9pfrvv7mHFkTaA0MXzjtXxA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732706; 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=aNeK2H/ne/glar5xPRr97ZgYSDkKLjqfRfr1orQbwpk=; b=KTyzwXs6pK2p6tZgXO5HAVysOCEpcfgEYPR7igKAGMp0D9gvhRVxC8H2yXw+PIYXwt03Bp FCsffQnxuQsIRvAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andy Shevchenko Subject: [PATCH printk v7 15/35] printk: nbcon: Do not rely on proxy headers Date: Sun, 4 Aug 2024 02:57:18 +0206 Message-Id: <20240804005138.3722656-16-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 headers kernel.h, serial_core.h, and console.h allow for the definitions of many types and functions from other headers. Rather than relying on these as proxy headers, explicitly include all headers providing needed definitions. Also sort the list alphabetically to be able to easily detect duplicates. Suggested-by: Andy Shevchenko Signed-off-by: John Ogness Reviewed-by: Andy Shevchenko Acked-by: Petr Mladek --- kernel/printk/internal.h | 8 ++++++-- kernel/printk/nbcon.c | 13 ++++++++++++- kernel/printk/printk_ringbuffer.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index dc8bc0890fd2..ccb916688178 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -2,11 +2,12 @@ /* * internal.h - printk internal definitions */ -#include #include -#include "printk_ringbuffer.h" +#include +#include =20 #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL) +struct ctl_table; void __init printk_sysctl_init(void); int devkmsg_sysctl_set_loglvl(const struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos); @@ -43,6 +44,9 @@ enum printk_info_flags { LOG_CONT =3D 8, /* text is a fragment of a continuation line */ }; =20 +struct printk_ringbuffer; +struct dev_printk_info; + extern struct printk_ringbuffer *prb; =20 __printf(4, 0) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 61f0ae6a4809..e8ddcb6f7053 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -2,13 +2,24 @@ // Copyright (C) 2022 Linutronix GmbH, John Ogness // Copyright (C) 2022 Intel, Thomas Gleixner =20 -#include +#include +#include #include #include +#include #include +#include +#include +#include +#include +#include #include +#include +#include #include +#include #include "internal.h" +#include "printk_ringbuffer.h" /* * Printk console printing implementation for consoles which does not depe= nd * on the legacy style console_lock mechanism. diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringb= uffer.h index 52626d0f1fa3..bd2a892deac1 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -5,6 +5,8 @@ =20 #include #include +#include +#include =20 /* * Meta information about each stored message. --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 1D5FC1B28A for ; Sun, 4 Aug 2024 00:51: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=1722732716; cv=none; b=WauvKlu4QNi9pnb9LjQ/5VtcgbGPr2O154Srx2OBgPcgGP85/rqMUJXGq9cJI7xOJNeqsBOr/5PlxXiXL/t10tKnqM4S9ef7rrB2AfY7I3fzZ0izDnynQsuvX4hlFWXFw/ak+0IeZ9Ei/2SeyWNE0lOHFymDk95ZJ/vaDqbi7oE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732716; c=relaxed/simple; bh=JFMKZtGPkrU5SIuPBdCMEj2nzyR/bidpQHk9sb/QEjk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=l4Hgg/FYcyCVQQmxFOlNxZr//tcc4Wz3lVkKUgHsmGqvy6chOIQ6QIjsPTRd2tTmQKOkHplkknWpPqxDQydmCPKsKhe93YP02VykUvUIk9CcI5GTbm/TuapKqj0Klw0NJleysMKXcoMqmw9x2zrZFs0Gl0TsQW8FgVIE7pajdZ4= 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=NIpd4oll; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ZxqrHUmh; 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="NIpd4oll"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZxqrHUmh" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732707; 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=V3J+O0K9BaumAkEip6m7BqVS+0WrsRdwTh0cSg6mOBk=; b=NIpd4ollVWMMPN/4Yd3Ek5Kgd2DEKwLeKrKEQc1SByi/oyorRTUUxy1zvdml1I4RpQi8eh 3qXDaBX2uJtyGww6r39s/F4v40beFDrgHrhA34mtYntwGSeRSv7qR9xJIdsH8ZJ4AAuFIu M1GUEpmMK6buWTYAuiilAO3S4yOJQZ7p8AbCPvUWKpIdxdJxAYcRUVP5bSIuHQmxiSSu/0 kJGyoMvFa//mYiMxEyCknbFd8QbE4OPQg1oxh4Cb04NgJW35tihXkSthXNUwEuej5AdhXW TNJ4WnheykWm0vXldeyqSi4iU9sn007zWMVmJ7OCbKqic9336ap0jGFj/coPug== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732707; 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=V3J+O0K9BaumAkEip6m7BqVS+0WrsRdwTh0cSg6mOBk=; b=ZxqrHUmhPcuJG05UWRkif/Ig2bIHAAP4CIX9fI/Wa5QziHbAexLCpnK8b+Aw6gkrKRe7BU O9FgXuP/9HNF2ODQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 16/35] printk: Make console_is_usable() available to nbcon.c Date: Sun, 4 Aug 2024 02:57:19 +0206 Message-Id: <20240804005138.3722656-17-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 ccb916688178..5d9deb56b582 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -84,6 +84,36 @@ void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(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 @@ -104,6 +134,8 @@ 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_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 7027f0868ef4..5a1fa8eebe5a 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2767,36 +2767,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:34:49 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 F26F519470 for ; Sun, 4 Aug 2024 00:51: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=1722732715; cv=none; b=qI8rUUZGAp3JNWoREetZkWroP9hOPB3l/5qPmpt7wbA0T+Udcqqi6+8JVGL+Hd219GaWypkrc0H7ngvPdREoHSsUQ/6nQfVK0XKueRUDPE7vV4wS5hMGmH7KneP13kIY5gTEc9Vfn35EEABVf19sT6E1IKM2xrblqizvvHK57iY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732715; c=relaxed/simple; bh=Ov5T451CRpMNZDviwPZPsNDiuJcBYgBNHBKPViE70gg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eybfKVMgu9S872jmaIumhiEHTqRQN1x959TE3/SsXnwNvt0So9t/+ZcvuHjH1XEkyb2a9h95GeZntUovFfGYn1qOGy6aMTqM5pdAPWdg523Pfd8uW9pwOEZthNkZr5ZIxAWXYLMr/O7AfLRX3nU/DE5kJTuC3UINWmmNhcetsxk= 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=xHVfOPKN; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mWJr5YiA; 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="xHVfOPKN"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mWJr5YiA" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732707; 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=SIvGXSI9zL3LqtnkQPrC0V491Iai+TRLLKit8ny2zJg=; b=xHVfOPKNjTQzcqRuLdxwA9LkG8+GCGmjmiL/RYaVYIWWtASK6FdKbeCYyA9txT+VIUiuI9 fuwhBYcOJk7wlPhgkXPRn/15JMCAenw2jfZ52t2+ijGmvYhcpaS9K6d9WsNMSVNR9+Pey+ 4YpeG5TzEDNpzPYu3SdPU31htAJr4t870Jcs3WpGkPzzF9HP5gxMtGOl3uw40rXy0OyTdu CsqD4f+HIrf63XOWXiJHtQhhkCV9/VnlyLQ6Bym06ICCWfySPdT+A1tRgbBcyn15gXHzbo Qlo7KjK94L29GcthRGiQzMVZmWVBKxdoF2XH3azk7NuCmJ+1ak8fLo91I/hr8w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732707; 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=SIvGXSI9zL3LqtnkQPrC0V491Iai+TRLLKit8ny2zJg=; b=mWJr5YiAamoJP+/EYI7lwo0JVoowG+VwS8RkaY38zS+uRCXJWjHkRgxbcf/6BDhJeVgZSj EKmo4i6zkttdIqBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 17/35] printk: Let console_is_usable() handle nbcon Date: Sun, 4 Aug 2024 02:57:20 +0206 Message-Id: <20240804005138.3722656-18-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 5d9deb56b582..448a5fcd5228 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -86,6 +86,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. @@ -100,8 +102,13 @@ static inline bool console_is_usable(struct console *c= on) 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:34:49 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 325971B947 for ; Sun, 4 Aug 2024 00:51: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=1722732716; cv=none; b=ZOmf6jiCp6hAX+22eCT3dvlcgjC8MpBtWRSZ3SoNcay6C6OkaXwTOX8w4iPfdugOZ0POeuhe9ZLgHUsb1bl50mhIKYsOzXmmSGFoPA+ilyFTGcSpKebPlY7IB8HTi6Ws2ylv7ew+LooNUGy0dGJczb+XWzkDGNx0Hk56IUn2+Eo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732716; c=relaxed/simple; bh=G+xWckYC4K1B7DC/KGSgacVTp49duoqdbQIL4F0uaCc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=EFf6tx7U6zbf2esXGpG2LxOxn4491uJdRRF9hIfhYb5tpJhJzwnW+Nmp2kBrKf8gEtATkDJqxGOKjwBf3yFGLdVGoBpoXR66DJL19W6nWC7hbAylgNhwEJhxkwsUaZQ/LdzYQd3Ql6lbbwyQXXdgsP0cdc5zP+kPX0uxkufSzFI= 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=sbyjMvhf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=u7TnVgsb; 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="sbyjMvhf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="u7TnVgsb" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732707; 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=6M8Gq7LSZt3yh7g7N/GWSaxKEw70meerv1IlbLtxnbM=; b=sbyjMvhfevmG6byImWqjZy8X+b/mMUdRs+DYWi36RMorpZobUFurvFq9WXMuBSAIrbxdL2 uAjSpaGbb93G0O1kvyQ2fcnhCPoraLeNsMKR8Rp+S3QZdFX6ZcwVR/YinRHLNvhVdDrHiv Xzw/uk4w3G2mNm6PzpoCQo1iv4Q0gqAqXbIrtIj6oTzNsckUSSkt8RRLof2d+us4UhMGvt DHBAGsw3BN5UvgN7VGiw5hd7pDdGrFWVAuy8YtQn+NdaMs4v3/l2Um4Hg6WJyvmZvZcgU6 /TZ7otggtJmkKf0aWkSWsczcEdleSWthffbInqVOLT8hOhid0lM3TXIDhTV3DA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732707; 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=6M8Gq7LSZt3yh7g7N/GWSaxKEw70meerv1IlbLtxnbM=; b=u7TnVgsbbdFDFGeCT1uBrfcHoOiN06igilwyvAcln53H2xLW4KSWS1IhFtdTSM0m7ptrt6 prBn5ogojZmTLIDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 18/35] printk: Add @flags argument for console_is_usable() Date: Sun, 4 Aug 2024 02:57:21 +0206 Message-Id: <20240804005138.3722656-19-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 448a5fcd5228..fe8d84d78f1c 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -89,13 +89,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 @@ -141,7 +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_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 5a1fa8eebe5a..92703249221e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3012,9 +3012,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 @@ -3925,7 +3926,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:34:49 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 4E8421BF2A for ; Sun, 4 Aug 2024 00:51: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=1722732716; cv=none; b=QYg7Ba31SFtLbypnA6IRFnW/JCCnTUqh7I5JMybvtfB+GyQUkIxdpt8t7PFSq6Uxhxky66Q+ziLFCSmS5arlJ7UDb1BVzKt5BPBuCae94UkrCcHCpebfndl15FKqk8HGmPdzSDJvCDrGnPJTT51F5B3Gi75PeEUisY3Co/nv+dU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732716; c=relaxed/simple; bh=/1fC2MDpEWd2pUlfHW4UNXW6hcss05zFf6RJFQQGxiI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iN8tEoQ+1+t8VIGWBsYNxPpoPUDA7Pm5kj/J6jZvSun3YbN3QM7lMe3tW/CfhY3M8XKmV6+YIusWxG31kRJ+E+9L/Xt+ng6kY90pEkvVAc+4QuOrGP1SItQB5iWjfeO7nPfvlnG3h1Ri5Upy/odLImmNa6YD+79wF6FasRkiw/g= 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=TJVPaMNJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=f1n8X4EU; 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="TJVPaMNJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="f1n8X4EU" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732708; 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=OkGQHNfVXtcl0EJbdL5BCwdq4g/JjMGFkJn6O0f7o4c=; b=TJVPaMNJUoJxX2T/0izhLxy8bPAGhq8R9ezabK4YOoVhCAD2wuIZa6H7atkCGOrpYh4Wud pwpdu9xRpAL2AXTmpByfHzplPX4NN8gqjqVmFVIu2QX2csQvSWiQThRaaRHc1L/6G5e3V8 S7KGt/F3UfUfMj36X5noQFJNYABr+r7l8I2vOKI009PTGUmuPLRdW1FHdPHigEmM4elmEQ 3Cq5zTIOG4XUVEY5VK/M7bh2BQQnKJC0PFLraXV8hNL082hlxNZOBohGWlgs0WCIHZu+CP gqvcw69jveCFXLQ9MS1jxOPIQUqePyZYwEUg+30dsSHUez3OUBOnXMH3PuHmEg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732708; 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=OkGQHNfVXtcl0EJbdL5BCwdq4g/JjMGFkJn6O0f7o4c=; b=f1n8X4EUp1Wv7eLUxNi2izc8ciX973txB3U7B4XLIBymk0H8vpTytJviy0iQoSk5MYzwMq /efLTJEU6njgXAAw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 19/35] printk: nbcon: Add helper to assign priority based on CPU state Date: Sun, 4 Aug 2024 02:57:22 +0206 Message-Id: <20240804005138.3722656-20-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 helper function to use the current state of the CPU to determine which priority to assign to the printing context. The EMERGENCY priority handling is added in a follow-up commit. It will use a per-CPU variable. Note: nbcon_device_try_acquire(), which is used by console drivers to acquire the nbcon console for non-printing activities, is hard-coded to always use NORMAL priority. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 ++ kernel/printk/nbcon.c | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index fe8d84d78f1c..72f229382cfa 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -83,6 +83,7 @@ u64 nbcon_seq_read(struct console *con); void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); void nbcon_free(struct console *con); +enum nbcon_prio nbcon_get_default_prio(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -136,6 +137,7 @@ static inline u64 nbcon_seq_read(struct console *con) {= return 0; } 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_free(struct console *con) { } +static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } =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 e8ddcb6f7053..c6a9aa9f62f6 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -973,6 +973,25 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon + * printing on the current CPU + * + * Context: Any context. + * Return: The nbcon_prio to use for acquiring an nbcon console in this + * context for printing. + * + * The function is safe for reading per-CPU data in any context because + * preemption is disabled if the current CPU is in the panic state. + */ +enum nbcon_prio nbcon_get_default_prio(void) +{ + if (this_cpu_in_panic()) + return NBCON_PRIO_PANIC; + + return NBCON_PRIO_NORMAL; +} + /** * nbcon_alloc - Allocate and init the nbcon console specific data * @con: Console to initialize --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 887691CAA4 for ; Sun, 4 Aug 2024 00:51: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=1722732717; cv=none; b=ifGyXrek//ekwrgMSs06Lww1iTzLTuWzr6g5DjO3s1uN7gu7f4FEmKyn46OmL5y3hhO7icj4pEJiOcxpaeNYeaWPI/5ShYwthcrSPHSw2oxvD18WI9vZX8qoSRCb1LCWAEA18964FxQ8CTjo8GiOb6nWrjWvmOe1vptM4r6T3mM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732717; c=relaxed/simple; bh=lzHDanAMA58/o4S6hTw6xhfPspsP5ayXvb2Thx441U0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sTWgBZH17BrIT8ysDA49jAF3gMagWw9CODRM5VUspabH49hF7bhMWmE/Qw/z0biQrW+MIo6HzatpLzVwsPisDIYC1Ly2pgtV69i9gVTKh5ho0U6SYeXUZuHp9vZY59AR8xIVzF9au5e9O10JJb2f2f+4NSUR/xRiaheq8EBSLQ0= 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=vLD6KQZ5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bC8Z7did; 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="vLD6KQZ5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bC8Z7did" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732708; 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=H8b4erBcGS06cHD0VP7/dc6kNZTfPA5xuY4jhY77fi8=; b=vLD6KQZ5FOlBbKi7+rNetiYAKm6ddr++BsdeEM3OTq5YBSe2oV9bfrWDZWsNoVt+FBe9cB 19kb+1toB+29+LQGpy2Eq1SHzojJ87k0WUobU2N/xUW2u056hWUxG1XBcXOy3SgRfSW5ka mjOnPezOwJKsjyOO2M2O4j8Xu4aTNaVhQflQ9v/f6kk5mhdTi/myFnIV9mJe3be2+J0S4G opEP0rnApG6uxAtW9dp1xAsII/ogzr2aS5hZ6XGNojsE4A+3OL5HxxMuQhKrEacE+rG7Cl S314JXgUJbY1KxV04vum7L6NBdjyi+qWZUvsPOxq7xyRZjMIkYqH0tvu+hdRbQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732708; 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=H8b4erBcGS06cHD0VP7/dc6kNZTfPA5xuY4jhY77fi8=; b=bC8Z7didNfLFROXqzfTmHBlSEkZoPMqzB1xJnB7aFFB12qJiLJy//JysgzEw+azh/gNn+D cOg+5WYbKYglOrDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 20/35] printk: nbcon: Provide function to flush using write_atomic() Date: Sun, 4 Aug 2024 02:57:23 +0206 Message-Id: <20240804005138.3722656-21-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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_pending() to perform flushing of all registered nbcon consoles using their write_atomic() callback. Unlike console_flush_all(), nbcon_atomic_flush_pending() 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. If new records are added while flushing, it is expected that the dedicated printer threads will print those records. If the printer thread is not available (which is always the case at this point in the rework), nbcon_atomic_flush_pending() _will_ flush all records in the ringbuffer. Unlike console_flush_all(), nbcon_atomic_flush_pending() will fully flush one console before flushing the next. This helps to guarantee that a block of pending records (such as a stack trace in an emergency situation) can be printed atomically at once before releasing console ownership. nbcon_atomic_flush_pending() is safe in any context because it uses write_atomic() and acquires with unsafe_takeover disabled. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- kernel/printk/internal.h | 2 + kernel/printk/nbcon.c | 151 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 152 insertions(+), 1 deletion(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 72f229382cfa..0dc9b92b5dd0 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -84,6 +84,7 @@ void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); void nbcon_free(struct console *con); enum nbcon_prio nbcon_get_default_prio(void); +void nbcon_atomic_flush_pending(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -138,6 +139,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_free(struct console *con) { } static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } +static inline void nbcon_atomic_flush_pending(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 c6a9aa9f62f6..3982d68979d6 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -886,7 +886,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); @@ -992,6 +991,156 @@ enum nbcon_prio nbcon_get_default_prio(void) return NBCON_PRIO_NORMAL; } =20 +/* + * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using = its + * write_atomic() callback + * @con: The nbcon console to flush + * @stop_seq: Flush up until this record + * + * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on + * failure. + * + * Errors: + * + * -EPERM: Unable to acquire console ownership. + * + * -EAGAIN: Another context took over ownership while printing. + * + * -ENOENT: A record before @stop_seq is not available. + * + * If flushing up to @stop_seq was not successful, it only makes sense for= the + * caller to try again when -EAGAIN was returned. When -EPERM is returned, + * this context is not allowed to acquire the console. When -ENOENT is + * returned, it cannot be expected that the unfinalized record will become + * available. + */ +static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_= seq) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + int err =3D 0; + + ctxt->console =3D con; + ctxt->spinwait_max_us =3D 2000; + ctxt->prio =3D nbcon_get_default_prio(); + + if (!nbcon_context_try_acquire(ctxt)) + return -EPERM; + + while (nbcon_seq_read(con) < stop_seq) { + /* + * 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 -EAGAIN; + + if (!ctxt->backlog) { + /* Are there reserved but not yet finalized records? */ + if (nbcon_seq_read(con) < stop_seq) + err =3D -ENOENT; + break; + } + } + + nbcon_context_release(ctxt); + return err; +} + +/** + * nbcon_atomic_flush_pending_con - Flush specified nbcon console using its + * write_atomic() callback + * @con: The nbcon console to flush + * @stop_seq: Flush up until this record + * + * This will stop flushing before @stop_seq if another context has ownersh= ip. + * That context is then responsible for the flushing. Likewise, if new rec= ords + * are added while this context was flushing and there is no other context + * to handle the printing, this context must also flush those records. + */ +static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq) +{ + unsigned long flags; + int err; + +again: + /* + * 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 to acquire console ownership. + */ + local_irq_save(flags); + + err =3D __nbcon_atomic_flush_pending_con(con, stop_seq); + + local_irq_restore(flags); + + /* + * If there was a new owner (-EPERM, -EAGAIN), that context is + * responsible for completing. + * + * Do not wait for records not yet finalized (-ENOENT) to avoid a + * possible deadlock. They will either get flushed by the writer or + * eventually skipped on panic CPU. + */ + if (err) + return; + + /* + * If flushing was successful but more records are available, this + * context must flush those remaining records because there is no + * other context that will do it. + */ + if (prb_read_valid(prb, nbcon_seq_read(con), NULL)) { + stop_seq =3D prb_next_reserve_seq(prb); + goto again; + } +} + +/** + * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their + * write_atomic() callback + * @stop_seq: Flush up until this record + */ +static void __nbcon_atomic_flush_pending(u64 stop_seq) +{ + struct console *con; + int cookie; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + + if (!(flags & CON_NBCON)) + continue; + + if (!console_is_usable(con, flags)) + continue; + + if (nbcon_seq_read(con) >=3D stop_seq) + continue; + + nbcon_atomic_flush_pending_con(con, stop_seq); + } + console_srcu_read_unlock(cookie); +} + +/** + * nbcon_atomic_flush_pending - 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 if there is another + * context available to handle the flushing. This is to avoid one CPU + * printing unbounded because other CPUs continue to add records. + */ +void nbcon_atomic_flush_pending(void) +{ + __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb)); +} + /** * nbcon_alloc - Allocate and init the nbcon console specific data * @con: Console to initialize --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 6D4F61CA96 for ; Sun, 4 Aug 2024 00:51: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=1722732716; cv=none; b=TuhPkU5H2fUXrTBOp8m7M5a7MbinPCYDusb6wn8ih9fAg4MLyYN7JYwBr+QeDp5Tv0EIAL+GX+rAE6rNlEOJxnGrnLTCEw3/MA5OIEwhJWOqpjjzVfOcrGMkuYpIQNBvlh77ZrIqp6U0SQv5EDNfEMu9YKojOyJOAF9C/jFCCdI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732716; c=relaxed/simple; bh=2yEvvgpxPi0fwOxyRUTE8YI5U24mFJe3k3lvFNTEOko=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RKonIe2rkvTDuTHmJhraCT4VEly0HJDCG+XgPWnZhlcZVLO+B0xOkBs9QGqK6hZO67AoDdPnf80WfHSpi9YN/Oqc5U/X4JiCc+bu8/cFWVXek5tgvCGxyR/mIRVlaQcC9q896tPd1cCtN96ecxx91to9hI7Bxe1o0PFHLMYQliA= 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=tcYQapjb; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pu0zK38+; 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="tcYQapjb"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pu0zK38+" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732708; 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=F0TVA7p3xKgsnI7/AR/VPpgrXQpo502eMwZ3DcBfU8s=; b=tcYQapjblWWPpK2I3B5GxWbVqr9xfAg9xa9wRNpzoodFolwzkQn50HY3W37ov16biu7i4G AETe9dzXkMoESbDGXOD495tquBFgVsovSFPIP1opURRQ6lmCkp/rQcRqk+ssxV1IbvCbtG afdRnhHP0V8tr3ljiE8VRX5aPinXZTbcFjLPNSXh8Ei7fuUL16pQfaSzXsU8czEoKZD5so 9ErmY7EnPCX3qfWNEjk++aLaGEbImWGfirrZ0EekfHE/7RwxIitNqyQ6f5Fdhgtfgx1NYH k+O1/CJr/ntu49zoMwXzRV8KyPmNt24tL0q0Vefb215KrK1Wh/DlTlvPiZlZ/w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732708; 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=F0TVA7p3xKgsnI7/AR/VPpgrXQpo502eMwZ3DcBfU8s=; b=pu0zK38+Wmn+pB9Q4HHZuqE003yrjBv31deZk9vcfnIDlfId9Pnu+rNFkiBOqyOBbKoQV6 1AcXn4ryH5igbBAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 21/35] printk: Track registered boot consoles Date: Sun, 4 Aug 2024 02:57:24 +0206 Message-Id: <20240804005138.3722656-22-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 92703249221e..be4b9c491d8d 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. + */ +static bool have_boot_console; + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -3610,6 +3618,9 @@ void register_console(struct console *newcon) newcon->seq =3D init_seq; } =20 + if (newcon->flags & CON_BOOT) + have_boot_console =3D true; + /* * If another context is actively using the hardware of this new * console, it will not be aware of the nbcon synchronization. This @@ -3680,7 +3691,9 @@ EXPORT_SYMBOL(register_console); static int unregister_console_locked(struct console *console) { bool use_device_lock =3D (console->flags & CON_NBCON) && console->write_a= tomic; + bool found_boot_con =3D false; unsigned long flags; + struct console *c; int res; =20 lockdep_assert_console_list_lock_held(); @@ -3738,6 +3751,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 found_boot_con; + return res; } =20 --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 A22CC1CD13 for ; Sun, 4 Aug 2024 00:51: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=1722732717; cv=none; b=C+5KLqcnMzcYaATwdDUbWT3upVv0unbipBhCldd0Hlzn24pQe5ow4TbHTTA2E5UF0SbcsQZEoSNq0TJ1DlUBLtpDAjtZC6NlnscnZ2AfgYLa1ucnSMKp50TDRGKa86Nf1+HmnxkxfI3dWt3sgGUVcUVLFAgy2A0GBQR6BD6Fhms= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732717; c=relaxed/simple; bh=k2WoZsep9yg3VY555WynR/NfKdYOXdH6B7fohfRZl1k=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VlC54ifE8uZSv11o6p0of9nEqTVJtni7CBPXXRufMU2XJE2chVHk1uS77lzRyquO2FpWWlbMQbtYWckkWonm5R0tZrBIsJtPTSuRL9yNGovR6uSlRr6uwqWtVFykZiLl2CLHDltdwSCO1/U8gPBrFOc6Q4Z4RyzXt3rRGwJI3/w= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=k6r7kHGw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=X6u+mM+F; 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="k6r7kHGw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="X6u+mM+F" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732709; 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=rK1kV2dH56snTzlyi/VpxzpCeokllNREHigInDlgMwE=; b=k6r7kHGwwYwUSqU8tmYEW2qm4dEn3mB0XmxNVWqzKn50CY3XbBYtdmipI85TqE9dk0SoGs Aaoa3nD3sVXT1q2b9vPDxtRHANOAsWfYTLlfOEiJ71IMLYqSP12TDxDhqoS1OVg75sve95 mv/l1fRgkP6cBB/1eYFyb6gGRipSlvFj71FbAlUIzpUoiz5BueVZZ88riHOCqVXyE+lHM5 2mxgdHLsHou+CefBk0RbqE0d6rBvClqtiAnoBQJThDJnASEbVXYulfSgm/94qYoPIDlJ8U nSDI/gcSIwZCeQgaQq4apwRGJXKac7SMcOk/LJWavOSs3ue6KN+/rAfyIM6hRw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732709; 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=rK1kV2dH56snTzlyi/VpxzpCeokllNREHigInDlgMwE=; b=X6u+mM+FI9hUo7HFqiiKtMRG4Tb1PbkoQC3igm8P1xUolj4wdj95dtIBrFdtpNrL2ONdXV ftwx2V5NrZpP73Bw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 22/35] printk: nbcon: Use nbcon consoles in console_flush_all() Date: Sun, 4 Aug 2024 02:57:25 +0206 Message-Id: <20240804005138.3722656-23-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 in console_flush_all(). Note that the legacy printing logic uses @handover to detect handovers for printing all consoles. For nbcon consoles, handovers/takeovers occur on a per-console basis and thus do not cause the console_flush_all() loop to abort. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 6 +++ kernel/printk/nbcon.c | 87 ++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 17 +++++--- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 0dc9b92b5dd0..44468f3828f3 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -78,6 +78,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); @@ -85,6 +87,8 @@ bool nbcon_alloc(struct console *con); void nbcon_free(struct console *con); enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_pending(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 @@ -140,6 +144,8 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } 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_pending(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 3982d68979d6..d09c084c9af4 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -992,6 +992,93 @@ enum nbcon_prio nbcon_get_default_prio(void) } =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: True, when a record has been printed and there are still + * pending records. The caller might want to continue flushing. + * + * False, when there is no pending record, or when the console + * context cannot be acquired, or the ownership has been lost. + * The caller should give up. Either the job is done, cannot be + * done, or will be handled by the owning context. + * + * 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. + * + * The higher priority printing context takes over responsibility + * to print the pending records. + */ + if (!nbcon_emit_next_record(wctxt)) + return false; + + nbcon_context_release(ctxt); + + return ctxt->backlog; +} + +/** + * 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 except NMI. + * Return: True, when a record has been printed and there are still + * pending records. The caller might want to continue flushing. + * + * False, when there is no pending record, or when the console + * context cannot be acquired, or the ownership has been lost. + * The caller should give up. Either the job is done, cannot be + * done, or will be handled by the owning context. + * + * 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); + unsigned long flags; + bool progress; + + /* 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_get_default_prio(); + + 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_pending_con - Flush specified nbcon console using = its * write_atomic() callback * @con: The nbcon console to flush diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index be4b9c491d8d..5f5fc5df2f09 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1860,7 +1860,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. @@ -1899,7 +1899,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 @@ -3021,13 +3021,20 @@ 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 @@ -3037,8 +3044,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:34:49 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 A229A1CD0C for ; Sun, 4 Aug 2024 00:51: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=1722732717; cv=none; b=g/Uwgnfh+Mfz2Z5Rn/uw5RO59sTrys9ASXfz97G5tDFAEkvInPFkJyNtwSGs6qtfOTJfwm0bqDgO/TP9qiVlKTfsUxPoIiZ/pPhg9GvMtM+U0A2G7Vzv/ukEWdru4j8ZCx6rC5Ie8tIqZdY6gOS+ISz7WYlUd/6qVAc9RoPibfs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732717; c=relaxed/simple; bh=vrJSfk1CYTt6iDqiThb69rXLu0bjlZT4StPtuxtDFxY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FntIebZbzsku4YjKU5gCztFiCTpgnaF7zUTSoPTglbvVkFIsCcEizY0cncTSe8Ln5fY+aUZppTrfsRN/EVlEUoJuUBxnBf8n7Bz5LdtnnxhuEKezEBDkfJvBPzTYl9Qa4860ftOGS+BlyjHMMTmjeKns25Dn8r4BffYjAvraCxw= 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=frccwcDl; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BkVqTG0j; 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="frccwcDl"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BkVqTG0j" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732709; 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=pZj5ZG2mfeXAb3IpB20OPoFPVSSec5uKMpkcM9Et3fQ=; b=frccwcDl+ggZ5RjDmd/m0qiLCc+g7itHO5m63UMLV1Bp/letMoRX12aTULOafS+n7Au9IE xG0BUBcVfAouGSZfxroOl0FBvBDLiBvKtPNCSuwcw2K3MJ+1qKB+fXa8GDPXYGXV6UF97w qFrZx0PdNslXhHqKzmtymdFryKRUp65xTYL1c8HR5iiOr5RdgpklrKeTZSc5tK1p4Ii4Ix AEjX2AYYUGBh0R7DtF9+FCYQY2+vqzeJtFKuAKCRatL5PUmo24JsKku4a04EQd9VZGVmc0 niNcXHgBg+H0YqkcS2M+2EeGizHzrvA0vA4/zS0VOqhtBfFOFtw3pmK6O5iKIQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732709; 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=pZj5ZG2mfeXAb3IpB20OPoFPVSSec5uKMpkcM9Et3fQ=; b=BkVqTG0jntkTD1uELEO6oyf0TSg6vBBw9zXs1CcSM+AztoXUKTNaEtTMeMsSJzE+j3UATZ ycNlNs/4V4AY2QBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 23/35] printk: Add is_printk_legacy_deferred() Date: Sun, 4 Aug 2024 02:57:26 +0206 Message-Id: <20240804005138.3722656-24-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 printk has been explicitly deferred or is called from NMI context, legacy console printing must be deferred to an irq_work context. Introduce a helper function is_printk_legacy_deferred() for a CPU to query if it must defer legacy console printing. In follow-up commits this helper will be needed at other call sites as well. Signed-off-by: John Ogness --- kernel/printk/internal.h | 2 ++ kernel/printk/printk_safe.c | 11 ++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 44468f3828f3..84706c1c934b 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -75,6 +75,7 @@ bool printk_percpu_data_ready(void); } while (0) =20 void defer_console_output(void); +bool is_printk_legacy_deferred(void); =20 u16 printk_parse_prefix(const char *text, int *level, enum printk_info_flags *flags); @@ -138,6 +139,7 @@ static inline bool console_is_usable(struct console *co= n, short flags) #define printk_safe_exit_irqrestore(flags) local_irq_restore(flags) =20 static inline bool printk_percpu_data_ready(void) { return false; } +static inline bool is_printk_legacy_deferred(void) { return false; } static inline u64 nbcon_seq_read(struct console *con) { return 0; } static inline void nbcon_seq_force(struct console *con, u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c index 4421ccac3113..86439fd20aab 100644 --- a/kernel/printk/printk_safe.c +++ b/kernel/printk/printk_safe.c @@ -38,6 +38,15 @@ void __printk_deferred_exit(void) __printk_safe_exit(); } =20 +bool is_printk_legacy_deferred(void) +{ + /* + * The per-CPU variable @printk_context can be read safely in any + * context. CPU migration is always disabled when set. + */ + return (this_cpu_read(printk_context) || in_nmi()); +} + asmlinkage int vprintk(const char *fmt, va_list args) { #ifdef CONFIG_KGDB_KDB @@ -50,7 +59,7 @@ asmlinkage int vprintk(const char *fmt, va_list args) * Use the main logbuf even in NMI. But avoid calling console * drivers that might have their own locks. */ - if (this_cpu_read(printk_context) || in_nmi()) + if (is_printk_legacy_deferred()) return vprintk_deferred(fmt, args); =20 /* No obstacles. */ --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 A1C331CD06 for ; Sun, 4 Aug 2024 00:51: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=1722732716; cv=none; b=AGHfS8Cz89NbFSLeFRFugv9gzx4T9tW98yBQ1RhM/7gge9Imad9CXadNOzMrZ2IvbWllIGmz/a9xowejWSOCcj6T6Wa8J9EZ5rdjyHiDC8SVdREnJybdhmS+ebnwM38W6chehR42kRS7+svR6lUsER8Iii+eHRPaMkQ3JmNuqn0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732716; c=relaxed/simple; bh=0jLUqIk/Bq5xbkFT5qKsbfWH4j7MIps3/9Afb6fdWJw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JfQ6f9Xw0pVK5wvlCDNul+Sf1bNsZbRMiW5MoeAqLK/emLOpqjbiWPq7lduJVS0oskjvQGbTGLIe4Al0WjZq0uRCrdMUZKOF+VbtbyKqCjZ33yS5XIaVKSvZsOLZB+murLw3pa6kPib+mUYtLT+WKnlbV6bEyNcDUB0ZaGPE4iI= 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=S8lfFgI+; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=w1NxKPFO; 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="S8lfFgI+"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="w1NxKPFO" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732709; 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=ITff3wOV77lXfeACa1fOZ40dYM9C+j+YBR4nrQ8pEvg=; b=S8lfFgI+7fm5OOjj04s745O2EA85crCjDGX+NoxzA7kFlRJ9QJqxFMK+nv0F0zM7MxNvX+ R35f5URSodZVZDLSDKWEdhd2PEGb5Q9lIxv/t6OkLXjl6dDH09qw1u7Jc8bZGebUnyykKc N35E7vuteJAzr+KbpzJyM+bvW5PNmeQz12SPekdAHwbBpOCKcN9dLqyGQ/eLasfAWV81eh khc7ofXlsOHfxklaWtaZtxau92IMWCq8GCP3pac+a4AoiJwBLpH089cKFmZKMNMTCJIh/m 3RyQydC+UjFI/kvnUiHMMTNNFxeAB281V736Lv2tI7I1q/ZebYdVGnMplxNikg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732709; 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=ITff3wOV77lXfeACa1fOZ40dYM9C+j+YBR4nrQ8pEvg=; b=w1NxKPFOVhUev4Zt5HFXP/LJIiotO2BCUGN8ZdD15raw2hMmKat1qtQFcfNrqPaI3M4afE I3fCiU57YY34AeAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 24/35] printk: nbcon: Flush new records on device_release() Date: Sun, 4 Aug 2024 02:57:27 +0206 Message-Id: <20240804005138.3722656-25-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" There may be new records that were added while a driver was holding the nbcon context for non-printing purposes. These new records must be flushed by the nbcon_device_release() context because no other context will do it. If boot consoles are registered, the legacy loop is used (either direct or per irq_work) to handle the flushing. Signed-off-by: John Ogness --- kernel/printk/internal.h | 2 ++ kernel/printk/nbcon.c | 20 ++++++++++++++++++++ kernel/printk/printk.c | 2 +- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 84706c1c934b..7679e18f24b3 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -153,6 +153,8 @@ static inline bool console_is_usable(struct console *co= n, short flags) { return =20 #endif /* CONFIG_PRINTK */ =20 +extern bool have_boot_console; + extern struct printk_buffers printk_shared_pbufs; =20 /** diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index d09c084c9af4..269aeed18064 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1326,10 +1326,30 @@ EXPORT_SYMBOL_GPL(nbcon_device_try_acquire); void nbcon_device_release(struct console *con) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_device_ctxt); + int cookie; =20 if (!nbcon_context_exit_unsafe(ctxt)) return; =20 nbcon_context_release(ctxt); + + /* + * This context must flush any new records added while the console + * was locked. The console_srcu_read_lock must be taken to ensure + * the console is usable throughout flushing. + */ + cookie =3D console_srcu_read_lock(); + if (console_is_usable(con, console_srcu_read_flags(con)) && + prb_read_valid(prb, nbcon_seq_read(con), NULL)) { + if (!have_boot_console) { + __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb)); + } else if (!is_printk_legacy_deferred()) { + if (console_trylock()) + console_unlock(); + } else { + printk_trigger_flush(); + } + } + console_srcu_read_unlock(cookie); } EXPORT_SYMBOL_GPL(nbcon_device_release); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 5f5fc5df2f09..d54eae0d06e9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -469,7 +469,7 @@ static DEFINE_MUTEX(syslog_lock); * the console lock. This is because boot consoles and nbcon consoles may * have mapped the same hardware. */ -static bool have_boot_console; +bool have_boot_console; =20 #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 B77EC1CD2B for ; Sun, 4 Aug 2024 00:51: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=1722732717; cv=none; b=Q+jax3Z/6jwaq50sTIJKjCLF2MtfOD1RaXLi/6ovfugD9P0Fsmo3RqaUY4YaEjF0z+UdcdNV7szdLa1WG5sJJnkY3ft2OxoeeB7JvUC+/7Y+bumZF3QTCSDeSE4XT2/X5aiUfqCqbXL0lJEscy4CA8gzlT3zBRLwALPRz2OSzoY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732717; c=relaxed/simple; bh=RDZ3qF+j30+orNYUh1q6eIsAysiEihmR66fcNeVaLGA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=trQOGU4XjLwDvDDWSyenxRcuZjB2nctrPev0DyRVKG/ZVL7ldGsEpMyDsUeBajdm3jWTCsv7zRUe5EDFiLSHyOOSwggK/p8bbTHsMQEGncy1PG7CVXf8meI4saN+o8FqKF+lFhox0EZlP4y1oRd0tefh9po3W+Op6FdMcxMtCKc= 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=Jl6sJAoa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=O2Ijat/O; 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="Jl6sJAoa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="O2Ijat/O" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732710; 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=XfipR2k1jdF4e+b1cRrN/G4ZDnI3s/6veQOnOWMQKzA=; b=Jl6sJAoagAt+bYZi8VSg8hTFPADbxXBX1EFpAt3SKVrSLuBdbfLVN83MLI1CiOMdCavwIm dcX3R2omxf16d9vK+xk7yYcOUH5oHHhrGbg4loVPdTAdqmiVeoMtOQUToGgybv+HOBcpSD 2e40G79SqFuYlQFlA16R7AafGpdN6FbM28QbaPG4JKwPkik7bZD7VCnuKom0HzlW7Lue0g /3Nak7eBLZbdLJJpxjnJ55rwyrybJOX1KqjPieysyvyubqGd1nsMbjZJR6xXGsVp3VvJJ5 XXda+wTTnFaiAZ1xmbKMPkFK4RXTqrHZx5Wecls/sU7lCmiSQHi/Y7aEiNJGSA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732710; 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=XfipR2k1jdF4e+b1cRrN/G4ZDnI3s/6veQOnOWMQKzA=; b=O2Ijat/O+6FeiJkWNMuu2AODPgWjAv3Ba/ZyQGmb/FLaTxxsiQPKZtU4qzUxfMr3EkTx6E A7Jx3uYuCBlwmhBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 25/35] printk: Flush nbcon consoles first on panic Date: Sun, 4 Aug 2024 02:57:28 +0206 Message-Id: <20240804005138.3722656-26-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" In console_flush_on_panic(), flush the nbcon consoles before flushing legacy consoles. The legacy write() callbacks are not fully safe when oops_in_progress is set. Signed-off-by: John Ogness --- kernel/printk/printk.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d54eae0d06e9..f61ecb84c3f6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3269,6 +3269,9 @@ void console_flush_on_panic(enum con_flush_mode mode) if (mode =3D=3D CONSOLE_REPLAY_ALL) __console_rewind_all(); =20 + if (!have_boot_console) + nbcon_atomic_flush_pending(); + console_flush_all(false, &next_seq, &handover); } =20 --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 B735C1CD2A for ; Sun, 4 Aug 2024 00:51: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=1722732717; cv=none; b=d2vwW2L1qVuSobye1ZW/AkN2P644qWWNVBCzNkB4EZ41iqkblStdQ6UA/evxEYe8QU6xgVaUr/BUwO41To4ky5jcsx1NImYZZADaBDuCmmw2Hzj0upBpW2HL0YPNvXD2mdX+t3hTsESZrItZUZuDCtifA3OkSTYRpnnlngqRWaM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732717; c=relaxed/simple; bh=TkTaBS20hEtEiHZ/KnwFlfxd3ElWVcxfaUHrEFUpMig=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JRmF6jER9l62gooLypr9UPXUSs37gSITQayr4gXGA5deUvWOQAaclDw33KcSNpWevzYUA+EKy8oMbIOF2S0zxUSiJ+sd/kih/f0zHm6gppPhg7h9Z2Wu8a8swZOvWG5iP0wZSWTZcejKH8gWRhcKrzGEybtm0H9XlrZAu6ms5NE= 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=CkwEkpPQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HKq846DA; 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="CkwEkpPQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HKq846DA" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732710; 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=5AL01Y9cfZ/udpFpA3TrLaCjrtlq2xqufDMV7WSRCj4=; b=CkwEkpPQKOoieQ1tPCuysetbRd7WTW8HQXATQ8apQ8UOMds7Xalzdrq0576+DF0Nc5sknz saM4kFY/IkQG2J/E67cUMmYv5l2M1hsvwP1nBXwEgu3tfL6yQogtjBbPN4HZQh1VQoR6Xl 3JO998UfazlYTEdxmXctBGV+WZBPhA1QCstQHa9Hbx8t2QjmIRw2ARvRAarh16eweGbqVU R9Fxjih5nHkro5tbxaL7S1lSMedxmn+Z2NJIjfaZ/w0nRSEtZ7TCKDfCoX172HlP4+/GIW LPjg9d0Yq0i2EgPDAdSLZUkaLhXZYeQmWdLz6stIQCm9l3VQJZxNChhYFvN4gg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732710; 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=5AL01Y9cfZ/udpFpA3TrLaCjrtlq2xqufDMV7WSRCj4=; b=HKq846DAGZwmhG4S3QsJqZeHbEQQYtgH+dmMFTnlaORYw6nRjfUVEubiN49xtsSNi53c2i 5Zzdqoo/XZLN7MDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Greg Kroah-Hartman , Jani Nikula , Kefeng Wang , Joel Granados , Uros Bizjak Subject: [PATCH printk v7 26/35] printk: nbcon: Add unsafe flushing on panic Date: Sun, 4 Aug 2024 02:57:29 +0206 Message-Id: <20240804005138.3722656-27-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 | 32 +++++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 9687089f5ace..2e083f01f8a3 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -202,6 +202,7 @@ void printk_trigger_flush(void); void console_try_replay_all(void); extern bool nbcon_device_try_acquire(struct console *con); extern void nbcon_device_release(struct console *con); +void nbcon_atomic_flush_unsafe(void); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -294,6 +295,10 @@ static inline void nbcon_device_release(struct console= *con) { } =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 f861bedc1925..a58631f42e79 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -457,6 +457,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 269aeed18064..afdb16c1c733 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1083,6 +1083,7 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on * failure. @@ -1101,7 +1102,8 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * returned, it cannot be expected that the unfinalized record will become * available. */ -static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_= seq) +static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_= seq, + bool allow_unsafe_takeover) { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); @@ -1110,6 +1112,7 @@ static int __nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq) ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; ctxt->prio =3D nbcon_get_default_prio(); + ctxt->allow_unsafe_takeover =3D allow_unsafe_takeover; =20 if (!nbcon_context_try_acquire(ctxt)) return -EPERM; @@ -1140,13 +1143,15 @@ static int __nbcon_atomic_flush_pending_con(struct = console *con, u64 stop_seq) * write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * This will stop flushing before @stop_seq if another context has ownersh= ip. * That context is then responsible for the flushing. Likewise, if new rec= ords * are added while this context was flushing and there is no other context * to handle the printing, this context must also flush those records. */ -static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq) +static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq, + bool allow_unsafe_takeover) { unsigned long flags; int err; @@ -1160,7 +1165,7 @@ static void nbcon_atomic_flush_pending_con(struct con= sole *con, u64 stop_seq) */ local_irq_save(flags); =20 - err =3D __nbcon_atomic_flush_pending_con(con, stop_seq); + err =3D __nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_take= over); =20 local_irq_restore(flags); =20 @@ -1190,8 +1195,9 @@ static void nbcon_atomic_flush_pending_con(struct con= sole *con, u64 stop_seq) * __nbcon_atomic_flush_pending - 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_pending(u64 stop_seq) +static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_t= akeover) { struct console *con; int cookie; @@ -1209,7 +1215,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq) if (nbcon_seq_read(con) >=3D stop_seq) continue; =20 - nbcon_atomic_flush_pending_con(con, stop_seq); + nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover); } console_srcu_read_unlock(cookie); } @@ -1225,7 +1231,19 @@ static void __nbcon_atomic_flush_pending(u64 stop_se= q) */ void nbcon_atomic_flush_pending(void) { - __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb)); + __nbcon_atomic_flush_pending(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_pending(prb_next_reserve_seq(prb), true); } =20 /** @@ -1342,7 +1360,7 @@ void nbcon_device_release(struct console *con) if (console_is_usable(con, console_srcu_read_flags(con)) && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { if (!have_boot_console) { - __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb)); + __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false); } else if (!is_printk_legacy_deferred()) { if (console_trylock()) console_unlock(); --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 DF422219F3 for ; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=gkWAnB7Jp3Y2Emc9t1/BlVyWwXCv9XB2QpLRR0pRdvSCWaeacbkcEkRaZkoaUTSbl1NKzZkTBBcyyj6q/Rl1hCBPmHoCEMqXFrZCLe8vxt8om+HcKMbSDK5yky2zx4R/4fS7qeXeirKgTJIObx16OtEZIOHREjHqrL+PS//raLY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=fHRlbO82M1ecm4QKgz9Fg4ODXedTXW3RX8ny9eOnknA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=UNugYcsT/kUtcXKVc/05TuCVbolmlqA8RDHal/GbZO/kl14nPg3zWzUOtEa9DhyPOGinzhJ/RFfL+ZUpsoLnOYvIBXNs+qLZ1xZTCBQHe4mRY2lshM4WhdPL+ebKDqRR1l6Qn+kL8W1N/YjejqTrcht8LsVnOstii35K0yDoI8A= 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=CTyCE+HI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=qfoZd1Th; 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="CTyCE+HI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qfoZd1Th" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732710; 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=qzRalWX9fgWLr4aSPnKrAfljE47gc8TiN9BZAtLeu0o=; b=CTyCE+HIxBWala6WkQUkSaazAjaYD//RbMJIubYWed2CtWH6ZscVLxafVIFKJmJAAjz4f5 eYufsKM2EicrM9IhkRBfIXmyX1LZLZJ2VXMlG7YvLlrItUNN8xk5duqe4clxbtReeeYusX gY1Hbtw1p/pAwMnCSYkiUv4L41pnK6NXwHVAg0sG3FhMt7yUSAfZvLyrQFIuKZn8iyD+kN 8S0R6RvoNM3qtsnFPFqoB+NgUwPjNHXtNnMMOd+zEyU+kxF8mX3AGMqQoEoNWGE7/V0KQ5 xZiS+2jT6MklQMnVU/O2nqVTYjHX8kMRdJOtV+LLWljIIJUWNMnAKaYXn3CRhw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732710; 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=qzRalWX9fgWLr4aSPnKrAfljE47gc8TiN9BZAtLeu0o=; b=qfoZd1ThjeYVN7tpBPLAOZVR5RdOJubhE7WqJyGYVXDkOE5PexmijH2Wy2rei5a2hZo/9D C+rRvzcfCvZ7XbCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 27/35] printk: Avoid console_lock dance if no legacy or boot consoles Date: Sun, 4 Aug 2024 02:57:30 +0206 Message-Id: <20240804005138.3722656-28-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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/printk.c | 39 ++++++++++++++++++++++++++++++++++----- 1 file changed, 34 insertions(+), 5 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index f61ecb84c3f6..a172c545e8dd 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/unlock dance + * whenever console flushing should occur. + */ +static 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 @@ -471,6 +478,14 @@ static DEFINE_MUTEX(syslog_lock); */ bool have_boot_console; =20 +/* + * 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) + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -2339,7 +2354,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 @@ -2359,7 +2374,7 @@ asmlinkage int vprintk_emit(int facility, int level, preempt_enable(); } =20 - if (in_sched) + if (in_sched && printing_via_unlock) defer_console_output(); else wake_up_klogd(); @@ -2718,7 +2733,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(); @@ -3625,6 +3640,7 @@ void register_console(struct console *newcon) if (newcon->flags & CON_NBCON) { nbcon_seq_force(newcon, init_seq); } else { + have_legacy_console =3D true; newcon->seq =3D init_seq; } =20 @@ -3701,6 +3717,7 @@ EXPORT_SYMBOL(register_console); static int unregister_console_locked(struct console *console) { bool use_device_lock =3D (console->flags & CON_NBCON) && console->write_a= tomic; + bool found_legacy_con =3D false; bool found_boot_con =3D false; unsigned long flags; struct console *c; @@ -3768,9 +3785,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 found_boot_con; + if (!found_legacy_con) + have_legacy_console =3D found_legacy_con; =20 return res; } @@ -3931,8 +3952,10 @@ static bool __pr_flush(struct console *con, int time= out_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; @@ -3945,6 +3968,12 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre * console->seq. Releasing console_lock flushes more * records in case @seq is still not printed on all * usable consoles. + * + * Holding the console_lock is not necessary if there + * are no legacy or boot consoles. However, such a + * console could register at any time. Always hold the + * console_lock as a precaution rather than + * synchronizing against register_console(). */ console_lock(); =20 --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 82950208AD for ; Sun, 4 Aug 2024 00:51: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=1722732717; cv=none; b=SZiBZPwqysLojJpygnq4iAiDlkQcztwwnaD3ZFq4ohBuobxK1DQB6X7VzzPFESlL3d2OiSf/xH411CKbDhPpBBwJ/7QcJz7bHHrf1N3EHEv5LL4Kzh3nP5p7el33bFlaU38FZz2vkBZEF76WAT16wjSb6TlGbZMekthGsY8lrtM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732717; c=relaxed/simple; bh=/2k5Mbe/jx6LPirTRLARhhYIxJ772ebHt4mVYxVCiNs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Xq5sUD7Pz8KKTd/95OiWPEgZ+2b0jW68d7xuapSK7N66SVsvrwkOUHnfNxIdL4Sw83mVPqLChm1PbsROo9sZ5/xJPwHXCZq9LPGDwwDP5c7NCrm1UpBf98qhcHM3rcNfUQgtSreJYNBHlyteTSw1hVGgmGnQUs2R8YShUqRKT+8= 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=U4KoX+7A; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wizBu2AU; 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="U4KoX+7A"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wizBu2AU" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732711; 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=eWjvOXkPQNS41YYvNIgIKfsF4CC3vfaJOPzid1/Jwk0=; b=U4KoX+7ArHiL98yZdLI2t7wAOrKsYMaD7d8/HgS1xl5JAFKJA1qompgxDNBB+GtiSK94vA EBGSLSUVKPpkiW0W88amvAtedhTGSGNktsbBHl/3IiHh0Zt+UVSgqCFGd/ekjo7ONW+NBQ 5MPLPNcDXcufgR3U/idjk9LBZJ+MBJl+JgYS9rQyI/VRuypExmn6mIcmYlL0TNa/nHwrTr otskX/8d/oBwvs79dvxqSzs91Eq6o82pndxw+g0cCFORbbHbvll0AHda48rXDoiwyJ/wzq zRAOn5QbScI96ov5WG1Be0WcRkuIHbGLIVzqUM1qTmt4jaPqE4zfKXonnEaQZg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732711; 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=eWjvOXkPQNS41YYvNIgIKfsF4CC3vfaJOPzid1/Jwk0=; b=wizBu2AUYO8jnSUKBhQZowc5saL/7/opBTubvu1FiuVng1A4a89fTvLamUPMJj63KQSSrQ GGopMDGFP03xL7Dg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 28/35] printk: Track nbcon consoles Date: Sun, 4 Aug 2024 02:57:31 +0206 Message-Id: <20240804005138.3722656-29-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index a172c545e8dd..2277871e1f12 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -470,6 +470,11 @@ static DEFINE_MUTEX(syslog_lock); */ static bool have_legacy_console; =20 +/* + * Specifies if an nbcon console is registered. + */ +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 @@ -3638,6 +3643,7 @@ void register_console(struct console *newcon) init_seq =3D get_init_console_seq(newcon, bootcon_registered); =20 if (newcon->flags & CON_NBCON) { + have_nbcon_console =3D true; nbcon_seq_force(newcon, init_seq); } else { have_legacy_console =3D true; @@ -3718,6 +3724,7 @@ static int unregister_console_locked(struct console *= console) { bool use_device_lock =3D (console->flags & CON_NBCON) && console->write_a= tomic; bool found_legacy_con =3D false; + bool found_nbcon_con =3D false; bool found_boot_con =3D false; unsigned long flags; struct console *c; @@ -3785,13 +3792,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 found_boot_con; if (!found_legacy_con) have_legacy_console =3D found_legacy_con; + if (!found_nbcon_con) + have_nbcon_console =3D found_nbcon_con; =20 return res; } --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 7F81C2033A for ; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=CBy+KcQF4NNkjCv/DjMm6xWZlTcb0XzBSEV8RoyL3glcCXlVogtZMFDBWHXxRxKuCR4secYpMiDaNQdna25pDg4J6hmwIKPZuguo1xyaIOqZOJLEi80tmTecvY9hfWD6GpcMADArYPJJ/kG3i+6i34kVbhrqtG2Zs1Eft/WJ1gk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=DXTUyKqM2Z24jCLnk0r9d/ubSBFi8VCoDdlVY6xX0nY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HhjyYTDdAXbq7VfAgfkRPx6UG6/H4OuoWwalApiCsa+0VwKA2GFuDZkNJQXTgJ72o4qgGPgF7AUzGKmRyRHWUtHQxUAF5nGKgLJpkqMQ4Ezkrxjpim4K0qJhpCqQkoxcqbSmA63/lno7pG5UC9Lx91zclcpqP3oOkk3rCkUmFHs= 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=E6Rakp/j; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rdOjJbWQ; 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="E6Rakp/j"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rdOjJbWQ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732711; 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=dV9290EWLNTQgmOyk6oxj7wIlktHocd6AbHZCImcmIo=; b=E6Rakp/jXErurOvwI7AUJOtANSPoIIF/Jw1dRFL8pmGuwSfo9+a2mWsiajujfnB6m77Jth y6bcDKQExnp8N0XgBRQKeyVYE7kRiANlddnzpaWq/W5cNK09yo9Dt1oc0AURKhW5Be3W0X zF4Sm8u91eXXXul8f8pqqM/5mNc5nXxin4P3xWhzoEIjF5yFhcivH6F46KTn7XkxZ87JpZ CybXPxEnLmu/IgMc1N6Eanm3FSksPPlRswLOqnJu/9HMTcB9oHIDs8Jc8SBL4pUCUspqyA KDOKW6K9ija6IgBhjzMgoiUAeNTOPv9pt/mICXFSQAXgwAmitBYxPeQ27d0Sxg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732711; 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=dV9290EWLNTQgmOyk6oxj7wIlktHocd6AbHZCImcmIo=; b=rdOjJbWQfKoSOxS3ILWKFaT3D/wPSXCnn+o0LgMHBMcqS9Hs4UyhwqGy/IyW+S4hoZh2JV QXzRxClD+bHnVDDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Greg Kroah-Hartman , Jani Nikula , Uros Bizjak , Joel Granados Subject: [PATCH printk v7 29/35] printk: Coordinate direct printing in panic Date: Sun, 4 Aug 2024 02:57:32 +0206 Message-Id: <20240804005138.3722656-30-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 legacy and nbcon consoles are registered and the nbcon consoles are allowed to flush (i.e. no boot consoles 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 or they are not allowed to flush, 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/internal.h | 2 ++ kernel/printk/printk.c | 54 ++++++++++++++++++++++++++++++++++------ 4 files changed, 56 insertions(+), 7 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 2e083f01f8a3..eca9bb2ee637 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -200,6 +200,7 @@ extern asmlinkage void dump_stack_lvl(const char *log_l= vl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); void console_try_replay_all(void); +void printk_legacy_allow_panic_sync(void); extern bool nbcon_device_try_acquire(struct console *con); extern void nbcon_device_release(struct console *con); void nbcon_atomic_flush_unsafe(void); @@ -286,6 +287,10 @@ static inline void console_try_replay_all(void) { } =20 +static inline void printk_legacy_allow_panic_sync(void) +{ +} + static inline bool nbcon_device_try_acquire(struct console *con) { return false; diff --git a/kernel/panic.c b/kernel/panic.c index a58631f42e79..84488daa14ce 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -368,6 +368,8 @@ void panic(const char *fmt, ...) =20 panic_other_cpus_shutdown(_crash_kexec_post_notifiers); =20 + printk_legacy_allow_panic_sync(); + /* * Run any panic handlers, including those that might need to * add information to the kmsg dump output. diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 7679e18f24b3..dbda90f0dc08 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -130,6 +130,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 legacy_allow_panic_sync 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 2277871e1f12..480c0993abd5 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -471,7 +471,9 @@ static DEFINE_MUTEX(syslog_lock); static bool have_legacy_console; =20 /* - * Specifies if an nbcon console is registered. + * 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; =20 @@ -2330,12 +2332,30 @@ 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. It also attempts to flush + * the legacy consoles in this context. + */ +void printk_legacy_allow_panic_sync(void) +{ + legacy_allow_panic_sync =3D true; + + if (printing_via_unlock && !is_printk_legacy_deferred()) { + if (console_trylock()) + console_unlock(); + } +} + 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; + bool defer_legacy =3D !do_trylock_unlock; int printed_len; - bool in_sched =3D false; =20 /* Suppress unimportant messages after panic happens */ if (unlikely(suppress_printk)) @@ -2349,17 +2369,35 @@ asmlinkage int vprintk_emit(int facility, int level, if (other_cpu_in_panic()) return 0; =20 + /* If called from the scheduler, we can not call up(). */ if (level =3D=3D LOGLEVEL_SCHED) { level =3D LOGLEVEL_DEFAULT; - in_sched =3D true; + do_trylock_unlock =3D false; + defer_legacy =3D true; } =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_nbcon_console && !have_boot_console) { + nbcon_atomic_flush_pending(); + + /* + * 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 and they are allowed to + * flush. + */ + if (this_cpu_in_panic() && !legacy_allow_panic_sync) { + do_trylock_unlock =3D false; + defer_legacy =3D false; + } + } + + if (do_trylock_unlock) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2379,7 +2417,7 @@ asmlinkage int vprintk_emit(int facility, int level, preempt_enable(); } =20 - if (in_sched && printing_via_unlock) + if (defer_legacy) defer_console_output(); else wake_up_klogd(); @@ -3292,7 +3330,9 @@ void console_flush_on_panic(enum con_flush_mode mode) if (!have_boot_console) nbcon_atomic_flush_pending(); =20 - console_flush_all(false, &next_seq, &handover); + /* Flush legacy consoles once allowed, even when dangerous. */ + if (legacy_allow_panic_sync) + console_flush_all(false, &next_seq, &handover); } =20 /* --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 E06A422081 for ; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=Ex1tcy/c5Gz/lRed4slkrL3Ps85eODZwbYgfrSbWjN9vHh1XP9KUgswKBIwgHPJ1XLJDazLGgwBi6Qkt/5CP9aEj0sW9seiIgCroj48W50m7OekaqK8nkBA5edrqiAEpkX3z7/Hw34aoN8ZuCs8NYBotNq5QVNnz3adIXcrDCO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=02uBS3SK0uI6i7egrbWBVa0YWyj8nlyA26FqupjG2Tk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lst/m4J7fi166hiLt7WGW/ujAUBljV6dQLs5r8Ji8MAMCTRFJscK6pawwebQlin3fRGlHcnPg2gJwP2kmG2kOqkZf+TfhXqcByNfDleojdbRs985NF65U6T56vPuBPaFvVJP/dzkWV5i7CS0seBBj/ReTNNYjc0qgaF3+t3rn5M= 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=c8AXeB0J; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=jzWkoBzJ; 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="c8AXeB0J"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="jzWkoBzJ" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732711; 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=tSVN0gPPr6tHTy/VzHokuF6g1s3xpImF0LVj64Z+MkU=; b=c8AXeB0JzQbSvM7FxA2ZIWmYXSCJlid4AfTvR0RN/UJh5xSa16Q0nJNBjSqgqvLSH0WHEA B7AyZwPSyIZFOJwnnkSc8jcXieWQejiJw0fxERdUZhDpUgMoDn7vzFnI76a7RoJf304Uy3 Ne2xdhAaoFE6dbWmcLIV8N95PAHLxz+5eK56M7iIkxf/kfbdgRs+ZGPG/MYEQ8TRFBFGM9 cucR8Kgw+2s8yv+AQgc3ILer88d8EGIzo+2B8qIkSHD9CFTmERmkyHo41BbZrdWU8vz9zF UF2l8pGODcALMsPiMbIcWQb1nPgAGip3dYQIE7pJxjIzJUGwSTrb2aEdVHc15g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732711; 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=tSVN0gPPr6tHTy/VzHokuF6g1s3xpImF0LVj64Z+MkU=; b=jzWkoBzJburWxIHcNXD9xFnnvJUUfge5mP18/7sHLOTvusDuXBzrrvp5bIKth1boGZ6G7y Kkf1/Sv4JwN307Cg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v7 30/35] printk: Add helper for flush type logic Date: Sun, 4 Aug 2024 02:57:33 +0206 Message-Id: <20240804005138.3722656-31-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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" There are many call sites where console flushing occur. Depending on the system state and types of consoles, the flush methods to use are different. A flush call site generally must consider: @have_boot_console @have_nbcon_console @have_legacy_console @legacy_allow_panic_sync is_printk_preferred() and take into account the current CPU state: NBCON_PRIO_NORMAL NBCON_PRIO_EMERGENCY NBCON_PRIO_PANIC in order to decide if it should: flush nbcon directly via atomic_write() callback flush legacy directly via console_unlock flush legacy via offload to irq_work All of these call sites use their own logic to make this decision, which is complicated and error prone. Especially later when two more flush methods will be introduced: flush nbcon via offload to kthread flush legacy via offload to kthread Introduce a new internal struct console_flush_type that specifies the flush method(s) that are available for a particular call site to use. Introduce a helper function to fill out console_flush_type to be used for flushing call sites. In many system states it is acceptable to flush legacy directly via console_unlock or via offload to irq_work. The caller must decide which of these methods it prefers. Replace the logic of all flushing call sites to use the new helper. Signed-off-by: John Ogness --- kernel/printk/internal.h | 65 +++++++++++++++++++++++++++++++++++++++ kernel/printk/nbcon.c | 25 ++++++++++++--- kernel/printk/printk.c | 66 ++++++++++++++++++---------------------- 3 files changed, 114 insertions(+), 42 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index dbda90f0dc08..58eb2a58b07b 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -48,6 +48,7 @@ struct printk_ringbuffer; struct dev_printk_info; =20 extern struct printk_ringbuffer *prb; +extern bool legacy_allow_panic_sync; =20 __printf(4, 0) int vprintk_store(int facility, int level, @@ -156,6 +157,70 @@ static inline bool console_is_usable(struct console *c= on, short flags) { return #endif /* CONFIG_PRINTK */ =20 extern bool have_boot_console; +extern bool have_nbcon_console; +extern bool have_legacy_console; + +/** + * struct console_flush_type - Define available console flush methods + * @nbcon_atomic: Flush directly using nbcon_atomic() callback + * @legacy_direct: Call the legacy loop in this context + * @legacy_offload: Offload the legacy loop into IRQ + * + * Note that the legacy loop also flushes the nbcon consoles. + */ +struct console_flush_type { + bool nbcon_atomic; + bool legacy_direct; + bool legacy_offload; +}; + +/* + * Identify which console flushing methods are available to the context of + * the caller. The caller can then decide which of the available flushing + * methods it will use. + */ +static inline void printk_get_console_flush_type(struct console_flush_type= *ft) +{ + memset(ft, 0, sizeof(*ft)); + + switch (nbcon_get_default_prio()) { + case NBCON_PRIO_NORMAL: + if (have_nbcon_console && !have_boot_console) + ft->nbcon_atomic =3D true; + + if (have_legacy_console || have_boot_console) { + ft->legacy_offload =3D true; + if (!is_printk_legacy_deferred()) + ft->legacy_direct =3D true; + } + break; + + case NBCON_PRIO_PANIC: + /* + * In panic, the nbcon consoles will directly print. But + * only allowed if there are no boot consoles. + */ + if (have_nbcon_console && !have_boot_console) + ft->nbcon_atomic =3D true; + + /* + * In panic, if nbcon atomic printing occurs, the legacy + * consoles must remain silent until explicitly allowed. + * Also, legacy consoles cannot print when deferred. However, + * console_flush_on_panic() will flush them anyway, even if + * unsafe. + */ + if ((legacy_allow_panic_sync || !ft->nbcon_atomic) && + !is_printk_legacy_deferred()) { + ft->legacy_direct =3D true; + } + break; + + default: + WARN_ON_ONCE(1); + break; + } +} =20 extern struct printk_buffers printk_shared_pbufs; =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index afdb16c1c733..9e13327b4fe3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1153,6 +1153,7 @@ static int __nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq, static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq, bool allow_unsafe_takeover) { + struct console_flush_type ft; unsigned long flags; int err; =20 @@ -1186,8 +1187,16 @@ static void nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq, * other context that will do it. */ if (prb_read_valid(prb, nbcon_seq_read(con), NULL)) { - stop_seq =3D prb_next_reserve_seq(prb); - goto again; + printk_get_console_flush_type(&ft); + /* + * If nbcon_atomic flushing is not available, this printing + * must be occurring via the legacy loop. Let that loop be + * responsible for flushing the new records. + */ + if (ft.nbcon_atomic) { + stop_seq =3D prb_next_reserve_seq(prb); + goto again; + } } } =20 @@ -1344,6 +1353,7 @@ EXPORT_SYMBOL_GPL(nbcon_device_try_acquire); void nbcon_device_release(struct console *con) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_device_ctxt); + struct console_flush_type ft; int cookie; =20 if (!nbcon_context_exit_unsafe(ctxt)) @@ -1359,12 +1369,17 @@ void nbcon_device_release(struct console *con) cookie =3D console_srcu_read_lock(); if (console_is_usable(con, console_srcu_read_flags(con)) && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { - if (!have_boot_console) { + /* + * If nbcon_atomic flushing is not available, fallback to + * using the legacy loop. + */ + printk_get_console_flush_type(&ft); + if (ft.nbcon_atomic) { __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false); - } else if (!is_printk_legacy_deferred()) { + } else if (ft.legacy_direct) { if (console_trylock()) console_unlock(); - } else { + } else if (ft.legacy_offload) { printk_trigger_flush(); } } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 480c0993abd5..6bed91b5a55b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -468,14 +468,14 @@ static DEFINE_MUTEX(syslog_lock); * present, it is necessary to perform the console lock/unlock dance * whenever console flushing should occur. */ -static bool have_legacy_console; +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; +bool have_nbcon_console; =20 /* * Specifies if a boot console is registered. If boot consoles are present, @@ -485,14 +485,6 @@ static bool have_nbcon_console; */ bool have_boot_console; =20 -/* - * 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) - #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -2332,7 +2324,7 @@ int vprintk_store(int facility, int level, return ret; } =20 -static bool legacy_allow_panic_sync; +bool legacy_allow_panic_sync; =20 /* * This acts as a one-way switch to allow legacy consoles to print from @@ -2341,9 +2333,12 @@ static bool legacy_allow_panic_sync; */ void printk_legacy_allow_panic_sync(void) { + struct console_flush_type ft; + legacy_allow_panic_sync =3D true; =20 - if (printing_via_unlock && !is_printk_legacy_deferred()) { + printk_get_console_flush_type(&ft); + if (ft.legacy_direct) { if (console_trylock()) console_unlock(); } @@ -2353,8 +2348,8 @@ 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; - bool defer_legacy =3D !do_trylock_unlock; + struct console_flush_type ft; + bool do_trylock_unlock; int printed_len; =20 /* Suppress unimportant messages after panic happens */ @@ -2369,34 +2364,23 @@ asmlinkage int vprintk_emit(int facility, int level, if (other_cpu_in_panic()) return 0; =20 + printk_get_console_flush_type(&ft); + /* If called from the scheduler, we can not call up(). */ if (level =3D=3D LOGLEVEL_SCHED) { level =3D LOGLEVEL_DEFAULT; do_trylock_unlock =3D false; - defer_legacy =3D true; + } else { + do_trylock_unlock =3D ft.legacy_direct; } =20 printk_delay(level); =20 printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 - if (have_nbcon_console && !have_boot_console) { + if (ft.nbcon_atomic) nbcon_atomic_flush_pending(); =20 - /* - * 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 and they are allowed to - * flush. - */ - if (this_cpu_in_panic() && !legacy_allow_panic_sync) { - do_trylock_unlock =3D false; - defer_legacy =3D false; - } - } - if (do_trylock_unlock) { /* * The caller may be holding system-critical or @@ -2417,7 +2401,7 @@ asmlinkage int vprintk_emit(int facility, int level, preempt_enable(); } =20 - if (defer_legacy) + if (!do_trylock_unlock && ft.legacy_offload) defer_console_output(); else wake_up_klogd(); @@ -2776,10 +2760,14 @@ void resume_console(void) */ static int console_cpu_notify(unsigned int cpu) { - if (!cpuhp_tasks_frozen && printing_via_unlock) { - /* If trylock fails, someone else is doing the printing */ - if (console_trylock()) - console_unlock(); + struct console_flush_type ft; + + if (!cpuhp_tasks_frozen) { + printk_get_console_flush_type(&ft); + if (ft.legacy_direct) { + if (console_trylock()) + console_unlock(); + } } return 0; } @@ -3304,6 +3292,7 @@ static void __console_rewind_all(void) */ void console_flush_on_panic(enum con_flush_mode mode) { + struct console_flush_type ft; bool handover; u64 next_seq; =20 @@ -3327,7 +3316,8 @@ void console_flush_on_panic(enum con_flush_mode mode) if (mode =3D=3D CONSOLE_REPLAY_ALL) __console_rewind_all(); =20 - if (!have_boot_console) + printk_get_console_flush_type(&ft); + if (ft.nbcon_atomic) nbcon_atomic_flush_pending(); =20 /* Flush legacy consoles once allowed, even when dangerous. */ @@ -3991,6 +3981,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre { unsigned long timeout_jiffies =3D msecs_to_jiffies(timeout_ms); unsigned long remaining_jiffies =3D timeout_jiffies; + struct console_flush_type ft; struct console *c; u64 last_diff =3D 0; u64 printk_seq; @@ -4004,7 +3995,8 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre seq =3D prb_next_reserve_seq(prb); =20 /* Flush the consoles so that records up to @seq are printed. */ - if (printing_via_unlock) { + printk_get_console_flush_type(&ft); + if (ft.legacy_direct) { console_lock(); console_unlock(); } --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 5AC6E22EED for ; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=llZRhN7wxknmckW8d2ZBfm12rimOZlLzDFPpZWQSt5zLMEpvf7CVSdzmUpv7dOla6PtpjVrSF9hKtujdBhpUVbhJpvR6TFr7LiokFNqMq2LaLJf+ZJfF5VXRC4Nvvhhzm3KkITyzln8bSQSQrhKexnW0iahU5fCCoy1CYKjN2eU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=wvurQxlYr2G6iuOCm/90ZTtbp/+JB66xiQ2YTstqyno=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=VT/1cCsfjIq1hS5pafkhW+vwd2dQejeMEfkz7gVqb6MmZ1Pf3v5+xv9iIFrqZaSSuHRyX4aRl/TJE5od9MWbQsNquoqYWCqibE8L5RcMoo8TBk7M4iFUKR+OT9GbGu7Cfg1ajz3dWix7vat2SVvd30raBhaeLCKwcIpaImu3ct0= 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=OR8aPoPB; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BKI5wOQa; 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="OR8aPoPB"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BKI5wOQa" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732712; 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=Hpuh35LGJrMZOt53Jr3PDFLYNJpazIKyCg0COHe0utM=; b=OR8aPoPBIpI78liagpuaEZWbE2OCCNWqDDNDOSab6nS6VJvzievGGZeN8ljlmxUlKnOQEO 6kua98JjvPCg8M/A6QmOTAccboNJ+br82LPLfQ0xybS1iNLweHJAYEQTIVX+4J8Nr/1Cbj 3f1ACS/Ey6e44898tAWu+YvnPf2ZOma+T0emObWirGyVK6/bvNDVHxiL9MMjresekxtxOG h43ZjO9hy0mnrY+q/BRGwWgBUCCmauZ2i9b+8i3a2j62K0olJr4w+Hat2sjYTmRaYIPSJj 4KIpNfCV9yBEWvE/bEQ8PkfcSpv8/bqTxLFqpvU9ALiiIdWoKcV/tjKXz3yUbw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732712; 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=Hpuh35LGJrMZOt53Jr3PDFLYNJpazIKyCg0COHe0utM=; b=BKI5wOQaq/JeMKvNagowkyJSHLsmBRvw4eOxOi52DNuHFrTzBt299zG9vkQG8KTmSXysJv 2vCeFH4eiyXRrUDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v7 31/35] printk: nbcon: Implement emergency sections Date: Sun, 4 Aug 2024 02:57:34 +0206 Message-Id: <20240804005138.3722656-32-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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(). This information should be pushed out to the consoles ASAP. 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. Perform direct console flushing at the emergency priority if the current CPU is in an emergency state and it is safe to do so. Note that the emergency state is not system-wide. While one CPU is in an emergency state, another CPU may attempt to print console messages at normal priority. Also note that printk() already attempts to flush consoles in the caller context for normal priority. However, follow-up changes will introduce printing kthreads, in which case the normal priority printk() calls will offload to the kthreads. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- include/linux/console.h | 4 +++ kernel/printk/internal.h | 1 + kernel/printk/nbcon.c | 75 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/include/linux/console.h b/include/linux/console.h index 3706f944de46..9a13f91b0c43 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -553,10 +553,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/internal.h b/kernel/printk/internal.h index 58eb2a58b07b..f51225a56f5f 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -185,6 +185,7 @@ static inline void printk_get_console_flush_type(struct= console_flush_type *ft) =20 switch (nbcon_get_default_prio()) { case NBCON_PRIO_NORMAL: + case NBCON_PRIO_EMERGENCY: if (have_nbcon_console && !have_boot_console) ft->nbcon_atomic =3D true; =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 9e13327b4fe3..a5551d8cbd50 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -972,6 +972,36 @@ 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 + * + * Context: For reading, any context. For writing, any context which could + * not be migrated to another CPU. + * Return: Either a pointer to the per CPU emergency nesting counter of + * the current CPU or to the init data during early boot. + * + * The function is safe for reading per-CPU variables in any context becau= se + * preemption is disabled if the current CPU is in the emergency state. See + * also nbcon_cpu_emergency_enter(). + */ +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; + + /* Open code this_cpu_ptr() without checking migration. */ + return per_cpu_ptr(&nbcon_pcpu_emergency_nesting, raw_smp_processor_id()); +} + /** * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon * printing on the current CPU @@ -981,13 +1011,20 @@ static bool nbcon_emit_next_record(struct nbcon_writ= e_context *wctxt) * context for printing. * * The function is safe for reading per-CPU data in any context because - * preemption is disabled if the current CPU is in the panic state. + * preemption is disabled if the current CPU is in the emergency or panic + * state. */ 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 @@ -1255,6 +1292,42 @@ void nbcon_atomic_flush_unsafe(void) __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), true); } =20 +/** + * nbcon_cpu_emergency_enter - Enter an emergency section where printk() + * messages for that CPU are flushed directly + * + * Context: Any context. Disables preemption. + * + * When within an emergency section, printk() calls will attempt to flush = any + * pending messages in the ringbuffer. + */ +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 + * + * Context: Within an emergency section. Enables preemption. + */ +void nbcon_cpu_emergency_exit(void) +{ + unsigned int *cpu_emergency_nesting; + + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + + if (!WARN_ON_ONCE(*cpu_emergency_nesting =3D=3D 0)) + (*cpu_emergency_nesting)--; + + preempt_enable(); +} + /** * nbcon_alloc - Allocate and init the nbcon console specific data * @con: Console to initialize --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 599311C20 for ; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=KOHuSuq8Qxfh0Ii37olNdfgvtBWA2AZ1yHvLpPO9rxgW3ShWKHbAtp3A7ebWjgGUoZh4mNQtcRHwmdnR2W5jQWviT8f6ybXhYXBCCdQ32mbsXFq0Tuet17O4s0C3TT/buV1KmQqzyALakEjA9ifRF1geoG5mFiV5GZBQ6MCh4bM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=I9k/HiJhGT4AzKbUgrNWAomwSzWWBar5d3qee4ryVBE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qczYaKiM+dmrzio7wqX3MJGkh/VzXscAdTCuDy7aKiIzh7odK2YO3rPOspHk43UaucHrx5/axlHamzW/gYEBB9TObGp6VGd6UfPKAcnEM9djyJuDvUQ9vPMOypLlFHkkKODSDhwix8P0Lbvjg1CyiwBFe4yiTXn8uqS1P47WnpU= 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=3EnH0wFw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0w9d3/wB; 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="3EnH0wFw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0w9d3/wB" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732712; 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=kdD8sHomyUtD7cE6/iBYkgE6+sjyvW1hdczecD36oTg=; b=3EnH0wFwdXjVWyBIftkK4XYAobwfRYg+4OecaGuHLV3YGVPGIXGf/AIW5CxJjo/xY3Dw5o Wmy9OC3fsK6gnXV0MEnEogSLmBVOEXonpW/TiEZVqv8eAd+NyIhCIHG1Y2T9R9Kh6lXDm3 cYYF/WVM4yJmFZ8RQV4q6V1xxODaiqcnq2AKVndVwPZv4XdXpSWzh/f4uV7k9uy+uY6HNG SWHdN7cZmqZDEjOxDx+gKs8Ho3DiQgdgWOroWYPQ3jeUXrqoU4t/ms6vzmwvv1tbea+iFa JLW00/T4TpxwV1lZRRTeMUtVcc+5xDg7UDB/70d/tci5BSllOKbV0HWAPmbilg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732712; 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=kdD8sHomyUtD7cE6/iBYkgE6+sjyvW1hdczecD36oTg=; b=0w9d3/wB/iHjcY5UNd/bMc9FL4wQx9gUYjQWLhRN7S23RiPC9Onfg/jH+mJOkgmtgIRjcP QsVQtho8Bc3pr8Bw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Jani Nikula , Greg Kroah-Hartman , Baoquan He , Lukas Wunner , Joel Granados , Uros Bizjak Subject: [PATCH printk v7 32/35] panic: Mark emergency section in warn Date: Sun, 4 Aug 2024 02:57:35 +0206 Message-Id: <20240804005138.3722656-33-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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, every printk() call will attempt to directly flush to the consoles using the EMERGENCY priority. 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 84488daa14ce..5fc44fd3b3dc 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -712,6 +712,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) @@ -747,6 +749,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:34:49 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 0DC0A28DBC for ; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=GcDpgSlYwLZm/sXe7JFO8EGAFLirxl1gOeCMDJu5jSguZ0495rGtnbHzKamytYMXCGElvE6eG72nUFc90Bd8Vadiu4oQTzzz+pJ9hQuX89COKJ0E/Gfggu/KYRaSwfsgBqJfisf7yttN0eULAq+YdJyhiKvptnGAk8fco0MMJWY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=rx8JQEOlPSXSGgJH/hN1890GlKdwGsxBTv9ERQw0ehM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CC5TPPFw1MgGdkVip72+tE/RugUFDV+Wtqu67IsouiKsTOV5Q4zbvH3XPTiCTZrGE1vLD5pJzTAYUU+ZhtgR2kSwyLVtns1dln8+Bhznzb9kkZZbLhbNlwPo/asku085dwAyzFbkGlij/vBPanBPJJBiUm6X6AGBmp4f76gR+JE= 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=A4uNr3mt; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Yk+sXboT; 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="A4uNr3mt"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Yk+sXboT" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732713; 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=dtyMYwpXcYpG7D03C8lxtJgU1iwrqHDYPSpb3n5f5II=; b=A4uNr3mtns0/fwdqdEI5WlnXILPV/noMt9yPjt5GUk0Bg/oVVlGTF1NJb21A00XooZy/iK CmwplsYrkoIwfBgujIQUsfH8cHhdJvprCoYS44zkK/kFYou9HeR6KTMdD1IUR5TglHijgS nZ2AzeJT0h0jmAV14fE53QP8wvoBM/gSyhLBwiW2DBfBCImBOn1ZuCzCqjl1VfGiEcFLu9 v/gUANqD+cF8stT5FFZxyNkuH7pQa3fltks1QuVRIPa7VS/Qrd4iHYzgWpQgHrpkdrSNea CW3thc28KWhIiW15Yq2ht3NID3x7FQctfLWIO8dcMU3GYelTvnRky/xltVcADw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732713; 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=dtyMYwpXcYpG7D03C8lxtJgU1iwrqHDYPSpb3n5f5II=; b=Yk+sXboTJ4zsgdoLYQgpkav19TY9wl/aprDYzF0yshY31cX7VQjpyYLtjmhvq9fMofbRMo NPs2gEdS51gwcCCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Greg Kroah-Hartman , Jani Nikula , Kefeng Wang , Uros Bizjak , Baoquan He Subject: [PATCH printk v7 33/35] panic: Mark emergency section in oops Date: Sun, 4 Aug 2024 02:57:36 +0206 Message-Id: <20240804005138.3722656-34-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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, every printk() call will attempt to directly flush to the consoles using the EMERGENCY priority. 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 flushed the oops messages. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/panic.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/panic.c b/kernel/panic.c index 5fc44fd3b3dc..0843a275531a 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -679,6 +679,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(); @@ -701,6 +702,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:34:49 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 C525926AFB; Sun, 4 Aug 2024 00:51: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=1722732718; cv=none; b=T3/60Y9VYCYCG8G1V9WQgkZW/fzNLiMIISMREQ7eB3XJxjpQRl59onurk2a+xCmXvfsFPs8ODZaQDoEkU85zhh5xZCLMFqcgwZzqUkkGt7HnsIi/CZPtPZE3s8vc8g4ahim40MmGDmqxVqmm20p41IkvmU0xMR5eH5jn/hHMAg4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732718; c=relaxed/simple; bh=/gP8ujgnDmo/PH09xoPdDL8CnHUSID9dOyO0b21i7JQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=iLVtvWvXeTOJBFf5wr/DeXWT6ykcduyUGjOBolQag+fruoIxdIYbbsJjykGsv8HZvbbbq6m1kjhE+NZV0KKpb3u8BFhYDX3Wt+6RNgkYFMpbnuNuaD4zIeLmKIJQG+kgQ0JUdlQxt6o/LwSPn18uQpIS2tSgIHxYuz6t3EaPJEc= 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=TIf7HXDI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=suroxdUF; 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="TIf7HXDI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="suroxdUF" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732713; 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=+J3uNsDrypTBncR2y3/sDGkKGKO6SJ0tlRYJyvsQeNQ=; b=TIf7HXDIY0ugmmNb4KJw92Zvct+hsyeRHu07SMfhx7m1OdVEvQwyeyOVAkEpzwHkzMJ/qL n8cLKA/joazxCvGboBUfsmgkCRv4EyDckVTDIZcUUORy6S81j/v+CUxY6IxGqz/oGlnRUo dOOsAZa6Dj2gOV/8nDZ0M5ESfBRZaq8BW+MjbcgNhY3pjMLCZFT8WUgNOdnt5JsalQXkps 6jAvlwsn17pHLNjrS3wPGrGTnY8ATDlz1d6NoP2whr9TeYe9kcwz9W6UkuWMXKntBrD7Dl km2fDLDrBwqcnq0/l5Q1/nefvClFwAfhV8Z1LTEEho/X6tcIQKFjKbrzHNZwWA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732713; 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=+J3uNsDrypTBncR2y3/sDGkKGKO6SJ0tlRYJyvsQeNQ=; b=suroxdUFbfVnC9Q5kzl6xAIA7Od+lvUWGLp/3RKKUwKdfAbRamDIA4OzjF+RMb7YDR1WWp hz75EGEgWXGWrIAw== 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 , Uladzislau Rezki , Mathieu Desnoyers , Lai Jiangshan , Zqiang , rcu@vger.kernel.org Subject: [PATCH printk v7 34/35] rcu: Mark emergency sections in rcu stalls Date: Sun, 4 Aug 2024 02:57:37 +0206 Message-Id: <20240804005138.3722656-35-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 emergency sections wherever multiple lines of rcu stall information are generated. In an emergency section, every printk() call will attempt to directly flush to the consoles using the EMERGENCY priority. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/rcu/tree_exp.h | 7 +++++++ kernel/rcu/tree_stall.h | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/kernel/rcu/tree_exp.h b/kernel/rcu/tree_exp.h index 4acd29d16fdb..f6b35a0585a8 100644 --- a/kernel/rcu/tree_exp.h +++ b/kernel/rcu/tree_exp.h @@ -7,6 +7,7 @@ * Authors: Paul E. McKenney */ =20 +#include #include =20 static void rcu_exp_handler(void *unused); @@ -590,6 +591,9 @@ static void synchronize_rcu_expedited_wait(void) return; if (rcu_stall_is_suppressed()) continue; + + nbcon_cpu_emergency_enter(); + j =3D jiffies; rcu_stall_notifier_call_chain(RCU_STALL_NOTIFY_EXP, (void *)(j - jiffies= _start)); trace_rcu_stall_warning(rcu_state.name, TPS("ExpeditedStall")); @@ -643,6 +647,9 @@ static void synchronize_rcu_expedited_wait(void) rcu_exp_print_detail_task_stall_rnp(rnp); } jiffies_stall =3D 3 * rcu_exp_jiffies_till_stall_check() + 3; + + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); } } diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h index 4b0e9d7c4c68..b3a6943127bc 100644 --- a/kernel/rcu/tree_stall.h +++ b/kernel/rcu/tree_stall.h @@ -7,6 +7,7 @@ * Author: Paul E. McKenney */ =20 +#include #include #include =20 @@ -605,6 +606,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 @@ -657,6 +660,8 @@ static void print_other_cpu_stall(unsigned long gp_seq,= unsigned long gps) rcu_check_gp_kthread_expired_fqs_timer(); rcu_check_gp_kthread_starvation(); =20 + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); =20 rcu_force_quiescent_state(); /* Kick them all. */ @@ -677,6 +682,8 @@ static void print_cpu_stall(unsigned long gps) if (rcu_stall_is_suppressed()) return; =20 + nbcon_cpu_emergency_enter(); + /* * OK, time to rat on ourselves... * See Documentation/RCU/stallwarn.rst for info on how to debug @@ -706,6 +713,8 @@ static void print_cpu_stall(unsigned long gps) jiffies + 3 * rcu_jiffies_till_stall_check() + 3); raw_spin_unlock_irqrestore_rcu_node(rnp, flags); =20 + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); =20 /* --=20 2.39.2 From nobody Fri Dec 19 20:34:49 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 993DE2C684 for ; Sun, 4 Aug 2024 00:51: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=1722732721; cv=none; b=QpyqhlBK9/UOU05YuSaxnkx9OR3aE/v+KR9Sure56WlzylOW3gtgmqqCXGmGzvjovWSRqKaD9gMBnwmHyGEW/BY279Lx8b/xGOu/w6fXESjBpJgDyurG3Dmxal7/U77T5gp71zqJyUIZGqmTDrmNbAcYP4HA4hACgysMY/sbXmM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1722732721; c=relaxed/simple; bh=4OdqDdNHwI+MpNe3A4JuCxp3X0/uGKJmw+4fmyl14ho=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=tePuKzWWnEu2QkrRNW3HsDTf/F4fNa7KyrU5NWMDiWl7RpfOYz3gsQam1R8h6rer8QIFzSkwaioaOWA4yb8k9seXhazVh+RF22o7Km3wbVfBZr8xz0G3KU0NfjSRA4P5XQQbywQrkYNnLh8hVlyb9beWOZWRmt91CDfYb9S+mTQ= 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=U7/6oQNO; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Zk8oo4Da; 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="U7/6oQNO"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Zk8oo4Da" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1722732714; 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=WbnnfoXadhhBBS7uyzhVWkZWyR3EDchUa4vB6qOLFEY=; b=U7/6oQNO6Q8SJQNumiXCbfkpbDKQImTgnvdOBlN/L3OzHLK5k0N7ZwlAEOh36gvtxRzTB3 0F2mZY8HEsBsGKn6MC+R1mWcrXy/XsPb6NS96GIENe7WXME6tqcqgxuSrL24dC8+nzvOg1 5JMfjIieYgSWB086BhmKltJhvGXnf4baY+N0qJGLBnjuSQgjlB31sydHtL8F3Qy2XcEtpL tFpCulrT6FX/383ofdx3bDr9fj89XXDGzRXgJUl/24po0ggABIaAbKn22t3U1xTcqiV58b WMWy7RzWwB/Tz8Kcyhg2Dr7Mdip70WtTlvOXFK1DHOy/lDhFshEXvR34gYcKAg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1722732714; 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=WbnnfoXadhhBBS7uyzhVWkZWyR3EDchUa4vB6qOLFEY=; b=Zk8oo4Da7A+ukFxiXKK0DJd0J8XnuXR2kHQPuLHP2+oIFvsNpoBBJneZEJP6dWOXaRh+fD eCMlr6Bv7d1yeJBA== 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 v7 35/35] lockdep: Mark emergency sections in lockdep splats Date: Sun, 4 Aug 2024 02:57:38 +0206 Message-Id: <20240804005138.3722656-36-john.ogness@linutronix.de> In-Reply-To: <20240804005138.3722656-1-john.ogness@linutronix.de> References: <20240804005138.3722656-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 emergency sections wherever multiple lines of lock debugging output are generated. In an emergency section, every printk() call will attempt to directly flush to the consoles using the EMERGENCY priority. Note that debug_show_all_locks() and lockdep_print_held_locks() rely on their callers to enter the emergency section. This is because these functions can also be called in non-emergency situations (such as sysrq). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/locking/lockdep.c | 83 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 81 insertions(+), 2 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 58c88220a478..fea538f7d131 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -56,6 +56,7 @@ #include #include #include +#include =20 #include =20 @@ -573,8 +574,10 @@ static struct lock_trace *save_trace(void) if (!debug_locks_off_graph_unlock()) return NULL; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); =20 return NULL; } @@ -887,11 +890,13 @@ look_up_lock_class(const struct lockdep_map *lock, un= signed int subclass) if (unlikely(subclass >=3D MAX_LOCKDEP_SUBCLASSES)) { instrumentation_begin(); debug_locks_off(); + nbcon_cpu_emergency_enter(); printk(KERN_ERR "BUG: looking up invalid subclass: %u\n", subclass); printk(KERN_ERR "turning off the locking correctness validator.\n"); dump_stack(); + nbcon_cpu_emergency_exit(); instrumentation_end(); return NULL; } @@ -968,11 +973,13 @@ static bool assign_lock_key(struct lockdep_map *lock) else { /* Debug-check: all keys must be persistent! */ debug_locks_off(); + nbcon_cpu_emergency_enter(); pr_err("INFO: trying to register non-static key.\n"); pr_err("The code is fine but needs lockdep annotation, or maybe\n"); pr_err("you didn't initialize this object before use?\n"); pr_err("turning off the locking correctness validator.\n"); dump_stack(); + nbcon_cpu_emergency_exit(); return false; } =20 @@ -1316,8 +1323,10 @@ register_lock_class(struct lockdep_map *lock, unsign= ed int subclass, int force) return NULL; } =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return NULL; } nr_lock_classes++; @@ -1349,11 +1358,13 @@ register_lock_class(struct lockdep_map *lock, unsig= ned int subclass, int force) if (verbose(class)) { graph_unlock(); =20 + nbcon_cpu_emergency_enter(); printk("\nnew class %px: %s", class->key, class->name); if (class->name_version > 1) printk(KERN_CONT "#%d", class->name_version); printk(KERN_CONT "\n"); dump_stack(); + nbcon_cpu_emergency_exit(); =20 if (!graph_lock()) { return NULL; @@ -1392,8 +1403,10 @@ static struct lock_list *alloc_list_entry(void) if (!debug_locks_off_graph_unlock()) return NULL; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return NULL; } nr_list_entries++; @@ -2039,6 +2052,8 @@ static noinline void print_circular_bug(struct lock_l= ist *this, =20 depth =3D get_lock_depth(target); =20 + nbcon_cpu_emergency_enter(); + print_circular_bug_header(target, depth, check_src, check_tgt); =20 parent =3D get_lock_parent(target); @@ -2057,6 +2072,8 @@ static noinline void print_circular_bug(struct lock_l= ist *this, =20 printk("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static noinline void print_bfs_bug(int ret) @@ -2569,6 +2586,8 @@ print_bad_irq_dependency(struct task_struct *curr, if (!debug_locks_off_graph_unlock() || 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=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: %s-safe -> %s-unsafe lock order detected\n", @@ -2618,11 +2637,13 @@ print_bad_irq_dependency(struct task_struct *curr, pr_warn(" and %s-irq-unsafe lock:\n", irqclass); next_root->trace =3D save_trace(); if (!next_root->trace) - return; + goto out; print_shortest_lock_dependencies(forwards_entry, next_root); =20 pr_warn("\nstack backtrace:\n"); dump_stack(); +out: + nbcon_cpu_emergency_exit(); } =20 static const char *state_names[] =3D { @@ -2987,6 +3008,8 @@ print_deadlock_bug(struct task_struct *curr, struct h= eld_lock *prev, if (!debug_locks_off_graph_unlock() || 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=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: possible recursive locking detected\n"); @@ -3009,6 +3032,8 @@ print_deadlock_bug(struct task_struct *curr, struct h= eld_lock *prev, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 /* @@ -3606,6 +3631,8 @@ static void print_collision(struct task_struct *curr, struct held_lock *hlock_next, struct lock_chain *chain) { + 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\n"); pr_warn("WARNING: chain_key collision\n"); @@ -3622,6 +3649,8 @@ static void print_collision(struct task_struct *curr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } #endif =20 @@ -3712,8 +3741,10 @@ static inline int add_chain_cache(struct task_struct= *curr, if (!debug_locks_off_graph_unlock()) return 0; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return 0; } chain->chain_key =3D chain_key; @@ -3730,8 +3761,10 @@ static inline int add_chain_cache(struct task_struct= *curr, if (!debug_locks_off_graph_unlock()) return 0; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return 0; } =20 @@ -3970,6 +4003,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 +4033,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 /* @@ -4032,6 +4069,8 @@ print_irq_inversion_bug(struct task_struct *curr, if (!debug_locks_off_graph_unlock() || 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=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: possible irq lock inversion dependency detected\n"); @@ -4072,11 +4111,13 @@ print_irq_inversion_bug(struct task_struct *curr, pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); root->trace =3D save_trace(); if (!root->trace) - return; + goto out; print_shortest_lock_dependencies(other, root); =20 pr_warn("\nstack backtrace:\n"); dump_stack(); +out: + nbcon_cpu_emergency_exit(); } =20 /* @@ -4153,6 +4194,8 @@ void print_irqtrace_events(struct task_struct *curr) { const struct irqtrace_events *trace =3D &curr->irqtrace; =20 + nbcon_cpu_emergency_enter(); + printk("irq event stamp: %u\n", trace->irq_events); printk("hardirqs last enabled at (%u): [<%px>] %pS\n", trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, @@ -4166,6 +4209,8 @@ void print_irqtrace_events(struct task_struct *curr) printk("softirqs last disabled at (%u): [<%px>] %pS\n", trace->softirq_disable_event, (void *)trace->softirq_disable_ip, (void *)trace->softirq_disable_ip); + + nbcon_cpu_emergency_exit(); } =20 static int HARDIRQ_verbose(struct lock_class *class) @@ -4686,10 +4731,12 @@ static int mark_lock(struct task_struct *curr, stru= ct held_lock *this, * We must printk outside of the graph_lock: */ if (ret =3D=3D 2) { + nbcon_cpu_emergency_enter(); printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); print_lock(this); print_irqtrace_events(curr); dump_stack(); + nbcon_cpu_emergency_exit(); } =20 return ret; @@ -4730,6 +4777,8 @@ print_lock_invalid_wait_context(struct task_struct *c= urr, if (debug_locks_silent) return 0; =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\n"); pr_warn("[ BUG: Invalid wait context ]\n"); @@ -4749,6 +4798,8 @@ print_lock_invalid_wait_context(struct task_struct *c= urr, pr_warn("stack backtrace:\n"); dump_stack(); =20 + nbcon_cpu_emergency_exit(); + return 0; } =20 @@ -4956,6 +5007,8 @@ print_lock_nested_lock_not_held(struct task_struct *c= urr, if (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=3D=3D\n"); pr_warn("WARNING: Nested lock was not taken\n"); @@ -4976,6 +5029,8 @@ print_lock_nested_lock_not_held(struct task_struct *c= urr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static int __lock_is_held(const struct lockdep_map *lock, int read); @@ -5024,11 +5079,13 @@ static int __lock_acquire(struct lockdep_map *lock,= unsigned int subclass, debug_class_ops_inc(class); =20 if (very_verbose(class)) { + nbcon_cpu_emergency_enter(); printk("\nacquire class [%px] %s", class->key, class->name); if (class->name_version > 1) printk(KERN_CONT "#%d", class->name_version); printk(KERN_CONT "\n"); dump_stack(); + nbcon_cpu_emergency_exit(); } =20 /* @@ -5155,6 +5212,7 @@ static int __lock_acquire(struct lockdep_map *lock, u= nsigned int subclass, #endif if (unlikely(curr->lockdep_depth >=3D MAX_LOCK_DEPTH)) { debug_locks_off(); + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); printk(KERN_DEBUG "depth: %i max: %lu!\n", curr->lockdep_depth, MAX_LOCK_DEPTH); @@ -5162,6 +5220,7 @@ static int __lock_acquire(struct lockdep_map *lock, u= nsigned int subclass, lockdep_print_held_locks(current); debug_show_all_locks(); dump_stack(); + nbcon_cpu_emergency_exit(); =20 return 0; } @@ -5181,6 +5240,8 @@ static void print_unlock_imbalance_bug(struct task_st= ruct *curr, if (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=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: bad unlock balance detected!\n"); @@ -5197,6 +5258,8 @@ static void print_unlock_imbalance_bug(struct task_st= ruct *curr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static noinstr int match_held_lock(const struct held_lock *hlock, @@ -5901,6 +5964,8 @@ static void print_lock_contention_bug(struct task_str= uct *curr, if (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=3D\n"); pr_warn("WARNING: bad contention detected!\n"); @@ -5917,6 +5982,8 @@ static void print_lock_contention_bug(struct task_str= uct *curr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static void @@ -6530,6 +6597,8 @@ print_freed_lock_bug(struct task_struct *curr, const = void *mem_from, if (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\n"); pr_warn("WARNING: held lock freed!\n"); @@ -6542,6 +6611,8 @@ print_freed_lock_bug(struct task_struct *curr, const = void *mem_from, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static inline int not_in_range(const void* mem_from, unsigned long mem_len, @@ -6588,6 +6659,8 @@ static void print_held_locks_bug(void) if (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=3D=3D=3D=3D\n"); pr_warn("WARNING: %s/%d still has locks held!\n", @@ -6597,6 +6670,8 @@ static void print_held_locks_bug(void) lockdep_print_held_locks(current); pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 void debug_check_no_locks_held(void) @@ -6654,6 +6729,7 @@ asmlinkage __visible void lockdep_sys_exit(void) if (unlikely(curr->lockdep_depth)) { if (!debug_locks_off()) return; + 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=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D\n"); pr_warn("WARNING: lock held when returning to user space!\n"); @@ -6662,6 +6738,7 @@ asmlinkage __visible void lockdep_sys_exit(void) pr_warn("%s/%d is leaving the kernel with locks still held!\n", curr->comm, curr->pid); lockdep_print_held_locks(curr); + nbcon_cpu_emergency_exit(); } =20 /* @@ -6678,6 +6755,7 @@ void lockdep_rcu_suspicious(const char *file, const i= nt line, const char *s) bool rcu =3D warn_rcu_enter(); =20 /* Note: the following can be executed concurrently, so be careful. */ + 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\n"); pr_warn("WARNING: suspicious RCU usage\n"); @@ -6716,6 +6794,7 @@ void lockdep_rcu_suspicious(const char *file, const i= nt line, const char *s) lockdep_print_held_locks(curr); pr_warn("\nstack backtrace:\n"); dump_stack(); + nbcon_cpu_emergency_exit(); warn_rcu_exit(rcu); } EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); --=20 2.39.2