From nobody Wed Dec 17 01:11:54 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 3999616DEB1 for ; Mon, 22 Jul 2024 17:19:47 +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=1721668788; cv=none; b=qmpHselnlEX2mTJm6LDDD9MvO+hRsmywE50lNN/L0LfmZcm5fG0VNAkHUoYxnczVmgDJoTjh5DujincNJexoM3omCOCNKwDwOzRWSNT3cWbb8mMhZlp8/KnKfwbSWGbHqqnJAJLDBMBNqGH6Mm8b51YUCkpS8VBhzr8GC81GQ50= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668788; c=relaxed/simple; bh=CFpAKElXQ4pLm8ggHq6++kEORvpGrXhuMecgqKmNbcE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=g08sgdgCGrafLojfBjrsTW92jWYmVjyQDTg5Czv4YRTbPwBX0G1s11Tow3W1pOxZI+KOH0yCKarPkr02ef/5qd71Uv2U733gBzO1wv74h4tUyO9hEwPhNQLP/KS7jHlFPa8SjR0cS3xYGJ2b742oB+EUmUKRuut5MVHGwoFgkLw= 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=pHKqjCrg; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=sJdh9Hau; 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="pHKqjCrg"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="sJdh9Hau" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668780; 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=XhBnGHE6CqmvVf3ZFd5i4KOYZ+QBtgmy2XGk94J87c8=; b=pHKqjCrg4LktkE9P2m1Lk9ZqdIycwWbtPlj27c1cGO1xcno+SMgqVUi9i6Vq+DyF4XSit2 hY9Slu0MEEJFbjZ8Dj5OryT1N6HSjYUP2IHdGwbzmRVTr/TGHqOzxmDY9jOS//NCw/tCCp GKJ/pLhOQ9D/a9O50fnLkkOcv7uBvJ/YwKI/I0luoCJpoM354GN1zh2cFfpRj1Aub+SwG6 iE7+9omMuvtSMPw4GRC+OvEUXIsJcuns5J0kHhbAp5h46aNpJ1PIH7C+HDDlMeluJ7CoyJ 9jdnJoGZwEVACYyVdm18r9621I4Vcx5Cgf7+aAdsKtD3Qh8R29bWI1hxi3a6/g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668780; 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=XhBnGHE6CqmvVf3ZFd5i4KOYZ+QBtgmy2XGk94J87c8=; b=sJdh9Hau+0IjZCWQjp9RWb7/PvitxcPslbB8wHevyFNEIOjhBj74gsZxoPSZoPERS0iCrZ gVFD6vyIg/2FfMDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 01/19] printk: nbcon: Clarify nbcon_get_default_prio() context Date: Mon, 22 Jul 2024 19:25:21 +0206 Message-Id: <20240722171939.3349410-2-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" Correct the kerneldoc and code comments that claimed migration must be disabled for nbcon_get_default_prio(). This is not true. Add the explation in the kerneldoc. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/nbcon.c | 5 ++++- kernel/printk/printk.c | 2 -- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index ef6e76db0f5a..d8388faa6500 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -970,9 +970,12 @@ static __ref unsigned int *nbcon_get_cpu_emergency_nes= ting(void) * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon * printing on the current CPU * - * Context: Any context which could not be migrated to another CPU. + * Context: Any context. * Return: The nbcon_prio to use for acquiring an nbcon console in this * context for printing. + * + * Allowing migration enabled relies on the fact that a context cannot be + * migrated to panic or emergency state CPUs. */ enum nbcon_prio nbcon_get_default_prio(void) { diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d0bff0b0abfd..5090c0591f88 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2401,8 +2401,6 @@ asmlinkage int vprintk_emit(int facility, int level, * printing of all remaining records to all consoles so that * this context can return as soon as possible. Hopefully * another printk() caller will take over the printing. - * - * Also, nbcon_get_default_prio() requires migration disabled. */ preempt_disable(); =20 --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 A55D816E894 for ; Mon, 22 Jul 2024 17:19: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=1721668790; cv=none; b=hfqYa4rEdn0YvokqVHYCzY/exS1/VlnGBlyeknnoW1slWi2NLsu9ELXYGRP5ZQT5qcu4E/mQeRCUTeRzkab0yzpKJmi5QyYnaiZwquNJZC7dSvrzdH/ENrJZpXQAu6WF83vWgNYjCyyycYmk8JsYfj2tBcFDroANpn3Grk8Adcg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668790; c=relaxed/simple; bh=A2eYnpHc8baJBUquJ1C2lgRQZHsDHryfmr0dAxCwRWw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AOej+ub8E3IikiZHmn7DQFRuZ45P1jQy40RU7A7NYXusc8gS8mPM5ca4o2eUUPxEKBsNQG/VxYdocSHnAF88JRw/87h888iFeqzyElGeQKx9nfJdqElKk9O4fsGP77OhkG4Xj84SFPI2A28dMv7voqFq9rXk5jC995onNypaMXk= 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=sDFljNOu; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=IlIvboqv; 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="sDFljNOu"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="IlIvboqv" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668780; 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=onDKVSWtrdlC7pr3Dto9zggnE9buyG6ngOh1tFL4sBE=; b=sDFljNOuuBzGf6bHhJEQGwdNuoAO9HnH7C/pr9FGHHYz5cmNqXF3SL5a7D1KGs3wMZJlnR Gq7Uuu/KviUfTxpJ2vl8yhM5boe3lRBWNNXARhAvhxRmMpVc5MMec1LrpcINqWj2sKG4MT jxChC12CGw9Gg3fA+4xFnNoee2l2NqmecVF4qOe63xOVVCfsQIaHvg7MfSFBDvnz7ejm/4 tMQfDvbfxG9erE8mMCNWhidvmSfc0++gBklU4n82DQBMVYQq5GikYSxb7gL/Wa72CVPv9/ etvKDJNYhzadiaL62kmVd+v09QezP8i2iJ0SQs0oPJWjN/jfmTq4W5P1+jl4aQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668780; 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=onDKVSWtrdlC7pr3Dto9zggnE9buyG6ngOh1tFL4sBE=; b=IlIvboqvdv183snpk7pJYDJOvtG4CEHQCAPgFz6GDMrbQlg5Vl5JbDvj7JAgAwpFZa7rS9 RmmkYg2+OdnQInBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 02/19] printk: nbcon: Consolidate alloc() and init() Date: Mon, 22 Jul 2024 19:25:22 +0206 Message-Id: <20240722171939.3349410-3-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 init_seq 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(). It also more closely matches the setup logic of the legacy consoles. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 -- kernel/printk/nbcon.c | 35 ++++++++++------------------------- kernel/printk/printk.c | 2 +- 3 files changed, 11 insertions(+), 28 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 0439cf2fdc22..d58f5cefbac3 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -86,7 +86,6 @@ int console_lock_spinning_disable_and_check(int cookie); 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, u64 init_seq); void nbcon_free(struct console *con); enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_pending(void); @@ -144,7 +143,6 @@ static inline bool printk_percpu_data_ready(void) { ret= urn 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, u64 init_seq) { } 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) { } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index d8388faa6500..1388e23a439f 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1343,17 +1343,21 @@ void nbcon_cpu_emergency_flush(void) } =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); + if (con->flags & CON_BOOT) { /* * Boot console printing is synchronized with legacy console @@ -1372,25 +1376,6 @@ bool nbcon_alloc(struct console *con) return true; } =20 -/** - * nbcon_init - Initialize the nbcon console specific data - * @con: Console to initialize - * @init_seq: Sequence number of the first record to be emitted - * - * nbcon_alloc() *must* be called and succeed before this function - * is called. - */ -void nbcon_init(struct console *con, u64 init_seq) -{ - struct nbcon_state state =3D { }; - - /* nbcon_alloc() must have been called and successful! */ - BUG_ON(!con->pbufs); - - nbcon_seq_force(con, init_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 5090c0591f88..641c2a8b0a09 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3631,7 +3631,7 @@ void register_console(struct console *newcon) =20 if (newcon->flags & CON_NBCON) { have_nbcon_console =3D true; - nbcon_init(newcon, init_seq); + nbcon_seq_force(newcon, init_seq); } else { have_legacy_console =3D true; newcon->seq =3D init_seq; --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 A558516E870 for ; Mon, 22 Jul 2024 17:19: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=1721668790; cv=none; b=JDWRz5K4XJqlJxE59e/FqDu/oPxs5jc9RTa3xfITMqGCG09Bvf2z1xONr47Q1yVQT5EseMwKFdxTZiPhEVmrUlnbLOLjeG7JVt/xzX9fxjYrmacSo+C6ZHgg7vMlD+ysF/5eimjdE/tLfcq9n55tIBuu5ueg2unVsKh4W/R02pc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668790; c=relaxed/simple; bh=HuC/CKF1NiRZ7YVPgWU3HdnJRNKQE8cNb/fsf4HFiqI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=u9CYTxa4YC9jBagcZvUc/V1GyHXpVH/LnaTxk7XF+lLdWfrxPm3F0O0LYWXxb0TbRzj5C+oHldwQS9bw6N8SktwISGNrBOmWBtjMhu8MBCC2m3d7U+q3c4J1Qxo1erxbOLR5XNwV5if5tgIz+b2b/eRKZqBzajTKSFtZMO1aRxg= 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=i5dvPBqw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cn3OKCHl; 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="i5dvPBqw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cn3OKCHl" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668781; 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=nWsbf0ByLpBwUeUrEFtNFqNZvJfvWeXWa7MTgQrlekU=; b=i5dvPBqw4j5CMQpj0GdW/MiZ1JPj2Movmzk2z/Sk1uBQe6lk/J+WdX43b7hEKcNJEr/V8m QURNtIZWqd9rLprBxDgT64MyzT8iGGeQ0NjXuPJS74kdBR8nDdu6Uh0ALr2B0AWL2l3Yrt MKeLQvNTsmEnbS3WSgBsgqA7+cd7HnXIBVYS3oxazaao94epCOHNxWPqyUuIBBk0BZ/7Ra c9HzL6QThyaA958O9/+ob2uQZMUCMC8lkhK6pAlMNzYxmBDO2nL5XNQjTxYe74qIANzyED oSxPgYXnvbp7K/wiMpvfZ0TNkpHm2V0FL9lLiki9an9rlXQ6s7OxKr2o47X01Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668781; 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=nWsbf0ByLpBwUeUrEFtNFqNZvJfvWeXWa7MTgQrlekU=; b=cn3OKCHlTy8Wp8lPF451APtllAC4qAQ4UDh01ye/CrwMRe79p0AheNmSI7IW59Tu1lEup4 5+yMG5k/dB/+J6DQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v3 03/19] printk: nbcon: Add function for printers to reacquire ownership Date: Mon, 22 Jul 2024 19:25:23 +0206 Message-Id: <20240722171939.3349410-4-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" Since ownership can be lost at any time due to handover or takeover, a printing context _must_ be prepared to back out immediately and carefully. However, there are scenarios where the printing context must reacquire ownership in order to finalize or revert hardware changes. One such example is when interrupts are disabled during printing. No other context will automagically re-enable the interrupts. For this case, the disabling context _must_ reacquire nbcon ownership so that it can re-enable the interrupts. Provide nbcon_reacquire_nobuf() for exactly this purpose. It allows a printing context to reacquire ownership using the same priority as its previous ownership. Note that after a successful reacquire the printing context will have no output buffer because that has been lost. This function cannot be used to resume printing. Signed-off-by: John Ogness --- include/linux/console.h | 6 +++++ kernel/printk/nbcon.c | 56 +++++++++++++++++++++++++++++++++++++---- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 4aaf053840ee..38ef6e64da19 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -366,6 +366,10 @@ struct console { * * The callback should allow the takeover whenever it is safe. It * increases the chance to see messages when the system is in trouble. + * If the driver must reacquire ownership in order to finalize or + * revert hardware changes, nbcon_reacquire_nobuf() can be used. + * However, on reacquire the buffer content is no longer available. A + * reacquire cannot be used to resume printing. * * The callback can be called from any context (including NMI). * Therefore it must avoid usage of any locking and instead rely @@ -559,6 +563,7 @@ extern void nbcon_cpu_emergency_flush(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); +extern void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt); #else static inline void nbcon_cpu_emergency_enter(void) { } static inline void nbcon_cpu_emergency_exit(void) { } @@ -566,6 +571,7 @@ static inline void nbcon_cpu_emergency_flush(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; } +static inline void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt= ) { } #endif =20 extern int console_set_on_cmdline; diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 1388e23a439f..18a83d181622 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -834,6 +834,47 @@ bool nbcon_exit_unsafe(struct nbcon_write_context *wct= xt) } EXPORT_SYMBOL_GPL(nbcon_exit_unsafe); =20 +static void nbcon_write_context_set_buf(struct nbcon_write_context *wctxt, + char *buf, unsigned int len) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con =3D ctxt->console; + struct nbcon_state cur; + + wctxt->outbuf =3D buf; + wctxt->len =3D len; + nbcon_state_read(con, &cur); + wctxt->unsafe_takeover =3D cur.unsafe_takeover; +} + +/** + * nbcon_reacquire_nobuf - Reacquire a console after losing ownership + * while printing + * @wctxt: The write context that was handed to the write callback + * + * Since ownership can be lost at any time due to handover or takeover, a + * printing context _must_ be prepared to back out immediately and + * carefully. However, there are scenarios where the printing context must + * reacquire ownership in order to finalize or revert hardware changes. + * + * This function allows a printing context to reacquire ownership using the + * same priority as its previous ownership. + * + * Note that after a successful reacquire the printing context will have no + * output buffer because that has been lost. This function cannot be used = to + * resume printing. + */ +void nbcon_reacquire_nobuf(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + + while (!nbcon_context_try_acquire(ctxt)) + cpu_relax(); + + nbcon_write_context_set_buf(wctxt, NULL, 0); +} +EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); + /** * nbcon_emit_next_record - Emit a record in the acquired context * @wctxt: The write context that will be handed to the write function @@ -859,7 +900,6 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt) .pbufs =3D ctxt->pbufs, }; unsigned long con_dropped; - struct nbcon_state cur; unsigned long dropped; =20 /* @@ -894,10 +934,7 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) goto update_con; =20 /* Initialize the write context for driver callbacks. */ - wctxt->outbuf =3D &pmsg.pbufs->outbuf[0]; - wctxt->len =3D pmsg.outbuf_len; - nbcon_state_read(con, &cur); - wctxt->unsafe_takeover =3D cur.unsafe_takeover; + nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_le= n); =20 if (con->write_atomic) { con->write_atomic(con, wctxt); @@ -911,6 +948,15 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return false; } =20 + if (!wctxt->outbuf) { + /* + * Ownership was lost and reacquired by the driver. + * Handle it as if ownership was lost. + */ + nbcon_context_release(ctxt); + return false; + } + /* * Since any dropped message was successfully output, reset the * dropped count for the console. --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 A561E16EB44 for ; Mon, 22 Jul 2024 17:19: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=1721668790; cv=none; b=ZPu7KYNE9Y/wj/DV7qIx82ZEByPiq7SDIRVfWSfQP3IN/Qt/7NkiAP7X+dZSqq/DkoKCLsrnYePA7d2+vg99p6KycY9GfaiEfTuQ2pzfTXcMh/e/eem750x6FcijwN+hvp88sd7J57CWkN18WJW8C8KhdpN7MNgQ14AeY5CuQj4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668790; c=relaxed/simple; bh=XnATik6cxgSBQomo+Y77x9GvHXh59HU0zmFsjquhHBI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=HWIwS7XTdwQ3Q//nwrVKFO8RJRztExUPy+tEGczz2hMnS07bv7BGIvPDjjZ9eKSVRnWxlhTtFbrmJ2ieiRFwgAntEWH0uUCv6qLA+gkfoFTe99EYjdxdmyQIGALwZLlgwi62BK0yjpp7u0IdQvDVFZLCOPjhUi5Tu7MULSZ7030= 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=4hf0RJrQ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=S+12s/A5; 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="4hf0RJrQ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="S+12s/A5" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668781; 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=O5+IoeYj/HBpqJqJrR+C44gazCIq5k30FQfC4+nSAcs=; b=4hf0RJrQVGC4JHeY1Na50NUoSKiAOL0MrXklC77vrOD7Z9LH94e2XuRxRRhpTcguIvxyIP W59yOiFyyV4hPJI2kwUp0eX6hJNjNnUhzfvEyW6DE+vlbpNvjnrz8jSdlrSt2pJCLGk1nA RAt1XuuN+26kFNTDdTDFNd2m21kJ6fGSKMYwz/KrbRteHqcM8ep85vprFcE3Ex7pYHcV6O I8595vNHExBT6A0i6VXmvjqC2otxitN7vK29PdWb9T6XppFVOw8GELbdh9lkh0qXVu7yH0 /tGj00hV2nc9StWcmteo9Pv45U+n2OGMaoZUa9pdFuTeI+2eTnu6wnh75Ca6mg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668781; 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=O5+IoeYj/HBpqJqJrR+C44gazCIq5k30FQfC4+nSAcs=; b=S+12s/A5L0otdcT7czT/g79126aA0y/mtAfVTXGyVhMIqJEn21JO6YKy2PmD/R6aI4voO9 w2wDx39iUaE2nOCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 04/19] printk: nbcon: Clarify rules of the owner/waiter matching Date: Mon, 22 Jul 2024 19:25:24 +0206 Message-Id: <20240722171939.3349410-5-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 18a83d181622..db1685a6d5cd 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -241,6 +241,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 @@ -272,18 +279,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); @@ -591,11 +609,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 Wed Dec 17 01:11:54 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 3044616F0E1 for ; Mon, 22 Jul 2024 17:19: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=1721668791; cv=none; b=ZF+HuKjpXcqi7oRNfWWf/tdKvO6pEciEBiXUaEclUcj08lcLT/vIAvGK3znvYmP2EDOBak/+HgQ+pLk6H59M5shcCZohF9fb9r49Vj1wG93rgHRzv8BMqSkZP9wl4gI/qW26fmQbF4atybl3BJpIVnjex6a7dO+bU0I/EP0PaKc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668791; c=relaxed/simple; bh=5fcRvyfyT2XAVwhr3OseZVux1LLmXi29ZOKScT/5qJc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MkGzeqRXXJyGyf3WQJNOIU5tsbromgoHqMvjf+UENaRHL8le3vPCHuVeSTh6V+wclfVlYc2koFx/bL9OwHyj6JwgPdPpwtH1oVcKCXNHFiP8POqJiOtQWQylHhFJm71tGEaGqlGuJYvrXTkvDsrtuKu5t6A4gAhUdJ2c61EODrk= 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=OfVkQd6Z; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=C+Qjl5jx; 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="OfVkQd6Z"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="C+Qjl5jx" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668781; 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=PR1ZCxr9HoxSs4tStLYm6lQv2PPsrt9cnd1Oqprb4tU=; b=OfVkQd6ZAzaq0V99uh651SPXzyUQ3DBPXzbFcEmrdb2R4p6Yqp3SzMjLjXg4jZ8VM8TKhW 6zv1twNFtUUo9KCVVAuruazpo1csLqT4+1HTXzfDYTqUWPs5ngwzS2kZ+Fz/7Zkgz9jnau QaFlic0g8/N2hJeUg4aQW4zQo1Pf1GJtqRQ+FRtEq4t4xqKZRH5jW3kZQr7TVqzeRMZQ+M F/x4Dt1zMjEt6sqjITBGTn63yJbRzptr9YflgXoZeP/isl1E+yU3A61KfpkTlfR5DQtc+T AsP7/pBG/3AHUo/mEYmlu0SlqLcjRSdjtnnZSzhx6XR6uRu9V7Cw0/dKri4V5Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668781; 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=PR1ZCxr9HoxSs4tStLYm6lQv2PPsrt9cnd1Oqprb4tU=; b=C+Qjl5jx3dv2M0RQRWctI6hR2I5Mx2akH6a1TTBxpt5B/CUjxLufj07XoXfi1tcAYYpMv3 uwhDQSIIMqpPwFDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 05/19] printk: Fail pr_flush() if before SYSTEM_SCHEDULING Date: Mon, 22 Jul 2024 19:25:25 +0206 Message-Id: <20240722171939.3349410-6-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" A follow-up change adds pr_flush() to console unregistration. However, with boot consoles unregistration can happen very early if there are also regular consoles registering as well. In this case the pr_flush() is not important because the regular console manually flushes the boot consoles before unregistering them. Allow pr_flush() to fail if @system_state has not yet reached SYSTEM_SCHEDULING. This avoids might_sleep() and msleep() explosions that would otherwise occur. Signed-off-by: John Ogness --- kernel/printk/printk.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 641c2a8b0a09..39db56a32c5e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3946,6 +3946,10 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre u64 diff; u64 seq; =20 + /* Sorry, pr_flush() will not work this early. */ + if (system_state < SYSTEM_SCHEDULING) + return false; + might_sleep(); =20 seq =3D prb_next_reserve_seq(prb); --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 2EA2016F0D8 for ; Mon, 22 Jul 2024 17:19: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=1721668791; cv=none; b=jd12RkK0dYcTVEV+dh79v5pRhxEJmeEnt3jaf5BFYQIapKFXNQw9y0ng5ZapFYeH5kR/Mqtuj4CuhKOGJUtnE1gzpjdbySQSteUrNjqtRdWgQXr0CQbt2erhppK5SEVE7I5ZZAuyQH7dfPAd+ene7EAAEQhTct3ETiyemc9mXu8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668791; c=relaxed/simple; bh=5wwxTBxiZ7aMHtCFtvCwkcsfy/4aw/2gv7yrLlc3+rc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=i2GoEArKi48H4QkZy1WeeaOEJ21c9mWDgF/OHA6OFxt5fqrFihECFVF8jOIokWJic8maCMYwyQfJE6pdtyc65NLmLZK3fubeq2MEmnb+r62l2Nrw+8eDBvDCjCrnOKRA5EDdVbhOAf4sLp5obzEA9rNB4qxEtc9SCWvjLa8HJuE= 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=YjLvp1yp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=v7EacWjs; 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="YjLvp1yp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="v7EacWjs" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668782; 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=TMfQdp4S9HasIDYxwBw+l30b818McatDGhM1+M+Ppvw=; b=YjLvp1ypdfw7lxYGRAkoOfoAIF4avkFLB6d+Jyos6h7N77UTvVsBKL9OhDs632hDndO9zM vCvLJDLGKq8EQ622NcGZouDCagqJdYujin1QYIm8DvAb0CErlAq0Fii71eT8sCGiqvzJkd 08iceLmxN+G+OQaYbW+GvdEf7c/CajFAj0qadymVT4HAqNAwuPj/+OI4yhULH8woVYV8CJ L7xDtA7DrmWA5hbN9Ki2J+FUUL09ysRn9dFr+dnJd8fDFdTEAxFAQc3Nh/F4d6dUbgBaub S4L8Zx9uFHAqGjnE7eFKcsteXHAKTc5QypfwkwTjN1cYGomDGG5Kuc8RpevaXg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668782; 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=TMfQdp4S9HasIDYxwBw+l30b818McatDGhM1+M+Ppvw=; b=v7EacWjsU2/JbBJqAYUlLxYC5gMKr7sNv1oUqMqbOfNJYShnAoqM+FnBLXjNwNTOpnIRIv beJYHgvPs7UKX/Cg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 06/19] printk: Flush console on unregister_console() Date: Mon, 22 Jul 2024 19:25:26 +0206 Message-Id: <20240722171939.3349410-7-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" Ensure consoles have flushed pending records before unregistering. The console should print up to at least its related "console disabled" record. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 39db56a32c5e..82440b1f0d1e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3727,11 +3727,16 @@ static int unregister_console_locked(struct console= *console) if (res > 0) return 0; =20 + if (!console_is_registered_locked(console)) + res =3D -ENODEV; + else if (console_is_usable(console, console->flags)) + __pr_flush(console, 1000, true); + /* Disable it unconditionally */ console_srcu_write_flags(console, console->flags & ~CON_ENABLED); =20 - if (!console_is_registered_locked(console)) - return -ENODEV; + if (res < 0) + return res; =20 /* * Use the driver synchronization to ensure that the hardware is not --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 B8EE716F267 for ; Mon, 22 Jul 2024 17:19: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=1721668792; cv=none; b=L8d31q29rJXhqtZTyfA6+GHMbyaSXgiIk6LpbgGyEv9cfo8vGP24Mu5isX7ROXSk9hilSgL3OR5YUW0l5wjSfnZZOM1OfvNx7jVWh7Q2MAicOFsrexlQvw870Ozqj8xo7nnyhW5LTYplSWR/UWsVf+Oeka2KI+dB5l/wJwLVmyU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668792; c=relaxed/simple; bh=BGQqE1E1rO6cNjCdjTz0mX/okTHEvHdejZfkDeZZq+M=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MArItGjpNk0l5xx7QgThlX/KcX9l0UdE0FFxy04gq8ywk0qqFPdN5hhj7bjyTr6aLrr2IUotZYB5d8lYDAJuSun9dXtfDGB6aZyk8XJMq4iH8O1inwCV5XwgGq0zmow0AUV0uznnBNcPjPljPvI7FsVQSLmNphl17eBUtzK07Os= 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=nf8p0Pcs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=YdcBLotU; 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="nf8p0Pcs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="YdcBLotU" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668782; 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=3jfxsbZMyZbZp6nvc6k5SK6umuoR31LJc1J7BsnGnjI=; b=nf8p0PcsHaU47cQ8r6Q+qb1x6ondAqP9QTbpPlhxxM78YwQiRRl/5dV1u8R/hmrFIYaT5t jmcW6BTL/ZEm3DwsndL3+ZvPcPEN89+JSfNvCVEgtv4kwkAE7bSCK9jbD1wWYK3TSijKTY MCnA/odXMSfiggfzpugqAgCJd76migXf49ytEmWD77WeAEFmqRxUMmHCh7LPDJv3yGENj9 AxJWMoB7jbob5y0/lfoNloxssIQVqiFqIr5ELJVpwsqrluICNEYoTylVgGjS2CFwQIvxdl ov1a9mGQJNQdL8LDyjLkWr/zMcD+NXYd3OnjjAxmwnGUZyPvzo8+Ek+AXx/37Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668782; 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=3jfxsbZMyZbZp6nvc6k5SK6umuoR31LJc1J7BsnGnjI=; b=YdcBLotULtb8g6LKEt2/CyFF9sKDF+6jlm7ULcOwJH7RiCVn8iAAVGLmP0TrGZsbOuBAlz RQx7zHkbBRiwL8Bg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 07/19] printk: Add helpers for flush type logic Date: Mon, 22 Jul 2024 19:25:27 +0206 Message-Id: <20240722171939.3349410-8-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 helper functions to fill out console_flush_type to be used for emergency and non-emergency call sites. In many system states it is acceptable to flush legacy directly via console_unlock or via offload to irq_work. For this reason the non-emergency helper provides an argument @prefer_offload for the caller to specify which method it is interested in performing. (The helper functions will never allow both.) Replace the logic of all flushing call sites to use the new helpers. Note that this cleans up various corner cases where is_printk_preferred() and @have_boot_console were not being considerered before. Signed-off-by: John Ogness --- kernel/printk/internal.h | 100 +++++++++++++++++++++++++++++++-- kernel/printk/nbcon.c | 32 ++++++++--- kernel/printk/printk.c | 116 ++++++++++++++++++++------------------- 3 files changed, 180 insertions(+), 68 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index d58f5cefbac3..ccdb81dc18f0 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -154,15 +154,105 @@ static inline bool console_is_usable(struct console = *con, short flags) { return #endif /* CONFIG_PRINTK */ =20 extern bool have_boot_console; +extern bool have_nbcon_console; extern bool have_legacy_console; +extern bool legacy_allow_panic_sync; + +/** + * struct console_flush_type - Define how to flush the consoles + * @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 + */ +struct console_flush_type { + bool nbcon_atomic; + bool legacy_direct; + bool legacy_offload; +}; =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. + * Decide while console flushing methods are to be used. Used + * for all flushing except when flushing from emergency state. + * + * Set @prefer_offload to true if the context is only interested in + * offloading. Then offloading types will be set instead of direct, + * if appropriate. */ -#define printing_via_unlock (have_legacy_console || have_boot_console) +static inline void printk_get_console_flush_type(struct console_flush_type= *ft, bool prefer_offload) +{ + memset(ft, 0, sizeof(*ft)); + + switch (nbcon_get_default_prio()) { + case NBCON_PRIO_NORMAL: + if (have_legacy_console || have_boot_console) { + if (prefer_offload || is_printk_deferred()) + ft->legacy_offload =3D true; + else + ft->legacy_direct =3D true; + } + + if (have_nbcon_console && !have_boot_console) + ft->nbcon_atomic =3D true; + break; + + case NBCON_PRIO_EMERGENCY: + /* + * Skip. The consoles will be flushed when exiting emergency + * state. See printk_get_emergency_console_flush_type(). + */ + break; + + case NBCON_PRIO_PANIC: + 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. + */ + if (legacy_allow_panic_sync || !ft->nbcon_atomic) + ft->legacy_direct =3D true; + break; + + default: + WARN_ON_ONCE(1); + break; + } +} + +/* + * Decide while console flushing methods are to be used from + * emergency state. + */ +static inline void printk_get_emergency_console_flush_type(struct console_= flush_type *ft) +{ + memset(ft, 0, sizeof(*ft)); + + switch (nbcon_get_default_prio()) { + case NBCON_PRIO_EMERGENCY: + if (have_nbcon_console && !have_boot_console) + ft->nbcon_atomic =3D true; + + if (have_legacy_console || have_boot_console) { + if (is_printk_deferred()) + ft->legacy_offload =3D true; + else + ft->legacy_direct =3D true; + } + break; + + case NBCON_PRIO_PANIC: + /* + * Skip. Console flushing is handled by panic code. + * See printk_get_console_flush_type(). + */ + 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 db1685a6d5cd..917022105b8b 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1235,6 +1235,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 @@ -1267,7 +1268,9 @@ static void nbcon_atomic_flush_pending_con(struct con= sole *con, u64 stop_seq, * 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)) { + printk_get_console_flush_type(&ft, false); + if (ft.nbcon_atomic && + prb_read_valid(prb, nbcon_seq_read(con), NULL)) { stop_seq =3D prb_next_reserve_seq(prb); goto again; } @@ -1362,6 +1365,7 @@ void nbcon_cpu_emergency_exit(void) { unsigned int *cpu_emergency_nesting; bool do_trigger_flush =3D false; + struct console_flush_type ft; =20 cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); =20 @@ -1374,19 +1378,25 @@ void nbcon_cpu_emergency_exit(void) * for the emergency messages is NBCON_PRIO_EMERGENCY. */ if (*cpu_emergency_nesting =3D=3D 1) { - nbcon_atomic_flush_pending(); + printk_get_emergency_console_flush_type(&ft); + + if (ft.nbcon_atomic) + nbcon_atomic_flush_pending(); =20 /* * Safely attempt to flush the legacy consoles in this * context. Otherwise an irq_work context is triggered * to handle it. */ - do_trigger_flush =3D true; - if (printing_via_unlock && !is_printk_deferred()) { + if (ft.legacy_direct) { if (console_trylock()) { do_trigger_flush =3D false; console_unlock(); + } else { + do_trigger_flush =3D true; } + } else { + do_trigger_flush =3D ft.legacy_offload; } } =20 @@ -1412,13 +1422,18 @@ void nbcon_cpu_emergency_exit(void) */ void nbcon_cpu_emergency_flush(void) { + struct console_flush_type ft; + /* The explicit flush is needed only in the emergency context. */ if (*(nbcon_get_cpu_emergency_nesting()) =3D=3D 0) return; =20 - nbcon_atomic_flush_pending(); + printk_get_emergency_console_flush_type(&ft); + + if (ft.nbcon_atomic) + nbcon_atomic_flush_pending(); =20 - if (printing_via_unlock && !is_printk_deferred()) { + if (ft.legacy_direct) { if (console_trylock()) console_unlock(); } @@ -1521,6 +1536,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)) @@ -1533,8 +1549,10 @@ void nbcon_device_release(struct console *con) * was locked. The console_srcu_read_lock must be taken to ensure * the console is usable throughout flushing. */ + printk_get_console_flush_type(&ft, false); cookie =3D console_srcu_read_lock(); - if (console_is_usable(con, console_srcu_read_flags(con)) && + if (ft.nbcon_atomic && + console_is_usable(con, console_srcu_read_flags(con)) && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false); } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 82440b1f0d1e..3fbe27551f75 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -472,7 +472,7 @@ bool have_legacy_console; * 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, @@ -482,6 +482,9 @@ static bool have_nbcon_console; */ bool have_boot_console; =20 +/* See printk_legacy_allow_panic_sync() for details. */ +bool legacy_allow_panic_sync; + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -2321,8 +2324,6 @@ 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 @@ -2330,9 +2331,13 @@ 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 && !in_nmi()) { + printk_get_console_flush_type(&ft, false); + + if (ft.legacy_direct && !in_nmi()) { if (console_trylock()) console_unlock(); } @@ -2342,7 +2347,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; + struct console_flush_type ft; + bool defer_legacy =3D false; int printed_len; =20 /* Suppress unimportant messages after panic happens */ @@ -2360,41 +2366,19 @@ asmlinkage int vprintk_emit(int facility, int level, if (level =3D=3D LOGLEVEL_SCHED) { level =3D LOGLEVEL_DEFAULT; /* If called from the scheduler, we can not call up(). */ - 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 (have_nbcon_console && !have_boot_console) { - bool is_panic_context =3D this_cpu_in_panic(); + printk_get_console_flush_type(&ft, false); =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. - */ - if (is_panic_context) - do_trylock_unlock &=3D legacy_allow_panic_sync; + if (ft.nbcon_atomic) + nbcon_atomic_flush_pending(); =20 - /* - * There are situations where nbcon atomic printing should - * happen in the printk() caller context: - * - * - When this CPU is in panic. - * - * Note that if boot consoles are registered, the console - * lock/unlock dance must be relied upon instead because nbcon - * consoles cannot print simultaneously with boot consoles. - */ - if (is_panic_context) - nbcon_atomic_flush_pending(); - } - - if (do_trylock_unlock) { + if (!defer_legacy && ft.legacy_direct) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2409,22 +2393,17 @@ asmlinkage int vprintk_emit(int facility, int level, * semaphore. The release will print out buffers. With the * spinning variant, this context tries to take over the * printing from another printing context. - * - * Skip it in EMERGENCY priority. The console will be - * explicitly flushed when exiting the emergency section. */ - if (nbcon_get_default_prio() !=3D NBCON_PRIO_EMERGENCY) { - if (console_trylock_spinning()) - console_unlock(); - } + if (console_trylock_spinning()) + console_unlock(); =20 preempt_enable(); } =20 - if (do_trylock_unlock) - wake_up_klogd(); - else + if ((defer_legacy && ft.legacy_direct) || ft.legacy_offload) defer_console_output(); + else + wake_up_klogd(); =20 return printed_len; } @@ -2728,10 +2707,15 @@ 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, false); + + if (ft.legacy_direct) { + if (console_trylock()) + console_unlock(); + } } return 0; } @@ -3256,6 +3240,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 @@ -3279,9 +3264,12 @@ void console_flush_on_panic(enum con_flush_mode mode) if (mode =3D=3D CONSOLE_REPLAY_ALL) __console_rewind_all(); =20 - nbcon_atomic_flush_pending(); + printk_get_console_flush_type(&ft, false); + + if (ft.nbcon_atomic) + nbcon_atomic_flush_pending(); =20 - if (printing_via_unlock) + if (ft.legacy_direct) console_flush_all(false, &next_seq, &handover); } =20 @@ -3943,6 +3931,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; @@ -3959,8 +3948,10 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre =20 seq =3D prb_next_reserve_seq(prb); =20 + printk_get_console_flush_type(&ft, false); + /* Flush the consoles so that records up to @seq are printed. */ - if (printing_via_unlock) { + if (ft.legacy_direct) { console_lock(); console_unlock(); } @@ -3968,18 +3959,19 @@ static bool __pr_flush(struct console *con, int tim= eout_ms, bool reset_on_progre for (;;) { unsigned long begin_jiffies; unsigned long slept_jiffies; - bool use_console_lock =3D printing_via_unlock; + + printk_get_console_flush_type(&ft, false); =20 /* - * Ensure the compiler does not optimize @use_console_lock to - * be @printing_via_unlock since the latter can change at any + * Ensure the compiler is re-reading the various state + * variables for setting @ft since they can change at any * time. */ barrier(); =20 diff =3D 0; =20 - if (use_console_lock) { + if (ft.legacy_direct) { /* * Hold the console_lock to guarantee safe access to * console->seq. Releasing console_lock flushes more @@ -4007,7 +3999,16 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre if (flags & CON_NBCON) { printk_seq =3D nbcon_seq_read(c); } else { - WARN_ON_ONCE(!use_console_lock); + /* + * Warn if this is a usable legacy console but + * the console lock was not taken because it + * is not allowed to do the direct printing + * when the console lock is released. Without + * the console lock, reading console->seq is + * unsafe. + */ + WARN_ON_ONCE(!ft.legacy_direct); + printk_seq =3D c->seq; } =20 @@ -4019,7 +4020,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre if (diff !=3D last_diff && reset_on_progress) remaining_jiffies =3D timeout_jiffies; =20 - if (use_console_lock) + if (ft.legacy_direct) console_unlock(); =20 /* Note: @diff is 0 if there are no usable consoles. */ @@ -4086,6 +4087,8 @@ static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_= work) =3D =20 static void __wake_up_klogd(int val) { + struct console_flush_type ft; + if (!printk_percpu_data_ready()) return; =20 @@ -4110,7 +4113,8 @@ static void __wake_up_klogd(int val) * registered legacy console when writing the message about it * being enabled. */ - if (!printing_via_unlock) + printk_get_console_flush_type(&ft, true); + if (!ft.legacy_offload) val &=3D ~PRINTK_PENDING_OUTPUT; =20 if (val) { --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 BB28C16F26D for ; Mon, 22 Jul 2024 17:19: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=1721668792; cv=none; b=fvyD55v0rxCRFwDAtde7Axoens+6a4e8KjM+SwoTDD8sm2eX2K+9bqF2OE7+6myRMCc+LaQvqlbAq0jwR5Fpg1fjQuutG86fzvicXQM/HDr0vfOkxFEkF0MZWYndRNU0FEFgCcGIYgrKJv5ZHGn9RQGtzW9+MGxoZlq2/HCq9j0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668792; c=relaxed/simple; bh=YawcJYPNd32k3XMh42rCyY3UosJYYeCPwQMuyikeX4c=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=pJhi2FT8Ba1hnw7bnBhEBAcIItt/seVVcNRIhL+axzex+vzJqHx2VYnsiGcOBtbEjyXxMePdNLAu1MNdJgjJ0TTA6BjkEuR9avjj+i7Cpe4ztyyo+l9XNEgog4cqT0P+TtixzSIts3X4u6sSMsuoRBUq6huu2ggoPkXp1vFrDIM= 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=f081GcMy; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DT8lEzvc; 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="f081GcMy"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DT8lEzvc" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668782; 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=C2c1xVI1G9qKeLg5Cn/WeTC9NVB748aI4bpBGv+D/NU=; b=f081GcMyF6rwHpTpW3bACjwXvwHitcwWkgteUVWDMzlXYG/2W52o0g2/4a30Utiq+YUUAJ dW8wUJeIKUZOZGztS6fLpOGCTm8LpfmbdQPXLVxOStnfwy2KjFuK9YjK0Wg2cJ8iySCFT5 0FK+O8to8ZTTc6N52kuFrX+Er19Uac9CUgw6XOCQf416ei4WP+DMv1dBUsayvmypbWVvvr tMxqcMRAi523IFBUnyvVmNOtHVs28nI+WaYbiDwQ78e2e5pDxrFGqMnOxrZYYgXs+Xlmfi jh6V2laSD2W+PGEDFSqlGb8YoMf5+7ddjvXR0fiAomrwAtD3FDnuCMWpiVzzjw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668782; 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=C2c1xVI1G9qKeLg5Cn/WeTC9NVB748aI4bpBGv+D/NU=; b=DT8lEzvcOx7LLAtXfplQF8sDmnyYVZ3ZVmnDbJPXERGEGfuDTy4XSp+gpqZXL7M3lRlOfB cij8rYBi9MiYxCAw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v3 08/19] printk: nbcon: Add context to usable() and emit() Date: Mon, 22 Jul 2024 19:25:28 +0206 Message-Id: <20240722171939.3349410-9-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 will have two callbacks to be used for different contexts. In order to determine if an nbcon console is usable, console_is_usable() must know if it is a context that will need to use the optional write_atomic() callback. Also, nbcon_emit_next_record() must know which callback it needs to call. Add an extra parameter @use_atomic to console_is_usable() and nbcon_emit_next_record() to specify this. Since so far only the write_atomic() callback exists, @use_atomic is set to true for all call sites. For legacy consoles, @use_atomic is not used. Signed-off-by: John Ogness --- include/linux/console.h | 2 +- kernel/printk/internal.h | 8 +++++--- kernel/printk/nbcon.c | 25 +++++++++++++++++++------ kernel/printk/printk.c | 6 +++--- 4 files changed, 28 insertions(+), 13 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 38ef6e64da19..5e9285e886f5 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -349,7 +349,7 @@ struct console { /** * @write_atomic: * - * NBCON callback to write out text in any context. + * 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. diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index ccdb81dc18f0..8211623aee9b 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -98,7 +98,7 @@ bool nbcon_legacy_emit_next_record(struct console *con, b= ool *handover, * which can also play a role in deciding if @con can be used to print * records. */ -static inline bool console_is_usable(struct console *con, short flags) +static inline bool console_is_usable(struct console *con, short flags, boo= l use_atomic) { if (!(flags & CON_ENABLED)) return false; @@ -107,7 +107,8 @@ static inline bool console_is_usable(struct console *co= n, short flags) return false; =20 if (flags & CON_NBCON) { - if (!con->write_atomic) + /* The write_atomic() callback is optional. */ + if (use_atomic && !con->write_atomic) return false; } else { if (!con->write) @@ -149,7 +150,8 @@ 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; } +static inline bool console_is_usable(struct console *con, short flags, + bool use_atomic) { return false; } =20 #endif /* CONFIG_PRINTK */ =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 917022105b8b..ae9cd6d0b740 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -914,6 +914,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); /** * nbcon_emit_next_record - Emit a record in the acquired context * @wctxt: The write context that will be handed to the write function + * @use_atomic: True if the write_atomic() callback is to be used * * Return: True if this context still owns the console. False if * ownership was handed over or taken. @@ -927,7 +928,7 @@ EXPORT_SYMBOL_GPL(nbcon_reacquire_nobuf); * When true is returned, @wctxt->ctxt.backlog indicates whether there are * still records pending in the ringbuffer, */ -static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) +static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool= use_atomic) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); struct console *con =3D ctxt->console; @@ -938,6 +939,18 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) unsigned long con_dropped; unsigned long dropped; =20 + /* + * This function should never be called for consoles that have not + * implemented the necessary callback for writing: i.e. legacy + * consoles and, when atomic, nbcon consoles with no write_atomic(). + * Handle it as if ownership was lost and try to continue. + */ + if (WARN_ON_ONCE((use_atomic && !con->write_atomic) || + !(console_srcu_read_flags(con) & CON_NBCON))) { + nbcon_context_release(ctxt); + return false; + } + /* * The printk buffers are filled within an unsafe section. This * prevents NBCON_PRIO_NORMAL and NBCON_PRIO_EMERGENCY from @@ -972,7 +985,7 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt) /* Initialize the write context for driver callbacks. */ nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_le= n); =20 - if (con->write_atomic) { + if (use_atomic) { con->write_atomic(con, wctxt); } else { /* @@ -1104,7 +1117,7 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_= context *wctxt) * The higher priority printing context takes over responsibility * to print the pending records. */ - if (!nbcon_emit_next_record(wctxt)) + if (!nbcon_emit_next_record(wctxt, true)) return false; =20 nbcon_context_release(ctxt); @@ -1205,7 +1218,7 @@ static int __nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq, * handed over or taken over. In both cases the context is no * longer valid. */ - if (!nbcon_emit_next_record(&wctxt)) + if (!nbcon_emit_next_record(&wctxt, true)) return -EAGAIN; =20 if (!ctxt->backlog) { @@ -1294,7 +1307,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq= , bool allow_unsafe_takeove if (!(flags & CON_NBCON)) continue; =20 - if (!console_is_usable(con, flags)) + if (!console_is_usable(con, flags, true)) continue; =20 if (nbcon_seq_read(con) >=3D stop_seq) @@ -1552,7 +1565,7 @@ void nbcon_device_release(struct console *con) printk_get_console_flush_type(&ft, false); cookie =3D console_srcu_read_lock(); if (ft.nbcon_atomic && - console_is_usable(con, console_srcu_read_flags(con)) && + console_is_usable(con, console_srcu_read_flags(con), true) && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false); } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 3fbe27551f75..e5e575384b3c 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3018,7 +3018,7 @@ static bool console_flush_all(bool do_cond_resched, u= 64 *next_seq, bool *handove u64 printk_seq; bool progress; =20 - if (!console_is_usable(con, flags)) + if (!console_is_usable(con, flags, true)) continue; any_usable =3D true; =20 @@ -3717,7 +3717,7 @@ static int unregister_console_locked(struct console *= console) =20 if (!console_is_registered_locked(console)) res =3D -ENODEV; - else if (console_is_usable(console, console->flags)) + else if (console_is_usable(console, console->flags, true)) __pr_flush(console, 1000, true); =20 /* Disable it unconditionally */ @@ -3993,7 +3993,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, flags)) + if (!console_is_usable(c, flags, true)) continue; =20 if (flags & CON_NBCON) { --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 DC07E16F839 for ; Mon, 22 Jul 2024 17:19: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=1721668794; cv=none; b=TVE0ZwLsu2J848CoNint0NwF081WYcRsN8/Sb2b2I5C/33JyRw50TM0zstupLKGQSQhqYC+4gathS5kucI0FW12fmK9ooHGPUwkNfuC+JdeaMVQqxeSnrcRz5G5KRcVdK6ACExgQkdaRVUnzyECOvrQyMMlLRoK/7r5k1ZniFOk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668794; c=relaxed/simple; bh=cWen4vYnz724hLmKR7UFhL+NUHjyCz1iTl7++hxKFz0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Py0ds6bHV7KQoU9/NWuH6xtZBvwRy7NADgFCi2TW60wmME6iYAIoUZ9sRv6KS+g8ji4SaASL+uawDuTki3JVKIOehFRHOk5w2Czx3T1wkf0cCqWX2dHWmzSnf6qipLjgmk9GciOIllOO3D4MkQszL8TS7PqimB5yRIpy9Q+9OD8= 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=X5VafPi0; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GnZt7jKP; 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="X5VafPi0"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GnZt7jKP" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668783; 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=17Bi15EZIim7Q8Yy+Y73cfLP9CjFh04+t2xjfxlTuCY=; b=X5VafPi0vSky+dlU56f3f+06nEMxPBMms2pvF8Zx21Vg1mSEhXoQaJPo2xNah/9GDRGzpJ IrSNTHyx+epkYQPxr5lVlAj8u/hyjAgKstMSpVFHvIe11i3T85B+wLXIxN+D4W5ALBaEvf VI5SAwrwKXr4UneM/X4kj+TPnOeAo1sullRctVRIjaHqY6YEn7n+bWJFQ8Z7ULYCQdYJMz 5aQukSY3ex137YVUj4engJ18sMEjG803BsOQJlgIHXILoiIrLvVIe4rJ7R9IyYs77JkBSz ymIu1ibV3t3zfeRoK8SB8hOca/9PD4Wm/GJnLNmEpoNlvcpJ00gW7OyE2zEpvg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668783; 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=17Bi15EZIim7Q8Yy+Y73cfLP9CjFh04+t2xjfxlTuCY=; b=GnZt7jKPcsXUSm4Ytyk7Vd3StG6TinX7MkA2HcwJ8mMoN1Zl1XyKb//BtlFGD7GJMmbNbQ siOBKDoiRzOcsACw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v3 09/19] printk: nbcon: Introduce printer kthreads Date: Mon, 22 Jul 2024 19:25:29 +0206 Message-Id: <20240722171939.3349410-10-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 the main implementation for running a printer kthread per nbcon console that is takeover/handover aware. This includes: - new mandatory write_thread() callback - kthread creation - kthread main printing loop - kthread wakeup mechanism - kthread shutdown kthread creation is a bit tricky because consoles may register before kthreads can be created. In such cases, registration will succeed, even though no kthread exists. Once kthreads can be created, an early_initcall will set @printk_kthreads_ready. If there are no registered boot consoles, the early_initcall creates the kthreads for all registered nbcon consoles. If kthread creation fails, the related console is unregistered. If there are registered boot consoles when @printk_kthreads_ready is set, no kthreads are created until the final boot console unregisters. Once kthread creation finally occurs, @printk_kthreads_running is set so that the system knows kthreads are available for all registered nbcon consoles. If @printk_kthreads_running is already set when the console is registering, the kthread is created during registration. If kthread creation fails, the registration will fail. Until @printk_kthreads_running is set, console printing occurs directly via the console_lock. kthread shutdown on system shutdown/reboot is necessary to ensure the printer kthreads finish their printing so that the system can cleanly transition back to direct printing via the console_lock in order to reliably push out the final shutdown/reboot messages. @printk_kthreads_running is cleared before shutting down the individual kthreads. The kthread uses a new mandatory write_thread() callback that is called with both device_lock() and the console context acquired. The console ownership handling is necessary for synchronization against write_atomic() which is synchronized only via the console context ownership. The device_lock() serializes acquiring the console context with NBCON_PRIO_NORMAL. It is needed in case the device_lock() does not disable preemption. It prevents the following race: CPU0 CPU1 [ task A ] nbcon_context_try_acquire() # success with NORMAL prio # .unsafe =3D=3D false; // safe for takeover [ schedule: task A -> B ] WARN_ON() nbcon_atomic_flush_pending() nbcon_context_try_acquire() # success with EMERGENCY prio # flushing nbcon_context_release() # HERE: con->nbcon_state is free # to take by anyone !!! nbcon_context_try_acquire() # success with NORMAL prio [ task B ] [ schedule: task B -> A ] nbcon_enter_unsafe() nbcon_context_can_proceed() BUG: nbcon_context_can_proceed() returns "true" because the console is owned by a context on CPU0 with NBCON_PRIO_NORMAL. But it should return "false". The console is owned by a context from task B and we do the check in a context from task A. Note that with these changes, the printer kthreads do not yet take over full responsibility for nbcon printing during normal operation. These changes only focus on the lifecycle of the kthreads. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- include/linux/console.h | 40 +++++++ kernel/printk/internal.h | 27 +++++ kernel/printk/nbcon.c | 244 +++++++++++++++++++++++++++++++++++++-- kernel/printk/printk.c | 108 +++++++++++++++++ 4 files changed, 409 insertions(+), 10 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 5e9285e886f5..b8fb9fb24cbf 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -16,7 +16,9 @@ =20 #include #include +#include #include +#include #include #include =20 @@ -324,6 +326,9 @@ struct nbcon_write_context { * @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 + * @kthread: Printer kthread for this console + * @rcuwait: RCU-safe wait object for @kthread waking + * @irq_work: Defer @kthread waking to IRQ work context */ struct console { char name[16]; @@ -377,6 +382,37 @@ struct console { */ void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); =20 + /** + * @write_thread: + * + * NBCON callback to write out text in task context. + * + * This callback must be called only in task context with both + * device_lock() and the nbcon console acquired with + * NBCON_PRIO_NORMAL. + * + * The same rules for console ownership verification and unsafe + * sections handling applies as with write_atomic(). + * + * The console ownership handling is necessary for synchronization + * against write_atomic() which is synchronized only via the context. + * + * The device_lock() provides the primary serialization for operations + * on the device. It might be as relaxed (mutex)[*] or as tight + * (disabled preemption and interrupts) as needed. It allows + * the kthread to operate in the least restrictive mode[**]. + * + * [*] Standalone nbcon_context_try_acquire() is not safe with + * the preemption enabled, see nbcon_owner_matches(). But it + * can be safe when always called in the preemptive context + * under the device_lock(). + * + * [**] The device_lock() makes sure that nbcon_context_try_acquire() + * would never need to spin which is important especially with + * PREEMPT_RT. + */ + void (*write_thread)(struct console *con, struct nbcon_write_context *wct= xt); + /** * @device_lock: * @@ -423,7 +459,11 @@ struct console { atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct nbcon_context __private nbcon_device_ctxt; + struct printk_buffers *pbufs; + struct task_struct *kthread; + struct rcuwait rcuwait; + struct irq_work irq_work; }; =20 #ifdef CONFIG_LOCKDEP diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 8211623aee9b..12605e0aff11 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 printk_kthreads_running; =20 __printf(4, 0) int vprintk_store(int facility, int level, @@ -91,6 +92,9 @@ 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); +bool nbcon_kthread_create(struct console *con); +void nbcon_kthread_stop(struct console *con); +void nbcon_wake_kthreads(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -126,12 +130,34 @@ static inline bool console_is_usable(struct console *= con, short flags, bool use_ return true; } =20 +/** + * nbcon_kthread_wake - Wake up a console printing thread + * @con: Console to operate on + */ +static inline void nbcon_kthread_wake(struct console *con) +{ + /* + * Guarantee any new records can be seen by tasks preparing to wait + * before this context checks if the rcuwait is empty. + * + * The full memory barrier in rcuwait_wake_up() pairs with the full + * memory barrier within set_current_state() of + * ___rcuwait_wait_event(), which is called after prepare_to_rcuwait() + * adds the waiter but before it has checked the wait condition. + * + * This pairs with nbcon_kthread_func:A. + */ + rcuwait_wake_up(&con->rcuwait); /* LMM(nbcon_kthread_wake:A) */ +} + #else =20 #define PRINTK_PREFIX_MAX 0 #define PRINTK_MESSAGE_MAX 0 #define PRINTKRB_RECORD_MAX 0 =20 +#define printk_kthreads_running (false) + /* * In !PRINTK builds we still export console_sem * semaphore and some of console functions (console_unlock()/etc.), so @@ -149,6 +175,7 @@ static inline enum nbcon_prio nbcon_get_default_prio(vo= id) { return NBCON_PRIO_N 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; } +static inline void nbcon_kthread_wake(struct console *con) { } =20 static inline bool console_is_usable(struct console *con, short flags, bool use_atomic) { return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index ae9cd6d0b740..69cecf97bf24 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -10,6 +10,7 @@ #include #include #include +#include #include #include #include @@ -985,17 +986,10 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt, bool use_a /* Initialize the write context for driver callbacks. */ nbcon_write_context_set_buf(wctxt, &pmsg.pbufs->outbuf[0], pmsg.outbuf_le= n); =20 - if (use_atomic) { + if (use_atomic) con->write_atomic(con, wctxt); - } else { - /* - * This function should never be called for legacy consoles. - * Handle it as if ownership was lost and try to continue. - */ - WARN_ON_ONCE(1); - nbcon_context_release(ctxt); - return false; - } + else + con->write_thread(con, wctxt); =20 if (!wctxt->outbuf) { /* @@ -1031,6 +1025,219 @@ static bool nbcon_emit_next_record(struct nbcon_wri= te_context *wctxt, bool use_a return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * nbcon_kthread_should_wakeup - Check whether a printer thread should wak= eup + * @con: Console to operate on + * @ctxt: The nbcon context from nbcon_context_try_acquire() + * + * Return: True if the thread should shutdown or if the console is + * allowed to print and a record is available. False otherwise. + * + * After the thread wakes up, it must first check if it should shutdown be= fore + * attempting any printing. + */ +static bool nbcon_kthread_should_wakeup(struct console *con, struct nbcon_= context *ctxt) +{ + bool ret =3D false; + short flags; + int cookie; + + if (kthread_should_stop()) + return true; + + cookie =3D console_srcu_read_lock(); + + flags =3D console_srcu_read_flags(con); + if (console_is_usable(con, flags, false)) { + /* Bring the sequence in @ctxt up to date */ + ctxt->seq =3D nbcon_seq_read(con); + + ret =3D prb_read_valid(prb, ctxt->seq, NULL); + } + + console_srcu_read_unlock(cookie); + return ret; +} + +/** + * nbcon_kthread_func - The printer thread function + * @__console: Console to operate on + * + * Return: 0 + */ +static int nbcon_kthread_func(void *__console) +{ + struct console *con =3D __console; + struct nbcon_write_context wctxt =3D { + .ctxt.console =3D con, + .ctxt.prio =3D NBCON_PRIO_NORMAL, + }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + short con_flags; + bool backlog; + int cookie; + +wait_for_event: + /* + * Guarantee this task is visible on the rcuwait before + * checking the wake condition. + * + * The full memory barrier within set_current_state() of + * ___rcuwait_wait_event() pairs with the full memory + * barrier within rcuwait_has_sleeper(). + * + * This pairs with rcuwait_has_sleeper:A and nbcon_kthread_wake:A. + */ + rcuwait_wait_event(&con->rcuwait, + nbcon_kthread_should_wakeup(con, ctxt), + TASK_INTERRUPTIBLE); /* LMM(nbcon_kthread_func:A) */ + + do { + if (kthread_should_stop()) + return 0; + + backlog =3D false; + + /* + * Keep the srcu read lock around the entire operation so that + * synchronize_srcu() can guarantee that the kthread stopped + * or suspended printing. + */ + cookie =3D console_srcu_read_lock(); + + con_flags =3D console_srcu_read_flags(con); + + if (console_is_usable(con, con_flags, false)) { + unsigned long lock_flags; + + con->device_lock(con, &lock_flags); + + /* + * Ensure this stays on the CPU to make handover and + * takeover possible. + */ + cant_migrate(); + + if (nbcon_context_try_acquire(ctxt)) { + /* + * If the emit fails, this context is no + * longer the owner. + */ + if (nbcon_emit_next_record(&wctxt, false)) { + nbcon_context_release(ctxt); + backlog =3D ctxt->backlog; + } + } + + con->device_unlock(con, lock_flags); + } + + console_srcu_read_unlock(cookie); + + cond_resched(); + + } while (backlog); + + goto wait_for_event; +} + +/** + * nbcon_irq_work - irq work to wake console printer thread + * @irq_work: The irq work to operate on + */ +static void nbcon_irq_work(struct irq_work *irq_work) +{ + struct console *con =3D container_of(irq_work, struct console, irq_work); + + nbcon_kthread_wake(con); +} + +static inline bool rcuwait_has_sleeper(struct rcuwait *w) +{ + /* + * Guarantee any new records can be seen by tasks preparing to wait + * before this context checks if the rcuwait is empty. + * + * This full memory barrier pairs with the full memory barrier within + * set_current_state() of ___rcuwait_wait_event(), which is called + * after prepare_to_rcuwait() adds the waiter but before it has + * checked the wait condition. + * + * This pairs with nbcon_kthread_func:A. + */ + smp_mb(); /* LMM(rcuwait_has_sleeper:A) */ + return rcuwait_active(w); +} + +/** + * nbcon_wake_kthreads - Wake up printing threads using irq_work + */ +void nbcon_wake_kthreads(void) +{ + struct console *con; + int cookie; + + if (!printk_kthreads_running) + return; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + /* + * Only schedule irq_work if the printing thread is + * actively waiting. If not waiting, the thread will + * notice by itself that it has work to do. + */ + if (rcuwait_has_sleeper(&con->rcuwait)) + irq_work_queue(&con->irq_work); + } + console_srcu_read_unlock(cookie); +} + +/* + * nbcon_kthread_stop - Stop a console printer thread + * @con: Console to operate on + */ +void nbcon_kthread_stop(struct console *con) +{ + lockdep_assert_console_list_lock_held(); + + if (!con->kthread) + return; + + kthread_stop(con->kthread); + con->kthread =3D NULL; +} + +/** + * nbcon_kthread_create - Create a console printer thread + * @con: Console to operate on + * + * Return: True if the kthread was started or already exists. + * Otherwise false and @con must not be registered. + * + * If @con was already registered, it must be unregistered before + * the global state variable @printk_kthreads_running can be set. + */ +bool nbcon_kthread_create(struct console *con) +{ + struct task_struct *kt; + + lockdep_assert_console_list_lock_held(); + + if (con->kthread) + return true; + + kt =3D kthread_run(nbcon_kthread_func, con, "pr/%s%d", con->name, con->in= dex); + if (WARN_ON(IS_ERR(kt))) { + con_printk(KERN_ERR, con, "failed to start printing thread\n"); + return false; + } + + con->kthread =3D kt; + + return true; +} + /* 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; @@ -1466,6 +1673,13 @@ bool nbcon_alloc(struct console *con) { struct nbcon_state state =3D { }; =20 + if (!con->write_thread) { + con_printk(KERN_ERR, con, "no write_thread() callback provided\n"); + return false; + } + + rcuwait_init(&con->rcuwait); + init_irq_work(&con->irq_work, nbcon_irq_work); nbcon_state_set(con, &state); =20 if (con->flags & CON_BOOT) { @@ -1481,6 +1695,13 @@ bool nbcon_alloc(struct console *con) con_printk(KERN_ERR, con, "failed to allocate printing buffer\n"); return false; } + + if (printk_kthreads_running) { + if (!nbcon_kthread_create(con)) { + kfree(con->pbufs); + return false; + } + } } =20 return true; @@ -1494,6 +1715,9 @@ void nbcon_free(struct console *con) { struct nbcon_state state =3D { }; =20 + if (printk_kthreads_running) + nbcon_kthread_stop(con); + nbcon_state_set(con, &state); =20 /* Boot consoles share global printk buffers. */ diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e5e575384b3c..cf0b35eaf047 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include #include @@ -493,6 +494,9 @@ static u64 syslog_seq; static size_t syslog_partial; static bool syslog_time; =20 +/* True when _all_ printer threads are available for printing. */ +bool printk_kthreads_running; + struct latched_seq { seqcount_latch_t latch; u64 val[2]; @@ -2974,6 +2978,8 @@ static bool console_emit_next_record(struct console *= con, bool *handover, int co return false; } =20 +static inline void printk_kthreads_check_locked(void) { } + #endif /* CONFIG_PRINTK */ =20 /* @@ -3334,6 +3340,102 @@ void console_start(struct console *console) } EXPORT_SYMBOL(console_start); =20 +#ifdef CONFIG_PRINTK +static int unregister_console_locked(struct console *console); + +/* True when system boot is far enough to create printer threads. */ +static bool printk_kthreads_ready __ro_after_init; + +/** + * printk_kthreads_shutdown - shutdown all threaded printers + * + * On system shutdown all threaded printers are stopped. This allows printk + * to transition back to atomic printing, thus providing a robust mechanism + * for the final shutdown/reboot messages to be output. + */ +static void printk_kthreads_shutdown(void) +{ + struct console *con; + + console_list_lock(); + if (printk_kthreads_running) { + printk_kthreads_running =3D false; + + for_each_console(con) { + if (con->flags & CON_NBCON) + nbcon_kthread_stop(con); + } + + /* + * The threads may have been stopped while printing a + * backlog. Flush any records left over. + */ + nbcon_atomic_flush_pending(); + } + console_list_unlock(); +} + +static struct syscore_ops printk_syscore_ops =3D { + .shutdown =3D printk_kthreads_shutdown, +}; + +/* + * If appropriate, start nbcon kthreads and set @printk_kthreads_running. + * If any kthreads fail to start, those consoles are unregistered. + * + * Must be called under console_list_lock(). + */ +static void printk_kthreads_check_locked(void) +{ + struct hlist_node *tmp; + struct console *con; + + lockdep_assert_console_list_lock_held(); + + if (!printk_kthreads_ready) + return; + + /* + * Printer threads cannot be started as long as any boot console is + * registered because there is no way to synchronize the hardware + * registers between boot console code and regular console code. + * It can only be known that there will be no new boot consoles when + * an nbcon console is registered. + */ + if (have_boot_console || !have_nbcon_console) { + /* Clear flag in case all nbcon consoles unregistered. */ + printk_kthreads_running =3D false; + return; + } + + if (printk_kthreads_running) + return; + + hlist_for_each_entry_safe(con, tmp, &console_list, node) { + if (!(con->flags & CON_NBCON)) + continue; + + if (!nbcon_kthread_create(con)) + unregister_console_locked(con); + } + + printk_kthreads_running =3D true; +} + +static int __init printk_set_kthreads_ready(void) +{ + register_syscore_ops(&printk_syscore_ops); + + console_list_lock(); + printk_kthreads_ready =3D true; + printk_kthreads_check_locked(); + console_list_unlock(); + + return 0; +} +early_initcall(printk_set_kthreads_ready); +#endif /* CONFIG_PRINTK */ + static int __read_mostly keep_bootcon; =20 static int __init keep_bootcon_setup(char *str) @@ -3689,6 +3791,9 @@ void register_console(struct console *newcon) unregister_console_locked(con); } } + + /* Changed console list, may require printer threads to start/stop. */ + printk_kthreads_check_locked(); unlock: console_list_unlock(); } @@ -3785,6 +3890,9 @@ static int unregister_console_locked(struct console *= console) if (!found_nbcon_con) have_nbcon_console =3D found_nbcon_con; =20 + /* Changed console list, may require printer threads to start/stop. */ + printk_kthreads_check_locked(); + return res; } =20 --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 A6F7F16F82C for ; Mon, 22 Jul 2024 17:19: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=1721668796; cv=none; b=K9dCnAwdREd96TZbspNsPR5H+WLRGtZLzlHhpgyPeCgjicT4QJxrNFwYcH9r6czI7e9m+iF/eC5gDM3Po9M5tdbmYJ2nuxIc8ZYRPGW4A4zBgC370t5HS5DzbVWbGZbhX2u2B0b3pWQJtCPKAbD6ZofDODk7A5asv1JCsH83fIY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668796; c=relaxed/simple; bh=0pWo/lQazb7NN5gzVmtuRvxoOY+AY0WMnuqYiPdi0fo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZzrPYganU6aO/KJMA42GP1MfEzOJbtCKB0fAnBaiolZ4lAHWwbPRaL0xi1aY8D6QY6UYkwqnl2vkM4a8tzd6nxslYKN6gMaPtCe5WSmWLwh7CmVQsjaP3BBhtkc8Ygp1/yoEz0hZ+NFGHLtVgFtUHuNvPnduaZzOg0wujOd2hNw= 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=nr1kIEKo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=EgmcqHaa; 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="nr1kIEKo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="EgmcqHaa" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668783; 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=HthQAGS/lHQGcobZEvlVi78Lh6Enw1uHeqNlN4mMXhw=; b=nr1kIEKoUJsm6Zc9pErp/I2ZBbg6dtF00n8Yd/MooTjNF60SVbrkyuAj+YfLxWpfmnoUwA turV1WintKW3nq2Ay1eRZreXhVm6nOpo4R/WSaSrE+LV+KxG0BatgMyjisJioXEnkjE5od y/Ah7Awq4bu8rrS0F6IKEaHfZCSWB09f4pBRRUhj9HerWZVxFGK3eslD5/+5nzu59tm/2N 0USD9Jkuta3P5CZ+PMvAKcrfP8q2gT6rZ9qF8yIahLJUaeSZKGG/uHV8iqM7CkXNf/wso2 oi2Ns2qhL1M4saUgWRP1O7GYS72dubePF4DM0FFrmyetiTTm+lr/9dtApWe+6w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668783; 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=HthQAGS/lHQGcobZEvlVi78Lh6Enw1uHeqNlN4mMXhw=; b=EgmcqHaaM6wI/5fb4CMBVq71nSOALcVKhtpLnHdVQMopBKXiPlu/JCpJD0Lhb8IH7RouHA 4XEvf2ucp9DtqKCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 10/19] printk: nbcon: Use thread callback if in task context for legacy Date: Mon, 22 Jul 2024 19:25:30 +0206 Message-Id: <20240722171939.3349410-11-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" When printing via console_lock, the write_atomic() callback is used for nbcon consoles. However, if it is known that the current context is a task context, the write_thread() callback can be used instead. Using write_thread() instead of write_atomic() helps to reduce large disabled preemption regions when the device_lock does not disable preemption. This is mainly a preparatory change to allow avoiding write_atomic() completely during normal operation if boot consoles are registered. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 4 +-- kernel/printk/nbcon.c | 54 +++++++++++++++++++++++++++++----------- kernel/printk/printk.c | 5 ++-- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 12605e0aff11..bb02788acc7c 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -91,7 +91,7 @@ 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); + int cookie, bool use_atomic); bool nbcon_kthread_create(struct console *con); void nbcon_kthread_stop(struct console *con); void nbcon_wake_kthreads(void); @@ -174,7 +174,7 @@ 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; } + int cookie, bool use_atomic) { return false; } static inline void nbcon_kthread_wake(struct console *con) { } =20 static inline bool console_is_usable(struct console *con, short flags, diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 69cecf97bf24..233ab8f90fef 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1294,9 +1294,10 @@ 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 + * nbcon_emit_one - Print one record for an nbcon console using the + * specified callback * @wctxt: An initialized write context struct to use for this context + * @use_atomic: True if the write_atomic() callback is to be used * * Return: True, when a record has been printed and there are still * pending records. The caller might want to continue flushing. @@ -1309,7 +1310,7 @@ enum nbcon_prio nbcon_get_default_prio(void) * 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) +static bool nbcon_emit_one(struct nbcon_write_context *wctxt, bool use_ato= mic) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); =20 @@ -1324,7 +1325,7 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_= context *wctxt) * The higher priority printing context takes over responsibility * to print the pending records. */ - if (!nbcon_emit_next_record(wctxt, true)) + if (!nbcon_emit_next_record(wctxt, use_atomic)) return false; =20 nbcon_context_release(ctxt); @@ -1341,6 +1342,13 @@ static bool nbcon_atomic_emit_one(struct nbcon_write= _context *wctxt) * both the console_lock and the SRCU read lock. Otherwise it * is set to false. * @cookie: The cookie from the SRCU read lock. + * @use_atomic: Set true when called in an atomic or unknown context. + * It affects which nbcon callback will be used: write_atomic() + * or write_thread(). + * + * When false, the write_thread() callback is used and would be + * called in a preemtible context unless disabled by the + * device_lock. The legacy handover is not allowed in this mode. * * Context: Any context except NMI. * Return: True, when a record has been printed and there are still @@ -1356,26 +1364,44 @@ static bool nbcon_atomic_emit_one(struct nbcon_writ= e_context *wctxt) * Essentially it is the nbcon version of console_emit_next_record(). */ bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, - int cookie) + int cookie, bool use_atomic) { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); unsigned long flags; bool progress; =20 - /* 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(); =20 - progress =3D nbcon_atomic_emit_one(&wctxt); + if (use_atomic) { + /* + * In an atomic or unknown context, use the same procedure as + * in console_emit_next_record(). It allows to handover. + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + stop_critical_timings(); + } else { + /* + * In task context, use the same procedure as in + * nbcon_kthread_func(). It might allow scheduling depending + * on the device_lock(). + */ + con->device_lock(con, &flags); + cant_migrate(); + } =20 - start_critical_timings(); - *handover =3D console_lock_spinning_disable_and_check(cookie); - printk_safe_exit_irqrestore(flags); + progress =3D nbcon_emit_one(&wctxt, use_atomic); + + if (use_atomic) { + start_critical_timings(); + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + } else { + con->device_unlock(con, flags); + *handover =3D false; + } =20 return progress; } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index cf0b35eaf047..71e946a8c5fa 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3024,12 +3024,13 @@ static bool console_flush_all(bool do_cond_resched,= u64 *next_seq, bool *handove u64 printk_seq; bool progress; =20 - if (!console_is_usable(con, flags, true)) + if (!console_is_usable(con, flags, !do_cond_resched)) continue; any_usable =3D true; =20 if (flags & CON_NBCON) { - progress =3D nbcon_legacy_emit_next_record(con, handover, cookie); + progress =3D nbcon_legacy_emit_next_record(con, handover, cookie, + !do_cond_resched); printk_seq =3D nbcon_seq_read(con); } else { progress =3D console_emit_next_record(con, handover, cookie); --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 A505E16F82B for ; Mon, 22 Jul 2024 17:19: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=1721668793; cv=none; b=U4pWSGSi87VFR19OPLUBjZDZLuCcwvFBUP1+qoamUKDABY2/8T4xV9a7H/npiQgorHCCCyVjhhoiT1qYN0W+PNYht3aTYilv8gPlt+n1O+9sfP2pDT6enBrJjzd0cWyBMrfeFlr3tA/Uo5eJIY5w086LeiyF7hb833UNAMkFsoI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668793; c=relaxed/simple; bh=8rZ+orOvhvkg3Xs7cSIelF6l3/TMCwIKMNzN05jG0hE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=a/wCCuBRyqGBuLfY0khjIKJlWoKzdf3p1Zwai2y/aEGEwsplo2Ka5viUynITHml0yF0agVGLnj6bztJdDDJxjr8tXuuBlH4VgBVTdfg9weFrHangDg+CDZW/PxjjxWLqo8XiuAUYeNFdhY6tLzvxn/LhZEdDz+/fowmud2VmSbk= 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=GCXuM4qA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ZGkz6TH9; 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="GCXuM4qA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ZGkz6TH9" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668783; 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=Wq5SsyqnFMbaCjD78xlUdl8Q0+6byHJFUpkZ0WBc8Uw=; b=GCXuM4qAg/bAX1oIQEUaod6+4xOvoFjIa/hChJ7T90yVODcBIudM0Vfw4weFSiv1UWwWQO zJqwDeJBiqvNHGql5D50M0YxZBAUyF0FR3Up1At8kSVcSZz+9cTr9P1eMR3dRwGLVCYlom +/kRMhycmqbjOrzwhZY8vytC5kjPDhFdQGofcdy9oMHCwZQw4kp2+RbPYu1OhTqWX94I9Q rmJzMC1uBoyCqJ4thOf/gjSAG9hwUKYQfcq98UC7VEglRCobfV4ucH26WoUJPKCudNVzSi ismduiaIPIaDS4jesjx1M6EpTjqhNx7t0cBdDoYpS1G2yciKsT/AgDKnHPfcVQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668783; 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=Wq5SsyqnFMbaCjD78xlUdl8Q0+6byHJFUpkZ0WBc8Uw=; b=ZGkz6TH9mm/Sqd0ihyXQ1PyFzuP42vyMX5iM2YjQqGELaiSK+qEx8ZiHhHnuLiJzm3J306 gxJvDImfGf7o6JDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 11/19] printk: nbcon: Rely on kthreads for normal operation Date: Mon, 22 Jul 2024 19:25:31 +0206 Message-Id: <20240722171939.3349410-12-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" Once the kthread is running and available (i.e. @printk_kthreads_running is set), the kthread becomes responsible for flushing any pending messages which are added in NBCON_PRIO_NORMAL context. Namely the legacy console_flush_all() and device_release() no longer flush the console. And nbcon_atomic_flush_pending() used by nbcon_cpu_emergency_exit() no longer flushes messages added after the emergency messages. The console context is safe when used by the kthread only when one of the following conditions are true: 1. Other caller acquires the console context with NBCON_PRIO_NORMAL with preemption disabled. It will release the context before rescheduling. 2. Other caller acquires the console context with NBCON_PRIO_NORMAL under the device_lock. 3. The kthread is the only context which acquires the console with NBCON_PRIO_NORMAL. This is satisfied for all atomic printing call sites: nbcon_legacy_emit_next_record() (#1) nbcon_atomic_flush_pending_con() (#1) nbcon_device_release() (#2) It is even double guaranteed when @printk_kthreads_running is set because then _only_ the kthread will print for NBCON_PRIO_NORMAL. (#3) Signed-off-by: John Ogness --- kernel/printk/internal.h | 6 +++++- kernel/printk/nbcon.c | 13 ++++++------ kernel/printk/printk.c | 46 +++++++++++++++++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 8 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index bb02788acc7c..66321836c3fe 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -190,11 +190,13 @@ extern bool legacy_allow_panic_sync; /** * struct console_flush_type - Define how to flush the consoles * @nbcon_atomic: Flush directly using nbcon_atomic() callback + * @nbcon_offload: Offload flush to printer thread * @legacy_direct: Call the legacy loop in this context * @legacy_offload: Offload the legacy loop into IRQ */ struct console_flush_type { bool nbcon_atomic; + bool nbcon_offload; bool legacy_direct; bool legacy_offload; }; @@ -220,7 +222,9 @@ static inline void printk_get_console_flush_type(struct= console_flush_type *ft, ft->legacy_direct =3D true; } =20 - if (have_nbcon_console && !have_boot_console) + if (printk_kthreads_running) + ft->nbcon_offload =3D true; + else if (have_nbcon_console && !have_boot_console) ft->nbcon_atomic =3D true; break; =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 233ab8f90fef..8cf9e9e8c6e4 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1511,10 +1511,10 @@ static void nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq, =20 /* * 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. + * context must flush those remaining records if the printer thread + * is not available do it. */ - printk_get_console_flush_type(&ft, false); + printk_get_console_flush_type(&ft, true); if (ft.nbcon_atomic && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { stop_seq =3D prb_next_reserve_seq(prb); @@ -1809,10 +1809,11 @@ void nbcon_device_release(struct console *con) =20 /* * 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. + * was locked if the printer thread is not available to do it. The + * console_srcu_read_lock must be taken to ensure the console is + * usable throughout flushing. */ - printk_get_console_flush_type(&ft, false); + printk_get_console_flush_type(&ft, true); cookie =3D console_srcu_read_lock(); if (ft.nbcon_atomic && console_is_usable(con, console_srcu_read_flags(con), true) && diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 71e946a8c5fa..620c02b069bc 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2382,6 +2382,9 @@ asmlinkage int vprintk_emit(int facility, int level, if (ft.nbcon_atomic) nbcon_atomic_flush_pending(); =20 + if (ft.nbcon_offload) + nbcon_wake_kthreads(); + if (!defer_legacy && ft.legacy_direct) { /* * The caller may be holding system-critical or @@ -2680,6 +2683,7 @@ void suspend_console(void) =20 void resume_console(void) { + struct console_flush_type ft; struct console *con; =20 if (!console_suspend_enabled) @@ -2697,6 +2701,10 @@ void resume_console(void) */ synchronize_srcu(&console_srcu); =20 + printk_get_console_flush_type(&ft, true); + if (ft.nbcon_offload) + nbcon_wake_kthreads(); + pr_flush(1000, true); } =20 @@ -3007,6 +3015,7 @@ static inline void printk_kthreads_check_locked(void)= { } */ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *h= andover) { + struct console_flush_type ft; bool any_usable =3D false; struct console *con; bool any_progress; @@ -3018,12 +3027,21 @@ static bool console_flush_all(bool do_cond_resched,= u64 *next_seq, bool *handove do { any_progress =3D false; =20 + printk_get_console_flush_type(&ft, true); + 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 + /* + * console_flush_all() is only for legacy consoles when + * the nbcon consoles have their printer threads. + */ + if ((flags & CON_NBCON) && ft.nbcon_offload) + continue; + if (!console_is_usable(con, flags, !do_cond_resched)) continue; any_usable =3D true; @@ -3334,9 +3352,28 @@ EXPORT_SYMBOL(console_stop); =20 void console_start(struct console *console) { + struct console_flush_type ft; + short flags; + int cookie; + console_list_lock(); console_srcu_write_flags(console, console->flags | CON_ENABLED); console_list_unlock(); + + /* + * Ensure that all SRCU list walks have completed. The related + * printing context must be able to see it is enabled so that + * it is guaranteed to wake up and resume printing. + */ + synchronize_srcu(&console_srcu); + + printk_get_console_flush_type(&ft, true); + cookie =3D console_srcu_read_lock(); + flags =3D console_srcu_read_flags(console); + if ((flags & CON_NBCON) && ft.nbcon_offload) + nbcon_kthread_wake(console); + console_srcu_read_unlock(cookie); + __pr_flush(console, 1000, true); } EXPORT_SYMBOL(console_start); @@ -4102,8 +4139,10 @@ static bool __pr_flush(struct console *con, int time= out_ms, bool reset_on_progre * that they make forward progress, so only increment * @diff for usable consoles. */ - if (!console_is_usable(c, flags, true)) + if (!console_is_usable(c, flags, true) && + !console_is_usable(c, flags, false)) { continue; + } =20 if (flags & CON_NBCON) { printk_seq =3D nbcon_seq_read(c); @@ -4605,8 +4644,13 @@ EXPORT_SYMBOL_GPL(kmsg_dump_rewind); */ void console_try_replay_all(void) { + struct console_flush_type ft; + + printk_get_console_flush_type(&ft, true); if (console_trylock()) { __console_rewind_all(); + if (ft.nbcon_offload) + nbcon_wake_kthreads(); /* Consoles are flushed as part of console_unlock(). */ console_unlock(); } --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 DC0EA16F83A for ; Mon, 22 Jul 2024 17:19: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=1721668795; cv=none; b=f88tJqtNuz61sERaCzS0HaMoFeSGvHPood07bMi+lcgXtwr29Ryxloj8RCLhwuLuBjg+3dQbgM49ZuThjn15fNU5L2ZZv79sUlLweXly/2jtrjguSdAwTJONjm8RVP0wViIPmNlr74WlGhUlL9yuAPT4JRBvvp4Gei2kJ7WEta0= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668795; c=relaxed/simple; bh=8CT2LEVHc07jzisDnWd0LQOE1K28cGvDJ6CINR5Lmmk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dc/EW7hq1PVm1IKqHq/T+9ApJMXJR6odgNWD6PlKLM+jmaa+b7KmcRg1raP4mm1NFpPH4PzAOZshAfwQ5MoxxFLPvesQQgzo4JtjlFG3q6dgEgxseA0nGOy5dEElGRuhMoBMBIDLDS6nU+WWt113lj5thler5ZhNcD3uJk7M6bY= 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=flsDpmlf; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ecCKxN+K; 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="flsDpmlf"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ecCKxN+K" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668784; 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=5Ol3S252804F7HDMU9xAB5UfdY7U+NwrMoiN/KMkNHk=; b=flsDpmlfbYyOVKvPp+jSsRaSGXeb8JVnERs8jrhHd/Hv/qfp4PUxZCBXhiUtbsZHimpJVI a60p09yZyOg1/qadd0MdbEtzW1QRSop2l18d6f8KfosvdkYx5acej2Soj3z26hGQwApmeg JRq+HsIuO02atJOqRWX1IeyLGRo21ory/LLC7TyOSumznH3O25CrtV4WGwJbRtU3TWumbB /T+b6QND/NfSo1aPFaz/VkNwTHpdWI6tv7BdmgCrV5+n59LgKKmrcrvHi1iooUyC5SRlYn SGi3pR2JTbS3qx1/mu4kgfwM2An7XHdwyljDaxuRNhFLzgnlO2FYzKS4RhYBmQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668784; 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=5Ol3S252804F7HDMU9xAB5UfdY7U+NwrMoiN/KMkNHk=; b=ecCKxN+KOVDUf9haQVJtJYsBuAGcc10iHlzpeHcEQArS3GHT4UpxHffSjzUK+Vkd83elMU 9sgBPZBftRxmadDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 12/19] printk: Provide helper for message prepending Date: Mon, 22 Jul 2024 19:25:32 +0206 Message-Id: <20240722171939.3349410-13-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 order to support prepending different texts to printk messages, split out the prepending code into a helper function. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 36 +++++++++++++++++++++++++----------- 1 file changed, 25 insertions(+), 11 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 620c02b069bc..89210a21cc50 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2790,30 +2790,31 @@ static void __console_unlock(void) #ifdef CONFIG_PRINTK =20 /* - * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". T= his - * is achieved by shifting the existing message over and inserting the dro= pped - * message. + * Prepend the message in @pmsg->pbufs->outbuf. This is achieved by shifti= ng + * the existing message over and inserting the scratchbuf message. * - * @pmsg is the printk message to prepend. - * - * @dropped is the dropped count to report in the dropped message. + * @pmsg is the original printk message. + * @fmt is the printf format of the message which will prepend the existin= g one. * - * If the message text in @pmsg->pbufs->outbuf does not have enough space = for - * the dropped message, the message text will be sufficiently truncated. + * If there is not enough space in @pmsg->pbufs->outbuf, the existing + * message text will be sufficiently truncated. * * If @pmsg->pbufs->outbuf is modified, @pmsg->outbuf_len is updated. */ -void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped) +__printf(2, 3) +static void console_prepend_message(struct printk_message *pmsg, const cha= r *fmt, ...) { struct printk_buffers *pbufs =3D pmsg->pbufs; const size_t scratchbuf_sz =3D sizeof(pbufs->scratchbuf); const size_t outbuf_sz =3D sizeof(pbufs->outbuf); char *scratchbuf =3D &pbufs->scratchbuf[0]; char *outbuf =3D &pbufs->outbuf[0]; + va_list args; size_t len; =20 - len =3D scnprintf(scratchbuf, scratchbuf_sz, - "** %lu printk messages dropped **\n", dropped); + va_start(args, fmt); + len =3D vscnprintf(scratchbuf, scratchbuf_sz, fmt, args); + va_end(args); =20 /* * Make sure outbuf is sufficiently large before prepending. @@ -2835,6 +2836,19 @@ void console_prepend_dropped(struct printk_message *= pmsg, unsigned long dropped) pmsg->outbuf_len +=3D len; } =20 +/* + * Prepend the message in @pmsg->pbufs->outbuf with a "dropped message". + * @pmsg->outbuf_len is updated appropriately. + * + * @pmsg is the printk message to prepend. + * + * @dropped is the dropped count to report in the dropped message. + */ +void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped) +{ + console_prepend_message(pmsg, "** %lu printk messages dropped **\n", drop= ped); +} + /* * Read and format the specified record (or a later record if the specified * record is not available). --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 43D5716F84F for ; Mon, 22 Jul 2024 17:19: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=1721668794; cv=none; b=tLQjZCxp0TcJtAGNvmsFYOMynX5zRiGF2q+PibwMw2tYhm2W55ptPED0ArD22pLdKX3DLcvlRENm4RSPN+mFpWbKRmJ+U77vfGgi+rhsSzA3ofUTUTqipXrOPR5GXwa6dGGwFaQ6sLtyZgspXukgE4w9GqBgwOkEhsC9TRfY/ow= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668794; c=relaxed/simple; bh=/untUGqgWj7u1xNK27SnwZK+1Wi7DgI+H3CTKmZxgh0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZHrGX2T1Pc+0I7y1l9fMjfib3fxH20p7WwI/Gd7N11jkYazMwF4IX+VxHMyzqDyuVpUcx/q8G2C9jWnKyEUl+iSigu+Gq6V2rcVdE4qSXE+K+3qCaMxid3UwlHBThGDl6eougKucJS2PdJBQFIuvSgzBixFcfnTyn2DraCIHTeo= 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=WN3UZcOi; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=eWrJEZT0; 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="WN3UZcOi"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="eWrJEZT0" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668784; 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=GGWVpENfq2/1/DjvkU8D8YMK8kxj1xPH74B4frVV15Y=; b=WN3UZcOiyjpvItIeqo+EjCIhJJdrCctRPeK6P2O36S4vedPq4ChV5iU8WsoQHO462W+G6F 7jIkpviwYWMX/QHE3hCM7TTXy6n6dgHPn+z64ryAWnyTOhizrxH9CcCV3w5jWxof2kMCb8 K+X9PlWVnzX+YVnjsFDitkEKwCchDvH14LBdjfIQ8eHB8AByj4uwQkC0dywXbmQF0afwJ3 Y4xPS2LhSaimzlJs8LAywlelIVZE2eB93CD7Oi9WzjhA/iPAaUdUwVZ9GASuNx5hj5P2SH gbAr28YEecThufOoNRIZiDV57b5vLxnJ0J+IYuCXZ/2TXaMmDwk+//f5bdSRQQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668784; 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=GGWVpENfq2/1/DjvkU8D8YMK8kxj1xPH74B4frVV15Y=; b=eWrJEZT01j4DxfxnxWbDbbNBJSj1P2itrEaxcYwVHNCw3H8GHJB5fqHBDXQrt2DXyRbXQi XDy8yc/TkdWEy6Bg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v3 13/19] printk: nbcon: Show replay message on takeover Date: Mon, 22 Jul 2024 19:25:33 +0206 Message-Id: <20240722171939.3349410-14-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" An emergency or panic context can takeover console ownership while the current owner was printing a printk message. The atomic printer will re-print the message that the previous owner was printing. However, this can look confusing to the user and may even seem as though a message was lost. [3430014.1 [3430014.181123] usb 1-2: Product: USB Audio Add a new field @nbcon_prev_seq to struct console to track the sequence number to print that was assigned to the previous console owner. If this matches the sequence number to print that the current owner is assigned, then a takeover must have occurred. In this case, print an additional message to inform the user that the previous message is being printed again. [3430014.1 ** replaying previous printk message ** [3430014.181123] usb 1-2: Product: USB Audio Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 2 ++ kernel/printk/internal.h | 1 + kernel/printk/nbcon.c | 26 ++++++++++++++++++++++++++ kernel/printk/printk.c | 11 +++++++++++ 4 files changed, 40 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index b8fb9fb24cbf..fde565e127b6 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -325,6 +325,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 + * @nbcon_prev_seq: Seq num the previous nbcon owner was assigned to print * @pbufs: Pointer to nbcon private buffer * @kthread: Printer kthread for this console * @rcuwait: RCU-safe wait object for @kthread waking @@ -459,6 +460,7 @@ struct console { atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct nbcon_context __private nbcon_device_ctxt; + atomic_long_t __private nbcon_prev_seq; =20 struct printk_buffers *pbufs; struct task_struct *kthread; diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 66321836c3fe..a4e7b40458b2 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -321,4 +321,5 @@ bool printk_get_next_message(struct printk_message *pms= g, u64 seq, =20 #ifdef CONFIG_PRINTK void console_prepend_dropped(struct printk_message *pmsg, unsigned long dr= opped); +void console_prepend_replay(struct printk_message *pmsg); #endif diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 8cf9e9e8c6e4..2a61f2c68ddc 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -938,7 +938,9 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt, bool use_a .pbufs =3D ctxt->pbufs, }; unsigned long con_dropped; + struct nbcon_state cur; unsigned long dropped; + unsigned long ulseq; =20 /* * This function should never be called for consoles that have not @@ -976,6 +978,29 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt, bool use_a if (dropped && !is_extended) console_prepend_dropped(&pmsg, dropped); =20 + /* + * If the previous owner was assigned the same record, this context + * has taken over ownership and is replaying the record. Prepend a + * message to let the user know the record is replayed. + */ + ulseq =3D atomic_long_read(&ACCESS_PRIVATE(con, nbcon_prev_seq)); + if (__ulseq_to_u64seq(prb, ulseq) =3D=3D pmsg.seq) { + console_prepend_replay(&pmsg); + } else { + /* + * Ensure this context is still the owner before trying to + * update @nbcon_prev_seq. Otherwise the value in @ulseq may + * not be from the previous owner and instead be some later + * value from the context that took over ownership. + */ + nbcon_state_read(con, &cur); + if (!nbcon_context_can_proceed(ctxt, &cur)) + return false; + + atomic_long_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_prev_seq), &ulseq, + __u64seq_to_ulseq(pmsg.seq)); + } + if (!nbcon_context_exit_unsafe(ctxt)) return false; =20 @@ -1706,6 +1731,7 @@ bool nbcon_alloc(struct console *con) =20 rcuwait_init(&con->rcuwait); init_irq_work(&con->irq_work, nbcon_irq_work); + atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL); nbcon_state_set(con, &state); =20 if (con->flags & CON_BOOT) { diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 89210a21cc50..6c9c0a42adf3 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2849,6 +2849,17 @@ void console_prepend_dropped(struct printk_message *= pmsg, unsigned long dropped) console_prepend_message(pmsg, "** %lu printk messages dropped **\n", drop= ped); } =20 +/* + * Prepend the message in @pmsg->pbufs->outbuf with a "replay message". + * @pmsg->outbuf_len is updated appropriately. + * + * @pmsg is the printk message to prepend. + */ +void console_prepend_replay(struct printk_message *pmsg) +{ + console_prepend_message(pmsg, "** replaying previous printk message **\n"= ); +} + /* * Read and format the specified record (or a later record if the specified * record is not available). --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 8364716F858 for ; Mon, 22 Jul 2024 17:19: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=1721668794; cv=none; b=uFgXxcsd5XJAVQ01G1LlS4UO34eg8liyAVNmqKpQytfjfyYH1uaaBvlsrMETU7tIW2t4nSCEeZdMr5CkLK+h45j0bzib34aUkHS+jShnQ+r/S/ufYDa7m6eTcpbrvbrEMPovfEbca2qFT/dAppsBK6aTZBtt7uS8oup4AXh8/6U= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668794; c=relaxed/simple; bh=z7t14psmClgJPnrTS3FePajFlnKKHKwtWJNjr7MCP1s=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FM3G36GFTuaH9Yp3Sp2fUnIPb4qfHafc0J0usNNl6oDT4hYS1KmDZlK+pPWOz9b5h9y1P/DYz17KMIzCl9Bg7yPgIjCSr4H0SR3OlrIS0BtAnCeQ81cciyQaZLD0YyV7GR85kXUftTpRTXKsVZmhHT0UY4TbY1PZd6hqTDgRZ84= 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=LVI0YCpS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Nr0Ev5lA; 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="LVI0YCpS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Nr0Ev5lA" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668784; 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=u1Oi5xduOOsbRA26g1GCX0jTw6STXCrfhNtn4TB0hdM=; b=LVI0YCpSL9I1KBnrkiAR0INdJoXK/Sq526h65n+QWQ7ck0WfByqWc0GEXTfS1C53mNAYlZ FDwEF4nPf36/Nff6f5rVjm9fflU2YOuQQRIkVTTr1oxD7mYSRQZiMOFRpngtd/PwXs1ebU dW134/IbvXeOOiBHIKfpTTD+bDkzdOSnySI/R0GgHgvgFlXI88YUvIpnXM+/MhgNyXWqUc 1x//+hifZxsPiO4HoRj9O52dUqpJ4e6+vltatPPgJ48uq4EjK1YFzilLkWs+tP2gy5sek5 AndsNTCgcFYxehP8nK/gXArKGz69LW5DHClQInx3kBTJd/XOui/yfEtBZ6ctGA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668784; 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=u1Oi5xduOOsbRA26g1GCX0jTw6STXCrfhNtn4TB0hdM=; b=Nr0Ev5lA1l6yOdYsZpivtRACyCdi1xMSpjGFWy994/l9f4UW70seNUklBcQ4gco7Ju7P0T jqvYkzuEV20ZTPDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 14/19] proc: consoles: Add notation to c_start/c_stop Date: Mon, 22 Jul 2024 19:25:34 +0206 Message-Id: <20240722171939.3349410-15-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" fs/proc/consoles.c:78:13: warning: context imbalance in 'c_start' - wrong count at exit fs/proc/consoles.c:104:13: warning: context imbalance in 'c_stop' - unexpected unlock Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- fs/proc/consoles.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/fs/proc/consoles.c b/fs/proc/consoles.c index e0758fe7936d..7036fdfa0bec 100644 --- a/fs/proc/consoles.c +++ b/fs/proc/consoles.c @@ -68,6 +68,7 @@ static int show_console_dev(struct seq_file *m, void *v) } =20 static void *c_start(struct seq_file *m, loff_t *pos) + __acquires(&console_mutex) { struct console *con; loff_t off =3D 0; @@ -94,6 +95,7 @@ static void *c_next(struct seq_file *m, void *v, loff_t *= pos) } =20 static void c_stop(struct seq_file *m, void *v) + __releases(&console_mutex) { console_list_unlock(); } --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 0196116F8F8; Mon, 22 Jul 2024 17:19: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=1721668794; cv=none; b=Iu3OtKSCuVdXF1KUG2w0z9+Ed2d1zmFv/y+RRqZKnEDZOBUWUo/fuDsgHyYePUtEmy3+nMJRGlGORQ/o2Hq4AUgPhEvuiS5Gz0xu8NDsv0gwpuwy6MQ7XnRjLWnv3fJYzjnK+LU4+ICJXzB9qbHSp2SEdycMbehbluSy1Z2/5JA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668794; c=relaxed/simple; bh=c8qynhdzyIK/U/Ozfr3U3Wi9LTUUnTpnQRxfUcOyN88=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oAgxEFI345jDd6j/cfPphuMDzo6dpKjnrturlNTVm91EzUj/3a73mSza+dYTuBDov8TCe4ii2S73izOMD/cmSrvgbgdeMjNAqizHS78W+LCC3i0zG8WVdTLqWrkkBlcJps/BG5N5c4zcEpH+IjcGizE/cgKdWUnsqBQmZHs3qTg= 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=t3F2w50B; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nA7DJV8+; 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="t3F2w50B"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nA7DJV8+" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668785; 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=So0AzHFpVy4lGb5Smj06XEnjo7Cib55zsw/bkGnKvh0=; b=t3F2w50BDxOJT4XMTwPy6mfz98fT2EhAPOdRbLrYyQiGSwT3aN7VbGlMowgDJAI0sTCQyH OAO/MFwgI+0tU3cmOCxb+/hio2GsCHV0lSBa8HMmFQNJ3YV1f5EJU9BcrWGARpQOPMVR6D IOVr96hceCB0moS/KrSJb8r+6jkcFJ0mKxTxzZgSERBZFPuG1MTxlRmHtz4bIBg9QzMdc/ +fWKrE7kU/pPyoGxUzY7wbJHfvfXfmO3wGw4bMw7VtXh/6oqXvXBjm3t7WVQRmvXMqtGRR GgS6J/U9gkzX6sizZsKF3FzunYAqkNfzvr3oMDCeK6rJWeoeLWg+HO6PViGhtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668785; 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=So0AzHFpVy4lGb5Smj06XEnjo7Cib55zsw/bkGnKvh0=; b=nA7DJV8+ncEMoS+2E9sdHMrpJs84fRaXOOaFI2fcdQ06fMejOsqoFdOyjvgS07c4Y6OTAO hIwELXssm39yJPBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org Subject: [PATCH printk v3 15/19] proc: Add nbcon support for /proc/consoles Date: Mon, 22 Jul 2024 19:25:35 +0206 Message-Id: <20240722171939.3349410-16-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" Update /proc/consoles output to show 'W' if an nbcon console is registered. Since the write_thread() callback is mandatory, it enough just to check if it is an nbcon console. Also update /proc/consoles output to show 'N' if it is an nbcon console. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- fs/proc/consoles.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/fs/proc/consoles.c b/fs/proc/consoles.c index 7036fdfa0bec..b7cab1ad990d 100644 --- a/fs/proc/consoles.c +++ b/fs/proc/consoles.c @@ -21,6 +21,7 @@ static int show_console_dev(struct seq_file *m, void *v) { CON_ENABLED, 'E' }, { CON_CONSDEV, 'C' }, { CON_BOOT, 'B' }, + { CON_NBCON, 'N' }, { CON_PRINTBUFFER, 'p' }, { CON_BRL, 'b' }, { CON_ANYTIME, 'a' }, @@ -58,8 +59,8 @@ static int show_console_dev(struct seq_file *m, void *v) seq_printf(m, "%s%d", con->name, con->index); seq_pad(m, ' '); seq_printf(m, "%c%c%c (%s)", con->read ? 'R' : '-', - con->write ? 'W' : '-', con->unblank ? 'U' : '-', - flags); + ((con->flags & CON_NBCON) || con->write) ? 'W' : '-', + con->unblank ? 'U' : '-', flags); if (dev) seq_printf(m, " %4d:%d", MAJOR(dev), MINOR(dev)); =20 --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 2627616F903; Mon, 22 Jul 2024 17:19: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=1721668794; cv=none; b=guHYAUs3swjqPfvk/8NPz2Kbs5Dd2vDz18FMMcl0XdlJpx2AhnTsDkBVHXj6YcqdOGQexosuzE71r6KHWwesIzzCPC8iehImrnUSmwLmCAF9oFYSZM2Iio7Vr//cS2h2i3naeCzg+8YLwKNuhNycExT0KqjQJlf3idBUKKLNxp4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668794; c=relaxed/simple; bh=GA0FHsOuyzgGhTea4jgXklRGbAD3dnv/ZQqXWojJ8gA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=mdIyse4kKot59vgrfOM9qea2Bpf+8zOjHj+QJRLIrppjQq8GFQ0QkJp2DoqYtNTkFKbEp6TSfCON7gcbRqs1PiGE2+Ow+P9ia6GNwBaZT2WeCV44j69pz8xkeDsRTFx6vZsoLpflS+oPnI2DzEGBJNWoiFSNsi6bS6US2b7aqy0= 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=hscES2pM; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=5gEWP+k8; 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="hscES2pM"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="5gEWP+k8" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668785; 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=pSvfpE6goL/87JMNQ367S+Uq3TCC1GnMITarxZW2fNs=; b=hscES2pMy9HgoeDGXcrcMmkr1+qVCf2u2H+LjM7DZNHXhqFBG/UME9KlDbRBJ3gS5QAQGa InE9bVsKn0anouM1zJmo7B6YDH67hs+09TBk6RWIENOjJACPNdieRs62hxFPTy+EaffIMm wZvcg8olExe+kT9M3G4Pf5sOHyNisJoQK5bfCuSgf1LYNNyRai4JbBmL/IihissOxpq+dl O2Sd8/5l2zRFaHQlrcpdtkMXAHM/j6+M1ZbcFgrXkg3ymRPnEqeXPSMBqJHCy+e252x6qm /7dr167XBWod0qvxU8ec9yBJWa7DTFLwp2zUUM5b1T33hGlxbQMnnZ7yhnauRA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668785; 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=pSvfpE6goL/87JMNQ367S+Uq3TCC1GnMITarxZW2fNs=; b=5gEWP+k8QbKpoMCQiDgw/q9wgTUiSPQ7tjQ350PrpYYnx9CG6qHlvcEF/ptbz2jeo/zCcJ GbfqpMvTdLmKnzDQ== 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 v3 16/19] tty: sysfs: Add nbcon support for 'active' Date: Mon, 22 Jul 2024 19:25:36 +0206 Message-Id: <20240722171939.3349410-17-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 the 'active' attribute to list nbcon consoles. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- drivers/tty/tty_io.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c index 407b0d87b7c1..9140825e810f 100644 --- a/drivers/tty/tty_io.c +++ b/drivers/tty/tty_io.c @@ -3567,7 +3567,7 @@ static ssize_t show_cons_active(struct device *dev, for_each_console(c) { if (!c->device) continue; - if (!c->write) + if (!(c->flags & CON_NBCON) && !c->write) continue; if ((c->flags & CON_ENABLED) =3D=3D 0) continue; --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 2957116F904 for ; Mon, 22 Jul 2024 17:19: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=1721668795; cv=none; b=I43i9IfPLFNfz+69KdD+4PlATO1ux34l3p8ydEJWG2oUxFLCpR9oov980WAJQDwVkqoKsw3QVC9KOYxjesbBs22MM4jUjDbIe0fDGTjW0Z6/Nt40Tn3uvuWygM30wposn8oA9tx6WIxCijWAQ40C3hGjBG7BCZAOxu0f6JMPQ/c= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668795; c=relaxed/simple; bh=78Y+/mL30CW2wNJZ/z19mZ2+DKHPX4jUfdFat/O24P0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DcxntJY8bQ/JjpBVxR939ESQxM1QXID3fSsraBKsaUFen7ndxdZIfQPK+XUT1WtYHYJOrCVcNMVpaHP8GP/jxZb/lpoXJkJ/SC+y3dvl63CP7739XYtrd5OSgd0EOi6cC/pkNP71w1/LTr7qgFdFSRwXN/DmT5kGplSuP8cJb1E= 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=jx4He5EY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=DMMuSyJ+; 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="jx4He5EY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DMMuSyJ+" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668785; 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=lPoDtEYbCFJccS865WEeFnUxysoSKgTlrtw+r66U+q4=; b=jx4He5EYOUdCU32wwq9qM9ih+4Osp1q06L+ItPy+rFtQ4oYw7Ie1+ggqyjgeoIbSqjrEpW YAvZHLwoAANpDIEsoxLHc8n57py/r2OZzMQSfCOpfidJLjjfVQghaQ/sZbl7HWQDShcwLS BrujVpHu/vdMEs/uPnn+rETF1VZU2z/rws7U8XBhdgduCIEDxkBw2vFbEMVUYx1Yay6XU8 5cFO12YQ8bqZi3OyjepN+7ckcMiPSMu88BX0+vFcR0uOFwqVOE0jUD0fQ9mPtGpQggFJAC M9KJN7Em+/nqlzlM0mpEHFAROukEnNTUIa8pYVuhy4QaVn+//OtzCBu3kEHQmg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668785; 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=lPoDtEYbCFJccS865WEeFnUxysoSKgTlrtw+r66U+q4=; b=DMMuSyJ+k5Pzua5lMDWGre+JsXn+F3x+36U2aNkIKI37oTq/6vQorG4bKEWaQtfJBugnVv 61V6ZPch3onbfcAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 17/19] printk: Implement legacy printer kthread for PREEMPT_RT Date: Mon, 22 Jul 2024 19:25:37 +0206 Message-Id: <20240722171939.3349410-18-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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() callback of legacy consoles usually makes use of spinlocks. This is not permitted with PREEMPT_RT in atomic contexts. For PREEMPT_RT, create a new kthread to handle printing of all the legacy consoles (and nbcon consoles if boot consoles are registered). This allows legacy consoles to work on PREEMPT_RT without requiring modification. (However they will not have the reliability properties guaranteed by nbcon atomic consoles.) Use the existing printk_kthreads_check_locked() to start/stop the legacy kthread as needed. Introduce the macro force_legacy_kthread() to query if the forced threading of legacy consoles is in effect. Although currently only enabled for PREEMPT_RT, this acts as a simple mechanism for the future to allow other preemption models to easily take advantage of the non-interference property provided by the legacy kthread. When force_legacy_kthread() is true, the legacy kthread fulfills the role of the console_flush_type @legacy_offload by waking the legacy kthread instead of printing via the console_lock in the irq_work. If the legacy kthread is not yet available, no legacy printing takes place (unless in panic). If for some reason the legacy kthread fails to create, any legacy consoles are unregistered. With force_legacy_kthread(), the legacy kthread is a critical component for legacy consoles. These changes only affect CONFIG_PREEMPT_RT. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 19 ++++- kernel/printk/printk.c | 162 +++++++++++++++++++++++++++++++++++---- 2 files changed, 162 insertions(+), 19 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index a4e7b40458b2..0ac4bdb9e3e8 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -21,6 +21,19 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, i= nt write, (con->flags & CON_BOOT) ? "boot" : "", \ con->name, con->index, ##__VA_ARGS__) =20 +/* + * Identify if legacy printing is forced in a dedicated kthread. If + * true, all printing via console lock occurs within a dedicated + * legacy printer thread. The only exception is on panic, after the + * nbcon consoles have had their chance to print the panic messages + * first. + */ +#ifdef CONFIG_PREEMPT_RT +# define force_legacy_kthread() (true) +#else +# define force_legacy_kthread() (false) +#endif + #ifdef CONFIG_PRINTK =20 #ifdef CONFIG_PRINTK_CALLER @@ -192,7 +205,7 @@ extern bool legacy_allow_panic_sync; * @nbcon_atomic: Flush directly using nbcon_atomic() callback * @nbcon_offload: Offload flush to printer thread * @legacy_direct: Call the legacy loop in this context - * @legacy_offload: Offload the legacy loop into IRQ + * @legacy_offload: Offload the legacy loop into IRQ or legacy thread */ struct console_flush_type { bool nbcon_atomic; @@ -216,7 +229,7 @@ static inline void printk_get_console_flush_type(struct= console_flush_type *ft, switch (nbcon_get_default_prio()) { case NBCON_PRIO_NORMAL: if (have_legacy_console || have_boot_console) { - if (prefer_offload || is_printk_deferred()) + if (force_legacy_kthread() || prefer_offload || is_printk_deferred()) ft->legacy_offload =3D true; else ft->legacy_direct =3D true; @@ -267,7 +280,7 @@ static inline void printk_get_emergency_console_flush_t= ype(struct console_flush_ ft->nbcon_atomic =3D true; =20 if (have_legacy_console || have_boot_console) { - if (is_printk_deferred()) + if (force_legacy_kthread() || is_printk_deferred()) ft->legacy_offload =3D true; else ft->legacy_direct =3D true; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6c9c0a42adf3..6f70d3a7153f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -488,6 +488,7 @@ bool legacy_allow_panic_sync; =20 #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); +static DECLARE_WAIT_QUEUE_HEAD(legacy_wait); /* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */ static u64 syslog_seq; @@ -2450,6 +2451,7 @@ static u64 syslog_seq; =20 static bool pr_flush(int timeout_ms, bool reset_on_progress) { return true= ; } static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_= progress) { return true; } +static inline void legacy_kthread_wake(void) { } =20 #endif /* CONFIG_PRINTK */ =20 @@ -2704,6 +2706,8 @@ void resume_console(void) printk_get_console_flush_type(&ft, true); if (ft.nbcon_offload) nbcon_wake_kthreads(); + if (ft.legacy_offload) + defer_console_output(); =20 pr_flush(1000, true); } @@ -3112,19 +3116,7 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove return false; } =20 -/** - * console_unlock - unblock the console subsystem from printing - * - * Releases the console_lock which the caller holds to block printing of - * the console subsystem. - * - * While the console_lock was held, console output may have been buffered - * by printk(). If this is the case, console_unlock(); emits - * the output prior to releasing the lock. - * - * console_unlock(); may be called from any context. - */ -void console_unlock(void) +static void __console_flush_and_unlock(void) { bool do_cond_resched; bool handover; @@ -3168,6 +3160,29 @@ void console_unlock(void) */ } while (prb_read_valid(prb, next_seq, NULL) && console_trylock()); } + +/** + * console_unlock - unblock the legacy console subsystem from printing + * + * Releases the console_lock which the caller holds to block printing of + * the legacy console subsystem. + * + * While the console_lock was held, console output may have been buffered + * by printk(). If this is the case, console_unlock() emits the output on + * legacy consoles prior to releasing the lock. + * + * console_unlock(); may be called from any context. + */ +void console_unlock(void) +{ + struct console_flush_type ft; + + printk_get_console_flush_type(&ft, false); + if (ft.legacy_direct) + __console_flush_and_unlock(); + else + __console_unlock(); +} EXPORT_SYMBOL(console_unlock); =20 /** @@ -3397,6 +3412,8 @@ void console_start(struct console *console) flags =3D console_srcu_read_flags(console); if ((flags & CON_NBCON) && ft.nbcon_offload) nbcon_kthread_wake(console); + else if (!(flags & CON_NBCON) && ft.legacy_offload) + defer_console_output(); console_srcu_read_unlock(cookie); =20 __pr_flush(console, 1000, true); @@ -3409,6 +3426,87 @@ static int unregister_console_locked(struct console = *console); /* True when system boot is far enough to create printer threads. */ static bool printk_kthreads_ready __ro_after_init; =20 +static struct task_struct *printk_legacy_kthread; + +static bool legacy_kthread_should_wakeup(void) +{ + struct console_flush_type ft; + struct console *con; + bool ret =3D false; + int cookie; + + if (kthread_should_stop()) + return true; + + printk_get_console_flush_type(&ft, true); + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + u64 printk_seq; + + /* + * The legacy printer thread is only for legacy consoles when + * the nbcon consoles have their printer threads. + */ + if ((flags & CON_NBCON) && ft.nbcon_offload) + continue; + + if (!console_is_usable(con, flags, false)) + continue; + + if (flags & CON_NBCON) { + printk_seq =3D nbcon_seq_read(con); + } else { + /* + * It is safe to read @seq because only this + * thread context updates @seq. + */ + printk_seq =3D con->seq; + } + + if (prb_read_valid(prb, printk_seq, NULL)) { + ret =3D true; + break; + } + } + console_srcu_read_unlock(cookie); + + return ret; +} + +static int legacy_kthread_func(void *unused) +{ + for (;;) { + wait_event_interruptible(legacy_wait, legacy_kthread_should_wakeup()); + + if (kthread_should_stop()) + break; + + console_lock(); + __console_flush_and_unlock(); + } + + return 0; +} + +static bool legacy_kthread_create(void) +{ + struct task_struct *kt; + + lockdep_assert_console_list_lock_held(); + + kt =3D kthread_run(legacy_kthread_func, NULL, "pr/legacy"); + if (WARN_ON(IS_ERR(kt))) { + pr_err("failed to start legacy printing thread\n"); + return false; + } + + printk_legacy_kthread =3D kt; + + return true; +} + /** * printk_kthreads_shutdown - shutdown all threaded printers * @@ -3458,6 +3556,27 @@ static void printk_kthreads_check_locked(void) if (!printk_kthreads_ready) return; =20 + if (have_legacy_console || have_boot_console) { + if (!printk_legacy_kthread && + force_legacy_kthread() && + !legacy_kthread_create()) { + /* + * All legacy consoles must be unregistered. If there + * are any nbcon consoles, they will set up their own + * kthread. + */ + hlist_for_each_entry_safe(con, tmp, &console_list, node) { + if (con->flags & CON_NBCON) + continue; + + unregister_console_locked(con); + } + } + } else if (printk_legacy_kthread) { + kthread_stop(printk_legacy_kthread); + printk_legacy_kthread =3D NULL; + } + /* * Printer threads cannot be started as long as any boot console is * registered because there is no way to synchronize the hardware @@ -4241,14 +4360,23 @@ static bool pr_flush(int timeout_ms, bool reset_on_= progress) =20 static DEFINE_PER_CPU(int, printk_pending); =20 +static void legacy_kthread_wake(void) +{ + if (printk_legacy_kthread) + wake_up_interruptible(&legacy_wait); +} + static void wake_up_klogd_work_func(struct irq_work *irq_work) { int pending =3D this_cpu_xchg(printk_pending, 0); =20 if (pending & PRINTK_PENDING_OUTPUT) { - /* If trylock fails, someone else is doing the printing */ - if (console_trylock()) - console_unlock(); + if (force_legacy_kthread()) { + legacy_kthread_wake(); + } else { + if (console_trylock()) + console_unlock(); + } } =20 if (pending & PRINTK_PENDING_WAKEUP) @@ -4676,6 +4804,8 @@ void console_try_replay_all(void) __console_rewind_all(); if (ft.nbcon_offload) nbcon_wake_kthreads(); + if (ft.legacy_offload) + defer_console_output(); /* Consoles are flushed as part of console_unlock(). */ console_unlock(); } --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 0AA8F16F8F9 for ; Mon, 22 Jul 2024 17:19: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=1721668796; cv=none; b=eYYvZLXajRyY6FQrFUCDbIGQH2kzwBXd3K9qU+hqt1jbUOKW9WJJCEh5JXC510t2mUr+zx/vqBfQSlO5pvpJ1IykiGQI25B73nieiYQmB1TkieLqS+P2NGPflaqVA/36Eqs+6NvlzVDnev/CFL1RANzwyrgfXDQDnCVMPVdOJeU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668796; c=relaxed/simple; bh=W6SIiSzGnRiYJ39aU23hut4hEAWhLT07CX45s0R0uU8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aMfhPEfOMq/40mwJFPMbkjow8+egMChCYrbuf0lvAi69/J10ijlcJhreOw3Nv+EiXXYylwFvDxwjnAX6iaQQVi5UmX44KdOpzbYKrIkOlUb/f7ywMgjv1alhSqGiaTAtopE7KeTbZiOzvVdvxy8EAih4jV3qP2UJQNTGkXBLjZM= 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=LtLU6zht; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=65c+mN8D; 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="LtLU6zht"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="65c+mN8D" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668786; 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=ctkIXtNtNGPOayfCp8EFtN+ArVTwSQWPWs4qM/qfgSo=; b=LtLU6zhtLEzBkYmUpkD9ERBErasUIQnFadM73HbtOlzNbeCzJsuZOryAcQleB6+28WnhtZ IvJUrUAAiDSMH29YSN+OWHDgi2hBhTmYezsf/I1ppU38aMf5lWNcgadmrh7EFxHLcoyJni HPjVHe/PsKnHwdi4czVJ0yo8P5JfLPEAzkUXnay6dBWBamSW/IbvQULVPfXX57de3U++mp 7TFT1wKqh5/TOQGhKN+qLaiWpOz4hxnU4cO0C1/xAMj7OlxXcYiRsU+hL8CAfWM6r08fQY rEbhQtKIY/vNlXine6unXrGoJWxQBIQU04MY3zH52GIoR5u2arxZ0Q6xYYPskg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668786; 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=ctkIXtNtNGPOayfCp8EFtN+ArVTwSQWPWs4qM/qfgSo=; b=65c+mN8DMD/xf6Yv6qMRHfKvvAS6u+8SdFJMN6WfuqgTDMbzrd7m4S8vEcFMefOX+maxvB 0eh/pKMmk4ushYAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 18/19] printk: nbcon: Assign nice -20 for printing threads Date: Mon, 22 Jul 2024 19:25:38 +0206 Message-Id: <20240722171939.3349410-19-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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 is important that console printing threads are scheduled shortly after a printk call and with generous runtime budgets. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/nbcon.c | 6 ++++++ kernel/printk/printk.c | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 2a61f2c68ddc..e34e5c5a7843 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1260,6 +1260,12 @@ bool nbcon_kthread_create(struct console *con) =20 con->kthread =3D kt; =20 + /* + * It is important that console printing threads are scheduled + * shortly after a printk call and with generous runtime budgets. + */ + sched_set_normal(con->kthread, -20); + return true; } =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6f70d3a7153f..36087a6d579f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3504,6 +3504,12 @@ static bool legacy_kthread_create(void) =20 printk_legacy_kthread =3D kt; =20 + /* + * It is important that console printing threads are scheduled + * shortly after a printk call and with generous runtime budgets. + */ + sched_set_normal(printk_legacy_kthread, -20); + return true; } =20 --=20 2.39.2 From nobody Wed Dec 17 01:11:54 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 5357617085D for ; Mon, 22 Jul 2024 17:19: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=1721668797; cv=none; b=GL5IuFtkIdCg56Mc4lffRvzindxd/jEMatP/OGq4ZaxgQjK4WDvewDfFZ4hpbNFgNACXQJaLTbUu1+FGhf3CSDCJ6ZuMwR7nM8Jtzyqi+XUuKuWD8IXZQ2/yl/JgzDC0YfNG8lrifnT4qj8gG65vmb4wMCS/XTi8kobksA3c5pI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721668797; c=relaxed/simple; bh=ie/yB40VO2T2PbTOGFrv4eyypliE8daW1zxHzpd13dY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KcIrhq8i8hYunaPWdn4dgENmtn29jMq7E0B/NOranVdREaIuIJ2B3wXabgjimILSZny1XGgK55Wy1q3JW8df3GZQv7dHpkC0SOZ4CmVgFt3AT2ndCxr8Il4frD037ChrF5y2oWCOZfsO1qAZm1qZcsmWagrTSrSnSwTrYNJsE4E= 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=YB0134SX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mzH2f4Tp; 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="YB0134SX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mzH2f4Tp" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1721668786; 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=VQwsWH/V/Au7PuCnIhWFcD0ed/HFZCMt6UFOALSimUg=; b=YB0134SXwL8VicHaLhRQyXIQIhtbsCxEuPTyCdkZ4piJhitsLiMccbc07461XU4lLu78c9 BSKIcntX4PVLJRboUgc4P4dXpCRBfAuSTNdAuQOTVWFIDvMnU+WERR4oeTj/JlhrKIUP76 Kbr5wk0XDgA62Tf2HhYjxxSq4C3qQQAh0OoGEEVExT8d3isyYWBz31mwcO2osDLeaOFtb9 RQvbodHDXNppVIkxNhst1NX5rLMY56M5WYzquF7y7iJch06dWKxQgzZIxrmH8klyaMOG6y z8HfBzxTlBzYohFxqts1aIZvlhHQmcG16lPzUYav6G1ANj1GLSwaKm2N7oTGVg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1721668786; 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=VQwsWH/V/Au7PuCnIhWFcD0ed/HFZCMt6UFOALSimUg=; b=mzH2f4TpxPuyo2yV1/BJSe0plxxDYNCLpB9Or1Zbq0dpuHB1NguV8wgxxAU20R4bMRNbym wMkly1XlirqAcrCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 19/19] printk: Avoid false positive lockdep report for legacy printing Date: Mon, 22 Jul 2024 19:25:39 +0206 Message-Id: <20240722171939.3349410-20-john.ogness@linutronix.de> In-Reply-To: <20240722171939.3349410-1-john.ogness@linutronix.de> References: <20240722171939.3349410-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" Legacy console printing from printk() caller context may invoke the console driver from atomic context. This leads to a lockdep splat because the console driver will acquire a sleeping lock and the caller may already hold a spinning lock. This is noticed by lockdep on !PREEMPT_RT configurations because it will lead to a problem on PREEMPT_RT. However, on PREEMPT_RT the printing path from atomic context is always avoided and the console driver is always invoked from a dedicated thread. Thus the lockdep splat on !PREEMPT_RT is a false positive. For !PREEMPT_RT override the lock-context before invoking the console driver to avoid the false positive. Do not override the lock-context for PREEMPT_RT in order to allow lockdep to catch any real locking context issues related to the write callback usage. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 83 ++++++++++++++++++++++++++++++++---------- 1 file changed, 63 insertions(+), 20 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 36087a6d579f..fadfbe56019b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2929,6 +2929,34 @@ bool printk_get_next_message(struct printk_message *= pmsg, u64 seq, return true; } =20 +/* + * Legacy console printing from printk() caller context does not respect + * raw_spinlock/spinlock nesting. For !PREEMPT_RT the lockdep warning is a + * false positive. For PREEMPT_RT the false positive condition does not + * occur. + * + * This map is used to temporarily establish LD_WAIT_SLEEP context for the + * console write() callback when legacy printing to avoid false positive + * lockdep complaints, thus allowing lockdep to continue to function for + * real issues. + */ +#ifdef CONFIG_PREEMPT_RT +static inline void printk_legacy_allow_spinlock_enter(void) { } +static inline void printk_legacy_allow_spinlock_exit(void) { } +#else +static DEFINE_WAIT_OVERRIDE_MAP(printk_legacy_map, LD_WAIT_SLEEP); + +static inline void printk_legacy_allow_spinlock_enter(void) +{ + lock_map_acquire_try(&printk_legacy_map); +} + +static inline void printk_legacy_allow_spinlock_exit(void) +{ + lock_map_release(&printk_legacy_map); +} +#endif /* CONFIG_PREEMPT_RT */ + /* * Used as the printk buffers for non-panic, serialized console printing. * This is for legacy (!CON_NBCON) as well as all boot (CON_BOOT) consoles. @@ -2978,31 +3006,46 @@ static bool console_emit_next_record(struct console= *con, bool *handover, int co con->dropped =3D 0; } =20 - /* - * While actively printing out messages, if another printk() - * were to occur on another CPU, it may wait for this one to - * finish. This task can not be preempted if there is a - * waiter waiting to take over. - * - * Interrupts are disabled because the hand over to a waiter - * must not be interrupted until the hand over is completed - * (@console_waiter is cleared). - */ - printk_safe_enter_irqsave(flags); - console_lock_spinning_enable(); + /* Write everything out to the hardware. */ =20 - /* Do not trace print latency. */ - stop_critical_timings(); + if (force_legacy_kthread() && !panic_in_progress()) { + /* + * With forced threading this function is in a task context + * (either legacy kthread or get_init_console_seq()). There + * is no need for concern about printk reentrance, handovers, + * or lockdep complaints. + */ =20 - /* Write everything out to the hardware. */ - con->write(con, outbuf, pmsg.outbuf_len); + con->write(con, outbuf, pmsg.outbuf_len); + con->seq =3D pmsg.seq + 1; + } else { + /* + * While actively printing out messages, if another printk() + * were to occur on another CPU, it may wait for this one to + * finish. This task can not be preempted if there is a + * waiter waiting to take over. + * + * Interrupts are disabled because the hand over to a waiter + * must not be interrupted until the hand over is completed + * (@console_waiter is cleared). + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); =20 - start_critical_timings(); + /* Do not trace print latency. */ + stop_critical_timings(); =20 - con->seq =3D pmsg.seq + 1; + printk_legacy_allow_spinlock_enter(); + con->write(con, outbuf, pmsg.outbuf_len); + printk_legacy_allow_spinlock_exit(); =20 - *handover =3D console_lock_spinning_disable_and_check(cookie); - printk_safe_exit_irqrestore(flags); + start_critical_timings(); + + con->seq =3D pmsg.seq + 1; + + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + } skip: return true; } --=20 2.39.2