From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 35D3615ECC8 for ; Tue, 2 Apr 2024 22:11:34 +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=1712095897; cv=none; b=VmwULyNOglSwapPbrQjtLeW+bzArM3+Si8cM3lv7UcsnAPSsViK/oC8aHVDfjsGWYSNiMFeJ1ibCdliciTo5jUGe0e4rCrpc3kp7+uWnma4QayGvASnhEnrlV8yzcQ2KVuGNPvANCGnqqxr+UW+T/ozx8NAPltYZihc3VmLa9eU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095897; c=relaxed/simple; bh=+x0B0DKqCUuY5JhcpCChNmzn4DAXIu7FX0iwPh4yj00=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=siHsjFuApnHeb37tMdebiVV+Z8qlPhUSvFtD99KOLi6YiK6Syrmxqsc97o4+vWeMB7R8UN+y+1dMxkNWlnd5+1ihlRgxCrfjA2ma5oqHDDUMvEEXPZNSX8STD7pFmWJDG/CCrgIQdzeIFgGk+QqI9aSi8Z44O2VsTrVrnOKff6I= 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=zfeJcwKX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=RidmNc0j; 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="zfeJcwKX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="RidmNc0j" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095893; 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=CzegSW+30fFMD0MZbnfdPKJm1E+ZrdzFvhWkWJc/40c=; b=zfeJcwKXXHueMeEDEdn+JAzbBB+a+cmmZB6wnzsurIJSMy/SqfjAjh2V5MUm/NrWa2zXTk Z7dhpE53PB76B58ipZT3lxvj5xIn1tA9fuEvDyphswzxJ7g4r0BbQJge+dsfTjvqF6p06n N8E47VK8slzHXvtUq52cCiJJ0DmqAVBgsxhyWJsbpDI3WdOlWSh/wPHdRCkT7cvE7PCceT whKxtcbBiQ+ZkeH/uzOsruzxRZ6GTcGEd5w94FO51fxCLvO0Jd6DwxGCHptlXBbIY8lWm1 JkjFPDf+9j9Ut9gTdhlQ5jRCe0brAW/Vr86yZlu0QfyAZ/Wai5HZnVROvPSaxw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095893; 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=CzegSW+30fFMD0MZbnfdPKJm1E+ZrdzFvhWkWJc/40c=; b=RidmNc0jX4fyRo2z6VZIWtpwsDGh6TsQrdJrlUZsVI6g88Dp5ol2HgzFwVqh3pld12EfdD DXNwIFK9Bn4OhNBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, "Paul E. McKenney" , Miguel Ojeda Subject: [PATCH printk v4 01/27] printk: Add notation to console_srcu locking Date: Wed, 3 Apr 2024 00:17:03 +0206 Message-Id: <20240402221129.2613843-2-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernel/printk/printk.c:284:5: sparse: sparse: context imbalance in 'console_srcu_read_lock' - wrong count at exit include/linux/srcu.h:301:9: sparse: sparse: context imbalance in 'console_srcu_read_unlock' - unexpected unlock Fixes: 6c4afa79147e ("printk: Prepare for SRCU console list protection") Signed-off-by: John Ogness --- kernel/printk/printk.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 612c73333848..c7c0ee2b47eb 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -282,6 +282,7 @@ EXPORT_SYMBOL(console_list_unlock); * Return: A cookie to pass to console_srcu_read_unlock(). */ int console_srcu_read_lock(void) + __acquires(&console_srcu) { return srcu_read_lock_nmisafe(&console_srcu); } @@ -295,6 +296,7 @@ EXPORT_SYMBOL(console_srcu_read_lock); * Counterpart to console_srcu_read_lock() */ void console_srcu_read_unlock(int cookie) + __releases(&console_srcu) { srcu_read_unlock_nmisafe(&console_srcu, cookie); } --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B48E15E80B for ; Tue, 2 Apr 2024 22:11:35 +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=1712095898; cv=none; b=KXB6qLHJem3p03fID9aiC647nMbWi2IP3N3twbiko8MFnj+NXO/DJc/7ewQcBpK2uzFIe9q3a1oqlUIRHtXUAuKpBkMRzYKe9yDsnESI1W6jkDmXGP8GIMT5ryH4BC+0IJPDqHxEJvRrqtn1umJx5kyd7s4dqqt6Bu3SIvRE874= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095898; c=relaxed/simple; bh=8gNFB2aN+FDgLO4RQJ229KG75St1FCE48jzNGYZ/Lqg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=aXSMFBVmzKkWfs2b4Cf2CxJMlZKGly0trBkW8kzS+87YHmjp8X8A1ToeCuld2uBbuGma1cthR1yddVWy9HY2qiDTEPRG6GoZZQqbYcw4Dsld4teH72VqRahNfMd6RPS8Rx+1ePADw4vWFPGKUnF3yMHRMjJJWarXlddhWTHqeT0= 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=STpUA7WP; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=abVaAe2b; 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="STpUA7WP"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="abVaAe2b" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095893; 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=clMqsySLzP4iDk2N/9w8Tlak39hMNqLWvIbOFYB9iUM=; b=STpUA7WP46KPFCg+EABRpi8ZJ6z/BmNct4cP7i+RwGUPsllyQHfuOQ+k2DBxop+S3tB/+7 94dn2PxGgNfTVf7KVzLRwSV55dp7e0kyaSs51M4EOyeWoNEP2Mdv2P8AC+yVvZAXOFofJP RIzADPKjRwFGQ2UNIJOkuh8sUEDDPQlpOtwZFosbcOlfV7QtKz2plb/GvSJxHUlUaV26i/ vhKW7MuSXFaUyrC3PYuxnZrv5UQy3Xb0fMxm0LL7oMMJX0wnYaFABsV13xEkSUDjuUt8iF JUxAHXeCamecG7l+Bf/Wm6O5M/GmMPe8z3DEac/GbuR+T4kuJ+8YZ9tC0iQCVQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095893; 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=clMqsySLzP4iDk2N/9w8Tlak39hMNqLWvIbOFYB9iUM=; b=abVaAe2bY6SNXtus/u286s8LbuB9sQPtVZn8ycCUKkX8cKJN1oBzqzjCSCFWh2ZTly1HA6 DJPkCPYV5dEy8RCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 02/27] printk: Properly deal with nbcon consoles on seq init Date: Wed, 3 Apr 2024 00:17:04 +0206 Message-Id: <20240402221129.2613843-3-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" If a non-boot console is registering and boot consoles exist, the consoles are flushed before being unregistered. This allows the non-boot console to continue where the boot console left off. If for whatever reason flushing fails, the lowest seq found from any of the enabled boot consoles is used. Until now con->seq was checked. However, if it is an nbcon boot console, the function nbcon_seq_read() must be used to read seq because con->seq is not updated for nbcon consoles. Check if it is an nbcon boot console and if so call nbcon_seq_read() to read seq. Also setup the nbcon sequence number and reset the legacy sequence number from register_console() (rather than in nbcon_init() and nbcon_seq_force()). This removes all legacy sequence handling from nbcon.c so the code is easier to follow and maintain. Signed-off-by: John Ogness --- kernel/printk/nbcon.c | 7 +------ kernel/printk/printk.c | 29 ++++++++++++++++++++++++----- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index c8093bcc01fe..d741659d26ec 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -172,9 +172,6 @@ void nbcon_seq_force(struct console *con, u64 seq) u64 valid_seq =3D max_t(u64, seq, prb_first_valid_seq(prb)); =20 atomic_long_set(&ACCESS_PRIVATE(con, nbcon_seq), __u64seq_to_ulseq(valid_= seq)); - - /* Clear con->seq since nbcon consoles use con->nbcon_seq instead. */ - con->seq =3D 0; } =20 /** @@ -964,8 +961,6 @@ bool nbcon_alloc(struct console *con) * * nbcon_alloc() *must* be called and succeed before this function * is called. - * - * This function expects that the legacy @con->seq has been set. */ void nbcon_init(struct console *con) { @@ -974,7 +969,7 @@ void nbcon_init(struct console *con) /* nbcon_alloc() must have been called and successful! */ BUG_ON(!con->pbufs); =20 - nbcon_seq_force(con, con->seq); + nbcon_seq_force(con, 0); nbcon_state_set(con, &state); } =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index c7c0ee2b47eb..b7e52b3f3e96 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3348,6 +3348,7 @@ static void try_enable_default_console(struct console= *newcon) newcon->flags |=3D CON_CONSDEV; } =20 +/* Set @newcon->seq to the first record this console should print. */ static void console_init_seq(struct console *newcon, bool bootcon_register= ed) { struct console *con; @@ -3396,11 +3397,20 @@ static void console_init_seq(struct console *newcon= , bool bootcon_registered) =20 newcon->seq =3D prb_next_seq(prb); for_each_console(con) { - if ((con->flags & CON_BOOT) && - (con->flags & CON_ENABLED) && - con->seq < newcon->seq) { - newcon->seq =3D con->seq; + u64 seq; + + if (!((con->flags & CON_BOOT) && + (con->flags & CON_ENABLED))) { + continue; } + + if (con->flags & CON_NBCON) + seq =3D nbcon_seq_read(con); + else + seq =3D con->seq; + + if (seq < newcon->seq) + newcon->seq =3D seq; } } =20 @@ -3517,9 +3527,18 @@ void register_console(struct console *newcon) newcon->dropped =3D 0; console_init_seq(newcon, bootcon_registered); =20 - if (newcon->flags & CON_NBCON) + if (newcon->flags & CON_NBCON) { nbcon_init(newcon); =20 + /* + * nbcon consoles have their own sequence counter. The legacy + * sequence counter is reset so that it is clear it is not + * being used. + */ + nbcon_seq_force(newcon, newcon->seq); + newcon->seq =3D 0; + } + /* * Put this console in the list - keep the * preferred driver at the head of the list. --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 568E315E816 for ; Tue, 2 Apr 2024 22:11:35 +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=1712095898; cv=none; b=OAEMZcmsRUBe3aIjGuVFq5rEoqrOLr3G6jLQMV5IbI70qMQqNP7IXYl6RoYzuJpF6bjSr2Iv1hhSIGtkMgFkBwh76mRcPEBfNNlDKOB73UTYfbRV3XD3EgRXtPIr+8fiRQz0j1/wx7DO51G+AI4VvbQnI5OqP01sdJIepqbf/8Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095898; c=relaxed/simple; bh=/0MxrjHFyOtBJDmRT8XjtwDOoZX0ohrlr35f/aHxS2Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kRoIrKynzp0zcsh0dYv+pvupsWUY9w5XfofFZ/xdHXIl5cEYa2ND3H/F0+5qhtAsKs3LhOYGfvZdH7vkybkciiMI2qmb/oZHOcFYsIh6mnf7TTzO83CJm0T9Kfyjb1GhTHG78JNjRreJMxtpsCXH8v9//WEIaDW/B29OHKE2KxA= 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=gOta4gtJ; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=GTrtdyTu; 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="gOta4gtJ"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="GTrtdyTu" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095894; 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=nd9y0ptddjz3VGhzIqvJOBfqj/DRxNA65wb3bV/cuvY=; b=gOta4gtJ6uOdbb3XFjZVywi1IvDppk6CbumLId+in4AwYsVieFFT+PYirKiFMMe4Mzz8ch pefePFlLqWzztw0pTBLkydiQln1mOta1CeGkIt9KVNdrq5E7cSWoa+2qrYbFHt6JrksOmo BW9D4/6diQ4BEnVzR5wzucq4tj/0prwaTEvpBlJMTzw3k/+DVGYxLhHf9/lI30illfAa/L cjvT7oSblViUYWH1L9oDdtkWpsKClXo4DDJ+EVE5oFzJt5lx0UcXtMQ+0EOMwTNFIXP5Zb P3MQ4UjmRjj+0ZqKTlztnzYl92IsKonFCbTEwTdHbMltmzsiCdelGDPILgx1kg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095894; 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=nd9y0ptddjz3VGhzIqvJOBfqj/DRxNA65wb3bV/cuvY=; b=GTrtdyTuYj3sX6Q/3QBdgs9fFHNA2Q+XV+aRiRWhbH94gb/XYQGrNiMqm/smRpRv2sSUtB KdMOK0sxfTd7pyDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 03/27] printk: nbcon: Remove return value for write_atomic() Date: Wed, 3 Apr 2024 00:17:05 +0206 Message-Id: <20240402221129.2613843-4-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The return value of write_atomic() does not provide any useful information. On the contrary, it makes things more complicated for the caller to appropriately deal with the information. Change write_atomic() to not have a return value. If the message did not get printed due to loss of ownership, the caller will notice this on its own. If ownership was not lost, it will be assumed that the driver successfully printed the message and the sequence number for that console will be incremented. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 2 +- kernel/printk/nbcon.c | 15 +++++++-------- 2 files changed, 8 insertions(+), 9 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 779d388af8a0..54b98e4f0544 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -327,7 +327,7 @@ struct console { struct hlist_node node; =20 /* nbcon console specific members */ - bool (*write_atomic)(struct console *con, + void (*write_atomic)(struct console *con, struct nbcon_write_context *wctxt); atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index d741659d26ec..2516449f921d 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -849,7 +849,6 @@ static bool nbcon_emit_next_record(struct nbcon_write_c= ontext *wctxt) unsigned long con_dropped; struct nbcon_state cur; unsigned long dropped; - bool done; =20 /* * The printk buffers are filled within an unsafe section. This @@ -889,16 +888,16 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt) wctxt->unsafe_takeover =3D cur.unsafe_takeover; =20 if (con->write_atomic) { - done =3D con->write_atomic(con, wctxt); + con->write_atomic(con, wctxt); } else { - nbcon_context_release(ctxt); + /* + * This function should never be called for legacy consoles. + * Handle it as if ownership was lost and try to continue. + */ WARN_ON_ONCE(1); - done =3D false; - } - - /* If not done, the emit was aborted. */ - if (!done) + nbcon_context_release(ctxt); return false; + } =20 /* * Since any dropped message was successfully output, reset the --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1164B15ECD9 for ; Tue, 2 Apr 2024 22:11:36 +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=1712095898; cv=none; b=q250+hZ1S/60hl5ndwclzvitLdNbifQk/56BRyJ/DGcfvbgBWkZFm8vxH50Xd9k1Yd7cJHB2AZNbLwDzCghQOU1NveTjuEsAtn26C/QQvfE+OAYrDUBr0WzR5CYLTqIKGHT7z2zRVHT+Eny30Q3BKmDGrUr5zpFDMuKo4Y7MdRs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095898; c=relaxed/simple; bh=jrBdWUk0rwcYI/oiQlw6iijdJI0T4nkboPn0QdreNXc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=MwVJwpDlYnyYT8hZC1owkeKDVAobBDOGVvVuEOaMgmXgny2S7ydZPwfDJ8sfpvW2IyzLP2CHhV6cgEi+yh8/Ymdgp19KJmSkR5Nu1kThXrZhJ38pmNT+BlRpsKEErNlz4v+KaAUkue1Oe2vHxBUv70nzccH7NS6r3OJDBt5SlyU= 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=JmpaqF7h; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=It7qU4S/; 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="JmpaqF7h"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="It7qU4S/" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095895; 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=j7f10dybj3FQO66c59r1wqvQXH3mAcKwMAOFWXgWZuw=; b=JmpaqF7hZ4FLu5RF6TYpoK+3EfVmWLLlR1kWZ951pqeuaLSVsruuIVzTy6aaO/tC/Dxm9d hFvGW0qCA4jzr8usSgmVTikp0EqQ/j+2QhvNHf6TWoN71TDBYcqiLZb5aTnNIn95gcVe/o tPQ1jvi88p0C5VnvaBkElqTJbKX4X/ZQbHQH1C3tPgOb3uuj3Y8bg83Pj6sH4tigHzI3K5 4G+Hpcu8KxOKSNcfSeZVhnzpu/BvQVCjIqMmiAH73JUmRiUrOyWDANmc7Zf4RCoUqE+3u3 +eh7DqaBWXlJzVKJ8uMd78Mkus0+6QrmGMyBHEf4uqAiEMQYs91RjCzsrD/azQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095895; 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=j7f10dybj3FQO66c59r1wqvQXH3mAcKwMAOFWXgWZuw=; b=It7qU4S/vnwflESxCbemUum5nBq739Y9mSUdt7m9223da5g7xjBH38haOBKxn7mA0UQIjv VnrH766dSMsTngDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Sebastian Andrzej Siewior Subject: [PATCH printk v4 04/27] printk: Check printk_deferred_enter()/_exit() usage Date: Wed, 3 Apr 2024 00:17:06 +0206 Message-Id: <20240402221129.2613843-5-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Sebastian Andrzej Siewior Add validation that printk_deferred_enter()/_exit() are called in non-migration contexts. Signed-off-by: Sebastian Andrzej Siewior Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 9 +++++---- kernel/printk/internal.h | 3 +++ kernel/printk/printk_safe.c | 12 ++++++++++++ 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 2fde40cc9677..d8b3f51d9e98 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -157,15 +157,16 @@ int _printk(const char *fmt, ...); */ __printf(1, 2) __cold int _printk_deferred(const char *fmt, ...); =20 -extern void __printk_safe_enter(void); -extern void __printk_safe_exit(void); +extern void __printk_deferred_enter(void); +extern void __printk_deferred_exit(void); + /* * The printk_deferred_enter/exit macros are available only as a hack for * some code paths that need to defer all printk console printing. Interru= pts * must be disabled for the deferred duration. */ -#define printk_deferred_enter __printk_safe_enter -#define printk_deferred_exit __printk_safe_exit +#define printk_deferred_enter() __printk_deferred_enter() +#define printk_deferred_exit() __printk_deferred_exit() =20 /* * Please don't use printk_ratelimit(), because it shares ratelimiting sta= te diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6c2afee5ef62..4e0edcb3c311 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -53,6 +53,9 @@ int vprintk_store(int facility, int level, __printf(1, 0) int vprintk_default(const char *fmt, va_list args); __printf(1, 0) int vprintk_deferred(const char *fmt, va_list args); =20 +void __printk_safe_enter(void); +void __printk_safe_exit(void); + bool printk_percpu_data_ready(void); =20 #define printk_safe_enter_irqsave(flags) \ diff --git a/kernel/printk/printk_safe.c b/kernel/printk/printk_safe.c index 6d10927a07d8..4421ccac3113 100644 --- a/kernel/printk/printk_safe.c +++ b/kernel/printk/printk_safe.c @@ -26,6 +26,18 @@ void __printk_safe_exit(void) this_cpu_dec(printk_context); } =20 +void __printk_deferred_enter(void) +{ + cant_migrate(); + __printk_safe_enter(); +} + +void __printk_deferred_exit(void) +{ + cant_migrate(); + __printk_safe_exit(); +} + asmlinkage int vprintk(const char *fmt, va_list args) { #ifdef CONFIG_KGDB_KDB --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF71615ECFF for ; Tue, 2 Apr 2024 22:11:38 +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=1712095900; cv=none; b=WYvorTAeCnSFt8cXIaYsLo33Hdm4TXsUhVcMGjJ7hzfvucAJj6TZqOzMPEHtnozflAULP+llyWnzNUB40PKzCVseMJMTrZQexGYYxv6TZIKMQsq+c8/1HicepYos+B+qE6vH/wokkXOeBAQAHsiOT4JhVBr0tlZZGv6X+HAf81k= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095900; c=relaxed/simple; bh=21FdHB8UvH4vxd4uNwfUpHVEPPOEDJI2uyu9KCT428w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=adAH9XTJbzTOJcaiefOiJNbqO0PfrQrR3CziITJnV1INDf/gRuMRlDG2JfCx0cqb/7g67+vqkLTRPM47EpYkH2Z3PjeXVyMGIaIZPStT6Obyqvy4TrQ7pS7xO20gBfg1QNUMy0Ti4cSjVa2rqerSDQTLYsoAIqZE4DOeYwWaYNo= 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=24p1yYvz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=yS099pn3; 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="24p1yYvz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="yS099pn3" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095895; 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=unFh0IQLca0RDLCECJ8AQ28E69e46Gh0slqQ0uqHhBI=; b=24p1yYvzeQzyN6QpK1ZVxPWcIipV8U50Z1YXxDS27hUChffQRgSJxzSL2q60TmiMpB4y0G FTNzV96KAGkgxLu3rGeTCN/eT5PlX8Dd+P6ipC8eKBD7LqbvVxe/sZMlGW+gn0ofXxZit5 UzyBXaEGEeyZygdFhQ0l+1cH+yznC2TUtwyi9Wzs+qRr1aTryJDf9iy9CL1zKBdKzxbcYr C6E2hQxW/wfKczJR/q7VyYp5Mjsvvptu3dEEBo09+oX7SdbS8EfAOtXTaJeyF1kqGBFCg4 ZY7i08mmES+mkUWL10yxRi5tEK/lfTqvKhMVkK11Hsbzk3c5YlWnaop7b7EXsA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095895; 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=unFh0IQLca0RDLCECJ8AQ28E69e46Gh0slqQ0uqHhBI=; b=yS099pn3rNLKW/Tlevuw3AD/qCG4pMi+LEHd2tCQu4VNWmZxhaTMSIyT9dzpyaMjVQPEqI fjZX4A1uQV5Iq7Ag== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 05/27] printk: nbcon: Add detailed doc for write_atomic() Date: Wed, 3 Apr 2024 00:17:07 +0206 Message-Id: <20240402221129.2613843-6-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The write_atomic() callback has special requirements and is allowed to use special helper functions. Provide detailed documentation of the callback so that a developer has a chance of implementing it correctly. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 31 +++++++++++++++++++++++++++---- 1 file changed, 27 insertions(+), 4 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 54b98e4f0544..e4028d4079e1 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -285,7 +285,7 @@ struct nbcon_write_context { /** * struct console - The console descriptor structure * @name: The name of the console driver - * @write: Write callback to output messages (Optional) + * @write: Legacy write callback to output messages (Optional) * @read: Read callback for console input (Optional) * @device: The underlying TTY device driver (Optional) * @unblank: Callback to unblank the console (Optional) @@ -302,7 +302,6 @@ struct nbcon_write_context { * @data: Driver private data * @node: hlist node for the console list * - * @write_atomic: Write callback for atomic context * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @pbufs: Pointer to nbcon private buffer @@ -327,8 +326,32 @@ struct console { struct hlist_node node; =20 /* nbcon console specific members */ - void (*write_atomic)(struct console *con, - struct nbcon_write_context *wctxt); + + /** + * @write_atomic: + * + * NBCON callback to write out text in any context. + * + * This callback is called with the console already acquired. The + * callback can use nbcon_can_proceed() at any time to verify that + * it is still the owner of the console. In the case that it has + * lost ownership, it is no longer allowed to go forward. In this + * case it must back out immediately and carefully. The buffer + * content is also no longer trusted since it no longer belongs to + * the context. + * + * If the callback needs to perform actions where ownership is not + * allowed to be taken over, nbcon_enter_unsafe() and + * nbcon_exit_unsafe() can be used to mark such sections. These + * functions are also points of possible ownership transfer. If + * either function returns false, ownership has been lost. + * + * This callback can be called from any context (including NMI). + * Therefore it must avoid usage of any locking and instead rely + * on the console ownership for synchronization. + */ + void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); + atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct printk_buffers *pbufs; --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id AF6D115ECFE for ; Tue, 2 Apr 2024 22:11:38 +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=1712095900; cv=none; b=rMg3SHxFpdkHo0c6Ly6W2CtrvMKQnDud0EvzS6TqX/2NYpnTG3zlLO4NQyZlarS6W5bvPhcUFL7ZWIRpzTe6oxIsU5w03ec6SuJpKLQh2AZYxowoqm+A5uZvk/w1SCj9WnG8ouc5cngGPlLloUa0Ihs5CyQiICV3fk2mY9VQfJs= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095900; c=relaxed/simple; bh=ZGfKxGfEmF/DaQH7qHtPYDaH5xaSTIeExUXAHoqBHGg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=quEqA/j56g5r375Jkdt8n47FH11EredXl2aCmusCCBIfOvqj4pHxVQaufIfp5tUlmMU2zw2Gb9mWk7rUDbJImXnEtRLhyJMZX+hEGibjVh/ltdzKI+2GLKtu2gaeKUZ+MhQIvKUcpYNW4y5DEG0nOVuVbzGedgNohPZIw8it2n4= 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=hFXhDRIa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UIYUPjc+; 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="hFXhDRIa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UIYUPjc+" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095896; 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=8yB2aLQdKE7JgW5QmS0138amGRuUP36LU9EqlH92t8Q=; b=hFXhDRIaUS1GUQ62DUUuwVndZc60ypo15bP5KCOzzx+kKEZbS/KroAQvZtjkJPufWAxmd8 J2KHlOB7Ow4swEPorfTPxmn/x4AZFgLOU3hGA5b9JKS+IPNh0Y+WlOil9a2ZNEsnu5A24E /2RettyikmlfQoWFlpbWqBfB7+/cDJ52stfULtAIXwZk8ddrjcXqX48LamEtedpNw5l78u BQ7S0O8PNTh0nd+eBnnSnvjoUyqKlALXr1+Fmaj4D2CFdyep9BlbgYVMw3luVlrHpHJz/q BB6OMR9cEHVn2WF2/AYtpZxc9TjlFxpdEcxzw0qQ2fsfvDVZlmHRoDFdFjo9Mg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095896; 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=8yB2aLQdKE7JgW5QmS0138amGRuUP36LU9EqlH92t8Q=; b=UIYUPjc+DAKmgQcthYjmBgxwriDD1XFrnT4cdAJ5Fg5doID6zeHrZW8AOKFK9pD2oye+WG D067ObgZ2qnyedCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 06/27] printk: nbcon: Add callbacks to synchronize with driver Date: Wed, 3 Apr 2024 00:17:08 +0206 Message-Id: <20240402221129.2613843-7-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Console drivers typically must deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls. Follow-up commits require that the printk subsystem is able to synchronize with the driver. Require nbcon consoles to implement two new callbacks (device_lock(), device_unlock()) that will use whatever synchronization mechanism the driver is using for itself (for example, the port lock for uart serial consoles). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 42 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index e4028d4079e1..ad85594e070e 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -352,6 +352,48 @@ struct console { */ void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); =20 + /** + * @device_lock: + * + * NBCON callback to begin synchronization with driver code. + * + * Console drivers typically must deal with access to the hardware + * via user input/output (such as an interactive login shell) and + * output of kernel messages via printk() calls. This callback is + * called by the printk-subsystem whenever it needs to synchronize + * with hardware access by the driver. It should be implemented to + * use whatever synchronization mechanism the driver is using for + * itself (for example, the port lock for uart serial consoles). + * + * This callback is always called from task context. It may use any + * synchronization method required by the driver. BUT this callback + * MUST also disable migration. The console driver may be using a + * synchronization mechanism that already takes care of this (such as + * spinlocks). Otherwise this function must explicitly call + * migrate_disable(). + * + * The flags argument is provided as a convenience to the driver. It + * will be passed again to device_unlock(). It can be ignored if the + * driver does not need it. + */ + void (*device_lock)(struct console *con, unsigned long *flags); + + /** + * @device_unlock: + * + * NBCON callback to finish synchronization with driver code. + * + * It is the counterpart to device_lock(). + * + * This callback is always called from task context. It must + * appropriately re-enable migration (depending on how device_lock() + * disabled migration). + * + * The flags argument is the value of the same variable that was + * passed to device_lock(). + */ + void (*device_unlock)(struct console *con, unsigned long flags); + atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct printk_buffers *pbufs; --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 64E0D15EFB7 for ; Tue, 2 Apr 2024 22:11:39 +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=1712095901; cv=none; b=T8Hp8h2FD2prrR4+zZFkKlmmMQi6Oe/u7nCvHXayz6ZoZVEELnjt9tM692fza2JaVbdD1Qr8djO6rS5WUQaQTRHIOpEdKxLFH1WlQa+BpMhHeIsJa/ONnqzcjMjpYDvPWDB3e8yAozUoVFaw6759QQCbVD10y+7RgytBkEyB5Bo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095901; c=relaxed/simple; bh=BL7aciVtdhyBPsYEVBpNHekCxnl1nHBbTv2QZkJVau0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nV04oZr2CJhBDsl500KCPTU+Sqk/ZSC5H6+7vfY86q5vj7DUWp1wTCYhvVn8VYyLAC4vGkXuEUABeHUCXmyQ1M+xfc//JqSTCg6TlqFKEMIhkWdKjsA7yUGDTKiAoFGjMvn8FFBVkw96Xl6j5P7PnQI4a7z8duThkmdKx95hquA= 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=MgmJroNo; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nd11a3Qd; 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="MgmJroNo"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nd11a3Qd" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095896; 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=OvqMC7tvfcOdwAxpMAcwYIY/7Ftke4WwpmbtwMe36rM=; b=MgmJroNoSgntuBNnk/JNtvXlfv3FMAZUrKbSLeSvFPZzWLOpd/17SGZI7TuNfcNCo1lfFk KKTqsCxyG81tjTZho05NXObJ3yN/xVUK9OZ8hC7eRzlS9NvMcaAUFqn/fNTdr2bC5/RzkA SDltAEJv2/iTa09JCyR4cgkzCmqVugogoiCvt8SWnx0f1JIVHswHcrkOeA2u+mEKruyb04 5zVB/7rx6opKg4F9CM6wLTZA2WqQ7/BIbjKHWYqPm303bpKG07DHgMFWq8B1n6wTNdCTQh EsQsUmRmp2gWp5rxltXlvyG23pywBY3viPe+9iElbnXzCLJwnsChZyahxEEihQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095896; 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=OvqMC7tvfcOdwAxpMAcwYIY/7Ftke4WwpmbtwMe36rM=; b=nd11a3QdIaOP1bHTF1XOOKFa0cyJUpm3KinO1YHj6UEo8NmZGcSBbyZ3vO5D8+on5l2Bcx 3VkcScatHskklcCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 07/27] printk: nbcon: Use driver synchronization while registering Date: Wed, 3 Apr 2024 00:17:09 +0206 Message-Id: <20240402221129.2613843-8-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-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" Depending on if an nbcon console is registered, a driver may handle its internal locking differently. If a driver is holding its internal lock while the nbcon console is registered, there may be a risk that two different contexts access the hardware simultaneously without synchronization. (For example, if the printk subsystem invokes atomic printing while another driver context acquired the internal lock without considering the atomic console.) Use the driver synchronization while a registering nbcon console transitions to being registered. This guarantees that if the driver acquires its internal lock when the nbcon console was not registered, it will remain unregistered until that context releases the lock. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index b7e52b3f3e96..cd32648372a0 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3448,6 +3448,7 @@ void register_console(struct console *newcon) struct console *con; bool bootcon_registered =3D false; bool realcon_registered =3D false; + unsigned long flags; int err; =20 console_list_lock(); @@ -3539,6 +3540,19 @@ void register_console(struct console *newcon) newcon->seq =3D 0; } =20 + /* + * If another context is actively using the hardware of this new + * console, it will not be aware of the nbcon synchronization. This + * is a risk that two contexts could access the hardware + * simultaneously if this new console is used for atomic printing + * and the other context is still using the hardware. + * + * Use the driver synchronization to ensure that the hardware is not + * in use while this new console transitions to being registered. + */ + if ((newcon->flags & CON_NBCON) && newcon->write_atomic) + newcon->device_lock(newcon, &flags); + /* * Put this console in the list - keep the * preferred driver at the head of the list. @@ -3563,6 +3577,10 @@ void register_console(struct console *newcon) * register_console() completes. */ =20 + /* This new console is now registered. */ + if ((newcon->flags & CON_NBCON) && newcon->write_atomic) + newcon->device_unlock(newcon, flags); + console_sysfs_notify(); =20 /* --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8192F15EFBE; Tue, 2 Apr 2024 22:11:39 +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=1712095901; cv=none; b=skC+nYvAMzO2W7ZH8tLb5v6UyhAWpJnEyTn97Y8ZWpCmdjqrxxtg9yyWxPhNIryOO+/yRVLBtBD6oUSMZh/cM3J+0e0noTDu3ivFltnr6EudkahLOm2spIfpOlDsSoZYDVTBTR7Ue2M/OMDR3w1vKJUSqoGXePc+vf6eqx5GTjM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095901; c=relaxed/simple; bh=/X5uefQjhA750hOZTgQlvyUovMXIIaaI0zWJuuBr4mg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=D/UTyo8ERb5QH2ivoEwuh+PWsMa6fxF9FgyYm7hWMvk5A8BbuNDYwMNkTnzmAWpNHYAwgth9k5uvllJKZq22upEqFjT0H8J9M6IzmkR7eBFYby7UFwDJ+T+HFRq5kd19YaSmWaSdJ8QKD67Jl2DlKmLzLg2/6F4WIZhvhV5T0sw= 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=pV1zFXK7; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zluu/oDS; 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="pV1zFXK7"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zluu/oDS" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095897; 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=np/D3T0A0n/PKPue9SIuCwjpbic3bBzP+cAanQ+lkJw=; b=pV1zFXK7RAbppXFv2IS8dg+AhtNAnjopkWGUF2CklgX9leSv1mvngtanG0zDD1M9HcNiHt 8fgHYo5P8BbLt/aOwP3OikrckUHgIXnFXFsFnXgxKX6BmLbStdCMdhu9kpQFllBVHoQg0h fj0rw92Zqk/LjGUqdggnaA60qWJODj5iJZjC9jGuIgMQqR8SCMdwE5PNERKXlHQiHLXheJ BbP2Umkls+hFnaKPkQFa4K9qFA79Cu2raSBpUaIArQx1ryayBkqFCkT9MK3nowVRGRGkOG EYSjzfuOKFdN1ETE8z4xRRDNz47WK0PjleGmNUApfrmwkJjihrO7Xf8FImquyA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095897; 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=np/D3T0A0n/PKPue9SIuCwjpbic3bBzP+cAanQ+lkJw=; b=zluu/oDSmszLBYJ/hpp2azudGkY5hPIYhQCXTs+b4t6Ld8QirZSsrSrRdoTSFQ98bEea+L RS7F5AOcNjPlSuAg== 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 v4 08/27] serial: core: Provide low-level functions to lock port Date: Wed, 3 Apr 2024 00:17:10 +0206 Message-Id: <20240402221129.2613843-9-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It will be necessary at times for the uart nbcon console drivers to acquire the port lock directly (without the additional nbcon functionality of the port lock wrappers). These are special cases such as the implementation of the device_lock()/device_unlock() callbacks or for internal port lock wrapper synchronization. Provide low-level variants __uart_port_lock_irqsave() and __uart_port_unlock_irqrestore() for this purpose. Signed-off-by: John Ogness Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek --- include/linux/serial_core.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 55b1f3ba48ac..bb3324d49453 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -588,6 +588,24 @@ struct uart_port { void *private_data; /* generic platform data pointer */ }; =20 +/* + * Only for console->device_lock()/_unlock() callbacks and internal + * port lock wrapper synchronization. + */ +static inline void __uart_port_lock_irqsave(struct uart_port *up, unsigned= long *flags) +{ + spin_lock_irqsave(&up->lock, *flags); +} + +/* + * Only for console->device_lock()/_unlock() callbacks and internal + * port lock wrapper synchronization. + */ +static inline void __uart_port_unlock_irqrestore(struct uart_port *up, uns= igned long flags) +{ + spin_unlock_irqrestore(&up->lock, flags); +} + /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 82C8115F30B; Tue, 2 Apr 2024 22:11:40 +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=1712095902; cv=none; b=Ec9ZOi1kjriZ49nDOaEDIJfmtrKSpd/5pJeTswC0u4kIlUtj3BNwNHJAVmPrHWx8DdgztkdPmdcwtFhtubxwcECpT6qLRGpTlMlq6Ht3kFuLr1jSEKrwXuMcLXdEBdAE5692+YEZGgpOrkGGoAnKftM5VHf5cFi26M8J+b2oKYQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095902; c=relaxed/simple; bh=mtGOEUKfUQNAmRmQTQh4Ma6J4SJ6z1AyObI0bodMtgQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NADd/TiWU4KkU2PvpGqsKbOnF2lDVBdA9ts2ykvDQcFb4xS6IvBi6R+PVXBnTTQcCOoELSf6tQwq/yc8pT4jGAkM67qX3Q0XTgM9vpyBzb6uWH2VRj3uhc8JP+UEbdocr7v3SS/XPXKLvUJsCjvfXIDb1zq8jWV0SZLMUyjMOsg= 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=BACRPbKa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=T6bvFuay; 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="BACRPbKa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="T6bvFuay" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095898; 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=qFbsuCl5o+zgXOz4kC55LRN5ZtRf7uxaMfN2yuGp6v8=; b=BACRPbKa4EKWfdgPrzKfP22fxcbcn0GXh593LKVkQTeXKx+HfZWUkN7RwmXr/iYvI7bqbo FgIvmBGLNNNF7g33/vM/+LXw9Nzl4U+6HXiUY+D7S3uA/VGxSBBVmuojl1nixmwBFGhZYQ 0VPt2qyhSx50uLXvZmrL6niKfMm5fvpuZOSDl8z10Vt6B1hWIOBUlDB7ghm7rqQPPjdOHq TYlc29CunC2gaIznj9NrU1htC5pngoSwZcVyz4rIXd6RnNMcNYnuD5SSIXfYkSTPs6TIhp Wd8wyfKMxef5EEESNUECiiH2lRG0C3zyhYBZw0VxZUSYnZgzFbeiXiVMKQBjBA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095898; 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=qFbsuCl5o+zgXOz4kC55LRN5ZtRf7uxaMfN2yuGp6v8=; b=T6bvFuayD1Wkc6Cti/Z4rCW+wZDzH8DyfbJ69pGy93cYf16vwDPewxRlSdcQqe6TjTl2M/ 9Blu/upy18Iug3DQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , Russell King , Tony Lindgren , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , Andy Shevchenko , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , =?UTF-8?q?Th=C3=A9o=20Lebrun?= , Linus Walleij , Lino Sanfilippo , Fabio Estevam , Arnd Bergmann , linux-serial@vger.kernel.org Subject: [PATCH printk v4 09/27] printk: nbcon: Implement processing in port->lock wrapper Date: Wed, 3 Apr 2024 00:17:11 +0206 Message-Id: <20240402221129.2613843-10-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently the port->lock wrappers uart_port_lock(), uart_port_unlock() (and their variants) only lock/unlock the spin_lock. If the port is an nbcon console, the wrappers must also acquire/release the console and mark the region as unsafe. This allows general port->lock synchronization to be synchronized with the nbcon console ownership. Introduce a new struct nbcon_drvdata within struct console that provides the necessary components for the port lock wrappers to acquire the nbcon console and track its ownership. Also introduce uart_port_set_cons() as a wrapper to set @cons of a uart_port. The wrapper sets @cons under the port lock in order to prevent @cons from disappearing while another context owns the port lock via the port lock wrappers. Also cleanup the description of the console_srcu_read_flags() function. It is used by the port lock wrappers to ensure a console cannot be fully unregistered while another context owns the port lock via the port lock wrappers. Signed-off-by: John Ogness --- drivers/tty/serial/8250/8250_core.c | 6 +- drivers/tty/serial/amba-pl011.c | 2 +- drivers/tty/serial/serial_core.c | 2 +- include/linux/console.h | 57 +++++++++++++---- include/linux/printk.h | 13 ++++ include/linux/serial_core.h | 98 ++++++++++++++++++++++++++++- kernel/printk/nbcon.c | 52 +++++++++++++++ 7 files changed, 212 insertions(+), 18 deletions(-) diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/= 8250_core.c index b62ad9006780..41d74ee3d95a 100644 --- a/drivers/tty/serial/8250/8250_core.c +++ b/drivers/tty/serial/8250/8250_core.c @@ -627,11 +627,11 @@ static int univ8250_console_setup(struct console *co,= char *options) =20 port =3D &serial8250_ports[co->index].port; /* link port to console */ - port->cons =3D co; + uart_port_set_cons(port, co); =20 retval =3D serial8250_console_setup(port, options, false); if (retval !=3D 0) - port->cons =3D NULL; + uart_port_set_cons(port, NULL); return retval; } =20 @@ -689,7 +689,7 @@ static int univ8250_console_match(struct console *co, c= har *name, int idx, continue; =20 co->index =3D i; - port->cons =3D co; + uart_port_set_cons(port, co); return serial8250_console_setup(port, options, true); } =20 diff --git a/drivers/tty/serial/amba-pl011.c b/drivers/tty/serial/amba-pl01= 1.c index cf2c890a560f..347aacf8400f 100644 --- a/drivers/tty/serial/amba-pl011.c +++ b/drivers/tty/serial/amba-pl011.c @@ -2496,7 +2496,7 @@ static int pl011_console_match(struct console *co, ch= ar *name, int idx, continue; =20 co->index =3D i; - port->cons =3D co; + uart_port_set_cons(port, co); return pl011_console_setup(co, options); } =20 diff --git a/drivers/tty/serial/serial_core.c b/drivers/tty/serial/serial_c= ore.c index d6a58a9e072a..2652b4d5c944 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -3146,7 +3146,7 @@ static int serial_core_add_one_port(struct uart_drive= r *drv, struct uart_port *u uport->state =3D state; =20 state->pm_state =3D UART_PM_STATE_UNDEFINED; - uport->cons =3D drv->cons; + uart_port_set_cons(uport, drv->cons); uport->minor =3D drv->tty_driver->minor_start + uport->line; uport->name =3D kasprintf(GFP_KERNEL, "%s%d", drv->dev_name, drv->tty_driver->name_base + uport->line); diff --git a/include/linux/console.h b/include/linux/console.h index ad85594e070e..e7c35c686720 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -282,6 +282,25 @@ struct nbcon_write_context { bool unsafe_takeover; }; =20 +/** + * struct nbcon_drvdata - Data to allow nbcon acquire in non-print context + * @ctxt: The core console context + * @srcu_cookie: Storage for a console_srcu_lock cookie, if needed + * @owner_index: Storage for the owning console index, if needed + * @locked: Storage for the locked state, if needed + * + * All fields (except for @ctxt) are available exclusively to the driver to + * use as needed. They are not used by the printk subsystem. + */ +struct nbcon_drvdata { + struct nbcon_context __private ctxt; + + /* reserved for driver use */ + int srcu_cookie; + short owner_index; + bool locked; +}; + /** * struct console - The console descriptor structure * @name: The name of the console driver @@ -396,6 +415,21 @@ struct console { =20 atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; + + /** + * @nbcon_drvdata: + * + * Data for nbcon ownership tracking to allow acquiring nbcon consoles + * in non-printing contexts. + * + * Drivers may need to acquire nbcon consoles in non-printing + * contexts. This is achieved by providing a struct nbcon_drvdata. + * Then the driver can call nbcon_driver_acquire() and + * nbcon_driver_release(). The struct does not require any special + * initialization. + */ + struct nbcon_drvdata *nbcon_drvdata; + struct printk_buffers *pbufs; }; =20 @@ -425,28 +459,29 @@ extern void console_list_unlock(void) __releases(cons= ole_mutex); extern struct hlist_head console_list; =20 /** - * console_srcu_read_flags - Locklessly read the console flags + * console_srcu_read_flags - Locklessly read flags of a possibly registered + * console * @con: struct console pointer of console to read flags from * - * This function provides the necessary READ_ONCE() and data_race() - * notation for locklessly reading the console flags. The READ_ONCE() - * in this function matches the WRITE_ONCE() when @flags are modified - * for registered consoles with console_srcu_write_flags(). + * Locklessly reading @con->flags provides a consistent read value because + * there is at most one CPU modifying @con->flags and that CPU is using on= ly + * read-modify-write operations to do so. * - * Only use this function to read console flags when locklessly - * iterating the console list via srcu. + * Requires console_srcu_read_lock to be held, which implies that @con mig= ht + * be a registered console. If the caller is holding the console_list_lock= or + * it is certain that the console is not registered, the caller may read + * @con->flags directly instead. * * Context: Any context. + * Return: The current value of the @con->flags field. */ static inline short console_srcu_read_flags(const struct console *con) { WARN_ON_ONCE(!console_srcu_read_lock_is_held()); =20 /* - * Locklessly reading console->flags provides a consistent - * read value because there is at most one CPU modifying - * console->flags and that CPU is using only read-modify-write - * operations to do so. + * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified + * for registered consoles with console_srcu_write_flags(). */ return data_race(READ_ONCE(con->flags)); } diff --git a/include/linux/printk.h b/include/linux/printk.h index d8b3f51d9e98..0ad3ee752635 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -9,6 +9,8 @@ #include #include =20 +struct console; + extern const char linux_banner[]; extern const char linux_proc_banner[]; =20 @@ -193,6 +195,8 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); +extern void nbcon_driver_acquire(struct console *con); +extern void nbcon_driver_release(struct console *con); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -272,6 +276,15 @@ static inline void dump_stack(void) static inline void printk_trigger_flush(void) { } + +static inline void nbcon_driver_acquire(struct console *con) +{ +} + +static inline void nbcon_driver_release(struct console *con) +{ +} + #endif =20 bool this_cpu_in_panic(void); diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index bb3324d49453..9a73dee32ad9 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -8,10 +8,13 @@ #define LINUX_SERIAL_CORE_H =20 #include +#include #include #include #include #include +#include +#include #include #include #include @@ -606,6 +609,83 @@ static inline void __uart_port_unlock_irqrestore(struc= t uart_port *up, unsigned spin_unlock_irqrestore(&up->lock, flags); } =20 +/** + * uart_port_set_cons - Safely set the @cons field for a uart + * @up: The uart port to set + * @con: The new console to set to + * + * This function must be used to set @up->cons. It uses the port lock to + * synchronize with the port lock wrappers in order to ensure that the con= sole + * cannot change or disappear while another context is holding the port lo= ck. + */ +static inline void uart_port_set_cons(struct uart_port *up, struct console= *con) +{ + unsigned long flags; + + __uart_port_lock_irqsave(up, &flags); + up->cons =3D con; + __uart_port_unlock_irqrestore(up, flags); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_acquire(struct uart_port *up) +{ + lockdep_assert_held_once(&up->lock); + + if (likely(!uart_console(up))) + return; + + if (up->cons->nbcon_drvdata) { + /* + * If @up->cons is registered, prevent it from fully + * unregistering until this context releases the nbcon. + */ + int cookie =3D console_srcu_read_lock(); + + /* Ensure console is registered and is an nbcon console. */ + if (!hlist_unhashed_lockless(&up->cons->node) && + (console_srcu_read_flags(up->cons) & CON_NBCON)) { + WARN_ON_ONCE(up->cons->nbcon_drvdata->locked); + + nbcon_driver_acquire(up->cons); + + /* + * Record @up->line to be used during release because + * @up->cons->index can change while the port and + * nbcon are locked. + */ + up->cons->nbcon_drvdata->owner_index =3D up->line; + up->cons->nbcon_drvdata->srcu_cookie =3D cookie; + up->cons->nbcon_drvdata->locked =3D true; + } else { + console_srcu_read_unlock(cookie); + } + } +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_release(struct uart_port *up) +{ + lockdep_assert_held_once(&up->lock); + + /* + * uart_console() cannot be used here because @up->cons->index might + * have changed. Check against @up->cons->nbcon_drvdata->owner_index + * instead. + */ + + if (unlikely(up->cons && + up->cons->nbcon_drvdata && + up->cons->nbcon_drvdata->locked && + up->cons->nbcon_drvdata->owner_index =3D=3D up->line)) { + WARN_ON_ONCE(!up->cons->nbcon_drvdata->locked); + + up->cons->nbcon_drvdata->locked =3D false; + nbcon_driver_release(up->cons); + console_srcu_read_unlock(up->cons->nbcon_drvdata->srcu_cookie); + } +} + /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure @@ -613,6 +693,7 @@ static inline void __uart_port_unlock_irqrestore(struct= uart_port *up, unsigned static inline void uart_port_lock(struct uart_port *up) { spin_lock(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -622,6 +703,7 @@ static inline void uart_port_lock(struct uart_port *up) static inline void uart_port_lock_irq(struct uart_port *up) { spin_lock_irq(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -632,6 +714,7 @@ static inline void uart_port_lock_irq(struct uart_port = *up) static inline void uart_port_lock_irqsave(struct uart_port *up, unsigned l= ong *flags) { spin_lock_irqsave(&up->lock, *flags); + __uart_port_nbcon_acquire(up); } =20 /** @@ -642,7 +725,11 @@ static inline void uart_port_lock_irqsave(struct uart_= port *up, unsigned long *f */ static inline bool uart_port_trylock(struct uart_port *up) { - return spin_trylock(&up->lock); + if (!spin_trylock(&up->lock)) + return false; + + __uart_port_nbcon_acquire(up); + return true; } =20 /** @@ -654,7 +741,11 @@ static inline bool uart_port_trylock(struct uart_port = *up) */ static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigne= d long *flags) { - return spin_trylock_irqsave(&up->lock, *flags); + if (!spin_trylock_irqsave(&up->lock, *flags)) + return false; + + __uart_port_nbcon_acquire(up); + return true; } =20 /** @@ -663,6 +754,7 @@ static inline bool uart_port_trylock_irqsave(struct uar= t_port *up, unsigned long */ static inline void uart_port_unlock(struct uart_port *up) { + __uart_port_nbcon_release(up); spin_unlock(&up->lock); } =20 @@ -672,6 +764,7 @@ static inline void uart_port_unlock(struct uart_port *u= p) */ static inline void uart_port_unlock_irq(struct uart_port *up) { + __uart_port_nbcon_release(up); spin_unlock_irq(&up->lock); } =20 @@ -682,6 +775,7 @@ static inline void uart_port_unlock_irq(struct uart_por= t *up) */ static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsig= ned long flags) { + __uart_port_nbcon_release(up); spin_unlock_irqrestore(&up->lock, flags); } =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 2516449f921d..38328cf0fd5c 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -3,9 +3,12 @@ // Copyright (C) 2022 Intel, Thomas Gleixner =20 #include +#include #include #include +#include #include +#include #include "internal.h" /* * Printk console printing implementation for consoles which does not depe= nd @@ -988,3 +991,52 @@ void nbcon_free(struct console *con) =20 con->pbufs =3D NULL; } + +/** + * nbcon_driver_acquire - Acquire nbcon console and enter unsafe section + * @con: The nbcon console to acquire + * + * Context: Any context which could not be migrated to another CPU. + * + * Console drivers will usually use their own internal synchronization + * mechasism to synchronize between console printing and non-printing + * activities (such as setting baud rates). However, nbcon console drivers + * supporting atomic consoles may also want to mark unsafe sections when + * performing non-printing activities. + * + * This function acquires the nbcon console using priority NBCON_PRIO_NORM= AL + * and marks it unsafe for handover/takeover. + * + * Console drivers using this function must have provided @nbcon_drvdata in + * their struct console, which is used to track ownership and state + * information. + */ +void nbcon_driver_acquire(struct console *con) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con->nbcon_drvdata, ctxt); + + cant_migrate(); + + do { + do { + memset(ctxt, 0, sizeof(*ctxt)); + ctxt->console =3D con; + ctxt->prio =3D NBCON_PRIO_NORMAL; + } while (!nbcon_context_try_acquire(ctxt)); + + } while (!nbcon_context_enter_unsafe(ctxt)); +} +EXPORT_SYMBOL_GPL(nbcon_driver_acquire); + +/** + * nbcon_driver_release - Exit unsafe section and release the nbcon console + * @con: The nbcon console acquired in nbcon_driver_acquire() + */ +void nbcon_driver_release(struct console *con) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con->nbcon_drvdata, ctxt); + + if (nbcon_context_exit_unsafe(ctxt)) + nbcon_context_release(ctxt); +} +EXPORT_SYMBOL_GPL(nbcon_driver_release); --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id CAAF415F32D for ; Tue, 2 Apr 2024 22:11:40 +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=1712095902; cv=none; b=Oezi26pN+FypG3Dqr13ZH4XIKNcdMH9F7TUDmbQ93F0VG/SgJQ4+ELykDN1oKm6iSUfy4wunWymr3xd6vIl+ngi+XTg7vQk4LV18W1dHxYvXK+A916Zm52LbrvAtG8gMZpMK6SH2aFlBlhNfo3ESUIqqd7Q/saA9ShdrVz0mcxA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095902; c=relaxed/simple; bh=qgbf7wzyZyP2i2kvRkavpANgVDXSmqKEab0lXkm1WnQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FsKApNnaNDhXGEIJBS8BHdX/JKPE46nChZHseKdjtLglr751XYBPdcTyf7phVvlRi1I/DGp13PqKm3A7Bw8/dksdzF3FAcYWTo+1T0fJOOgLQodWnUlLu/hr5R+u8XqnWdiESHH/GYrIIUF0stuZtytthdP+amK1cG8C7OkRNfg= 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=vIVPjn+w; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=XpvPlopo; 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="vIVPjn+w"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="XpvPlopo" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095899; 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=0GzrUUzQwt9A0JfUhizcLMvUJMEtY0MSYaFGPPBVIb4=; b=vIVPjn+wUo/v3OmdzfNEc8Ag3X5youcZKjz9r8Y/LjVq3m4Y7i3BvL4PRSZO4yo6UL+Ixh m39MjS7PH+iWeaL/GePtooNLcgMtMB2QkkkIC8FcutCOP8VUPiirloEzxDSTyrKPUsvkr0 y5tZM0YZLt85/xifyNHMjqoEJMKYrWVG98I67LUC3lrFysZXDqnFH4EC80GlLCqG4/oF56 KZ/HG/U4uT0RePpCAQicDsNOjODdQj0H7urnHscZYaO6rAubFNk83NmFiJ4VzzyEWehxIo tSOTH1/7ybywI3VhYQ6PQES4xJBB4hgkEpqabkshz88Y7kPAvAs5BzUDIzDVlQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095899; 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=0GzrUUzQwt9A0JfUhizcLMvUJMEtY0MSYaFGPPBVIb4=; b=XpvPlopog8JXD/+Q7a622mIt9ySj1HKTo8ih2revZoWzg48eXZq2jshfDqDkYpa58MaF1b eY+SebAjyO8884AQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andy Shevchenko Subject: [PATCH printk v4 10/27] printk: nbcon: Do not rely on proxy headers Date: Wed, 3 Apr 2024 00:17:12 +0206 Message-Id: <20240402221129.2613843-11-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The headers kernel.h, serial_core.h, and console.h allow for the definitions of many types and functions from other headers. Rather than relying on these as proxy headers, explicitly include all headers providing needed definitions. Also sort the list alphabetically to be able to easily detect duplicates. Suggested-by: Andy Shevchenko Signed-off-by: John Ogness Acked-by: Petr Mladek Reviewed-by: Andy Shevchenko --- kernel/printk/internal.h | 8 ++++++-- kernel/printk/nbcon.c | 12 +++++++++++- kernel/printk/printk_ringbuffer.h | 2 ++ 3 files changed, 19 insertions(+), 3 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 4e0edcb3c311..c040fc8f1fd9 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -2,11 +2,12 @@ /* * internal.h - printk internal definitions */ -#include #include -#include "printk_ringbuffer.h" +#include +#include =20 #if defined(CONFIG_PRINTK) && defined(CONFIG_SYSCTL) +struct ctl_table; void __init printk_sysctl_init(void); int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void *buffer, size_t *lenp, loff_t *ppos); @@ -43,6 +44,9 @@ enum printk_info_flags { LOG_CONT =3D 8, /* text is a fragment of a continuation line */ }; =20 +struct printk_ringbuffer; +struct dev_printk_info; + extern struct printk_ringbuffer *prb; =20 __printf(4, 0) diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 38328cf0fd5c..1de6062d4ce3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -2,14 +2,24 @@ // Copyright (C) 2022 Linutronix GmbH, John Ogness // Copyright (C) 2022 Intel, Thomas Gleixner =20 -#include +#include #include #include #include +#include #include +#include +#include +#include +#include +#include #include +#include +#include #include +#include #include "internal.h" +#include "printk_ringbuffer.h" /* * Printk console printing implementation for consoles which does not depe= nd * on the legacy style console_lock mechanism. diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringb= uffer.h index 52626d0f1fa3..bd2a892deac1 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -5,6 +5,8 @@ =20 #include #include +#include +#include =20 /* * Meta information about each stored message. --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 683C615F3F5 for ; Tue, 2 Apr 2024 22:11:41 +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=1712095903; cv=none; b=CxZ536y4kR/FZXdRHLfZcvEmwOP+xUgk4bXYVW7bIwLp3W9YOeI9nsGTcnXaHwoKDlcBUOgy8YorMML9I4rhIkIOC1ohVQPOslSvVzHZL7c2oUU58N1JauntqghA7wcyrVk7GZFssXm54f34XGbBwyvc43ZWjZMbQzaOLxigBK8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095903; c=relaxed/simple; bh=H8Y6h4f+pnDIq01N0fQkcihCHtRyqbPCpn/ulCQW4A8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=R4q/AALsYF8UoJhvFRJFoY4AizPNFqBGDfp4tigrOFfYq4vNLQGvk6g13r+hwMUf0w3FTtDbqQ7ou4aWQeUni+bD9/L4ZsUucm5Wxq7lMUyllViZ6hc5alox271d90xi6AWznJoQdfyg/euAaUg/mIXQnHknKSuedXRdSHwiTBQ= 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=orRhzLBw; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7XGskGEa; 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="orRhzLBw"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7XGskGEa" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095899; 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=9Ht98dgt7fflhXi7xwdr8D666RniCtKRAaw59pcdcaw=; b=orRhzLBwweHWfysRCvB62UA+YhniOqlwKgKuGBc9/ggHICSRUGZao3N7LkUiA1+Jj2+3BX tt7V3hrjNd7J3qNh4RTWlQv7t/c5KN7UupU3qfJBt0a4s37wGH+dn4gWpPpwMlHK9l1aev /sxTeugGs8tuTSDlAby8x9q5Q1uWfY/tsUQIIEwvI4blYF1gAbHI5/+NsxqkGoqDsDUtSX 3RKsoC07aYAKhB2Hv354x2+9LKma5sDhzUfJu3VZjhFbkzsqXGIhYu0W0pnKQWyC+v+y5t i5f4Bq6OlXXt2PC8XYBabXzLGMxs9QAZXTOTZoETDSdCsGgf77plrtjCoaKQoQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095899; 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=9Ht98dgt7fflhXi7xwdr8D666RniCtKRAaw59pcdcaw=; b=7XGskGEaEanzfpedqUohrkuPUoZ2sgt2vEjj+i+a6RU43Y74/V+lZwN+5BXTOxLI3TUHAn wG+STzDT8Q6hx8BA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Randy Dunlap Subject: [PATCH printk v4 11/27] printk: nbcon: Fix kerneldoc for enums Date: Wed, 3 Apr 2024 00:17:13 +0206 Message-Id: <20240402221129.2613843-12-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Kerneldoc requires enums to be specified as such. Otherwise it is interpreted as function documentation. Signed-off-by: John Ogness Reviewed-by: Randy Dunlap Reviewed-by: Petr Mladek --- include/linux/console.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index e7c35c686720..5f1758891aec 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -137,7 +137,7 @@ static inline int con_debug_leave(void) */ =20 /** - * cons_flags - General console flags + * enum cons_flags - General console flags * @CON_PRINTBUFFER: Used by newly registered consoles to avoid duplicate * output of messages that were already shown by boot * consoles or read by userspace via syslog() syscall. @@ -218,7 +218,7 @@ struct nbcon_state { static_assert(sizeof(struct nbcon_state) <=3D sizeof(int)); =20 /** - * nbcon_prio - console owner priority for nbcon consoles + * enum nbcon_prio - console owner priority for nbcon consoles * @NBCON_PRIO_NONE: Unused * @NBCON_PRIO_NORMAL: Normal (non-emergency) usage * @NBCON_PRIO_EMERGENCY: Emergency output (WARN/OOPS...) --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 162F315F41A for ; Tue, 2 Apr 2024 22:11:42 +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=1712095903; cv=none; b=ShT3adaOfxEPFPNtxH5aCJ5kuUlzC2n17G9gTf2g2Lz3zj+AK7SSWKQ2GoEbVdFQmqmbgumVU5ydqgicwbFv8qRrrfXhdMSJvBRXHDbWlPEVgRmkd1Cp2UPgXhxt6usVt6ZfVqkN2MbzEL8NRngFTpd+U7EgZYUiMeht8B/7Bqo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095903; c=relaxed/simple; bh=Wetid+W3GuC+Ymmd1WqU1sS4wWD0Phnpab/ybnqHMFI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=PMPHQcc5gPSD1EDW8I5IyP/lyZhpnlCos4k1Ype4JF4KSnC+Rrw8l9K/2CUXu9rxGhjHgvOHFEF8D2DDVZFXIgxH4Va82SF9S5p2/nA0pmzV318k/5h3+Wkv892E+4JWmTv/Yux3jhLrNdxXs9dzcz6B1Ux9wsBEAe7H80ANzzE= 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=3bsHSuMB; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W9Zpe5zD; 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="3bsHSuMB"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W9Zpe5zD" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095900; 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=8tgGYCDtdP5lKrc5acCUoGxSE5RROIy0dFDLevKQFSM=; b=3bsHSuMBHth9eC3A0fyARWWGfDGHwae1U8X/emri9Ag3BEuCoiK1WpS/F8qCO3gV+81c+1 HdK4sehD4sH38r7D+EYNd2MU6qpN0U5cfK5TckyGJSoA6vqOlPGnfay4wtw/83cWmnxJ2q 9luftdMF6JMy7rcqSAf4lLL28TDSiRopTrXc74FtwJ1w4adZwuanxvh2LLfyVrvFwA/g2U 30QKawR3i2HJGmLkDSNNdBNS2agrIj8I/kxHhdRZHdwCm8GNLo0WglwGlo/6zoucM+vUaA eIFso9MBB8//iAky8gqqWQyowRJGncL8qxUeIsS6O6dePI2vHkOMiXRhYFLSKA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095900; 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=8tgGYCDtdP5lKrc5acCUoGxSE5RROIy0dFDLevKQFSM=; b=W9Zpe5zDc2FpMfZJOPFjkTwpupjBY8O/HU5cr4Cq7W0KJlwcmsX50cz1sxXRkqjYOrD6a/ QA6BEP94y5z+ZNDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 12/27] printk: Make console_is_usable() available to nbcon Date: Wed, 3 Apr 2024 00:17:14 +0206 Message-Id: <20240402221129.2613843-13-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Move console_is_usable() as-is into internal.h so that it can be used by nbcon printing functions as well. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 32 ++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 30 ------------------------------ 2 files changed, 32 insertions(+), 30 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index c040fc8f1fd9..bad22092cd5e 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -85,6 +85,36 @@ bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); =20 +/* + * Check if the given console is currently capable and allowed to print + * records. + * + * Requires the console_srcu_read_lock. + */ +static inline bool console_is_usable(struct console *con) +{ + short flags =3D console_srcu_read_flags(con); + + if (!(flags & CON_ENABLED)) + return false; + + if ((flags & CON_SUSPENDED)) + return false; + + if (!con->write) + return false; + + /* + * Console drivers may assume that per-cpu resources have been + * allocated. So unless they're explicitly marked as being able to + * cope (CON_ANYTIME) don't call them until this CPU is officially up. + */ + if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME)) + return false; + + return true; +} + #else =20 #define PRINTK_PREFIX_MAX 0 @@ -106,6 +136,8 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } =20 +static inline bool console_is_usable(struct console *con) { return false; } + #endif /* CONFIG_PRINTK */ =20 extern struct printk_buffers printk_shared_pbufs; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index cd32648372a0..9ea51cb2aca9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2697,36 +2697,6 @@ int is_console_locked(void) } EXPORT_SYMBOL(is_console_locked); =20 -/* - * Check if the given console is currently capable and allowed to print - * records. - * - * Requires the console_srcu_read_lock. - */ -static inline bool console_is_usable(struct console *con) -{ - short flags =3D console_srcu_read_flags(con); - - if (!(flags & CON_ENABLED)) - return false; - - if ((flags & CON_SUSPENDED)) - return false; - - if (!con->write) - return false; - - /* - * Console drivers may assume that per-cpu resources have been - * allocated. So unless they're explicitly marked as being able to - * cope (CON_ANYTIME) don't call them until this CPU is officially up. - */ - if (!cpu_online(raw_smp_processor_id()) && !(flags & CON_ANYTIME)) - return false; - - return true; -} - static void __console_unlock(void) { console_locked =3D 0; --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F11D115FA85 for ; Tue, 2 Apr 2024 22:11:42 +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=1712095904; cv=none; b=l4S6naFXP8ftiEpEDd2ypzNJTcdPc7W3EaEcwmKiPkh51kLD+hr2sUMQdl71lj1baOeLIcIVmHGiulp3mseljcUSzvA9FgfAuQDHJvKAXfvt+qRutFykx+idb/5E5ZuxovjDUTXyV5DpTaS6le1KpfHYwb2phq+EtEeMsG6aULc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095904; c=relaxed/simple; bh=hx4xaO/u4i/Ycgs2YXfR0xt1CzcK8YE0NqUsdUu6qZ0=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XY+hTNMgs8OKnlZ3dtgK+dLyuHwX9JxleBTef/9Z9qGRdkNEGFbrnCkLlMeIYAMzIWaMbSfv8JNtqaoGXMEn0iboYNdlH0ZuT74VJKz9LS4lUBUhOaxKlAvF/h5eIMlf2DL7eq360Iz31b5cQaXCXHs+orAuw5cbkgGP18p8K8g= 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=TGtgEOWL; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Yi2ciriB; 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="TGtgEOWL"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Yi2ciriB" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095901; 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=i+qJ3uw8FF3SLnXWdWW9caWOJk87KBanZukA+yTmjng=; b=TGtgEOWLDZrUvzi6HxEb+jd840svAuCDAKgRRs9lPU6UJ89ucUGveHuaRJHDwJ1ISzSCX7 P94pSJgUwWQCl7KeR/LZLxnNZS+9r25J7SK/eQqmRm6wax+DqSzMSJjsvubwW+tFfutfI2 E2c5bR9+vhq3xf92Q4alW57I5l+it6zpsxUDvnBsoAdrRcxdccIBcWcZvknAmUquMshLpK lb/upa/j662/4i13OeEQzq7vVcduLkmTm5Er9+f9PHuwLMtOAA8UWsN9xDJMiqfZ3jN8dn UQscsiHaZHTb7G12fC1p5bgS+zH8ftCu0BwXdGr70QP5m5NYEEu1Ftg5Qk37vA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095901; 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=i+qJ3uw8FF3SLnXWdWW9caWOJk87KBanZukA+yTmjng=; b=Yi2ciriBDJJMADCkQuLvnRqql/zUlgXVIWNopY9M7TNmsdaOEX3uRRp2FW0qkk6knLmUmJ 2y5cXmz5HxGXYmCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 13/27] printk: Let console_is_usable() handle nbcon Date: Wed, 3 Apr 2024 00:17:15 +0206 Message-Id: <20240402221129.2613843-14-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The nbcon consoles use a different printing callback. For nbcon consoles, check for the write_atomic() callback instead of write(). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index bad22092cd5e..398e99a30427 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -87,6 +87,8 @@ void nbcon_free(struct console *con); =20 /* * Check if the given console is currently capable and allowed to print + * records. Note that this function does not consider the current context, + * which can also play a role in deciding if @con can be used to print * records. * * Requires the console_srcu_read_lock. @@ -101,8 +103,13 @@ static inline bool console_is_usable(struct console *c= on) if ((flags & CON_SUSPENDED)) return false; =20 - if (!con->write) - return false; + if (flags & CON_NBCON) { + if (!con->write_atomic) + return false; + } else { + if (!con->write) + return false; + } =20 /* * Console drivers may assume that per-cpu resources have been --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8B63815FCE2 for ; Tue, 2 Apr 2024 22:11:43 +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=1712095905; cv=none; b=Ev3TvyZnzjHD9wFZuWomujjxVEKry1RiQbCm/o0+GaqA1I2lBLAymMMSiEnx6FUI0jR4DtwkBeGgDp8VFus71fXBbzXPbMEREPxmq9vmxWqE+aCWt0YHE/fq4iP/YrPnW2hiLhBFzvN1Ahfnq1yE09/C0FAR8XrLDM0GO78ZRYw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095905; c=relaxed/simple; bh=VSpMNtTyJiGrRPB9RAaRfeLkD8vqxjsPOGjsfXw1Ptc=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=DBmQkmzisnwq1RcQyKOdB9FNEmPUKi+/+mE0C2MLhra/SaNxnfpYzJJcyj/j7o2ERkH/iGCXlrOKzps3IgXL5JTjckcqR1l5ezxAk+URPs+Xmr78nPsWoo8qmYGwBKtqxUyeDUJI+xAIFh4i4SrgMzwDvyjw0qcoc0JqLoNV1E4= 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=NKqoB5au; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=rs3veSnS; 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="NKqoB5au"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="rs3veSnS" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095901; 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=JcT8QKbkv/y1+FVd9sExuz/3XCroZtJW9JEMQxy9v3w=; b=NKqoB5auNPp6orU1zh/lKnD3P0qVINEUBNK/HLIAsypteh1v4PJg3fbR5zwtn4fmRuzX6B ZaOz2ETKimGJK5TjJmJzQswKeNlE8gn3nbFSBCB/LX0qTRdy2TFhDI0da6vksjoW2Y1WVO LGBCrHZbNo0vDXDseJPPKRvtMGw8lgEEVN45T6zUCXsSfbnsc5Vei5CZ7P4YGNwdTH3lPH 9TFp/qRwpQWBRHclHofkB/RtR0yykF3kf7okns8PpIiznkB/uMocJhAPziXTs2759MRprD y60J/3mkAtNxTO1jJ2WRT9wHgHI2gXuLSXEgbkYfSVf8Z5goEVJjxU9kNjiT6A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095901; 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=JcT8QKbkv/y1+FVd9sExuz/3XCroZtJW9JEMQxy9v3w=; b=rs3veSnSbOEtPnLvhs9UouuFlg5y5SpXoP4wUoqciuXCfMeoA1tBIwemI1UF55XtI25dly Wno9UDkQBybokXDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 14/27] printk: Add @flags argument for console_is_usable() Date: Wed, 3 Apr 2024 00:17:16 +0206 Message-Id: <20240402221129.2613843-15-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The caller of console_is_usable() usually needs @console->flags for its own checks. Rather than having console_is_usable() read its own copy, make the caller pass in the @flags. This also ensures that the caller saw the same @flags value. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 8 ++------ kernel/printk/printk.c | 5 +++-- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 398e99a30427..b7a0072eb2a4 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -90,13 +90,9 @@ void nbcon_free(struct console *con); * records. Note that this function does not consider the current context, * which can also play a role in deciding if @con can be used to print * records. - * - * Requires the console_srcu_read_lock. */ -static inline bool console_is_usable(struct console *con) +static inline bool console_is_usable(struct console *con, short flags) { - short flags =3D console_srcu_read_flags(con); - if (!(flags & CON_ENABLED)) return false; =20 @@ -143,7 +139,7 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } =20 -static inline bool console_is_usable(struct console *con) { return false; } +static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } =20 #endif /* CONFIG_PRINTK */ =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 9ea51cb2aca9..fe06856f7653 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2942,9 +2942,10 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove =20 cookie =3D console_srcu_read_lock(); for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); bool progress; =20 - if (!console_is_usable(con)) + if (!console_is_usable(con, flags)) continue; any_usable =3D true; =20 @@ -3814,7 +3815,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre * that they make forward progress, so only increment * @diff for usable consoles. */ - if (!console_is_usable(c)) + if (!console_is_usable(c, flags)) continue; =20 if (flags & CON_NBCON) { --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C446C15FCEC for ; Tue, 2 Apr 2024 22:11:43 +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=1712095905; cv=none; b=CzRcxwCfwBnsFe17wH3uGiVtpXabtQHbmJfwyIbunkyqyaGjowSHuXxA2ZM+M4PxCRsO9F1/2U6yQXVFeLXfYbTtmaHnQ6wGtHGo/t0tj9mjKlUXHHLxTi6k3Auosqdghn/MSPf7frQunJICz/h8DU0dItP+Fe4CwpE6ortPa0E= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095905; c=relaxed/simple; bh=PVJ+rhXeGE3ArSPWyipgCCjhGauuCodgDEyKuljedLw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=myPs81BHSgRc8RuoWfYoOQ8W2RCv7j+XByGmcUthpECqZIP8igF4qCPETh0iQfz0qVU8mH3rQqKcPwOfVu5CbBG9KPA66cI9dfaSlqh6Pfe/ZW1eiJZIccHlNZ09jZLFOW/PtTLAfDweFIVPrw+l/BjuUpTY7enE+h6yRw1GVLQ= 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=1ns2wHTj; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UvHj88+3; 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="1ns2wHTj"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UvHj88+3" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095902; 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=OiBtbPe76V5QyDUwRqi5gzdP2C/dYecf5xSN7mqsJdE=; b=1ns2wHTjKxICz+BSk22mCfbRCZ7s0419iM9Qyd3uSdL04U5W+1ofIbtJUUwtYQEQl01dnX zpP1sdKchxlEBKICG1f3nphgJ1c1li4rw3ot8ydB5Nk0zVXBinkyI5CQ6iZxVjppJB7JpD rQmi11zqISKcQPHcjEDnYFxGzT309PqXUUWbaLso1Kck+2aaeYSJVWciMuPquobkOswBDk ynwxQuO5bGYdW78HS+2O5wRMTm8m6PITJb9iixZw8EMCgx5lie1sj9/Pmyshw4h27ueHX3 EY+vvD25lYNlivelqPeJ5vHfgIFbYExdh7j5B/VgfpqsPGJwJpBk0tvsmAUEEw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095902; 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=OiBtbPe76V5QyDUwRqi5gzdP2C/dYecf5xSN7mqsJdE=; b=UvHj88+34uhQWNw2nFmwAiCaTYzqBTuGLWP27lfxks1Ws/EV34BGeVXfVjxx7ZVkId48Ll AAcFlqSANyGIHWCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 15/27] printk: nbcon: Provide function to flush using write_atomic() Date: Wed, 3 Apr 2024 00:17:17 +0206 Message-Id: <20240402221129.2613843-16-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Provide nbcon_atomic_flush_pending() to perform flushing of all registered nbcon consoles using their write_atomic() callback. Unlike console_flush_all(), nbcon_atomic_flush_pending() will only flush up through the newest record at the time of the call. This prevents a CPU from printing unbounded when other CPUs are adding records. Also unlike console_flush_all(), nbcon_atomic_flush_pending() will fully flush one console before flushing the next. This helps to guarantee that a block of pending records (such as a stack trace in an emergency situation) can be printed atomically at once before releasing console ownership. nbcon_atomic_flush_pending() is safe in any context because it uses write_atomic() and acquires with unsafe_takeover disabled. Use it in console_flush_on_panic() before flushing legacy consoles. The legacy write() callbacks are not fully safe when oops_in_progress is set. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 + kernel/printk/nbcon.c | 104 ++++++++++++++++++++++++++++++++++++++- kernel/printk/printk.c | 2 + 3 files changed, 106 insertions(+), 2 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index b7a0072eb2a4..a8df764fd0c5 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -84,6 +84,7 @@ void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); +void nbcon_atomic_flush_pending(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -138,6 +139,7 @@ static inline void nbcon_seq_force(struct console *con,= u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } +static inline void nbcon_atomic_flush_pending(void) { } =20 static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 1de6062d4ce3..fcdab2eaaedb 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -548,7 +548,6 @@ static struct printk_buffers panic_nbcon_pbufs; * in an unsafe state. Otherwise, on success the caller may assume * the console is not in an unsafe state. */ -__maybe_unused static bool nbcon_context_try_acquire(struct nbcon_context *ctxt) { unsigned int cpu =3D smp_processor_id(); @@ -850,7 +849,6 @@ EXPORT_SYMBOL_GPL(nbcon_exit_unsafe); * When true is returned, @wctxt->ctxt.backlog indicates whether there are * still records pending in the ringbuffer, */ -__maybe_unused static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); @@ -937,6 +935,108 @@ static bool nbcon_emit_next_record(struct nbcon_write= _context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using = its + * write_atomic() callback + * @con: The nbcon console to flush + * @stop_seq: Flush up until this record + * + * Return: True if taken over while printing. Otherwise false. + * + * If flushing up to @stop_seq was not successful, it only makes sense for= the + * caller to try again when true was returned. When false is returned, eit= her + * there are no more records available to read or this context is not allo= wed + * to acquire the console. + */ +static bool __nbcon_atomic_flush_pending_con(struct console *con, u64 stop= _seq) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + + ctxt->console =3D con; + ctxt->spinwait_max_us =3D 2000; + ctxt->prio =3D NBCON_PRIO_NORMAL; + + if (!nbcon_context_try_acquire(ctxt)) + return false; + + while (nbcon_seq_read(con) < stop_seq) { + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + */ + if (!nbcon_emit_next_record(&wctxt)) + return true; + + if (!ctxt->backlog) + break; + } + + nbcon_context_release(ctxt); + + return false; +} + +/** + * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their + * write_atomic() callback + * @stop_seq: Flush up until this record + */ +static void __nbcon_atomic_flush_pending(u64 stop_seq) +{ + struct console *con; + bool should_retry; + int cookie; + + do { + should_retry =3D false; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + unsigned long irq_flags; + + if (!(flags & CON_NBCON)) + continue; + + if (!console_is_usable(con, flags)) + continue; + + if (nbcon_seq_read(con) >=3D stop_seq) + continue; + + /* + * Atomic flushing does not use console driver + * synchronization (i.e. it does not hold the port + * lock for uart consoles). Therefore IRQs must be + * disabled to avoid being interrupted and then + * calling into a driver that will deadlock trying + * to acquire console ownership. + */ + local_irq_save(irq_flags); + + should_retry |=3D __nbcon_atomic_flush_pending_con(con, stop_seq); + + local_irq_restore(irq_flags); + } + console_srcu_read_unlock(cookie); + } while (should_retry); +} + +/** + * nbcon_atomic_flush_pending - Flush all nbcon consoles using their + * write_atomic() callback + * + * Flush the backlog up through the currently newest record. Any new + * records added while flushing will not be flushed. This is to avoid + * one CPU printing unbounded because other CPUs continue to add records. + */ +void nbcon_atomic_flush_pending(void) +{ + __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb)); +} + /** * nbcon_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fe06856f7653..6404f2044ceb 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3172,6 +3172,8 @@ void console_flush_on_panic(enum con_flush_mode mode) console_srcu_read_unlock(cookie); } =20 + nbcon_atomic_flush_pending(); + console_flush_all(false, &next_seq, &handover); } =20 --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BA12C15ECCF for ; Tue, 2 Apr 2024 22:11:44 +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=1712095906; cv=none; b=cHcxvXtJ+7DBFf5nKpbmgbiRGkBhDUlRiYfoFmFrQsjHIvIB37jjmmYgv5JaFMUg+Z/runJPZdUlR2yNnzwzLzND0sXlEbngI81JQCLqncMWJjvr/LZW5a7xw/s2b4MT//JdeYdA5y0xh+kaP8QVwhUUgcBytWQgIl6MHNyV3tU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095906; c=relaxed/simple; bh=TS4bAdNkoZ+0goSa9kqRyHujH5ai41OqMG6rA/WUJxg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZLaVntAAvs4HUGVQzufYQDCPJKBKpb++6c/uHmMyazlZeJPvcst9ZAVZZw/5sA4yolu36ymp3Hfy2eDYvmT62ASif85yi0mm9viugMXsiaYn9yrrIki2cDlV5mE3N0/l/ltQI9qXWxG8XhXHI+x8YwMBNmAogkUEk6ZsX82Kx50= 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=oUDQJHLm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bklhxsNR; 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="oUDQJHLm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bklhxsNR" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095902; 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=njd4JtOG9TQM4vCbEz3L/KsWtYnSSvFEDH3F97Q//58=; b=oUDQJHLm2ib2I/FfRr3s6EIt5CFGaFnPfmLAjOYeOgER719S7bCNEZSqXodJGd+QQ7MkI1 Mm9F4nTZYdYo/EYOgGiQUI7Y0OaVAkDa/wUaXt4avKJGQBbngUXylfrFlvgdFP3i5qpn16 //hAUqQCH0Sbs2lQOTMVau5nybDQFcAOMqZ1YBHANOGaHvcXw9g88ExRzlkuAeDJ4jI9Gi LuR56qyhOJJdulY8Irs3y//e3q3PbDdVEGRogbnFZKp9SNQbMt/68SsjQ6FmIiU56mfCXC EcgADShu3whSz0ktlkIlXPE77LVX1h6dipYiADyVrDmSqpZF+XDphdrDev8a2Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095902; 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=njd4JtOG9TQM4vCbEz3L/KsWtYnSSvFEDH3F97Q//58=; b=bklhxsNRc3GcgoRf7Iw9i5tJmE28nuMOZh/IhzyF2ChlZYZM/hUHOQWHXE4DYNic45ayGz ButLixT4+AjAMfDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 16/27] printk: Track registered boot consoles Date: Wed, 3 Apr 2024 00:17:18 +0206 Message-Id: <20240402221129.2613843-17-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Unfortunately it is not known if a boot console and a regular (legacy or nbcon) console use the same hardware. For this reason they must not be allowed to print simultaneously. For legacy consoles this is not an issue because they are already synchronized with the boot consoles using the console lock. However nbcon consoles can be triggered separately. Add a global flag @have_boot_console to identify if any boot consoles are registered. This will be used in follow-up commits to ensure that boot consoles and nbcon consoles cannot print simultaneously. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6404f2044ceb..a1b3309e12c1 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -463,6 +463,14 @@ static int console_msg_format =3D MSG_FORMAT_DEFAULT; /* syslog_lock protects syslog_* variables and write access to clear_seq. = */ static DEFINE_MUTEX(syslog_lock); =20 +/* + * Specifies if a boot console is registered. If boot consoles are present, + * nbcon consoles cannot print simultaneously and must be synchronized by + * the console lock. This is because boot consoles and nbcon consoles may + * have mapped the same hardware. + */ +static bool have_boot_console; + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -3513,6 +3521,9 @@ void register_console(struct console *newcon) newcon->seq =3D 0; } =20 + if (newcon->flags & CON_BOOT) + have_boot_console =3D true; + /* * If another context is actively using the hardware of this new * console, it will not be aware of the nbcon synchronization. This @@ -3582,6 +3593,8 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + bool found_boot_con =3D false; + struct console *c; int res; =20 lockdep_assert_console_list_lock_held(); @@ -3629,6 +3642,17 @@ static int unregister_console_locked(struct console = *console) if (console->exit) res =3D console->exit(console); =20 + /* + * With this console gone, the global flags tracking registered + * console types may have changed. Update them. + */ + for_each_console(c) { + if (c->flags & CON_BOOT) + found_boot_con =3D true; + } + if (!found_boot_con) + have_boot_console =3D found_boot_con; + return res; } =20 --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07CE2160784 for ; Tue, 2 Apr 2024 22:11:45 +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=1712095907; cv=none; b=MMqz8nnEWGoZ+k/TWw9iUMWpNAwUNY0RUuTPJAGd5DPR0F8hAXetQUK51Ng8lrGNeGonHkzJQwUp13dK6yh3vpza2WmpKIfxnZ48smNzKmn2CKw1J6rXf+9uUT4HaYok4UB4u3tADRnyjz7O4JqKpeIQ156YAv2Zr1+6WXMwqfM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095907; c=relaxed/simple; bh=z0EAC/Y2Ie4y+zt3dvWpk09O5Hod2omIJg/QrXj5w3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dgHwfGex0CDQG4SoUd3BsWhqPeFZKPjmdZIz3Ag3uwCohHYu2Ongt7CQNw/cZXAdC/P5tKW/iQMrgu8sALt8gz9RM8MfB/R1Qm/l27Mon3zGLeTDMvgpuIu+OxvjJxshZpqoqA5r4HlS1+C1q7Qz8htmm6UJzW/D0u3AqjynwDI= 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=4KsA66rD; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hUMvRiV+; 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="4KsA66rD"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hUMvRiV+" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095903; 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=3qTtvBCC1FB1iFzJGtCPvpWwllmrX3We+4s6ahKMrS0=; b=4KsA66rD8Wvz1i0pvurd3ZFbVX0oZI7AfS+9c/84YSI7cH3C5Qcp1BYnr3MvUBCZVCe/bu h4vD1Zp4+u7KHckUojG9BtolHFTHLTNMXnl2dxbhnyhp9Uee4rPca+jIQLBEtaJU3ifzBw wn7WJ41TOUpSgvYsCLtx2+DDnCY6zmfTxsbXD9lNir/p64AZlF53UMDJ/PX8jDbaucigms b2W9x0h0O0yeUCJBwAuCX2xFlKoqTI64fZB6N9rcaZTBWhcBYexfuogCw0DcevBc+I/MWA SNLkJnHLGFhtn2k1R8Gg4FRvR5LRiHdu5gE4jzrz/pcSKN8lqKv7bOj8UxRMtQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095903; 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=3qTtvBCC1FB1iFzJGtCPvpWwllmrX3We+4s6ahKMrS0=; b=hUMvRiV+NewBAKe1O2C8hxq93H/Qjj85yZcvC29QEaVNLQUinI7YGTpbT/tuJ1XS3k6myB 4UW7ymwLq+HkAdAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 17/27] printk: nbcon: Use nbcon consoles in console_flush_all() Date: Wed, 3 Apr 2024 00:17:19 +0206 Message-Id: <20240402221129.2613843-18-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow nbcon consoles to print messages in the legacy printk() caller context (printing via unlock) by integrating them into console_flush_all(). The write_atomic() callback is used for printing. Provide nbcon_legacy_emit_next_record(), which acts as the nbcon variant of console_emit_next_record(). Call this variant within console_flush_all() for nbcon consoles. Since nbcon consoles use their own @nbcon_seq variable to track the next record to print, this also must be appropriately handled. Signed-off-by: John Ogness --- kernel/printk/internal.h | 6 ++++ kernel/printk/nbcon.c | 77 ++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 17 ++++++--- 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index a8df764fd0c5..acf53c35b7a0 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -78,6 +78,8 @@ void defer_console_output(void); =20 u16 printk_parse_prefix(const char *text, int *level, enum printk_info_flags *flags); +void console_lock_spinning_enable(void); +int console_lock_spinning_disable_and_check(int cookie); =20 u64 nbcon_seq_read(struct console *con); void nbcon_seq_force(struct console *con, u64 seq); @@ -85,6 +87,8 @@ bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); void nbcon_atomic_flush_pending(void); +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie); =20 /* * Check if the given console is currently capable and allowed to print @@ -140,6 +144,8 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } static inline void nbcon_atomic_flush_pending(void) { } +static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, + int cookie) { return false; } =20 static inline bool console_is_usable(struct console *con, short flags) { r= eturn false; } =20 diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index fcdab2eaaedb..599fff3c0ab3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -541,6 +541,7 @@ static struct printk_buffers panic_nbcon_pbufs; * nbcon_context_try_acquire - Try to acquire nbcon console * @ctxt: The context of the caller * + * Context: Any context which could not be migrated to another CPU. * Return: True if the console was acquired. False otherwise. * * If the caller allowed an unsafe hostile takeover, on success the @@ -935,6 +936,82 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * nbcon_atomic_emit_one - Print one record for an nbcon console using the + * write_atomic() callback + * @wctxt: An initialized write context struct to use for this context + * + * Return: False if it is known there are no more records to print, + * otherwise true. + * + * This is an internal helper to handle the locking of the console before + * calling nbcon_emit_next_record(). + */ +static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + + if (!nbcon_context_try_acquire(ctxt)) + return true; + + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + */ + if (!nbcon_emit_next_record(wctxt)) + return true; + + nbcon_context_release(ctxt); + + return ctxt->backlog; +} + +/** + * nbcon_legacy_emit_next_record - Print one record for an nbcon console + * in legacy contexts + * @con: The console to print on + * @handover: Will be set to true if a printk waiter has taken over the + * console_lock, in which case the caller is no longer holding + * both the console_lock and the SRCU read lock. Otherwise it + * is set to false. + * @cookie: The cookie from the SRCU read lock. + * + * Context: Any context except NMI. + * Return: False if the given console has no next record to print, + * otherwise true. + * + * This function is meant to be called by console_flush_all() to print rec= ords + * on nbcon consoles from legacy context (printing via console unlocking). + * Essentially it is the nbcon version of console_emit_next_record(). + */ +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + unsigned long flags; + bool progress; + + *handover =3D false; + + /* Use the same procedure as console_emit_next_record(). */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + stop_critical_timings(); + + ctxt->console =3D con; + ctxt->prio =3D NBCON_PRIO_NORMAL; + + progress =3D nbcon_atomic_emit_one(&wctxt); + + start_critical_timings(); + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + + return progress; +} + /** * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using = its * write_atomic() callback diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index a1b3309e12c1..df84c6bfbb2d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1860,7 +1860,7 @@ static bool console_waiter; * there may be a waiter spinning (like a spinlock). Also it must be * ready to hand over the lock at the end of the section. */ -static void console_lock_spinning_enable(void) +void console_lock_spinning_enable(void) { /* * Do not use spinning in panic(). The panic CPU wants to keep the lock. @@ -1899,7 +1899,7 @@ static void console_lock_spinning_enable(void) * * Return: 1 if the lock rights were passed, 0 otherwise. */ -static int console_lock_spinning_disable_and_check(int cookie) +int console_lock_spinning_disable_and_check(int cookie) { int waiter; =20 @@ -2951,13 +2951,20 @@ static bool console_flush_all(bool do_cond_resched,= u64 *next_seq, bool *handove cookie =3D console_srcu_read_lock(); for_each_console_srcu(con) { short flags =3D console_srcu_read_flags(con); + u64 printk_seq; bool progress; =20 if (!console_is_usable(con, flags)) continue; any_usable =3D true; =20 - progress =3D console_emit_next_record(con, handover, cookie); + if (flags & CON_NBCON) { + progress =3D nbcon_legacy_emit_next_record(con, handover, cookie); + printk_seq =3D nbcon_seq_read(con); + } else { + progress =3D console_emit_next_record(con, handover, cookie); + printk_seq =3D con->seq; + } =20 /* * If a handover has occurred, the SRCU read lock @@ -2967,8 +2974,8 @@ static bool console_flush_all(bool do_cond_resched, u= 64 *next_seq, bool *handove return false; =20 /* Track the next of the highest seq flushed. */ - if (con->seq > *next_seq) - *next_seq =3D con->seq; + if (printk_seq > *next_seq) + *next_seq =3D printk_seq; =20 if (!progress) continue; --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 892C316079C for ; Tue, 2 Apr 2024 22:11:45 +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=1712095907; cv=none; b=cGljrNoOl7zGV1TBg4dNYa6CHfhBE26lV+dYCD4aLiTuPt3AOkP6dtHsaUT/uPObuLjW3NFMP02BfkmPEZ7NA1ffhGm6I/6lHs7KUf0LSglDwWfQh7ggU4IurAFjaDty1bpFWYuGb9uydkbM2lufOSPPDpjutNF2917H3gi/MEU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095907; c=relaxed/simple; bh=/yzLu1pIIII3CwMbKuTCCS8fK4EldDhvROOaEuVQjWA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=kau4q9xgeGatmizdiZHBw9ako/DblJNX+tukknNOaLxpt0EUE8mM26rhYxys5bEAXRWwqLS0j0PxwQf+AhJb7JUL2G0k2tc3+pMmNei1qLIOkvCr/MS7ql5n5KTPG3yKHCY1xVOJzbYzR935LBv5ihgG1rXwdOxp+ZyYlGgUS/k= 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=iVqydypU; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=zFndZ9+h; 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="iVqydypU"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="zFndZ9+h" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095904; 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=MJEBBwWx+7a9Zf6U00LLvkq51McCiYjQLaxDLROSFw4=; b=iVqydypUc7tJSDm6VB9QaXe/HXQ5DEwl210/4AxLpwljFtDmsSN//45eB0hNGYdxPPPPf6 ZW4tVFFVt7Wb4wEfjk+JXJ+v3KyMDR64Gc4ANWk5O/lYRu9/OPA1U7UCJbtiTcFGVkAwUp y+bgkmFe0fN0fLnTW52dX3+cK+/V2kOlF+6ySyulcwjujOi6UVwYBUBGBQSNNDTAb5//bd hEQilKsodOpvulQrvVDtE3Fu2DivKoDpwg2/ptrIqrwnuA+TgF62Ogu8p6Y4nhSLASzHq2 hEDjTSfN5dYN7kjvYUIRfBR+wTwRsZSk5uW5AHlFYLBVwl3sCSiN0HgRVrwL2Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095904; 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=MJEBBwWx+7a9Zf6U00LLvkq51McCiYjQLaxDLROSFw4=; b=zFndZ9+h4vcpEiCP4WGOKnhKSrlI9j9E+NdpsnYdNYmJgrGqOTD4vBUNhvwy1EG1OKAGHu P3LjnK4RZMIRECDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 18/27] printk: nbcon: Assign priority based on CPU state Date: Wed, 3 Apr 2024 00:17:20 +0206 Message-Id: <20240402221129.2613843-19-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Use the current state of the CPU to determine which priority to assign to the printing context. The EMERGENCY priority handling is added in a follow-up commit. It will use a per-CPU variable. Note: nbcon_driver_acquire(), which is used by console drivers to acquire the nbcon console for non-printing activities, will always use NORMAL priority. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 2 ++ kernel/printk/nbcon.c | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index acf53c35b7a0..bcf2105a5c5c 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -86,6 +86,7 @@ void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); +enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_pending(void); bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, int cookie); @@ -143,6 +144,7 @@ static inline void nbcon_seq_force(struct console *con,= u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } +static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } static inline void nbcon_atomic_flush_pending(void) { } static inline bool nbcon_legacy_emit_next_record(struct console *con, bool= *handover, int cookie) { return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 599fff3c0ab3..fe5a96ab1f40 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -967,6 +967,22 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_c= ontext *wctxt) return ctxt->backlog; } =20 +/** + * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon + * printing on the current CPU + * + * Context: Any context which could not be migrated to another CPU. + * Return: The nbcon_prio to use for acquiring an nbcon console in this + * context for printing. + */ +enum nbcon_prio nbcon_get_default_prio(void) +{ + if (this_cpu_in_panic()) + return NBCON_PRIO_PANIC; + + return NBCON_PRIO_NORMAL; +} + /** * nbcon_legacy_emit_next_record - Print one record for an nbcon console * in legacy contexts @@ -1001,7 +1017,7 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, stop_critical_timings(); =20 ctxt->console =3D con; - ctxt->prio =3D NBCON_PRIO_NORMAL; + ctxt->prio =3D nbcon_get_default_prio(); =20 progress =3D nbcon_atomic_emit_one(&wctxt); =20 @@ -1032,7 +1048,7 @@ static bool __nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq) =20 ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; - ctxt->prio =3D NBCON_PRIO_NORMAL; + ctxt->prio =3D nbcon_get_default_prio(); =20 if (!nbcon_context_try_acquire(ctxt)) return false; --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4B5431607BF for ; Tue, 2 Apr 2024 22:11:46 +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=1712095908; cv=none; b=t0Yj/GNeeGIRyCniTMyEQ1H+Qev4kemh5QOed6pdWGSfmkYcGKzTS9WwGtjAF1OiDE3ra5q//qa4AKdvIeSPvLkQUAtrYUlnRWn3I5hrAWuNDPqccfMn7Gefmt+edHvbKbGbdk8IfvfHz0avlntVHjz0ERZDQFKT6L7oEfIAt00= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095908; c=relaxed/simple; bh=vknBybsjEs93x4cnJ5nN/M/uvYkr0NDGiVf9T/dwPxE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ilT3E/GuDlbAhJh+/uLRBM45DRG5Xw2vrzO0pHHn7Dkj1I9djzoLCOc3790nUir0iGbBDWwOT2OpSWSysFh2RFMk3gtJ8qFiksS/sEd/K+6ZT1qTJ9roao9JYzTIqfbKuilRcVwk+sTJhzkOJBbJ3tbyfTyYH9UnbnRLwWW1mLU= 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=Bs5z6lCt; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JtVI6yVM; 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="Bs5z6lCt"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JtVI6yVM" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095904; 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=FlH5Xlqmpyf6Et7+NQWZIZIzLlU2T3ZP6VsVzNw72mg=; b=Bs5z6lCtmnNc5XRPHCnYQsQr9aG+m3hMM5gVtuaaqvnX277wxTBV3Ohmwel+mP49sQR38M sO7n/93qbiQG5FZRI2eiItg5pIZOGyYIPhs42Hi9GlppjzefnbkF0MG2v8wfICQmrKKgqW 3VCoqBqKtJsJVCIV3MkwilmLPCjX4Ns5WMJcUB7vE/ZXDz6PDfkcSI1SDb2raChMKWJx2O uyxD0J2EZJ02wCPbuG8/jjari/P9pXSSqB0RwBhQ5pX3Fc6SgJzfEvRVMruveWqxwEn/67 b8uTjf1nrhZ/YepPG/tAWPob/8OaEp9dwoYkKnlfdrog7qtZrTOK4Y0NaKNRYg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095904; 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=FlH5Xlqmpyf6Et7+NQWZIZIzLlU2T3ZP6VsVzNw72mg=; b=JtVI6yVMNrELdLxgZ0YH2PNE9ECFwujqpVs58sZ65U+8+Z0KRpO5pAd/SMhxjEHWqcmN9u l5+ci4ri6AH3uNBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Lukas Wunner , Arnd Bergmann , Uros Bizjak , Kefeng Wang Subject: [PATCH printk v4 19/27] printk: nbcon: Add unsafe flushing on panic Date: Wed, 3 Apr 2024 00:17:21 +0206 Message-Id: <20240402221129.2613843-20-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add nbcon_atomic_flush_unsafe() to flush all nbcon consoles using the write_atomic() callback and allowing unsafe hostile takeovers. Call this at the end of panic() as a final attempt to flush any pending messages. Note that legacy consoles use unsafe methods for flushing from the beginning of panic (see bust_spinlocks()). Therefore, systems using both legacy and nbcon consoles may still fail to see panic messages due to unsafe legacy console usage. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 5 +++++ kernel/panic.c | 1 + kernel/printk/nbcon.c | 26 +++++++++++++++++++++----- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 0ad3ee752635..866683a293af 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -197,6 +197,7 @@ extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); extern void nbcon_driver_acquire(struct console *con); extern void nbcon_driver_release(struct console *con); +void nbcon_atomic_flush_unsafe(void); #else static inline __printf(1, 0) int vprintk(const char *s, va_list args) @@ -285,6 +286,10 @@ static inline void nbcon_driver_release(struct console= *con) { } =20 +static inline void nbcon_atomic_flush_unsafe(void) +{ +} + #endif =20 bool this_cpu_in_panic(void); diff --git a/kernel/panic.c b/kernel/panic.c index f22d8f33ea14..c039f8e1ddae 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -453,6 +453,7 @@ void panic(const char *fmt, ...) * Explicitly flush the kernel log buffer one last time. */ console_flush_on_panic(CONSOLE_FLUSH_PENDING); + nbcon_atomic_flush_unsafe(); =20 local_irq_enable(); for (i =3D 0; ; i +=3D PANIC_TIMER_STEP) { diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index fe5a96ab1f40..47f39402a22b 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1033,6 +1033,7 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * Return: True if taken over while printing. Otherwise false. * @@ -1041,7 +1042,8 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * there are no more records available to read or this context is not allo= wed * to acquire the console. */ -static bool __nbcon_atomic_flush_pending_con(struct console *con, u64 stop= _seq) +static bool __nbcon_atomic_flush_pending_con(struct console *con, u64 stop= _seq, + bool allow_unsafe_takeover) { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); @@ -1049,6 +1051,7 @@ static bool __nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq) ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; ctxt->prio =3D nbcon_get_default_prio(); + ctxt->allow_unsafe_takeover =3D allow_unsafe_takeover; =20 if (!nbcon_context_try_acquire(ctxt)) return false; @@ -1075,8 +1078,9 @@ static bool __nbcon_atomic_flush_pending_con(struct c= onsole *con, u64 stop_seq) * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their * write_atomic() callback * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers */ -static void __nbcon_atomic_flush_pending(u64 stop_seq) +static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_t= akeover) { struct console *con; bool should_retry; @@ -1109,8 +1113,8 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq) */ local_irq_save(irq_flags); =20 - should_retry |=3D __nbcon_atomic_flush_pending_con(con, stop_seq); - + should_retry |=3D __nbcon_atomic_flush_pending_con(con, stop_seq, + allow_unsafe_takeover); local_irq_restore(irq_flags); } console_srcu_read_unlock(cookie); @@ -1127,7 +1131,19 @@ static void __nbcon_atomic_flush_pending(u64 stop_se= q) */ void nbcon_atomic_flush_pending(void) { - __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb)); + __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), false); +} + +/** + * nbcon_atomic_flush_unsafe - Flush all nbcon consoles using their + * write_atomic() callback and allowing unsafe hostile takeovers + * + * Flush the backlog up through the currently newest record. Unsafe hostile + * takeovers will be performed, if necessary. + */ +void nbcon_atomic_flush_unsafe(void) +{ + __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), true); } =20 /** --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4368161308 for ; Tue, 2 Apr 2024 22:11:46 +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=1712095908; cv=none; b=j/Y3KkgAFTZmEhD/wbO5zGkikhJ7KCU8mz38batTOvEAE3qWQx2oXyegnNl9LLp5vAUdCznKxzf/pSLtdDEDfbkOBfsTB2MZcHeGGh9Tlat2uUadeiQHG3QnZ09xTU/U8ZJYs7V9TR2pjFBv0fV23FkHLVakhjxIh7UuNWzW/Nw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095908; c=relaxed/simple; bh=gTqx/v0rb2tHlQCiMAUpXZKcD6qWubYj3ZattR525Fs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=n3BzaG8ldZIGX8lrQO9No6r8lnQ4jff+wMpAjsbHpXdQoAgKOWfaxwX3kCelH+dEwFPx5vNudZehaHErsCTfyD04pBE+qL0f9fZ0bjAxo+w9cydGcCoBfNALu24arK2cWTXDwWLog4o7ZHrgm8uXjhCiUE4f0ekd+VkHe3YqK7U= 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=OHTaL5Il; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=ygWHRd95; 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="OHTaL5Il"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="ygWHRd95" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095905; 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=EjQ48cpkb+zaV+h93O06qb7/bP9cVz61JUJsepicz6Q=; b=OHTaL5Ilg30j42YJBi4UTS2HxzE4cOrPD8OgyKMyaWGZVq72kef/m4ELHfUmxISwYyv8nP C/R/6vtc1fOASeQ9mVzgBrAJedgVCKY6uk1s8/eErIGSJk1rQ3IbiufhQ/q1azpVFOpy4g r8HJYhrBDku+BYY1oplRzMb9HssHkoVRQGK0L89IMIh7OB86xq3258ySMMJ7DtndFRbxpc LRR5HhPwPBxm3SG4hq48G9opcKAZB0G+Gazk/NSIBwmptKue+U6Wqds/xyBxtv/3RwCmB0 Pb5SO2o4MUPcCrXM1MoBArWDQoCOjsP16Z3+86a0KsipkBtkWy/dBJHOs607mA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095905; 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=EjQ48cpkb+zaV+h93O06qb7/bP9cVz61JUJsepicz6Q=; b=ygWHRd95QUs3aU+sNNj2E43ofcfyAQC9j8uihbB+2x7aBxS+u59lfntAGlC9f8djp8cCHR f6uDNuiBAwNwnfBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 20/27] printk: Avoid console_lock dance if no legacy or boot consoles Date: Wed, 3 Apr 2024 00:17:22 +0206 Message-Id: <20240402221129.2613843-21-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Currently the console lock is used to attempt legacy-type printing even if there are no legacy or boot consoles registered. If no such consoles are registered, the console lock does not need to be taken. Add tracking of legacy console registration and use it with boot console tracking to avoid unnecessary code paths, i.e. do not use the console lock if there are no boot consoles and no legacy consoles. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 77 +++++++++++++++++++++++++++++++++++------- 1 file changed, 64 insertions(+), 13 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index df84c6bfbb2d..4ff3800e8e8e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -463,6 +463,13 @@ static int console_msg_format =3D MSG_FORMAT_DEFAULT; /* syslog_lock protects syslog_* variables and write access to clear_seq. = */ static DEFINE_MUTEX(syslog_lock); =20 +/* + * Specifies if a legacy console is registered. If legacy consoles are + * present, it is necessary to perform the console lock/unlock dance + * whenever console flushing should occur. + */ +static bool have_legacy_console; + /* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by @@ -471,6 +478,14 @@ static DEFINE_MUTEX(syslog_lock); */ static bool have_boot_console; =20 +/* + * Specifies if the console lock/unlock dance is needed for console + * printing. If @have_boot_console is true, the nbcon consoles will + * be printed serially along with the legacy consoles because nbcon + * consoles cannot print simultaneously with boot consoles. + */ +#define printing_via_unlock (have_legacy_console || have_boot_console) + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -2339,7 +2354,7 @@ asmlinkage int vprintk_emit(int facility, int level, printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 /* If called from the scheduler, we can not call up(). */ - if (!in_sched) { + if (!in_sched && printing_via_unlock) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2648,7 +2663,7 @@ void resume_console(void) */ static int console_cpu_notify(unsigned int cpu) { - if (!cpuhp_tasks_frozen) { + if (!cpuhp_tasks_frozen && printing_via_unlock) { /* If trylock fails, someone else is doing the printing */ if (console_trylock()) console_unlock(); @@ -3189,7 +3204,8 @@ void console_flush_on_panic(enum con_flush_mode mode) =20 nbcon_atomic_flush_pending(); =20 - console_flush_all(false, &next_seq, &handover); + if (printing_via_unlock) + console_flush_all(false, &next_seq, &handover); } =20 /* @@ -3526,6 +3542,8 @@ void register_console(struct console *newcon) */ nbcon_seq_force(newcon, newcon->seq); newcon->seq =3D 0; + } else { + have_legacy_console =3D true; } =20 if (newcon->flags & CON_BOOT) @@ -3600,6 +3618,7 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + bool found_legacy_con =3D false; bool found_boot_con =3D false; struct console *c; int res; @@ -3656,9 +3675,13 @@ static int unregister_console_locked(struct console = *console) for_each_console(c) { if (c->flags & CON_BOOT) found_boot_con =3D true; + if (!(c->flags & CON_NBCON)) + found_legacy_con =3D true; } if (!found_boot_con) have_boot_console =3D found_boot_con; + if (!found_legacy_con) + have_legacy_console =3D found_legacy_con; =20 return res; } @@ -3810,6 +3833,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre u64 last_diff =3D 0; u64 printk_seq; short flags; + bool locked; int cookie; u64 diff; u64 seq; @@ -3819,22 +3843,35 @@ static bool __pr_flush(struct console *con, int tim= eout_ms, bool reset_on_progre seq =3D prb_next_reserve_seq(prb); =20 /* Flush the consoles so that records up to @seq are printed. */ - console_lock(); - console_unlock(); + if (printing_via_unlock) { + console_lock(); + console_unlock(); + } =20 for (;;) { unsigned long begin_jiffies; unsigned long slept_jiffies; =20 + locked =3D false; diff =3D 0; =20 + if (printing_via_unlock) { + /* + * Hold the console_lock to guarantee safe access to + * console->seq. Releasing console_lock flushes more + * records in case @seq is still not printed on all + * usable consoles. + */ + console_lock(); + locked =3D true; + } + /* - * Hold the console_lock to guarantee safe access to - * console->seq. Releasing console_lock flushes more - * records in case @seq is still not printed on all - * usable consoles. + * Ensure the compiler does not optimize @locked to be + * @printing_via_unlock since the latter can change at any + * time. */ - console_lock(); + barrier(); =20 cookie =3D console_srcu_read_lock(); for_each_console_srcu(c) { @@ -3854,6 +3891,7 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre if (flags & CON_NBCON) { printk_seq =3D nbcon_seq_read(c); } else { + WARN_ON_ONCE(!locked); printk_seq =3D c->seq; } =20 @@ -3865,7 +3903,8 @@ static bool __pr_flush(struct console *con, int timeo= ut_ms, bool reset_on_progre if (diff !=3D last_diff && reset_on_progress) remaining_jiffies =3D timeout_jiffies; =20 - console_unlock(); + if (locked) + console_unlock(); =20 /* Note: @diff is 0 if there are no usable consoles. */ if (diff =3D=3D 0 || remaining_jiffies =3D=3D 0) @@ -3935,6 +3974,7 @@ static void __wake_up_klogd(int val) return; =20 preempt_disable(); + /* * Guarantee any new records can be seen by tasks preparing to wait * before this context checks if the wait queue is empty. @@ -3946,11 +3986,22 @@ static void __wake_up_klogd(int val) * * This pairs with devkmsg_read:A and syslog_print:A. */ - if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */ - (val & PRINTK_PENDING_OUTPUT)) { + if (!wq_has_sleeper(&log_wait)) /* LMM(__wake_up_klogd:A) */ + val &=3D ~PRINTK_PENDING_WAKEUP; + + /* + * Simple read is safe. register_console() would flush a newly + * registered legacy console when writing the message about it + * being enabled. + */ + if (!printing_via_unlock) + val &=3D ~PRINTK_PENDING_OUTPUT; + + if (val) { this_cpu_or(printk_pending, val); irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); } + preempt_enable(); } =20 --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2F756161319 for ; Tue, 2 Apr 2024 22:11: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=1712095908; cv=none; b=GWMC3Stuy0gwouKY/0+JL0uBpT1H5uBY1Rgztu4bW30hsvU1oXsFY0IfQ8lkdCFj1s61/TgD8kx1yiJhLA2fVLwZsZP0YUGvXOpT/Nn3JZTZnAqNZ2fBUgEtatEFbiPeDrSQWzJC5hfMUvMcGjEyMAphZbYSdMjRHjkJ8iFA6gg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095908; c=relaxed/simple; bh=ea/VSkLziN3LfF9dxUVRYcPw9vAR9EyCyVl6lSgqECM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Rbj1H0VFO5G9/Aympyssufpy/+LX7OrT7/t014D/MfI614KghFKm9o7Uhst1G5gIMVNJ+oYxPZwq3pGXemWBV86dr2DHRxwKzJmmfWJvXeOcVg2gf+4x6qLh4zN4gzNVwwn2yWVE3j9QGuL+N+mPOVQGglS6CY8TrzFIi35f4rg= 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=2CniPFW5; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=6lH22voz; 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="2CniPFW5"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="6lH22voz" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095905; 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=kU8IWDIJKFpfEhPU59onSvKonMZgsk+kpNnh+HiGC7g=; b=2CniPFW5uube6TVk/d4kn8aPetcO9M/ExOy5ajsCLwBPl2HmcSKqE3ZA8CQXP0ElT/9UxX +TDC9kGLNP8VpmVHzTD5TUHcR3ILVXsvgbQRza0NJDCA2AjjR1LzKvKJeLIB2EuSKrr2eD mzCQe1XjoCiwM9pLiVivKzwA//T+LO/sakSEnvS0q5drpajBJBGct+J4SLp2fcPpJkS7pL R6spksFUYbCsCc7za+P0fCcMBC2OQw8oQtnhFun31IzitFzCMmTmPF/qKEowm3GeHzj0WT Sqhw++EskvWGYComVoH4xy+kfZ9ZntgVXkLkVj0pbUx3Jq680qP65I+tp0Yivw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095905; 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=kU8IWDIJKFpfEhPU59onSvKonMZgsk+kpNnh+HiGC7g=; b=6lH22vozQipEEMG2qPHiu4Si6m5fdkfeSaV+42aQXUsVLnHsTryadC8rRPLQH9ISaDg0al R6yNebYJCDkAltDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 21/27] printk: Track nbcon consoles Date: Wed, 3 Apr 2024 00:17:23 +0206 Message-Id: <20240402221129.2613843-22-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a global flag @have_nbcon_console to identify if any nbcon consoles are registered. This will be used in follow-up commits to preserve legacy behavior when no nbcon consoles are registered. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 4ff3800e8e8e..329b8507f8a0 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -470,6 +470,11 @@ static DEFINE_MUTEX(syslog_lock); */ static bool have_legacy_console; =20 +/* + * Specifies if an nbcon console is registered. + */ +static bool have_nbcon_console; + /* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by @@ -3533,6 +3538,7 @@ void register_console(struct console *newcon) console_init_seq(newcon, bootcon_registered); =20 if (newcon->flags & CON_NBCON) { + have_nbcon_console =3D true; nbcon_init(newcon); =20 /* @@ -3619,6 +3625,7 @@ EXPORT_SYMBOL(register_console); static int unregister_console_locked(struct console *console) { bool found_legacy_con =3D false; + bool found_nbcon_con =3D false; bool found_boot_con =3D false; struct console *c; int res; @@ -3675,13 +3682,18 @@ static int unregister_console_locked(struct console= *console) for_each_console(c) { if (c->flags & CON_BOOT) found_boot_con =3D true; - if (!(c->flags & CON_NBCON)) + + if (c->flags & CON_NBCON) + found_nbcon_con =3D true; + else found_legacy_con =3D true; } if (!found_boot_con) have_boot_console =3D found_boot_con; if (!found_legacy_con) have_legacy_console =3D found_legacy_con; + if (!found_nbcon_con) + have_nbcon_console =3D found_nbcon_con; =20 return res; } --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 330CE161906 for ; Tue, 2 Apr 2024 22:11: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=1712095910; cv=none; b=BaqKuQECuv9lvQDDFevIpevXhSOQBCFzfGAKxW1XSWjfjS2scqZozvzpK2QUYz+HmqpkZvz0WhJc1IcZ4iORCGt2bdEKxt6ppeywf7B/pqqRnep5GgncKWUT06z1l043cvxOYEyTz1Whmjanh6BPTsUnCPI2rn/zmioktJmv4HM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095910; c=relaxed/simple; bh=/2YalWiH5b5qa5L7GAkkMCFr2Ysu3vUqBpGiqlFgVfw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=LINGhQsIH76Ue3NrYImEs8P7fBVoXTg2cNLNJk1fxB8QvLAqvVNvnHlctPnrYGDv4f9ZkDSsBkNojBSmkjjjZVjyVkO8AO2kI2zsZgJHE7yTUEhuk0d+2rlPl4suPH6bb3BcZa71cyJeQ4ZUncIJ6U6IxYawFonN7UhkV79MTYY= 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=NRgXxmbm; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=7xNycNcW; 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="NRgXxmbm"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="7xNycNcW" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095906; 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=hBm4KPhB7O3k5Gk9a5QvVeFiJpUE5tPuhtoHD45poAY=; b=NRgXxmbmdlQIWzJaevplzFtZf1eAAv9Z4nN0ehQuhwmsp9xpwmnIzSEm8ikRISAvJ9daFf jXr3gvfoAAWn5qEoe+NtZH61f9ee4NJ3A/NSK9EZK48orLPv4Olc0EfX/N2ec3adbGLMBE zfZeD8TSUb8pkk4HGMYgVNszYxPQss0vTz2v3sLr1Jrf20tkxF0zxXQK6qjbXqgAoyXuA5 NctN1K8VFoUwsuGIEPi5Mx5BDRT6PejlNtmdF+8CV1e8lTb7MDZYs7h9sUXkbyAmuRSFGM a1Gn0Nzwk5C+VX+7C38pWtDPe0Y7cQB/Yuarjcl04dKD1/ctWk9bEFW0M0+QOg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095906; 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=hBm4KPhB7O3k5Gk9a5QvVeFiJpUE5tPuhtoHD45poAY=; b=7xNycNcWDMJ2UxvHeUlAsHtpUQh2P7m5Jl7GVkVRxbXxeF/nOea4DOHFsMvAuAnFc8mN4k 0hL5mMqyzRabFADQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Lukas Wunner , Kefeng Wang , Uros Bizjak , Arnd Bergmann Subject: [PATCH printk v4 22/27] printk: Coordinate direct printing in panic Date: Wed, 3 Apr 2024 00:17:24 +0206 Message-Id: <20240402221129.2613843-23-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Perform printing by nbcon consoles on the panic CPU from the printk() caller context in order to get panic messages printed as soon as possible. If legacy and nbcon consoles are registered, the legacy consoles will no longer perform direct printing on the panic CPU until after the backtrace has been stored. This will give the safe nbcon consoles a chance to print the panic messages before allowing the unsafe legacy consoles to print. If no nbcon consoles are registered, there is no change in behavior (i.e. legacy consoles will always attempt to print from the printk() caller context). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 5 ++++ kernel/panic.c | 2 ++ kernel/printk/printk.c | 62 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 61 insertions(+), 8 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 866683a293af..2aa7c302d616 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -195,6 +195,7 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); +void printk_legacy_allow_panic_sync(void); extern void nbcon_driver_acquire(struct console *con); extern void nbcon_driver_release(struct console *con); void nbcon_atomic_flush_unsafe(void); @@ -278,6 +279,10 @@ static inline void printk_trigger_flush(void) { } =20 +static inline void printk_legacy_allow_panic_sync(void) +{ +} + static inline void nbcon_driver_acquire(struct console *con) { } diff --git a/kernel/panic.c b/kernel/panic.c index c039f8e1ddae..de8115c829cf 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -364,6 +364,8 @@ void panic(const char *fmt, ...) =20 panic_other_cpus_shutdown(_crash_kexec_post_notifiers); =20 + printk_legacy_allow_panic_sync(); + /* * Run any panic handlers, including those that might need to * add information to the kmsg dump output. diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 329b8507f8a0..e3fd157d7ba3 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -471,7 +471,9 @@ static DEFINE_MUTEX(syslog_lock); static bool have_legacy_console; =20 /* - * Specifies if an nbcon console is registered. + * Specifies if an nbcon console is registered. If nbcon consoles are pres= ent, + * synchronous printing of legacy consoles will not occur during panic unt= il + * the backtrace has been stored to the ringbuffer. */ static bool have_nbcon_console; =20 @@ -2330,12 +2332,29 @@ int vprintk_store(int facility, int level, return ret; } =20 +static bool legacy_allow_panic_sync; + +/* + * This acts as a one-way switch to allow legacy consoles to print from + * the printk() caller context on a panic CPU. It also attempts to flush + * the legacy consoles in this context. + */ +void printk_legacy_allow_panic_sync(void) +{ + legacy_allow_panic_sync =3D true; + + if (printing_via_unlock && !in_nmi()) { + if (console_trylock()) + console_unlock(); + } +} + asmlinkage int vprintk_emit(int facility, int level, const struct dev_printk_info *dev_info, const char *fmt, va_list args) { + bool do_trylock_unlock =3D printing_via_unlock; int printed_len; - bool in_sched =3D false; =20 /* Suppress unimportant messages after panic happens */ if (unlikely(suppress_printk)) @@ -2351,15 +2370,42 @@ asmlinkage int vprintk_emit(int facility, int level, =20 if (level =3D=3D LOGLEVEL_SCHED) { level =3D LOGLEVEL_DEFAULT; - in_sched =3D true; + /* If called from the scheduler, we can not call up(). */ + do_trylock_unlock =3D false; } =20 printk_delay(level); =20 printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 - /* If called from the scheduler, we can not call up(). */ - if (!in_sched && printing_via_unlock) { + if (have_nbcon_console && !have_boot_console) { + bool is_panic_context =3D this_cpu_in_panic(); + + /* + * In panic, the legacy consoles are not allowed to print from + * the printk calling context unless explicitly allowed. This + * gives the safe nbcon consoles a chance to print out all the + * panic messages first. This restriction only applies if + * there are nbcon consoles registered. + */ + if (is_panic_context) + do_trylock_unlock &=3D legacy_allow_panic_sync; + + /* + * There are situations where nbcon atomic printing should + * happen in the printk() caller context: + * + * - When this CPU is in panic. + * + * Note that if boot consoles are registered, the console + * lock/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) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2379,10 +2425,10 @@ asmlinkage int vprintk_emit(int facility, int level, preempt_enable(); } =20 - if (in_sched) - defer_console_output(); - else + if (do_trylock_unlock) wake_up_klogd(); + else + defer_console_output(); =20 return printed_len; } --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0126016192B for ; Tue, 2 Apr 2024 22:11: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=1712095911; cv=none; b=bg9a9Y+ipac5XQw8Bf4IzByF47q+zzP7dIEdJz6/evvsCvX7PaqZDnXh6hGOeW7p8MkXqzSbL8jCc6g+O3Z66k6q7vBe6s4yaOIYTSiWHTQHPQ02f0GLep0mBTaLCb396xTSQeqwpFbn5cmV62/V0nBxD+/WmhSdyGbihDuCz68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095911; c=relaxed/simple; bh=PgiKWKAgmNvz35ONRrpILBxwpuOVIu85979RvgYSqls=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YrrYFUJGZ/inz5QAHYqGNW2ILfJMGmyaBONDUPOl4cJF1Fga/iEQNUghHdKPlmOUmE51SLhJPpg0TmiWnmendCWwB1VF+J3rqSYdme7GHkx5vIc9/awrQ3jqpwUX0Q9Yz7IUSu1LKoFpi/pZ8QAkow1QnZ+xoW/7KOvhVzGhCPA= 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=RiI+QERh; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Ml+zxwoF; 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="RiI+QERh"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Ml+zxwoF" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095907; 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=7YYD1HrU/gCuCLj4UZrPD3LcgwMCesrdxGaUJGV8It8=; b=RiI+QERhnKuEHzcV0IYrB14l/EJjriZYlDOvZdH7oBD8k10U0UJ4b37jgxnzQIYvrqk2zN n1/V8JLxO1qul49bFGz9ZdXuDQg3kwlbJSENhm2+ALd4dFIRvjmsmtbgsDiSdT3FQk/2Za fPQ50S7SLsygepBhZCyLRvn/8Skm6wwMDFpU+33NApkOn3tA0bjhOl3ZYiu65zxqLeOE2u f/ix5JOxXW5OOv/JUgKtgII5sNdEzl3Z0KuAJlecHFib4U1IFAuR4eIJOVY/esDG/OJYrY NolEwmVPoK2pTJwrGgUUgwQ/X26VxA781x6+jL7TJFqWaxc5rOOzEvh3aVqzuA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095907; 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=7YYD1HrU/gCuCLj4UZrPD3LcgwMCesrdxGaUJGV8It8=; b=Ml+zxwoFpCrwkCl8czM0t6TAxS8PDFYb0ZRJIrJevbkB08tTkcIwxwc3e2OW2Ukr7WKdRH BsrOidIyP1/BgIBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 23/27] printk: nbcon: Implement emergency sections Date: Wed, 3 Apr 2024 00:17:25 +0206 Message-Id: <20240402221129.2613843-24-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner In emergency situations (something has gone wrong but the system continues to operate), usually important information (such as a backtrace) is generated via printk(). Each individual printk record has little meaning. It is the collection of printk messages that is most often needed by developers and users. In order to help ensure that the collection of printk messages in an emergency situation are all stored to the ringbuffer as quickly as possible, disable console output for that CPU while it is in the emergency situation. The consoles need to be flushed when exiting the emergency situation. Add per-CPU emergency nesting tracking because an emergency can arise while in an emergency situation. Add functions to mark the beginning and end of emergency sections where the urgent messages are generated. Do not print if the current CPU is in an emergency state. When exiting all emergency nesting, flush nbcon consoles directly using their atomic callback. Legacy consoles are triggered for flushing via irq_work because it is not known if the context was safe for a trylock on the console lock. Note that the emergency state is not system-wide. While one CPU is in an emergency state, another CPU may continue to print console messages. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) Reviewed-by: Petr Mladek --- include/linux/console.h | 4 ++ kernel/printk/nbcon.c | 83 +++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 13 ++++++- 3 files changed, 98 insertions(+), 2 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 5f1758891aec..7712e4145164 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -559,10 +559,14 @@ static inline bool console_is_registered(const struct= console *con) hlist_for_each_entry(con, &console_list, node) =20 #ifdef CONFIG_PRINTK +extern void nbcon_cpu_emergency_enter(void); +extern void nbcon_cpu_emergency_exit(void); extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); #else +static inline void nbcon_cpu_emergency_enter(void) { } +static inline void nbcon_cpu_emergency_exit(void) { } static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { = return false; } static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) {= return false; } static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { = return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 47f39402a22b..4c852c2e8d89 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -936,6 +936,29 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/* Track the nbcon emergency nesting per CPU. */ +static DEFINE_PER_CPU(unsigned int, nbcon_pcpu_emergency_nesting); +static unsigned int early_nbcon_pcpu_emergency_nesting __initdata; + +/** + * nbcon_get_cpu_emergency_nesting - Get the per CPU emergency nesting poi= nter + * + * Return: Either a pointer to the per CPU emergency nesting counter of + * the current CPU or to the init data during early boot. + */ +static __ref unsigned int *nbcon_get_cpu_emergency_nesting(void) +{ + /* + * The value of __printk_percpu_data_ready gets set in normal + * context and before SMP initialization. As a result it could + * never change while inside an nbcon emergency section. + */ + if (!printk_percpu_data_ready()) + return &early_nbcon_pcpu_emergency_nesting; + + return this_cpu_ptr(&nbcon_pcpu_emergency_nesting); +} + /** * nbcon_atomic_emit_one - Print one record for an nbcon console using the * write_atomic() callback @@ -977,9 +1000,15 @@ static bool nbcon_atomic_emit_one(struct nbcon_write_= context *wctxt) */ enum nbcon_prio nbcon_get_default_prio(void) { + unsigned int *cpu_emergency_nesting; + if (this_cpu_in_panic()) return NBCON_PRIO_PANIC; =20 + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + if (*cpu_emergency_nesting) + return NBCON_PRIO_EMERGENCY; + return NBCON_PRIO_NORMAL; } =20 @@ -1146,6 +1175,60 @@ void nbcon_atomic_flush_unsafe(void) __nbcon_atomic_flush_pending(prb_next_reserve_seq(prb), true); } =20 +/** + * nbcon_cpu_emergency_enter - Enter an emergency section where printk() + * messages for that CPU are only stored + * + * Upon exiting the emergency section, all stored messages are flushed. + * + * Context: Any context. Disables preemption. + * + * When within an emergency section, no printing occurs on that CPU. This + * is to allow all emergency messages to be dumped into the ringbuffer bef= ore + * flushing the ringbuffer. The actual printing occurs when exiting the + * outermost emergency section. + */ +void nbcon_cpu_emergency_enter(void) +{ + unsigned int *cpu_emergency_nesting; + + preempt_disable(); + + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + (*cpu_emergency_nesting)++; +} + +/** + * nbcon_cpu_emergency_exit - Exit an emergency section and flush the + * stored messages + * + * Flushing only occurs when exiting all nesting for the CPU. + * + * Context: Any context. Enables preemption. + */ +void nbcon_cpu_emergency_exit(void) +{ + unsigned int *cpu_emergency_nesting; + bool do_trigger_flush =3D false; + + cpu_emergency_nesting =3D nbcon_get_cpu_emergency_nesting(); + + WARN_ON_ONCE(*cpu_emergency_nesting =3D=3D 0); + + if (*cpu_emergency_nesting =3D=3D 1) { + nbcon_atomic_flush_pending(); + do_trigger_flush =3D true; + } + + /* Undo the nesting count of nbcon_cpu_emergency_enter(). */ + (*cpu_emergency_nesting)--; + + preempt_enable(); + + if (do_trigger_flush) + printk_trigger_flush(); +} + /** * nbcon_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e3fd157d7ba3..ab5dade1352d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2412,16 +2412,25 @@ asmlinkage int vprintk_emit(int facility, int level, * printing of all remaining records to all consoles so that * this context can return as soon as possible. Hopefully * another printk() caller will take over the printing. + * + * Also, nbcon_get_default_prio() requires migration disabled. */ preempt_disable(); + /* * Try to acquire and then immediately release the console * semaphore. The release will print out buffers. With the * spinning variant, this context tries to take over the * printing from another printing context. + * + * Skip it in EMERGENCY priority. The console will be + * explicitly flushed when exiting the emergency section. */ - if (console_trylock_spinning()) - console_unlock(); + if (nbcon_get_default_prio() !=3D NBCON_PRIO_EMERGENCY) { + if (console_trylock_spinning()) + console_unlock(); + } + preempt_enable(); } =20 --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8A42E161B53 for ; Tue, 2 Apr 2024 22:11:49 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095911; cv=none; b=Hdmy0iQP1HfSzqbxtdvVkgfY50HEj9ZOwMnKrlhtuJWF/ldoAASdKy5gicwFqu7+Lgaf0lj0PPbUU8dQnJ5IL93tiUv9Kmv3tvZVj3rW+WuPHi47akjua+8Sh8VqiD7sCFR7jXoSkROskmgYjwVdJavBor+2YbZfnprtbc7w6Eg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095911; c=relaxed/simple; bh=AEn1kMCZQRezyf2WMmaayQEC4zAM6aF9ELGCLd+z7bI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=nIfocT9qEQ0iJ++TsgGXrOjR5pgvnSe5twqwW9kdj06CmU0YiT/tfh1h3MPQ7sjC7ROyelE82pe2LSc7i8t+veQLQ/IOWk6wWwNqweF4zY6D0OMDAldal4/aoJ6s/gtfZWt+zHwZ1RcWHk9syM/ff1+hOxfkArN8TxYlwbn+in8= 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=NXYykXZ3; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=E8ZXF0KY; 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="NXYykXZ3"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="E8ZXF0KY" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095908; 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=aWb2ejOVxlZ3v7+K9dsSM3w74v6x9Bx6kvybMFlfTXc=; b=NXYykXZ3wBR6jVDDnE7QmpKfTlFvQ68QfjHGjE6pMty5wKbhJe1ClSymgkx+CMPs8old1Q +He78mUJwd2g+gvwAjKgiLfIZpaMbcUBQVTJkK9ijQkx6fl4OVf5S8zDdhZcOkumJlrW1j sUKo5O4sewMFCZP3unD9knwNbgppiqBqdABKCUqVZ+NuZ7CzyRrEVzKNCK1ccRsrTOsnoc DlW0qw39tFfAV5Yw0W9uElxIdsZsM3g+8B+2QSJ/lj8tzbQPz3FfAUSLeGmQHAGpguibNT z1SJt/Ux4Z4qQ/t4i/ZsrDE6F36tm8RHkSm2Ial4bTgFU51TqowYy2DpJn61rw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095908; 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=aWb2ejOVxlZ3v7+K9dsSM3w74v6x9Bx6kvybMFlfTXc=; b=E8ZXF0KYj3Y3uxpQRkkUOfwub2nPQKvnrOk3dEgZKOEekOxHQQYCl7782XAoSRAHN8+P6/ alre4vb83o68ARDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Uros Bizjak , Kefeng Wang , Lukas Wunner , Arnd Bergmann Subject: [PATCH printk v4 24/27] panic: Mark emergency section in warn Date: Wed, 3 Apr 2024 00:17:26 +0206 Message-Id: <20240402221129.2613843-25-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Mark the full contents of __warn() as an emergency section. In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) Reviewed-by: Petr Mladek --- kernel/panic.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/kernel/panic.c b/kernel/panic.c index de8115c829cf..ee03193f9495 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -667,6 +667,8 @@ struct warn_args { void __warn(const char *file, int line, void *caller, unsigned taint, struct pt_regs *regs, struct warn_args *args) { + nbcon_cpu_emergency_enter(); + disable_trace_on_warning(); =20 if (file) @@ -697,6 +699,8 @@ void __warn(const char *file, int line, void *caller, u= nsigned taint, =20 /* Just a warning, don't kill lockdep. */ add_taint(taint, LOCKDEP_STILL_OK); + + nbcon_cpu_emergency_exit(); } =20 #ifdef CONFIG_BUG --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DB9E516A1C5 for ; Tue, 2 Apr 2024 22:11: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=1712095912; cv=none; b=bP+UkWFZOjq9+FGXEhcASudnVExqG3Ap0koR/g20CpgFcz3q3XdugU5vPHCnoxk1ONNAM6ylyo++I6TWWYJetS7nJ0kIynAx+PTpiudvhDxjR3Y733u1O0yHsyY/1BYkhra6YrqRS2H/Aeya+fkLqTqEZiykYL0VWpE4WvE1n4w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095912; c=relaxed/simple; bh=c6akV9Q4yr/rVYenMeX346mAEuxD5wte66aQQO78Ys4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=FDnggPWLFPNACVCD90QsjsZlhZQPDul+rkHWDdlVWG1+Z/9PnhCiIxaQLEPcKVkp4KKpxibIBdpSfg6W8Wa4AMWDtpOwVshbrXR9908ZGufhHos1P8g6V8QSfrSFa5iRrAqOKYxv7i9GZMVmU/V8zX8aQ5N/pY+/Ra2I68fppys= 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=jEUQYJPU; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=8UYn3jmD; 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="jEUQYJPU"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="8UYn3jmD" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095908; 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=kRn0di1HIXp5SiW2VwTfWrnEOjP1EiU0PA9D4LBG0KY=; b=jEUQYJPUMBBAm3IA0xtFogfZYqN8W6tk7xUNjjINI11G289jXRJUfyFKHWIKN6MR0k3jUk chUu61nuVRMycoinG+POGGDHbT+Du+0H4G7cHJfJhOKhHyVq9ETm4wVvcxeFM5lGJLIbqu MO9cNkOVpeWzHpS+gFgYb7vZhbAerUSJBna/pETFTuLBMoUJg2C+XYPmU7qr75br10XZZa uK9Y5W+KeqcsoINaRAyAI9FR2pnMH1lNuXPMXQ8m0M4QD99O1yBkP9vu57rRKrFDRUqSFL FRWCKjEnBp5xHVHUge5um1GmTTJwLR7FxKFJRdWHSX43IrdjgzRbtWl5MyKDDA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095908; 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=kRn0di1HIXp5SiW2VwTfWrnEOjP1EiU0PA9D4LBG0KY=; b=8UYn3jmDJCT+vLtiD+HIuun0WTy8GnE/FgLQA8pHn/0RkeFSKs9TW1kQpbpyPxGskCjZQV qLOR3bfzO9aCTWCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , "Peter Zijlstra (Intel)" , Josh Poimboeuf , Uros Bizjak , Kefeng Wang , Arnd Bergmann Subject: [PATCH printk v4 25/27] panic: Mark emergency section in oops Date: Wed, 3 Apr 2024 00:17:27 +0206 Message-Id: <20240402221129.2613843-26-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mark an emergency section beginning with oops_enter() until the end of oops_exit(). In this section, the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. The very end of oops_exit() performs a kmsg_dump(). This is not included in the emergency section because it is another flushing mechanism that should occur after the consoles have been triggered to flush. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/panic.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/panic.c b/kernel/panic.c index ee03193f9495..3754a2471b4f 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -634,6 +634,7 @@ bool oops_may_print(void) */ void oops_enter(void) { + nbcon_cpu_emergency_enter(); tracing_off(); /* can't trust the integrity of the kernel anymore: */ debug_locks_off(); @@ -656,6 +657,7 @@ void oops_exit(void) { do_oops_enter_exit(); print_oops_end_marker(); + nbcon_cpu_emergency_exit(); kmsg_dump(KMSG_DUMP_OOPS); } =20 --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8791916A1E0; Tue, 2 Apr 2024 22:11: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=1712095913; cv=none; b=ufCH7A+SAi81/QoI9XfXkwXq+mKWhT1W0JL4hH6AnZ3NDHP4GkNMX18YPN5sOj/P/kE/x//zhdQgrtDtQDYIg4iRrpTzTJDTe+QSDpUMTGbmsC+81QNjpokRSpg0QnqahWaN3nTvdvgKZwKzg82y3ISKDrtEboeWT7FK6/vcpxk= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095913; c=relaxed/simple; bh=j2Ps0sUvXa5FFGYYiI49hHUu4c3C8/680t0hKerAtnE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lxD9CEzr2fW3C3irAi7nkVfYJCApzIOoD2EL4x4nKfL2hkovSqTmiLrxum6avcbM2XPc/MH5YrGrtV2dmp/PE8u5Zq7Huge5j0ezS2hxGP0VloA6ftNZBVJXRcpBcMtNNQIc+zCpTfWUOCe88OWYxuF72ykgOvST3tJLt7snURI= 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=dO5zg+CI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tJQ7G1rI; 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="dO5zg+CI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tJQ7G1rI" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095909; 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=Oz8b+gVj+4FTYt+4Ek3fAB/5aQD+Yo6GKug3uRyPfAM=; b=dO5zg+CIEKrGHq6Dupt6+7LhUYJm8SL8mpx+fM39W+ss2I4gf69o4rlCBRIb1DlGbLdVt7 zRuxtuyTf7992BY+eL2tf6+sihsQOD3mGXtFtS7TtRQu1Waz5+MAgIZKSvqSjVWz93j44M 8x+4q2/6NhaJ8z4fw0r9JlQVgFea5lW0E3t4nht/eirqYEYziaD2uxF//KlKBoWZKiz5Eh rYq0dkzhDtx4yoTwZj57jOp/mlOHqDQT1NPhY+CDRf6n+hzUec95YRcOGbFj8UlAbv3Vqj dt7S04iw10YX/sIekI3Z7fmoCvJHrVXrOkl6+vgY5XF2eKmMy//bxFTnf9eXvQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095909; 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=Oz8b+gVj+4FTYt+4Ek3fAB/5aQD+Yo6GKug3uRyPfAM=; b=tJQ7G1rIEdNc01IoZPTdXpUmpC/eTVUsUntyOQgUHvlfBkOFe1UroCGIagflpVGvGuEmWR JijfC4O3+oaJ4VBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, "Paul E. McKenney" , Frederic Weisbecker , Neeraj Upadhyay , Joel Fernandes , Josh Triplett , Boqun Feng , Mathieu Desnoyers , Lai Jiangshan , Zqiang , rcu@vger.kernel.org Subject: [PATCH printk v4 26/27] rcu: Mark emergency section in rcu stalls Date: Wed, 3 Apr 2024 00:17:28 +0206 Message-Id: <20240402221129.2613843-27-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mark emergency sections wherever multiple lines of rcu stall information are generated. In an emergency section the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. This allows the full message block to be stored as quickly as possible in the ringbuffer. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/rcu/tree_exp.h | 7 +++++++ kernel/rcu/tree_stall.h | 9 +++++++++ 2 files changed, 16 insertions(+) diff --git a/kernel/rcu/tree_exp.h b/kernel/rcu/tree_exp.h index 6b83537480b1..0a135e94da08 100644 --- a/kernel/rcu/tree_exp.h +++ b/kernel/rcu/tree_exp.h @@ -7,6 +7,7 @@ * Authors: Paul E. McKenney */ =20 +#include #include =20 static void rcu_exp_handler(void *unused); @@ -571,6 +572,9 @@ static void synchronize_rcu_expedited_wait(void) return; if (rcu_stall_is_suppressed()) continue; + + nbcon_cpu_emergency_enter(); + j =3D jiffies; rcu_stall_notifier_call_chain(RCU_STALL_NOTIFY_EXP, (void *)(j - jiffies= _start)); trace_rcu_stall_warning(rcu_state.name, TPS("ExpeditedStall")); @@ -624,6 +628,9 @@ static void synchronize_rcu_expedited_wait(void) rcu_exp_print_detail_task_stall_rnp(rnp); } jiffies_stall =3D 3 * rcu_exp_jiffies_till_stall_check() + 3; + + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); } } diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h index 5d666428546b..f4d73ca20c76 100644 --- a/kernel/rcu/tree_stall.h +++ b/kernel/rcu/tree_stall.h @@ -7,6 +7,7 @@ * Author: Paul E. McKenney */ =20 +#include #include #include =20 @@ -604,6 +605,8 @@ static void print_other_cpu_stall(unsigned long gp_seq,= unsigned long gps) if (rcu_stall_is_suppressed()) return; =20 + nbcon_cpu_emergency_enter(); + /* * OK, time to rat on our buddy... * See Documentation/RCU/stallwarn.rst for info on how to debug @@ -655,6 +658,8 @@ static void print_other_cpu_stall(unsigned long gp_seq,= unsigned long gps) rcu_check_gp_kthread_expired_fqs_timer(); rcu_check_gp_kthread_starvation(); =20 + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); =20 rcu_force_quiescent_state(); /* Kick them all. */ @@ -675,6 +680,8 @@ static void print_cpu_stall(unsigned long gps) if (rcu_stall_is_suppressed()) return; =20 + nbcon_cpu_emergency_enter(); + /* * OK, time to rat on ourselves... * See Documentation/RCU/stallwarn.rst for info on how to debug @@ -703,6 +710,8 @@ static void print_cpu_stall(unsigned long gps) jiffies + 3 * rcu_jiffies_till_stall_check() + 3); raw_spin_unlock_irqrestore_rcu_node(rnp, flags); =20 + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); =20 /* --=20 2.39.2 From nobody Mon Feb 9 19:25:25 2026 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 13387161B56 for ; Tue, 2 Apr 2024 22:11: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=1712095914; cv=none; b=scZREIJpVaiANAwdt85GulHJ4tZc6LbEC/ySYnqBJ983LtCukHHKRAXChIB3XvFTBTD1zkh5FB0QJuBzKhmc3asaROAVptIfMWcgJAS2wxXc59csWeUGqfRstNVfeXVcDiRLUc0EakBYIOEZ+z7OW8UipqCw7/TC8bEwIhtw+KE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095914; c=relaxed/simple; bh=Fz+6ghLU6cuNsspdKh2jKQNyrr0w/MXxwLKzXAPSTY4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=S2HXqAkWgH+o1hDsBYz8qETQ4xT0PMoXWvoF55z/66s6d/qv+z1Kzh+4VlOFTrwqXb15/6FxiePBXt7CIl5Lg/z1146JiEHRwxyDa1+OyIE4bqgBW9hq2koEIq9vXduH0ce1YlESOaWNSXiYbIjYY3cwhUdObvU+GVSllp4QEoU= 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=m++Glsj3; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=pphhqj2A; 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="m++Glsj3"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="pphhqj2A" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1712095910; 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=CO7KUNjF5smgMIXwjoa7feK/SnbzBGmL0SPNqnsQ0MY=; b=m++Glsj3NT3O7hMbUHu69ZOthwa/9m1JFD8Dsw8Vhe5MQQEu63/sej2ydONMAFYVUaH5uu 2V4yhOyCcdvVBfJwMDz8y8RayelMcvRmUGlibpwcx6/6z1CmLncbeReAlL7ZopiX/q2mKa 7jaiNLyJv30qHe6W8POzZX9izItab8oLNj+U3aSIPjDHzZaIDvgApmJKkWZBgXHEpNX1C5 N4KjWEWn/DEHEzsQSs50uLZ220d4gJUKNqksxo1GPS410qAkP/J6pIZqcMnpXxyUT6swpY lLCL6ZpOh1UrlOg0Ft3E+fgcCNboDZpfX1XcKasneagLeyi/By/SCORE/3pJBg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095910; 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=CO7KUNjF5smgMIXwjoa7feK/SnbzBGmL0SPNqnsQ0MY=; b=pphhqj2Ah49yCaQnL+6U/PE74NZu+DjG14U9wY0BQyfQPa9szg1tDENsErAtl/ialHhU3V r0XgRuYDxSBCxeCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Peter Zijlstra , Ingo Molnar , Will Deacon , Waiman Long , Boqun Feng Subject: [PATCH printk v4 27/27] lockdep: Mark emergency sections in lockdep splats Date: Wed, 3 Apr 2024 00:17:29 +0206 Message-Id: <20240402221129.2613843-28-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Mark emergency sections wherever multiple lines of lock debugging output are generated. In an emergency section the CPU will not perform console output for the printk() calls. Instead, a flushing of the console output is triggered when exiting the emergency section. This allows the full message block to be stored as quickly as possible in the ringbuffer. Signed-off-by: John Ogness --- kernel/locking/lockdep.c | 91 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 88 insertions(+), 3 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 151bd3de5936..80cfbe7b340e 100644 --- a/kernel/locking/lockdep.c +++ b/kernel/locking/lockdep.c @@ -56,6 +56,7 @@ #include #include #include +#include =20 #include =20 @@ -574,8 +575,10 @@ static struct lock_trace *save_trace(void) if (!debug_locks_off_graph_unlock()) return NULL; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_STACK_TRACE_ENTRIES too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); =20 return NULL; } @@ -782,6 +785,8 @@ static void lockdep_print_held_locks(struct task_struct= *p) { int i, depth =3D READ_ONCE(p->lockdep_depth); =20 + nbcon_cpu_emergency_enter(); + if (!depth) printk("no locks held by %s/%d.\n", p->comm, task_pid_nr(p)); else @@ -792,11 +797,13 @@ static void lockdep_print_held_locks(struct task_stru= ct *p) * and it's not the current task. */ if (p !=3D current && task_is_running(p)) - return; + goto out; for (i =3D 0; i < depth; i++) { printk(" #%d: ", i); print_lock(p->held_locks + i); } +out: + nbcon_cpu_emergency_exit(); } =20 static void print_kernel_ident(void) @@ -888,11 +895,13 @@ look_up_lock_class(const struct lockdep_map *lock, un= signed int subclass) if (unlikely(subclass >=3D MAX_LOCKDEP_SUBCLASSES)) { instrumentation_begin(); debug_locks_off(); + nbcon_cpu_emergency_enter(); printk(KERN_ERR "BUG: looking up invalid subclass: %u\n", subclass); printk(KERN_ERR "turning off the locking correctness validator.\n"); dump_stack(); + nbcon_cpu_emergency_exit(); instrumentation_end(); return NULL; } @@ -969,11 +978,13 @@ static bool assign_lock_key(struct lockdep_map *lock) else { /* Debug-check: all keys must be persistent! */ debug_locks_off(); + nbcon_cpu_emergency_enter(); pr_err("INFO: trying to register non-static key.\n"); pr_err("The code is fine but needs lockdep annotation, or maybe\n"); pr_err("you didn't initialize this object before use?\n"); pr_err("turning off the locking correctness validator.\n"); dump_stack(); + nbcon_cpu_emergency_exit(); return false; } =20 @@ -1317,8 +1328,10 @@ register_lock_class(struct lockdep_map *lock, unsign= ed int subclass, int force) return NULL; } =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_KEYS too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return NULL; } nr_lock_classes++; @@ -1350,11 +1363,13 @@ register_lock_class(struct lockdep_map *lock, unsig= ned int subclass, int force) if (verbose(class)) { graph_unlock(); =20 + nbcon_cpu_emergency_enter(); printk("\nnew class %px: %s", class->key, class->name); if (class->name_version > 1) printk(KERN_CONT "#%d", class->name_version); printk(KERN_CONT "\n"); dump_stack(); + nbcon_cpu_emergency_exit(); =20 if (!graph_lock()) { return NULL; @@ -1393,8 +1408,10 @@ static struct lock_list *alloc_list_entry(void) if (!debug_locks_off_graph_unlock()) return NULL; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_ENTRIES too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return NULL; } nr_list_entries++; @@ -2040,6 +2057,8 @@ static noinline void print_circular_bug(struct lock_l= ist *this, =20 depth =3D get_lock_depth(target); =20 + nbcon_cpu_emergency_enter(); + print_circular_bug_header(target, depth, check_src, check_tgt); =20 parent =3D get_lock_parent(target); @@ -2058,6 +2077,8 @@ static noinline void print_circular_bug(struct lock_l= ist *this, =20 printk("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static noinline void print_bfs_bug(int ret) @@ -2570,6 +2591,8 @@ print_bad_irq_dependency(struct task_struct *curr, if (!debug_locks_off_graph_unlock() || debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: %s-safe -> %s-unsafe lock order detected\n", @@ -2619,11 +2642,13 @@ print_bad_irq_dependency(struct task_struct *curr, pr_warn(" and %s-irq-unsafe lock:\n", irqclass); next_root->trace =3D save_trace(); if (!next_root->trace) - return; + goto out; print_shortest_lock_dependencies(forwards_entry, next_root); =20 pr_warn("\nstack backtrace:\n"); dump_stack(); +out: + nbcon_cpu_emergency_exit(); } =20 static const char *state_names[] =3D { @@ -2988,6 +3013,8 @@ print_deadlock_bug(struct task_struct *curr, struct h= eld_lock *prev, if (!debug_locks_off_graph_unlock() || debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: possible recursive locking detected\n"); @@ -3010,6 +3037,8 @@ print_deadlock_bug(struct task_struct *curr, struct h= eld_lock *prev, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 /* @@ -3607,6 +3636,8 @@ static void print_collision(struct task_struct *curr, struct held_lock *hlock_next, struct lock_chain *chain) { + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: chain_key collision\n"); @@ -3623,6 +3654,8 @@ static void print_collision(struct task_struct *curr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } #endif =20 @@ -3713,8 +3746,10 @@ static inline int add_chain_cache(struct task_struct= *curr, if (!debug_locks_off_graph_unlock()) return 0; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_CHAINS too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return 0; } chain->chain_key =3D chain_key; @@ -3731,8 +3766,10 @@ static inline int add_chain_cache(struct task_struct= *curr, if (!debug_locks_off_graph_unlock()) return 0; =20 + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCKDEP_CHAIN_HLOCKS too low!"); dump_stack(); + nbcon_cpu_emergency_exit(); return 0; } =20 @@ -3971,6 +4008,8 @@ print_usage_bug(struct task_struct *curr, struct held= _lock *this, if (!debug_locks_off() || debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: inconsistent lock state\n"); @@ -3999,6 +4038,8 @@ print_usage_bug(struct task_struct *curr, struct held= _lock *this, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 /* @@ -4033,6 +4074,8 @@ print_irq_inversion_bug(struct task_struct *curr, if (!debug_locks_off_graph_unlock() || debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: possible irq lock inversion dependency detected\n"); @@ -4073,11 +4116,13 @@ print_irq_inversion_bug(struct task_struct *curr, pr_warn("\nthe shortest dependencies between 2nd lock and 1st lock:\n"); root->trace =3D save_trace(); if (!root->trace) - return; + goto out; print_shortest_lock_dependencies(other, root); =20 pr_warn("\nstack backtrace:\n"); dump_stack(); +out: + nbcon_cpu_emergency_exit(); } =20 /* @@ -4154,6 +4199,8 @@ void print_irqtrace_events(struct task_struct *curr) { const struct irqtrace_events *trace =3D &curr->irqtrace; =20 + nbcon_cpu_emergency_enter(); + printk("irq event stamp: %u\n", trace->irq_events); printk("hardirqs last enabled at (%u): [<%px>] %pS\n", trace->hardirq_enable_event, (void *)trace->hardirq_enable_ip, @@ -4167,6 +4214,8 @@ void print_irqtrace_events(struct task_struct *curr) printk("softirqs last disabled at (%u): [<%px>] %pS\n", trace->softirq_disable_event, (void *)trace->softirq_disable_ip, (void *)trace->softirq_disable_ip); + + nbcon_cpu_emergency_exit(); } =20 static int HARDIRQ_verbose(struct lock_class *class) @@ -4687,10 +4736,12 @@ static int mark_lock(struct task_struct *curr, stru= ct held_lock *this, * We must printk outside of the graph_lock: */ if (ret =3D=3D 2) { + nbcon_cpu_emergency_enter(); printk("\nmarked lock as {%s}:\n", usage_str[new_bit]); print_lock(this); print_irqtrace_events(curr); dump_stack(); + nbcon_cpu_emergency_exit(); } =20 return ret; @@ -4731,6 +4782,8 @@ print_lock_invalid_wait_context(struct task_struct *c= urr, if (debug_locks_silent) return 0; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("[ BUG: Invalid wait context ]\n"); @@ -4750,6 +4803,8 @@ print_lock_invalid_wait_context(struct task_struct *c= urr, pr_warn("stack backtrace:\n"); dump_stack(); =20 + nbcon_cpu_emergency_exit(); + return 0; } =20 @@ -4954,6 +5009,8 @@ print_lock_nested_lock_not_held(struct task_struct *c= urr, if (debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: Nested lock was not taken\n"); @@ -4974,6 +5031,8 @@ print_lock_nested_lock_not_held(struct task_struct *c= urr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static int __lock_is_held(const struct lockdep_map *lock, int read); @@ -5019,11 +5078,13 @@ static int __lock_acquire(struct lockdep_map *lock,= unsigned int subclass, debug_class_ops_inc(class); =20 if (very_verbose(class)) { + nbcon_cpu_emergency_enter(); printk("\nacquire class [%px] %s", class->key, class->name); if (class->name_version > 1) printk(KERN_CONT "#%d", class->name_version); printk(KERN_CONT "\n"); dump_stack(); + nbcon_cpu_emergency_exit(); } =20 /* @@ -5150,6 +5211,7 @@ static int __lock_acquire(struct lockdep_map *lock, u= nsigned int subclass, #endif if (unlikely(curr->lockdep_depth >=3D MAX_LOCK_DEPTH)) { debug_locks_off(); + nbcon_cpu_emergency_enter(); print_lockdep_off("BUG: MAX_LOCK_DEPTH too low!"); printk(KERN_DEBUG "depth: %i max: %lu!\n", curr->lockdep_depth, MAX_LOCK_DEPTH); @@ -5157,6 +5219,7 @@ static int __lock_acquire(struct lockdep_map *lock, u= nsigned int subclass, lockdep_print_held_locks(current); debug_show_all_locks(); dump_stack(); + nbcon_cpu_emergency_exit(); =20 return 0; } @@ -5176,6 +5239,8 @@ static void print_unlock_imbalance_bug(struct task_st= ruct *curr, if (debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: bad unlock balance detected!\n"); @@ -5192,6 +5257,8 @@ static void print_unlock_imbalance_bug(struct task_st= ruct *curr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static noinstr int match_held_lock(const struct held_lock *hlock, @@ -5895,6 +5962,8 @@ static void print_lock_contention_bug(struct task_str= uct *curr, if (debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: bad contention detected!\n"); @@ -5911,6 +5980,8 @@ static void print_lock_contention_bug(struct task_str= uct *curr, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static void @@ -6524,6 +6595,8 @@ print_freed_lock_bug(struct task_struct *curr, const = void *mem_from, if (debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D\n"); pr_warn("WARNING: held lock freed!\n"); @@ -6536,6 +6609,8 @@ print_freed_lock_bug(struct task_struct *curr, const = void *mem_from, =20 pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 static inline int not_in_range(const void* mem_from, unsigned long mem_len, @@ -6582,6 +6657,8 @@ static void print_held_locks_bug(void) if (debug_locks_silent) return; =20 + nbcon_cpu_emergency_enter(); + pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: %s/%d still has locks held!\n", @@ -6591,6 +6668,8 @@ static void print_held_locks_bug(void) lockdep_print_held_locks(current); pr_warn("\nstack backtrace:\n"); dump_stack(); + + nbcon_cpu_emergency_exit(); } =20 void debug_check_no_locks_held(void) @@ -6609,6 +6688,7 @@ void debug_show_all_locks(void) pr_warn("INFO: lockdep is turned off.\n"); return; } + nbcon_cpu_emergency_enter(); pr_warn("\nShowing all locks held in the system:\n"); =20 rcu_read_lock(); @@ -6623,6 +6703,7 @@ void debug_show_all_locks(void) =20 pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D\n\= n"); + nbcon_cpu_emergency_exit(); } EXPORT_SYMBOL_GPL(debug_show_all_locks); #endif @@ -6648,6 +6729,7 @@ asmlinkage __visible void lockdep_sys_exit(void) if (unlikely(curr->lockdep_depth)) { if (!debug_locks_off()) return; + nbcon_cpu_emergency_enter(); pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D\n"); pr_warn("WARNING: lock held when returning to user space!\n"); @@ -6656,6 +6738,7 @@ asmlinkage __visible void lockdep_sys_exit(void) pr_warn("%s/%d is leaving the kernel with locks still held!\n", curr->comm, curr->pid); lockdep_print_held_locks(curr); + nbcon_cpu_emergency_exit(); } =20 /* @@ -6672,6 +6755,7 @@ void lockdep_rcu_suspicious(const char *file, const i= nt line, const char *s) bool rcu =3D warn_rcu_enter(); =20 /* Note: the following can be executed concurrently, so be careful. */ + nbcon_cpu_emergency_enter(); pr_warn("\n"); pr_warn("=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D\n"); pr_warn("WARNING: suspicious RCU usage\n"); @@ -6710,6 +6794,7 @@ void lockdep_rcu_suspicious(const char *file, const i= nt line, const char *s) lockdep_print_held_locks(curr); pr_warn("\nstack backtrace:\n"); dump_stack(); + nbcon_cpu_emergency_exit(); warn_rcu_exit(rcu); } EXPORT_SYMBOL_GPL(lockdep_rcu_suspicious); --=20 2.39.2