From nobody Tue Feb 10 06:25:23 2026 Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F1D7C3128B4 for ; Sat, 27 Dec 2025 12:17:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.128.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766837830; cv=none; b=ka9mgyBwWDge8edFeW9JPfASZi44WnTJafih4y8R3fMb3BhrVuK/0sUrOyUShGVvJX8tXsVFDPQ1MuIB+TsovkG2LwoO4/D7742vI1eXwIUFjR5MRBZFsoVCczQ/iaIzd/20hji2r+7ear0Kpk76q1ynmrbFIKMAJtZ8Ua/SgQA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1766837830; c=relaxed/simple; bh=neUSae1tSSd9ZUIMhNnIsDOes9ZbVtbWmizb8QV3UdY=; h=From:Date:Subject:MIME-Version:Content-Type:Message-Id:References: In-Reply-To:To:Cc; b=AMPccHGBrmrEGIJQwEQL9iuFjuWBkOoTSHKqdPG/YmVz4crsm7MhCIgk53bZxt/y7Qx2Ap68gbPT+cXrQcklQfrLT57OQLIVJJAR44tEYZIMuicCQFAhwWZaj0Dkzb3HXvPpd0aszGhZ3aO5xxsccH3/JEw5372VyFje8Qd/aAk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b=fQ5XtgxD; arc=none smtp.client-ip=209.85.128.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com header.b="fQ5XtgxD" Received: by mail-wm1-f42.google.com with SMTP id 5b1f17b1804b1-47a8195e515so48972415e9.0 for ; Sat, 27 Dec 2025 04:17:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=google; t=1766837826; x=1767442626; darn=vger.kernel.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=rhFoe+/fNVQYtucZwNZBdpwy2GyzZc29S0jBT7dXZmU=; b=fQ5XtgxDILmZosKw2clJzy9LmxfnwkTL4qmNG2RyK/MiJG4XV/EB0jGIYUb41YqzV/ 4zFOfE1k1BWBlDPCtsMcnbI+zZ6huHL2fjqi4UEESPZNyI/COJ4q79xErJbIrxgTGyVA 2yuFXjASs3hM68SbdqTNm8fEnOaVCWUrmgqtx2v1hJMgDYwGEylFx99fJsMiqeJIguk3 4EPWje5a+dD0sZ0bEpScnZz6IntDeStXXcUB1RUXlrtIwbPk89rAfCD8IAt1FinWmRNJ Ml05v7ugRPe4CWRYhIK6jHkTsD/H/DECiE641CKM+8hTBQFQfqGGh/4UBpO439soXeEj d21g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1766837826; x=1767442626; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-gg:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=rhFoe+/fNVQYtucZwNZBdpwy2GyzZc29S0jBT7dXZmU=; b=f3mu3eAxFyeiswzY3c8sJ5UiBh+Y4iCyU229FMntalk5kah82/22keTnjHYpUZ8xE+ yjHEqC5w4p+3KquRXES+TdsILwBZ8rw4Lx1jKm2phkJii2gVeF2H4I3x/K6OYZWZey8P 6fENLU8lPSmn5TvPfRY75BpYUZNHlnxWrfoyeHii8+TJ3rNhtoDaPT8NYWmBS4mHMqiU w/iA+YxXm7CPb9xJbYFAA3WbPduPHEFKQH/MZJl+95TyncRz+HJ5AXvkaJMw6IPnKKdy GZrfcWHqzf8mPxwiklG/0t2+gSikidEg0Xd5doz8NW3P0LiOouizXxUF2ngUddb9/xLz 2cmw== X-Forwarded-Encrypted: i=1; AJvYcCU8WyX4DqC7RIcqYSKECyadKpuHieUF/kl5PYI1zTDE/eB4pFOBKHdRbRz6g+SlBdBqN3fT+x2WCk9oTa8=@vger.kernel.org X-Gm-Message-State: AOJu0Ywqb3+tDHi3qwRWZvWaZvmzMUH8Abf/OKFHng0zK+4+7aka1wcq Q9dhi61BJR8HFOcYBLLI223I0rGuXO8iERs1D67XggaoeWmyVrI0BEh9HEALCbBzTPA= X-Gm-Gg: AY/fxX4H37DHaF+YfOZfnvFtbs5r4K4r1AY9LU086uFfD3+TcELjAkU/eyOwiU3WObX ugA+suwLzFGeszBaYlYuT5+/S2J3dlraT0Oyc4n5cxpA8OJHf2glw0u2nhCMS8PiyN5MofXKFJI eYjvDGZ+76posuJm11nPNtuCZ0/zAJDyZau/cMzaqaFlqVSOtqlDFbXZa1NJr1zmYPRKFXCxp8C xWyezKhgB49KYAWi6px5JYCcG1dP+1/agDwqxLvQKu0xUH6oiHurRfFeY/cf/2H+6k+V+wycKET 3G0MwfTeatMQPzbdHIgRxk240uSw6GGkIkwRpLCw784+5HpuS+qwvDEPBs+TYCEqrDjtVx8UgBj pgUkf2ux7D4vLng1ISHkc/NLGzSAVG9prEiyVHlUwT3lOI0KUbyLySSFPf2ajl/wxUsCEB0QFRJ JazRoe380S X-Google-Smtp-Source: AGHT+IHT9M2beqQrZzp9xbbyJud8/h6Q+UOZtBpjbh3fLwHrPjz2pLTjAJscQz+yaZcvqZw8y6NMSQ== X-Received: by 2002:a05:600c:3b88:b0:47a:814c:ee95 with SMTP id 5b1f17b1804b1-47d19556cf7mr356539265e9.12.1766837826003; Sat, 27 Dec 2025 04:17:06 -0800 (PST) Received: from [127.0.0.1] ([2804:5078:811:d400:58f2:fc97:371f:2]) by smtp.gmail.com with ESMTPSA id a92af1059eb24-121724cfdd0sm97940127c88.4.2025.12.27.04.16.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 27 Dec 2025 04:17:05 -0800 (PST) From: Marcos Paulo de Souza Date: Sat, 27 Dec 2025 09:16:09 -0300 Subject: [PATCH 02/19] printk: Introduce console_is_nbcon Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Message-Id: <20251227-printk-cleanup-part3-v1-2-21a291bcf197@suse.com> References: <20251227-printk-cleanup-part3-v1-0-21a291bcf197@suse.com> In-Reply-To: <20251227-printk-cleanup-part3-v1-0-21a291bcf197@suse.com> To: Richard Weinberger , Anton Ivanov , Johannes Berg , Greg Kroah-Hartman , Jason Wessel , Daniel Thompson , Douglas Anderson , Petr Mladek , Steven Rostedt , John Ogness , Sergey Senozhatsky , Jiri Slaby , Breno Leitao , Andrew Lunn , "David S. Miller" , Eric Dumazet , Jakub Kicinski , Paolo Abeni , Geert Uytterhoeven , Kees Cook , Tony Luck , "Guilherme G. Piccoli" , Madhavan Srinivasan , Michael Ellerman , Nicholas Piggin , Christophe Leroy , Andreas Larsson , Alexander Shishkin , Maxime Coquelin , Alexandre Torgue , Jacky Huang , Shan-Chun Hung , Laurentiu Tudor Cc: linux-um@lists.infradead.org, linux-kernel@vger.kernel.org, kgdb-bugreport@lists.sourceforge.net, linux-serial@vger.kernel.org, netdev@vger.kernel.org, linux-m68k@lists.linux-m68k.org, linux-hardening@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, sparclinux@vger.kernel.org, linux-stm32@st-md-mailman.stormreply.com, linux-arm-kernel@lists.infradead.org, linux-fsdevel@vger.kernel.org, Marcos Paulo de Souza X-Mailer: b4 0.15-dev X-Developer-Signature: v=1; a=ed25519-sha256; t=1766837798; l=5401; i=mpdesouza@suse.com; s=20231031; h=from:subject:message-id; bh=neUSae1tSSd9ZUIMhNnIsDOes9ZbVtbWmizb8QV3UdY=; b=gCYQ59/fzh5QOgA05PXMV/E/oNmzy4c9swZbzFlKNzJFERHXZDcBlPYBdYFwyrufnetFktUrT qugGUjzspdLDAy+AfxtSwqRvBilWtZIJfksSlShjgsgxcBNFgQvlHOk X-Developer-Key: i=mpdesouza@suse.com; a=ed25519; pk=/Ni/TsKkr69EOmdZXkp1Q/BlzDonbOBRsfPa18ySIwU= Besides checking if the current console is NBCON or not, console->flags is also being read in order to serve as argument of the console_is_usable function. But CON_NBCON flag is unique: it's set just once in the console registration and never cleared. In this case it can be possible to read the flag when console_srcu_lock is held (which is the case when using for_each_console). This change makes possible to remove the flags argument from console_is_usable in the next patches. Signed-off-by: Petr Mladek Signed-off-by: Marcos Paulo de Souza Reviewed-by: Petr Mladek --- include/linux/console.h | 27 +++++++++++++++++++++++++++ kernel/debug/kdb/kdb_io.c | 2 +- kernel/printk/nbcon.c | 2 +- kernel/printk/printk.c | 15 ++++++--------- 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 35c03fc4ed51..dd4ec7a5bff9 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -561,6 +561,33 @@ static inline void console_srcu_write_flags(struct con= sole *con, short flags) WRITE_ONCE(con->flags, flags); } =20 +/** + * console_srcu_is_nbcon - Locklessly check whether the console is nbcon + * @con: struct console pointer of console to check + * + * Requires console_srcu_read_lock to be held, which implies that @con mig= ht + * be a registered console. The purpose of holding console_srcu_read_lock = is + * to guarantee that no exit/cleanup routines will run if the console + * is currently undergoing unregistration. + * + * If the caller is holding the console_list_lock or it is _certain_ that + * @con is not and will not become registered, the caller may read + * @con->flags directly instead. + * + * Context: Any context. + * Return: True when CON_NBCON flag is set. + */ +static inline bool console_is_nbcon(const struct console *con) +{ + WARN_ON_ONCE(!console_srcu_read_lock_is_held()); + + /* + * The CON_NBCON flag is statically initialized and is never + * set or cleared at runtime. + */ + return data_race(con->flags & CON_NBCON); +} + /* Variant of console_is_registered() when the console_list_lock is held. = */ static inline bool console_is_registered_locked(const struct console *con) { diff --git a/kernel/debug/kdb/kdb_io.c b/kernel/debug/kdb/kdb_io.c index 6ffb962392a4..d6de512b433a 100644 --- a/kernel/debug/kdb/kdb_io.c +++ b/kernel/debug/kdb/kdb_io.c @@ -596,7 +596,7 @@ static void kdb_msg_write(const char *msg, int msg_len) if (c =3D=3D dbg_io_ops->cons) continue; =20 - if (flags & CON_NBCON) { + if (console_is_nbcon(c)) { struct nbcon_write_context wctxt =3D { }; =20 /* diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 13865ef85990..f0f42e212caa 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1647,7 +1647,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq) for_each_console_srcu(con) { short flags =3D console_srcu_read_flags(con); =20 - if (!(flags & CON_NBCON)) + if (!console_is_nbcon(con)) continue; =20 if (!console_is_usable(con, flags, NBCON_USE_ATOMIC)) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 5f4b84f9562e..bd0d574be3cf 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3200,7 +3200,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * * nbcon consoles when the nbcon consoles cannot print via * their atomic or threaded flushing. */ - if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload)) + if (console_is_nbcon(con) && (ft.nbcon_atomic || ft.nbcon_offload)) continue; =20 if (!console_is_usable(con, flags, @@ -3209,7 +3209,7 @@ static bool console_flush_one_record(bool do_cond_res= ched, u64 *next_seq, bool * continue; any_usable =3D true; =20 - if (flags & CON_NBCON) { + if (console_is_nbcon(con)) { progress =3D nbcon_legacy_emit_next_record(con, handover, cookie, !do_cond_resched); printk_seq =3D nbcon_seq_read(con); @@ -3458,7 +3458,6 @@ void console_unblank(void) static void __console_rewind_all(void) { struct console *c; - short flags; int cookie; u64 seq; =20 @@ -3466,9 +3465,7 @@ static void __console_rewind_all(void) =20 cookie =3D console_srcu_read_lock(); for_each_console_srcu(c) { - flags =3D console_srcu_read_flags(c); - - if (flags & CON_NBCON) { + if (console_is_nbcon(c)) { nbcon_seq_force(c, seq); } else { /* @@ -3632,13 +3629,13 @@ static bool legacy_kthread_should_wakeup(void) * consoles when the nbcon consoles cannot print via their * atomic or threaded flushing. */ - if ((flags & CON_NBCON) && (ft.nbcon_atomic || ft.nbcon_offload)) + if (console_is_nbcon(con) && (ft.nbcon_atomic || ft.nbcon_offload)) continue; =20 if (!console_is_usable(con, flags, NBCON_USE_THREAD)) continue; =20 - if (flags & CON_NBCON) { + if (console_is_nbcon(con)) { printk_seq =3D nbcon_seq_read(con); } else { /* @@ -4490,7 +4487,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre if (!console_is_usable(c, flags, NBCON_USE_ANY)) continue; =20 - if (flags & CON_NBCON) { + if (console_is_nbcon(c)) { printk_seq =3D nbcon_seq_read(c); } else { printk_seq =3D c->seq; --=20 2.52.0