From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id DD2001DDF8 for ; Thu, 2 May 2024 21:38: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=1714685927; cv=none; b=ch92jPj8xCLk3tY8xWEylePnIZjVsoF1ZlDXsyLSNKpYnrwd606JDIr8w3FAbNCSM7JSyMaKcSzXoA4RW1bhZn/egpcAnHmL7EMiCJ33UBI42970sZ+DArhDlTPquoH5OFjqld5AWD1cqSMvxVCrMdAqoch/t61zabJR+5AVTnA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685927; c=relaxed/simple; bh=+x0B0DKqCUuY5JhcpCChNmzn4DAXIu7FX0iwPh4yj00=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gPQFCCxDTbgm8OZiJOtoX+7xXaJvLXCBYrnsN7ivE9p5XYFKGO8AqltZ+TBS8KMto7HID9tm64RqRuoFGy7dRcNMQW5l1CPB8fbTAaVfY6V32eih8D8fsh68X2mqIK4gK5nhaEr8Cvf0CZiqTzyPvtxCN8LD6wlJ6h8j5YCQHEY= 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=uz1bhtTp; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BDzcvGoh; 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="uz1bhtTp"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BDzcvGoh" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685924; 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=uz1bhtTpgjos+cZ2g3IwiFLhNzOdWw9j8mS9gMg+PWeDfG39cr0+ScxrCR12iuz22Hs3BF m9UthHCxaKUmIqqZv4qe5v/PbBzdUwGhI9sfpJBoWrZc2a9K9TXTzNcjStYaZGD5qRqGjW VCM3rFYxYcC8pKGwsGfFiVarbJ45LxqfcFWK9JEqvK5kyTIq33VfS0qsC1HfKwT5e5+MTj bIpu8nO/c75Otr5H6eO1W0cOFsynCUHEWZdGwyeRHhmSsiLeW5EeowlCNLU3h7xwyagB5l EqdxPo6obrJVoPtYtePXbDTCSrjhxsn6lp21yJw4CeIFBQmC37YSHGyUln4EVg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685924; 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=BDzcvGohSvy38CtWxEEQFhEkqw5Gte97pjf8KmhK1JNEQ/yIWScYi7GSkwTevQ552O8SPi RqhxC3F0l8q9GfAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, "Paul E. McKenney" , Miguel Ojeda Subject: [PATCH printk v5 01/30] printk: Add notation to console_srcu locking Date: Thu, 2 May 2024 23:44:10 +0206 Message-Id: <20240502213839.376636-2-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" kernel/printk/printk.c:284:5: sparse: sparse: context imbalance in 'console_srcu_read_lock' - wrong count at exit include/linux/srcu.h:301:9: sparse: sparse: context imbalance in 'console_srcu_read_unlock' - unexpected unlock Fixes: 6c4afa79147e ("printk: Prepare for SRCU console list protection") Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7AE8B172BCB for ; Thu, 2 May 2024 21:38: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=1714685928; cv=none; b=slAhdlqa4lEUydVvbMxspq/uYPOOg3stJJb6EOUhFWT8m5e73D2SrTWIKaIBYMYUXEusCuD9ApAoL7cZ+bs4LTa/+MzNazMeZ/8ZsfEbAgdLEYgKBzHH66JzuXDNRkiFdZhR0VUUfXtQixSrETIcsllvJ/xC8ho3sC6DKvRYsfE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685928; c=relaxed/simple; bh=B9z0Do1Q1k86KsBEBPg78+535DgVuorMENKSK2N91PA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CHWIFzjjVpS30VkP7SOgosJPM7yhQT8Rw1lmCpTOhAJ1aIsHP6EHotR3bpZBYdm+/Mqt2hVzDP5aH34rLEAiqazBRsPDBGT/4DSENQHDduhj3V/OB2hRLGAHxSd77IkVx7nLQgzKooEc2l+ORbsJp8DxVrZBkM1xeIvLkA7iejo= 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=etpRiIm0; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=50GssRTK; 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="etpRiIm0"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="50GssRTK" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685924; 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=K6ESpqnB9vRJCiZCln55bmHgvujrBmLHD5zot49QaRE=; b=etpRiIm0V8TejGggDaTvhgf68fxkb8dZqJlAcZRuzbu74ykrVI9ru7c6NxpvEanmFt/DK7 elezdEBI0/prCAvNHR2IUU0C5nlYCqOjOcsiG4PPv3+E0TgIDXOUTpxPG1oN9Hrso3OYym l+4TV0GLC91l7lfSZK4n7cL7hJurWa4WzIvFuMoShiwotUFLxx5NWuNJfvUI0SJsBkzvo6 5NbJm6twoWhye+g5Gg0D/IMGjmV1VMBk+wegUi7ogbAdXZi2/8QMgLcPfnqD9QaM0BxZjB XqLdfJjUoGLcHfqyRa2vl0oxrWZW6RTnjv2CLSq4pH6tryFqkovcGnMKx8Pgrg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685924; 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=K6ESpqnB9vRJCiZCln55bmHgvujrBmLHD5zot49QaRE=; b=50GssRTK0qJQethWFSYAqUfdmiE+zwgWEuxvIz/WBEhU5skIzwl7UjL1xbPyupzSmW7102 abJo070DyjHZyGAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 02/30] printk: Properly deal with nbcon consoles on seq init Date: Thu, 2 May 2024 23:44:11 +0206 Message-Id: <20240502213839.376636-3-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Petr Mladek If a non-boot console is registering and boot consoles exist, the consoles are flushed before being unregistered. This allows the non-boot console to continue where the boot console left off. If for whatever reason flushing fails, the lowest seq found from any of the enabled boot consoles is used. Until now con->seq was checked. However, if it is an nbcon boot console, the function nbcon_seq_read() must be used to read seq because con->seq is not updated for nbcon consoles. Check if it is an nbcon boot console and if so call nbcon_seq_read() to read seq. Also, avoid usage of con->seq as temporary storage of the starting record. Instead, rename console_init_seq() to get_init_console_seq() and just return the value. For nbcon consoles set the sequence via nbcon_init(), for legacy consoles set con->seq. The cleaned design should make sure that the value stays and is set before the printing kthread is created. Signed-off-by: Petr Mladek Reviewed-by: John Ogness --- kernel/printk/internal.h | 4 ++-- kernel/printk/nbcon.c | 10 +++------- kernel/printk/printk.c | 41 ++++++++++++++++++++++++++++------------ 3 files changed, 34 insertions(+), 21 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6c2afee5ef62..739b82adc383 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -75,7 +75,7 @@ u16 printk_parse_prefix(const char *text, int *level, u64 nbcon_seq_read(struct console *con); void nbcon_seq_force(struct console *con, u64 seq); bool nbcon_alloc(struct console *con); -void nbcon_init(struct console *con); +void nbcon_init(struct console *con, u64 init_seq); void nbcon_free(struct console *con); =20 #else @@ -96,7 +96,7 @@ static inline bool printk_percpu_data_ready(void) { retur= n false; } static inline u64 nbcon_seq_read(struct console *con) { return 0; } static inline void nbcon_seq_force(struct console *con, u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } -static inline void nbcon_init(struct console *con) { } +static inline void nbcon_init(struct console *con, u64 init_seq) { } static inline void nbcon_free(struct console *con) { } =20 #endif /* CONFIG_PRINTK */ diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index c8093bcc01fe..de260531b0da 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 /** @@ -961,20 +958,19 @@ bool nbcon_alloc(struct console *con) /** * nbcon_init - Initialize the nbcon console specific data * @con: Console to initialize + * @init_seq: Sequence number of the first record to be emitted * * nbcon_alloc() *must* be called and succeed before this function * is called. - * - * This function expects that the legacy @con->seq has been set. */ -void nbcon_init(struct console *con) +void nbcon_init(struct console *con, u64 init_seq) { struct nbcon_state state =3D { }; =20 /* nbcon_alloc() must have been called and successful! */ BUG_ON(!con->pbufs); =20 - nbcon_seq_force(con, con->seq); + nbcon_seq_force(con, init_seq); nbcon_state_set(con, &state); } =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index c7c0ee2b47eb..e6b91401895f 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3348,19 +3348,21 @@ static void try_enable_default_console(struct conso= le *newcon) newcon->flags |=3D CON_CONSDEV; } =20 -static void console_init_seq(struct console *newcon, bool bootcon_register= ed) +/* Return the starting sequence number for a newly registered console. */ +static u64 get_init_console_seq(struct console *newcon, bool bootcon_regis= tered) { struct console *con; bool handover; + u64 init_seq; =20 if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) { /* Get a consistent copy of @syslog_seq. */ mutex_lock(&syslog_lock); - newcon->seq =3D syslog_seq; + init_seq =3D syslog_seq; mutex_unlock(&syslog_lock); } else { /* Begin with next message added to ringbuffer. */ - newcon->seq =3D prb_next_seq(prb); + init_seq =3D prb_next_seq(prb); =20 /* * If any enabled boot consoles are due to be unregistered @@ -3381,7 +3383,7 @@ static void console_init_seq(struct console *newcon, = bool bootcon_registered) * Flush all consoles and set the console to start at * the next unprinted sequence number. */ - if (!console_flush_all(true, &newcon->seq, &handover)) { + if (!console_flush_all(true, &init_seq, &handover)) { /* * Flushing failed. Just choose the lowest * sequence of the enabled boot consoles. @@ -3394,19 +3396,30 @@ static void console_init_seq(struct console *newcon= , bool bootcon_registered) if (handover) console_lock(); =20 - newcon->seq =3D prb_next_seq(prb); + init_seq =3D prb_next_seq(prb); for_each_console(con) { - if ((con->flags & CON_BOOT) && - (con->flags & CON_ENABLED) && - con->seq < newcon->seq) { - newcon->seq =3D con->seq; + u64 seq; + + if (!(con->flags & CON_BOOT) || + !(con->flags & CON_ENABLED)) { + continue; } + + if (con->flags & CON_NBCON) + seq =3D nbcon_seq_read(con); + else + seq =3D con->seq; + + if (seq < init_seq) + init_seq =3D seq; } } =20 console_unlock(); } } + + return init_seq; } =20 #define console_first() \ @@ -3438,6 +3451,7 @@ void register_console(struct console *newcon) struct console *con; bool bootcon_registered =3D false; bool realcon_registered =3D false; + u64 init_seq; int err; =20 console_list_lock(); @@ -3515,10 +3529,13 @@ void register_console(struct console *newcon) } =20 newcon->dropped =3D 0; - console_init_seq(newcon, bootcon_registered); + init_seq =3D get_init_console_seq(newcon, bootcon_registered); =20 - if (newcon->flags & CON_NBCON) - nbcon_init(newcon); + if (newcon->flags & CON_NBCON) { + nbcon_init(newcon, init_seq); + } else { + newcon->seq =3D init_seq; + } =20 /* * Put this console in the list - keep the --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E58FA180A9E for ; Thu, 2 May 2024 21:38: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=1714685928; cv=none; b=hoZFcaZ4GuHe7k3MyV/juMZD/VPBeCwIZZf3sq36CEs1nlrkYjaoUP1azMN270A0fB2MWDmeOK11aRr8obAqEHP0CgC7PjpCb2iSGRSAFiVx2Vm3v1fd3cgWciEcTrVc4RV5hXtueBFcm6HtkqNUkyqnDyEbxudcVvwJES964Z4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685928; c=relaxed/simple; bh=y85BCsAtj/oIZY53Aq9gsvAF7SkfMiwPpUsNnn9BKuo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ZmW/2POr3PYvW3mA44PtfXeBO4q02YXrWmiqSHCMaVOK+iJ3nOjWhZW5j0f2fEVB5tQCm6swhGAf26iuU6JI7dfjyUo5MSKDuhitO0nHbV/9J5AflWuMBsuufS2mJ0hRKwA7CQ6uADIPLadgpkJ/kOGUN2JT50Ds+lEBwxj1ltE= 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=bsMC1Ueu; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=sLAhE8DK; 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="bsMC1Ueu"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="sLAhE8DK" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685925; 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=FGxVNv6QbVc1S9vlUzY53XnVo+kA+8x3wI8r9c33MRA=; b=bsMC1Ueu4QL/FV59npAdNDqzjpmrtAZ/POtmA+9+31VgEJcbUkZVSXB1XCdgJzS/rluA1S HjyGj9VEARxYxNXk0SacPvw+9hMpc8wxkSPDvjwY1sVDZavR/vczJHCkYxnzwnQNJJKk8p w0f2CcIOGvnbGxTw/PsCWAsXvfi7jMdKyM9gH0BtaSZT5Rnj2K4to26/3mtJx+AJRbyn4/ i/gE6H8wdBpJddjkzxleoaK+Gr1zTHOwM4PkZ4uy0P0l53yFotSmSJ+MHabUZTUn8cuRzN FgtAEDlhyHaozi/tmr/0rtBGXCRDfbXWjbyA30XconxFEw5QyRvbRy9Rgsk60w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685925; 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=FGxVNv6QbVc1S9vlUzY53XnVo+kA+8x3wI8r9c33MRA=; b=sLAhE8DKrpSZfCWypbS8O3IlmQ1SjOC8VGrelZOUHpUgdr+8ZFlpVRafWA5RHb2efqBe1R 5kFjsNXIIThrkHCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 03/30] printk: nbcon: Remove return value for write_atomic() Date: Thu, 2 May 2024 23:44:12 +0206 Message-Id: <20240502213839.376636-4-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 de260531b0da..dd7c3180b335 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 2A03518132D for ; Thu, 2 May 2024 21:38: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=1714685928; cv=none; b=gdlDkKh/jmWcX2PEOGwiH3mFqrnaNetNriQiSqb1Cw8q0O7tAX9G99KHu2U3Clk/HssKv1mDtheV59qPJ97gN2Br9/98+AkLaDnbS7owVK78dwax1eoS0IMrP5BsY3fE1MjVTxt51W6VrJBD5RSE3mMF+Lcyz0HEl6J08A9lxMI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685928; c=relaxed/simple; bh=NbX9KqSu+VKC9sOzIh/s7S68iv2vejeD4ejGhNdGI1Q=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=XXGa/37Jjtmypn5BKtF3/dpksK0AvJIGezqwokHPVsQxCidsQ7jbLHnrTSRHcJ71PNnotX+W2mc2OWwrAnjgHVHt3gtkaW6kWW3CVg6MWOVF4w+Qoao1+xJlklA/HZXVSBGeJ/HFsRhePyV1v4O/iXA/vbS4eEmshFnFe0bnLfM= 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=mHVEbyNa; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=tdV4BBLA; 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="mHVEbyNa"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="tdV4BBLA" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685925; 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=iGNvVJcsNgc3jll4nDoPF6g4hs94SRVjeR/SV60Zi28=; b=mHVEbyNadP/y1Aos7cR+kyTwG2WXBYdr8zim+Ij95yqUPwx6QXNQxyleYFc2zrkmmGhdVW vOmnUG0r/5prUSrKhwh3UIfggbBnjvJtw7vRaYUuaDKRonp+K/a6bFHyRk6O0YfOcFrDkL 59VqfLSWbH9DIb/62uQ9s+PGFiSgU7avdjgtVQxgJ0ILBCehAPCBgEWI0rAS2ycnjLY0Q6 tEB1j/WqhJ81rPRc1e+aAYmjP/7aTCQRKLrZLwKheHQdluicbuEQpF7IakMWUU6G4ji4TR Sphs4L3GeGiL33C8Eso/uXs5xPjF9wsasW8pZ4F8+b6VlDu856phRvMto2O40w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685925; 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=iGNvVJcsNgc3jll4nDoPF6g4hs94SRVjeR/SV60Zi28=; b=tdV4BBLA7zeb6GnBsc/iksSNwSqxn2/A5A0Hj8cJEN8Qlx3pEaJZejdQjtpdTBvFTCfMLr s4uskHnUNj9VDQCw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Sebastian Andrzej Siewior Subject: [PATCH printk v5 04/30] printk: Check printk_deferred_enter()/_exit() usage Date: Thu, 2 May 2024 23:44:13 +0206 Message-Id: <20240502213839.376636-5-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 739b82adc383..831f95f26d48 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E5A7518411A for ; Thu, 2 May 2024 21:38: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=1714685931; cv=none; b=FjPtl8/aB/FKliNoBTI4u2scPU63s3OUD/ALzorfJcz57mEtWBayMQvkcVG/nZdgl6QlUMUrP5K9+Yu/wVooOFSUEcuUuDidLN/MPzEeRDav2G8Vw66hRFFeIwa22tGH2UNLc2ap8UGn2vM4oUk5j0xd7L2WvEBW+4rRDyQC7/8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685931; c=relaxed/simple; bh=M6731ktSPnZgfCgxzWZvBUYPs+vk9Ud8/zAT7Lv5tCA=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=X1GSPYT0tfvwPTQKnj8ZWkF2En0sN9Td4Co193DRYHeimohEj8SYXocxzrdWeZ+L1qQFgsym7ITFNDMJlBVTql1+QyjeGQNozjaVpj62bKshzHMg0/diXsyLRFpBgfJ/JyJzZMtvbvOvM2+85RQd3bhh04HEnpIkIdGJ9uUaOxc= 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=bdb8wETF; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=wIQolroO; 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="bdb8wETF"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="wIQolroO" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685926; 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=92SfCOf+RX+OwCPiZ2+/qccTofi7pfYp20D/1pHbIz4=; b=bdb8wETFWt7YOianV1jgkpgzkzRkZnH4WKtGt0x+GFNBb+l6vSY18hiFN/8qTg3/r8B+ko k3ySI10sr9MvGGaRQ1vEkIR34G/rOI41iDtBuPWdFidQCVf0ZXXvVQnI7WpaBSQ9pHuoz9 5DrHQEk4LOpmHgXMC4k22acf2EL5eNSCjyjQQBTTD4lTKFAaWt+/lgHcUx2TwwwZu7qadQ pslRt/NsgTra7lG+0o3FxQPlWpgEXj0//Wry1gm3XOkqb3wqLAIuXnJ+EEQzkgW1JjraFM q1AcisJ1rRdpY/vw8T4YU7qrGcMW8/RRn16r9Itq9l99FCdvhetLYhVD3hl+Qg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685926; 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=92SfCOf+RX+OwCPiZ2+/qccTofi7pfYp20D/1pHbIz4=; b=wIQolroOk21T8TCIUIJ3m5I8IhLHtKwoDTHg/dntuJJ7iQnh2azw+/gwcAMpIZPta76m8t mzU4Lei1jYKZzGBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 05/30] printk: nbcon: Add detailed doc for write_atomic() Date: Thu, 2 May 2024 23:44:14 +0206 Message-Id: <20240502213839.376636-6-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The write_atomic() callback has special requirements and is allowed to use special helper functions. Provide detailed documentation of the callback so that a developer has a chance of implementing it correctly. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 33 +++++++++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 54b98e4f0544..3291cc340f1a 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,34 @@ struct console { struct hlist_node node; =20 /* nbcon console specific members */ - void (*write_atomic)(struct console *con, - struct nbcon_write_context *wctxt); + + /** + * @write_atomic: + * + * NBCON callback to write out text in any context. + * + * This callback is called with the console already acquired. However, + * a higher priority context is allowed to take it over by default. + * + * The callback must call nbcon_enter_unsafe() and nbcon_exit_unsafe() + * around any code where the takeover is not safe, for example, when + * manipulating the serial port registers. + * + * nbcon_enter_unsafe() will fail if the context has lost the console + * ownership in the meantime. In this case, the callback is no longer + * allowed to go forward. It must back out immediately and carefully. + * The buffer content is also no longer trusted since it no longer + * belongs to the context. + * + * The callback should allow the takeover whenever it is safe. It + * increases the chance to see messages when the system is in trouble. + * + * The callback can be called from any context (including NMI). + * Therefore it must avoid usage of any locking and instead rely + * on the console ownership for synchronization. + */ + void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); + atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct printk_buffers *pbufs; --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 07422184139 for ; Thu, 2 May 2024 21:38: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=1714685930; cv=none; b=gNTwFXQGTqKtuZA8x3wnTLQLTAD1pZcg9mFbNQK5yL8r/k+AoeY2xjiTKIsEdwwYKKNvY/ClR0kaTASCejTe6hIehUMvqISoI4unmjMUefRj9JwBgMzBIi90Gh+G3oPVhtjExuUvSEX4OfGSdyJMz/yXR6rQ2Kj+gVZbCJ1UpjY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685930; c=relaxed/simple; bh=nIpG4fXaTa8mSjCbv1ZSGShw/vQloUe1E2WzNi1qKkQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fuoYQdM3c3liYIYaJ71xaAiEVpqdmT8Tq3PBecM38Ee8WGgFZ+eHBwMBwU8Nv4rLTrIzH5jnIduuKTUdyg2KrC/1WNwpzWkZIuJn+xZoWAqk1fyDjmX7DwfXY4ZwnqM5Xe5N+f1mAja7b+NQW1VK9HrARTZ9Kko1uUY9X1HzjzI= 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=Lnaxy3Mz; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=mHfXRFBh; 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="Lnaxy3Mz"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="mHfXRFBh" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685926; 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=ikaIBhkO9a0LJOxhudxcKYnEZt0PSHVLhaJwgBGZFyQ=; b=Lnaxy3MzelPLT6Dypxah9BXa9yO3bszXdMn0azEFPWwppEid3+frayfJxKp9U9cGCt8FDG rXe/yYr1kx7RzdFx13+DfajkQ6twUZghnjAjOnfiLR4qr1L55QmEJdxSJUWa7ywx45IhAu /07sihJQB2B+7Jtmf1HVQheYsxWMED4/5JmonRfMUE4zBManaqthVmtbiMDiRpiToJ4Aew PEurfjsu7hsSyVhwMov3axLiLO4k94mKBtScwfcLN7q0FaEVJphXCouBijZR3prtiKPlaP Y+MpEs0gCbAfjOG86biT4FSJP8xkhtMKQLaTuNYN9+kuaMIm3hOswQZ6y5IDWw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685926; 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=ikaIBhkO9a0LJOxhudxcKYnEZt0PSHVLhaJwgBGZFyQ=; b=mHfXRFBhR+Y1FEepmsSL098XTQAXcNo0Rhv+48QUKQYIWB7luDPWPkbNpRv0NK9WroEoEh tXQm21rVwqRWZqCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 06/30] printk: nbcon: Add callbacks to synchronize with driver Date: Thu, 2 May 2024 23:44:15 +0206 Message-Id: <20240502213839.376636-7-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Console drivers typically must deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls. To provide the necessary synchronization, usually some driver-specific locking mechanism is used (for example, the port spinlock for uart serial consoles). Until now, usage of this driver-specific locking has been hidden from the printk-subsystem and implemented within the various console callbacks. However, for nbcon consoles, it is necessary that the printk-subsystem uses the driver-specific locking so that nbcon console ownership can be acquired _after_ the driver-specific locking has succeeded. This allows for lock contention to exist on the more context-friendly driver-specific locking rather than nbcon console ownership (for non-emergency and non-panic cases). Require nbcon consoles to implement two new callbacks (device_lock(), device_unlock()) that will use whatever synchronization mechanism the driver is using for itself. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 43 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index 3291cc340f1a..33a029d976c3 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -354,6 +354,49 @@ struct console { */ void (*write_atomic)(struct console *con, struct nbcon_write_context *wct= xt); =20 + /** + * @device_lock: + * + * NBCON callback to begin synchronization with driver code. + * + * Console drivers typically must deal with access to the hardware + * via user input/output (such as an interactive login shell) and + * output of kernel messages via printk() calls. This callback is + * called by the printk-subsystem whenever it needs to synchronize + * with hardware access by the driver. It should be implemented to + * use whatever synchronization mechanism the driver is using for + * itself (for example, the port lock for uart serial consoles). + * + * The callback is always called from task context. It may use any + * synchronization method required by the driver. + * + * IMPORTANT: The callback MUST disable migration. The console driver + * may be using a synchronization mechanism that already takes + * care of this (such as spinlocks). Otherwise this function must + * explicitly call migrate_disable(). + * + * The flags argument is provided as a convenience to the driver. It + * will be passed again to device_unlock(). It can be ignored if the + * driver does not need it. + */ + void (*device_lock)(struct console *con, unsigned long *flags); + + /** + * @device_unlock: + * + * NBCON callback to finish synchronization with driver code. + * + * It is the counterpart to device_lock(). + * + * This callback is always called from task context. It must + * appropriately re-enable migration (depending on how device_lock() + * disabled migration). + * + * The flags argument is the value of the same variable that was + * passed to device_lock(). + */ + void (*device_unlock)(struct console *con, unsigned long flags); + atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct printk_buffers *pbufs; --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EA0C194C84 for ; Thu, 2 May 2024 21:38: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=1714685930; cv=none; b=iWDExgfhmALvkgTiIakc3wldt/PZbtONTz6h1I4FMIt7btnjOcyZw4No2oSAYhypxQ1UoiTaJ2H2uUvDkopLxodA5OBhuSrVi4fUuRzzkDCGx2ph6PC/7U+b0pg65I+CPlvgoEPaI2EBtAZIEq6OCdFXNOjpPa/Hzwo8FrGD7SE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685930; c=relaxed/simple; bh=TpqIckwvGSQpdKGqCSiygziHu6G/RKJgVV2bED2IMjM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Eo4jQxz2tCuifs0yyRkTwTvywjpv1YC+o+UPolRAgltjgcbQV4IS777T1KYvkf/etO48xAG5VVd9OxhojYqPnAxQaKuQYbfaF1ZM6pQaLEP5Azpzc8xXbMs1EGYPmt4YSsQ2xwR17dLLsVqG86Q9AFdB/nVI5TYnF+ZCUKBp3G4= 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=wnIbi3yn; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=nQoBQxyc; 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="wnIbi3yn"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="nQoBQxyc" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685926; 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=qFo66fB3Dr4Q0iDMaowiC7x8lIqphhEIOzcW8UZuab4=; b=wnIbi3ynycOQsHazB1brRXRuRYZlyybYoMQaOPPPtQSY18itzKVUMa3bTHPFNi+wSNBoRO LKu5cZPhnDAoLWgVxdBlaMhcTs03yUjbpv4/hM4WlPGyi30cBu5TgbjFWem5MHvbWppkuV xBFGb4v8zzU6qieTx8w0sGnPCp/7b9aIYB1lF/AMWgi9PJS3BXh0MN9zK7Rd7YVkXGE0uF 1QlV2wQgNAZhcjtlQ2geSFnq6t75A/9vH1WItppvPKJyzuip6qIrcVmq9uPy51Q6HiyeLG Ex5QhX1AcpWI/NUAfGqp4iGFzri+Hk8d5ASz8zwduCnZfDiIeK4yjRyREl+E+A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685926; 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=qFo66fB3Dr4Q0iDMaowiC7x8lIqphhEIOzcW8UZuab4=; b=nQoBQxyc4wvn8p9aSROxofDmeC77kkudPik29ElIS2AHPKRgWOzt9hU8Yk1Au8r50vemEY tLseV/JikeGNTEDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 07/30] printk: nbcon: Use driver synchronization while (un)registering Date: Thu, 2 May 2024 23:44:16 +0206 Message-Id: <20240502213839.376636-8-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Console drivers typically have to deal with access to the hardware via user input/output (such as an interactive login shell) and output of kernel messages via printk() calls. They use some classic driver-specific locking mechanism in most situations. But console->write_atomic() callbacks, used by nbcon consoles, are synchronized only by acquiring the console context. The synchronization via the console context ownership is possible only when the console driver is registered. It is when a particular device driver is connected with a particular console driver. The two synchronization mechanisms must be synchronized between each other. It is tricky because the console context ownership is quite special. It might be taken over by a higher priority context. Also CPU migration must be disabled. The most tricky part is to (dis)connect these two mechanisms during the console (un)registration. Use the driver-specific locking callbacks: device_lock(), device_unlock(). They allow taking the device-specific lock while the device is being (un)registered by the related console driver. For example, these callbacks lock/unlock the port lock for serial port drivers. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e6b91401895f..15d19d8461ed 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3451,6 +3451,7 @@ void register_console(struct console *newcon) struct console *con; bool bootcon_registered =3D false; bool realcon_registered =3D false; + unsigned long flags; u64 init_seq; int err; =20 @@ -3537,6 +3538,19 @@ void register_console(struct console *newcon) newcon->seq =3D init_seq; } =20 + /* + * If another context is actively using the hardware of this new + * console, it will not be aware of the nbcon synchronization. This + * is a risk that two contexts could access the hardware + * simultaneously if this new console is used for atomic printing + * and the other context is still using the hardware. + * + * Use the driver synchronization to ensure that the hardware is not + * in use while this new console transitions to being registered. + */ + if ((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. @@ -3561,6 +3575,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 /* @@ -3589,6 +3607,7 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + unsigned long flags; int res; =20 lockdep_assert_console_list_lock_held(); @@ -3607,8 +3626,18 @@ static int unregister_console_locked(struct console = *console) if (!console_is_registered_locked(console)) return -ENODEV; =20 + /* + * Use the driver synchronization to ensure that the hardware is not + * in use while this console transitions to being unregistered. + */ + if ((console->flags & CON_NBCON) && console->write_atomic) + console->device_lock(console, &flags); + hlist_del_init_rcu(&console->node); =20 + if ((console->flags & CON_NBCON) && console->write_atomic) + console->device_unlock(console, flags); + /* * * If this isn't the last console and it has CON_CONSDEV set, we --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1C61C194C82; Thu, 2 May 2024 21:38: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=1714685930; cv=none; b=UemXEzY6e5xIShaGgDe2nxpylwVmn2hE6Y/3KXRZJ1liQFIGNMVSd9naS6Jr+YfyJ28moXwXB1GfgJG/kPTqsgYXQj9IVhQDTzVY0TeKsDoCz4VRI0VsUwQN6w/iPdCNiLko2CoW0RX2Kv5svjC00uTuMBpcQPugcGeGYOgFoOY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685930; c=relaxed/simple; bh=AiH8EnN6GRzi8B2qcObkU/HEqg04wntd+nSfmS7tVhM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=obfO9Jx9jODlMDaiI9uhEyvOHV+zyYGQLwlOSwF4VPhLWTYwLkv9SGfbA6R8ogWlPB9hN/mjNHhTa5JDdnrQv6dlH078UmLhx05AYS2oBd89JgLay1sz9gEYBE1rj3j6/sLtW2RuZbIjI5GNQrgNGLraLEeYtJQqe2xDeN4/nY4= 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=kk8VD7pT; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=s8Ey1dq2; 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="kk8VD7pT"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="s8Ey1dq2" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685927; 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=TkEmSH+TeCzW4mgc3Qx6oM4UTLT7owCiQ5MwVZZnUf0=; b=kk8VD7pTXAIheqaqkPXOTdrJTW8fze+gim+F43pfEbl+kcsw+kE55hvDaaTRjDKKW3IU64 XJIxmLXffSvx19JZym3M30yfv6/1oez7u9mOwI2a9AA9oFwgb7kWBjt+kG3vZQWuddTDLH tYBQttmPIKGxkPVj4w/O66KaFyTcZFFBxqrlAFst1cMDAYu7A1ISvzjvZfuJcMrVbx0YAn iFRrXMhBh2+aVEAg3Ls8RiZVaDMiBbI5ut6MEonRVYqIttJq8uF6V0GZlPhpVwQG1710dr Yz3nXr3UYp+13YjUlj7NFxMj27bDsQBdX/t6Y85xNSszUr6MbeYETGvTqSaSWg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685927; 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=TkEmSH+TeCzW4mgc3Qx6oM4UTLT7owCiQ5MwVZZnUf0=; b=s8Ey1dq2S0Xo9P9sf+bTQCXOfYgSv/3zPCVQmslOZEzOEnoaaNgTxYePAeg0mWF2R+TPew Er6mGVceIF4vDOBw== 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 v5 08/30] serial: core: Provide low-level functions to lock port Date: Thu, 2 May 2024 23:44:17 +0206 Message-Id: <20240502213839.376636-9-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It will be necessary at times for the uart nbcon console drivers to acquire the port lock directly (without the additional nbcon functionality of the port lock wrappers). These are special cases such as the implementation of the device_lock()/device_unlock() callbacks or for internal port lock wrapper synchronization. Provide low-level variants __uart_port_lock_irqsave() and __uart_port_unlock_irqrestore() for this purpose. Signed-off-by: John Ogness Reviewed-by: Petr Mladek Acked-by: Greg Kroah-Hartman --- include/linux/serial_core.h | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7C1ED1A0AE6; Thu, 2 May 2024 21:38: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=1714685931; cv=none; b=qPbt1HF56XIN4WeAh2CyiAvYHnbmAhBfTMF/+4Bov9yBn2q4OBErJt8HJ7yZu+64xAsUiMRuWOGuL9JwIKjFc642kkUBqUSYYMZPeTZ8kF+cIb7u5Kup9Hmn+wiEYZAc2DVfD1oIPc9x/QFLGEvhKqJeVAa1mKIvDmVlBQ9FoVI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685931; c=relaxed/simple; bh=3RNIRC95YwkSxio89zLt/lJIBzFyR9QneWMaF/wgmVI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eLSXfr1B8c7P7xIe6vY1uXmSMMGgJiwjLE7khKihs3d7GGPlhClva61EDXT+jM6U30f0T6aqEAmPoIlxp5un9b+EtmU84maiy7Wyqz0dx6nLxAf6yExqXj9+fzX26e08552Lbmv00KuYBVyF5hK3Hk2qWLjwVYk1E3KKvdNt0Ls= 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=WxHBLqXI; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=9+i/8QDR; 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="WxHBLqXI"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="9+i/8QDR" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685928; 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=eaATRs9117EMzRD+7tFfr5M9kuNyDlYTa2bO2w7iv9A=; b=WxHBLqXIocUmwhU1GouBuCrQo/8ClFBUs4OhpstrHP4GJPx3wxkttlr3eQJhAyAEuSqLse kdIiwRTcuR05a+YiJYtTzc9croJZZm51tVAP5rpOhH/FblB0ObA6WeMxAdhI6o1vXXWR85 ANoax2tdUmZ6ghpZpM0kT2NcYgvvwJZVZiwiJcphTKb6GSE9pa1gq393fiq/9ZyoibFVFE KfmUAodV4MpmjyxHHCOTuJSFQF4pBElOLWQlVLY8pk709J93L3XGKUI8zCyDQC93bSasK5 OzLhV09GpLy76fMTrdZswsXXVSv1uhG8SJNMw8Fnbd5PfeOu8d9sVkcFS29Ufg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685928; 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=eaATRs9117EMzRD+7tFfr5M9kuNyDlYTa2bO2w7iv9A=; b=9+i/8QDRVKjQIJsdY6A+lbKZvBCvovWh77aZ6GZrUED3vse0MTixwhqlhigo1fQlaAA0py lheZLOL6fmcw/lDQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , Russell King , Tony Lindgren , Andy Shevchenko , =?UTF-8?q?Ilpo=20J=C3=A4rvinen?= , =?UTF-8?q?Uwe=20Kleine-K=C3=B6nig?= , =?UTF-8?q?Th=C3=A9o=20Lebrun?= , Linus Walleij , Hongyu Xie , Christophe JAILLET , Arnd Bergmann , Lino Sanfilippo , linux-serial@vger.kernel.org Subject: [PATCH printk v5 09/30] serial: core: Introduce wrapper to set @uart_port->cons Date: Thu, 2 May 2024 23:44:18 +0206 Message-Id: <20240502213839.376636-10-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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" Introduce uart_port_set_cons() as a wrapper to set @cons of a uart_port. The wrapper sets @cons under the port lock in order to prevent @cons from disappearing while another context is holding the port lock. This is necessary for a follow-up commit relating to the port lock wrappers, which rely on @cons not changing between lock and unlock. Signed-off-by: John Ogness Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek Tested-by: Th=C3=A9o Lebrun # EyeQ5, AMBA-PL011 --- drivers/tty/serial/8250/8250_core.c | 6 +++--- drivers/tty/serial/amba-pl011.c | 2 +- drivers/tty/serial/serial_core.c | 16 ++++++++-------- include/linux/serial_core.h | 17 +++++++++++++++++ 4 files changed, 29 insertions(+), 12 deletions(-) diff --git a/drivers/tty/serial/8250/8250_core.c b/drivers/tty/serial/8250/= 8250_core.c index 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..0c13ea6a3afa 100644 --- a/drivers/tty/serial/serial_core.c +++ b/drivers/tty/serial/serial_core.c @@ -3145,8 +3145,15 @@ static int serial_core_add_one_port(struct uart_driv= er *drv, struct uart_port *u state->uart_port =3D uport; uport->state =3D state; =20 + /* + * If this port is in use as a console then the spinlock is already + * initialised. + */ + if (!uart_console_registered(uport)) + uart_port_spin_lock_init(uport); + state->pm_state =3D UART_PM_STATE_UNDEFINED; - uport->cons =3D drv->cons; + uart_port_set_cons(uport, drv->cons); uport->minor =3D drv->tty_driver->minor_start + uport->line; uport->name =3D kasprintf(GFP_KERNEL, "%s%d", drv->dev_name, drv->tty_driver->name_base + uport->line); @@ -3155,13 +3162,6 @@ static int serial_core_add_one_port(struct uart_driv= er *drv, struct uart_port *u goto out; } =20 - /* - * If this port is in use as a console then the spinlock is already - * initialised. - */ - if (!uart_console_registered(uport)) - uart_port_spin_lock_init(uport); - if (uport->cons && uport->dev) of_console_check(uport->dev->of_node, uport->cons->name, uport->line); =20 diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index bb3324d49453..038693774f21 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -606,6 +606,23 @@ static inline void __uart_port_unlock_irqrestore(struc= t uart_port *up, unsigned spin_unlock_irqrestore(&up->lock, flags); } =20 +/** + * uart_port_set_cons - Safely set the @cons field for a uart + * @up: The uart port to set + * @con: The new console to set to + * + * This function must be used to set @up->cons. It uses the port lock to + * synchronize with the port lock wrappers in order to ensure that the con= sole + * cannot change or disappear while another context is holding the port lo= ck. + */ +static inline void uart_port_set_cons(struct uart_port *up, struct console= *con) +{ + unsigned long flags; + + __uart_port_lock_irqsave(up, &flags); + up->cons =3D con; + __uart_port_unlock_irqrestore(up, flags); +} /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1DE6A1BED8F for ; Thu, 2 May 2024 21:38: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=1714685931; cv=none; b=d23DjOjbR+bOktP3AedJP1XfA65Z+9uNS2fGNwoP3yN4RFCJ51wpOCpuv4zRCMj9u+Q5cvboyVwFEkGVVWUKB6unpq8aq/+QJaePhzeKPTWLZ13/HS82HZYvhCXi55t+mtu/mpEi3a35DNJv0Mn4t7Sc1pu22u6NxWBSSx8V4cg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685931; c=relaxed/simple; bh=nzXHdmgGHM2QSq1RQgJ659dlvXdtfhLYf/m9dCbBats=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=s+i/ng6coSUcfk5XMcJLA7S/cZZaQn1t0DUWniyHceGUFj39Yyk4Xvj5nGeSInYdlY9phsD+kF4FA4zjjpqh//8Si5tg99NtR0xCrldNQYQAHg2KPz10c2MY76HwU9vERAB9ho5XqQJ4vNYowUVHE9ytjRjzpc+s7632GQP/uog= 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=mJfuKLmN; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=xTqSDxmT; 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="mJfuKLmN"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xTqSDxmT" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685928; 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=SspE94wEVwK1yFu/e9otElRRg8AlYMKwqViOUc8d8w4=; b=mJfuKLmNTjK4pgFYAss3hLTLyB1zfj0nHGCUtoj+5jSLaBgG1ZbNN6BDxiPwaKjfnruNob ivrP0+5Y+gl+pMBdAkhN6sGmwy+3lZ5l3YT+TWm5rRY9z1Am8gPua72Wj+raTXrTr+9sNu Wajxvhlh+uNeNMWfw0owhevElikx5/Y121AURJkciqxuYhYmnPIMigmht+FODetBhM2oKd ac4RnvhmIJLYWxOLPFj8tibSx4ZHb8KHQ2XgWWm1jTmGWECdxkigixb6ImEWicdHgXYNsd xMO0rvUWe93Ds5u2Bcg2n0Ioh/UXZ0BleyqglqQGrQJNVXVzFTWmIoT8OqFCuw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685928; 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=SspE94wEVwK1yFu/e9otElRRg8AlYMKwqViOUc8d8w4=; b=xTqSDxmTEAdynqiThYO1tnJXr3Gh2+hDN/rIFMuwWIG0OWlukLMQhpnBPXn6XkDhZnxAS/ 3ocP/2usEwamLiDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 10/30] console: Improve console_srcu_read_flags() comments Date: Thu, 2 May 2024 23:44:19 +0206 Message-Id: <20240502213839.376636-11-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" It was not clear when exactly console_srcu_read_flags() must be used vs. directly reading @console->flags. Refactor and clarify that console_srcu_read_flags() is only needed if the console is registered or the caller is in a context where the registration status of the console may change (due to another context). The function requires the caller holds @console_srcu, which will ensure that the caller sees an appropriate @flags value for the registered console and that exit/cleanup routines will not run if the console is in the process of unregistration. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 28 +++++++++++++++++----------- 1 file changed, 17 insertions(+), 11 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 33a029d976c3..aea4f5aa4a45 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -428,28 +428,34 @@ extern void console_list_unlock(void) __releases(cons= ole_mutex); extern struct hlist_head console_list; =20 /** - * console_srcu_read_flags - Locklessly read the console flags + * console_srcu_read_flags - Locklessly read flags of a possibly registered + * console * @con: struct console pointer of console to read flags from * - * This function provides the necessary READ_ONCE() and data_race() - * notation for locklessly reading the console flags. The READ_ONCE() - * in this function matches the WRITE_ONCE() when @flags are modified - * for registered consoles with console_srcu_write_flags(). + * Locklessly reading @con->flags provides a consistent read value because + * there is at most one CPU modifying @con->flags and that CPU is using on= ly + * read-modify-write operations to do so. * - * Only use this function to read console flags when locklessly - * iterating the console list via srcu. + * Requires console_srcu_read_lock to be held, which implies that @con mig= ht + * be a registered console. The purpose of holding console_srcu_read_lock = is + * to guarantee that the console state is valid (CON_SUSPENDED/CON_ENABLED) + * and that no exit/cleanup routines will run if the console is currently + * undergoing unregistration. + * + * If the caller is holding the console_list_lock or it is _certain_ that + * @con is not and will not become registered, the caller may read + * @con->flags directly instead. * * Context: Any context. + * Return: The current value of the @con->flags field. */ static inline short console_srcu_read_flags(const struct console *con) { WARN_ON_ONCE(!console_srcu_read_lock_is_held()); =20 /* - * Locklessly reading console->flags provides a consistent - * read value because there is at most one CPU modifying - * console->flags and that CPU is using only read-modify-write - * operations to do so. + * The READ_ONCE() matches the WRITE_ONCE() when @flags are modified + * for registered consoles with console_srcu_write_flags(). */ return data_race(READ_ONCE(con->flags)); } --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5D51B1BF6DD for ; Thu, 2 May 2024 21:38: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=1714685932; cv=none; b=EMAkA59XUScSUmKiuDNPadz2tfRXVzIvAbeVQ++wkbtvNdb9jxXGEHvyb00axC3B09RSjhhRLdbbtIoo+eO+0aYyXFgjMn5Kd6KqRV86MJp06WRBqqD8riGyL9orGwQTN4W64myrCeTSpszUSXnSqLy+q04QearH6S9UlNhuYVQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685932; c=relaxed/simple; bh=V5mxrRSekt1IFCanlpGWcpd6IZlLeeHC7Umzth2wwT4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sEYL5qlBrgnunR+gPatdmY3BDu089F1+J0isz7kk+lMlKfCdHFwwxC4zRpcMxtkSet0GZklOoaizHRebMyLO+ckaXptr2m3A918dbTqLMXH3gFImv6VhelwfpeADMTEn0rx8KDmbttzurxoa5CNuKp9aqSpNIYE5Xcg9pLcZn0U= 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=nLlpGNdG; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=LKodxfWh; 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="nLlpGNdG"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="LKodxfWh" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685928; 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=t9DvvEOPQaKJAiNHj9V1cPMF/dPZtPshaTQ4VubjDeI=; b=nLlpGNdGe/OTfuVowh0Mr2DmsUboCRFrCjWGT8lEin7RIooN9m+ydrp/nnPHk61VFG1/xO jASLVKqog55kMhugDjewPpTKfFkBe5zapnFctVMZ2NVsRSRiMGaRhsf2DotUttONd/w+CQ 57my5sp5e8DJBJ1VjbB3SgwOreCsG1Lim0iC3wsLVORfJlWDRXXVFtpOJzLKFtuWD2HiSo Jx4uQUS9g44M8JSkpec/EUHzHm3GO2j25GMqRIsBMT7kU7UXoewJ6w8Qof3V18+sVZvpMZ 95BP0xeXRhtsOHlV8ev/EFCr9uTEBtTKN1AuvmOFcXw463BsxMlLRjHRXfmJ7A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685928; 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=t9DvvEOPQaKJAiNHj9V1cPMF/dPZtPshaTQ4VubjDeI=; b=LKodxfWhxpWyIDKiMK4KuldsgVPy4jzQeRd9d94+PzXyOq7DRRvCxJbSA+jPobfkLx6D3O 1aYScOgKPWsly9CQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 11/30] nbcon: Provide functions for drivers to acquire console for non-printing Date: Thu, 2 May 2024 23:44:20 +0206 Message-Id: <20240502213839.376636-12-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Provide functions nbcon_driver_try_acquire() and nbcon_driver_release() to allow drivers to acquire the nbcon console and mark it unsafe for handover/takeover. These functions are to be used by nbcon drivers when performing non-printing activities that should be synchronized with their atomic_write() callback. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 2 ++ include/linux/printk.h | 14 +++++++++++ kernel/printk/nbcon.c | 55 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 70 insertions(+), 1 deletion(-) diff --git a/include/linux/console.h b/include/linux/console.h index aea4f5aa4a45..3c20938f3676 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -304,6 +304,7 @@ struct nbcon_write_context { * * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print + * @nbcon_driver_ctxt: Context available for driver non-printing operations * @pbufs: Pointer to nbcon private buffer */ struct console { @@ -399,6 +400,7 @@ struct console { =20 atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; + struct nbcon_context __private nbcon_driver_ctxt; struct printk_buffers *pbufs; }; =20 diff --git a/include/linux/printk.h b/include/linux/printk.h index d8b3f51d9e98..d0a1106388d1 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 bool nbcon_driver_try_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,16 @@ static inline void dump_stack(void) static inline void printk_trigger_flush(void) { } + +static inline bool nbcon_driver_try_acquire(struct console *con) +{ + return false; +} + +static inline void nbcon_driver_release(struct console *con) +{ +} + #endif =20 bool this_cpu_in_panic(void); diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index dd7c3180b335..b8a7e3e136d3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -5,7 +5,9 @@ #include #include #include +#include #include +#include #include "internal.h" /* * Printk console printing implementation for consoles which does not depe= nd @@ -528,6 +530,7 @@ static struct printk_buffers panic_nbcon_pbufs; * nbcon_context_try_acquire - Try to acquire nbcon console * @ctxt: The context of the caller * + * Context: Under @ctxt->con->device_lock() or local_irq_save(). * Return: True if the console was acquired. False otherwise. * * If the caller allowed an unsafe hostile takeover, on success the @@ -535,7 +538,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(); @@ -989,3 +991,54 @@ void nbcon_free(struct console *con) =20 con->pbufs =3D NULL; } + +/** + * nbcon_driver_try_acquire - Try to acquire nbcon console and enter unsafe + * section + * @con: The nbcon console to acquire + * + * Context: Under the locking mechanism implemented in + * @con->device_lock() including disabling migration. + * + * Console drivers will usually use their own internal synchronization + * mechasism to synchronize between console printing and non-printing + * activities (such as setting baud rates). However, nbcon console drivers + * supporting atomic consoles may also want to mark unsafe sections when + * performing non-printing activities in order to synchronize against their + * atomic_write() callback. + * + * This function acquires the nbcon console using priority NBCON_PRIO_NORM= AL + * and marks it unsafe for handover/takeover. + */ +bool nbcon_driver_try_acquire(struct console *con) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_driver_ctxt); + + cant_migrate(); + + memset(ctxt, 0, sizeof(*ctxt)); + ctxt->console =3D con; + ctxt->prio =3D NBCON_PRIO_NORMAL; + + if (!nbcon_context_try_acquire(ctxt)) + return false; + + if (!nbcon_context_enter_unsafe(ctxt)) + return false; + + return true; +} +EXPORT_SYMBOL_GPL(nbcon_driver_try_acquire); + +/** + * nbcon_driver_release - Exit unsafe section and release the nbcon console + * @con: The nbcon console acquired in nbcon_driver_try_acquire() + */ +void nbcon_driver_release(struct console *con) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_driver_ctxt); + + if (nbcon_context_exit_unsafe(ctxt)) + nbcon_context_release(ctxt); +} +EXPORT_SYMBOL_GPL(nbcon_driver_release); --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B48241C0DC7; Thu, 2 May 2024 21:38: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=1714685932; cv=none; b=Jm3YfVAOgson00w19oSqyoVohD4WPLY41HpqaiQP4spe1Rk6dJxpsUihBE5O0BGQ9zEPvjKzHS7Yx29Ipp4UBgbDtrlzJt0674uKIKjZekkZvJXmum+mPbV+AZBhhFks2n/1F1OIbKtBpv/q9/mW8Fz9ynLtZ0km1yAYN8Evxf8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685932; c=relaxed/simple; bh=srR/EU4bu+X+MU/e0RZwp80S2gQQhwvRUTVL0x1Wui4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y6UclchGm1F/SImaEabufpvfa0Ex5j51gWpSVxYT+Ds7Fd0Qj98ng4i+XGD7BF4WfyKPF+TkSsPAXbTIK04Y/D2ckDeFfbDxA4+eRnKW9PJhlYP6NvMorCHQcdCOLFuMrBhPjWECDkS9fwbgl8PePlCB6VzHxNLPBX2bgXICjzk= 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=aZe8W9Sj; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=5+n2CmFo; 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="aZe8W9Sj"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="5+n2CmFo" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685929; 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=XMOfjsio8q+enlxkVZ0j+9BtcTeI3xwELU/31QEEo9o=; b=aZe8W9Sjo3BYhl80HQeLmX3SjyegySo8XXe07/p5OKOmdjIzHZttvHGXsrm5YcdoZ3q+xl gXhJd+vCDShiQyfCOvE6rIOqZJv8Wv/VJer3vtqsHCe00pbXQXDn08NXlaGsykeJj1iGFk CdSzFHUD9jd7YWHMbN0nT5tGSSI4dmSlzw69YuhGUtrxu91tMfM5/uAW9mJVyJceWgNfBU lH95YXgbBZCR2x+tEY+e8Exq1ZKtKAFqtmQgey4M1IfzK1bZbMybpafWhz661OInZpzkrJ YkpignhRhoSjqtOwZ1BivJ3LnyiKZ3sTzgS3zH5ak2Ygab7CR7BNqGbKwvfR1Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685929; 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=XMOfjsio8q+enlxkVZ0j+9BtcTeI3xwELU/31QEEo9o=; b=5+n2CmFoXbxIeCA3Cwr7AwJnIgNeQK2rxGV8mSQRmr2drfyQmhdUe+LxttrwvFxo7t5Bnu 49wei5CgqPJRv9CA== 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 v5 12/30] serial: core: Implement processing in port->lock wrapper Date: Thu, 2 May 2024 23:44:21 +0206 Message-Id: <20240502213839.376636-13-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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. Note that __uart_port_using_nbcon() relies on the port->lock being held while a console is added and removed from the console list (i.e. all uart nbcon drivers *must* take the port->lock in their device_lock() callbacks). Signed-off-by: John Ogness Acked-by: Greg Kroah-Hartman Reviewed-by: Petr Mladek --- include/linux/serial_core.h | 82 ++++++++++++++++++++++++++++++++++++- 1 file changed, 80 insertions(+), 2 deletions(-) diff --git a/include/linux/serial_core.h b/include/linux/serial_core.h index 038693774f21..a2f307dce221 100644 --- a/include/linux/serial_core.h +++ b/include/linux/serial_core.h @@ -12,6 +12,8 @@ #include #include #include +#include +#include #include #include #include @@ -623,6 +625,60 @@ static inline void uart_port_set_cons(struct uart_port= *up, struct console *con) up->cons =3D con; __uart_port_unlock_irqrestore(up, flags); } + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_using_nbcon(struct uart_port *up) +{ + lockdep_assert_held_once(&up->lock); + + if (likely(!uart_console(up))) + return false; + + /* + * @up->cons is only modified under the port lock. Therefore it is + * certain that it cannot disappear here. + * + * @up->cons->node is added/removed from the console list under the + * port lock. Therefore it is certain that the registration status + * cannot change here, thus @up->cons->flags can be read directly. + */ + if (hlist_unhashed_lockless(&up->cons->node) || + !(up->cons->flags & CON_NBCON) || + !up->cons->write_atomic) { + return false; + } + + return true; +} + +/* Only for internal port lock wrapper usage. */ +static inline bool __uart_port_nbcon_try_acquire(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return true; + + return nbcon_driver_try_acquire(up->cons); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_acquire(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return; + + while (!nbcon_driver_try_acquire(up->cons)) + cpu_relax(); +} + +/* Only for internal port lock wrapper usage. */ +static inline void __uart_port_nbcon_release(struct uart_port *up) +{ + if (!__uart_port_using_nbcon(up)) + return; + + nbcon_driver_release(up->cons); +} + /** * uart_port_lock - Lock the UART port * @up: Pointer to UART port structure @@ -630,6 +686,7 @@ static inline void uart_port_set_cons(struct uart_port = *up, struct console *con) static inline void uart_port_lock(struct uart_port *up) { spin_lock(&up->lock); + __uart_port_nbcon_acquire(up); } =20 /** @@ -639,6 +696,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 /** @@ -649,6 +707,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 /** @@ -659,7 +718,15 @@ static inline void uart_port_lock_irqsave(struct uart_= port *up, unsigned long *f */ static inline bool uart_port_trylock(struct uart_port *up) { - return spin_trylock(&up->lock); + if (!spin_trylock(&up->lock)) + return false; + + if (!__uart_port_nbcon_try_acquire(up)) { + spin_unlock(&up->lock); + return false; + } + + return true; } =20 /** @@ -671,7 +738,15 @@ static inline bool uart_port_trylock(struct uart_port = *up) */ static inline bool uart_port_trylock_irqsave(struct uart_port *up, unsigne= d long *flags) { - return spin_trylock_irqsave(&up->lock, *flags); + if (!spin_trylock_irqsave(&up->lock, *flags)) + return false; + + if (!__uart_port_nbcon_try_acquire(up)) { + spin_unlock_irqrestore(&up->lock, *flags); + return false; + } + + return true; } =20 /** @@ -680,6 +755,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 @@ -689,6 +765,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 @@ -699,6 +776,7 @@ static inline void uart_port_unlock_irq(struct uart_por= t *up) */ static inline void uart_port_unlock_irqrestore(struct uart_port *up, unsig= ned long flags) { + __uart_port_nbcon_release(up); spin_unlock_irqrestore(&up->lock, flags); } =20 --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3E0B71C0DFD for ; Thu, 2 May 2024 21:38: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=1714685932; cv=none; b=rHRKszkuiTlr9agAGCfX6Vwio8UFsG2Upcj59tfGlsGw0d44/SXujelKjAMEiu5GUQlrU5TF/czyIY9ssSw9kspM9ZqYFbcn96lyaUVYYVw/Zl60ru6mNERD7s0Xj4iOSGFJcKwH1MpzSlrI8AIW6BonJCqlEvMER3w1MIhek0I= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685932; c=relaxed/simple; bh=iA960+saI7gEnS5x6t8Ea+sjaf4rVAr0JyKNQ+CNr70=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sjXuFtIby3B68wcCjlBB+le4ioGPtsaUtvGhjrtbJc4WFPbSb22FrdZZgOvVvBv86yW8wtHBl8UAu1pxbC3xJaHESv5rf6fxbfeRvH37PR054ATeRC+zdfT5uXQRO6hrW0ZbcR2dTStVdPV4M4Uven9r8tpje4bY9vVIEHXz6lE= 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=gPA1Cq3r; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=koZ+HFiw; 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="gPA1Cq3r"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="koZ+HFiw" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685929; 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=Kjjk2yQaSdehlXyvncmPkRMwdSdvenQb1yl20R7F0SU=; b=gPA1Cq3rEIMA5tnEYeFbB62YkV2ult4GYTqdUgk53OYwzbuS08LNmNTf4AWFxBZCrWhjom o1UYAkTlZp0WdoAhhhHjimhf42Pu0CzF886MHtY6YqbJnAlUDJkZOVGEZT20M6n1UjbA01 K/nSH7VZF2PqEAB8c3ZJ1ojcYLrq2Zn1HXLdArxHv6RiLfIBmk60/E7uB77O61LPCexmBK pBJRLz7EYscAfO4/g3JsbGTyi5i4gIb3ZIcDw+Ee3UsgxZKg2YdyXWpuSRyN9gC9z6j2I3 rgYgxHM1WclrJ5MLbPukCe1Ws0gygyuZutmoYejkTnlS3aghVirNUoS5/YJzFg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685929; 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=Kjjk2yQaSdehlXyvncmPkRMwdSdvenQb1yl20R7F0SU=; b=koZ+HFiwRQvRSIg5gSD7BWhXo8357rjtQVzyhs6tCj6Djvs29yhCVQkBmTitQJPhg6ZViY rGiH2fgaAr4Nz9DA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andy Shevchenko Subject: [PATCH printk v5 13/30] printk: nbcon: Do not rely on proxy headers Date: Thu, 2 May 2024 23:44:22 +0206 Message-Id: <20240502213839.376636-14-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" The headers kernel.h, serial_core.h, and console.h allow for the definitions of many types and functions from other headers. Rather than relying on these as proxy headers, explicitly include all headers providing needed definitions. Also sort the list alphabetically to be able to easily detect duplicates. Suggested-by: Andy Shevchenko Signed-off-by: John Ogness Reviewed-by: Andy Shevchenko Acked-by: Petr Mladek --- kernel/printk/internal.h | 8 ++++++-- kernel/printk/nbcon.c | 13 ++++++++++++- kernel/printk/printk_ringbuffer.h | 2 ++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 831f95f26d48..6c1d01eabf3b 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 b8a7e3e136d3..c429f4cfbb2d 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -2,13 +2,24 @@ // Copyright (C) 2022 Linutronix GmbH, John Ogness // Copyright (C) 2022 Intel, Thomas Gleixner =20 -#include +#include +#include #include #include +#include #include +#include +#include +#include +#include +#include #include +#include +#include #include +#include #include "internal.h" +#include "printk_ringbuffer.h" /* * Printk console printing implementation for consoles which does not depe= nd * on the legacy style console_lock mechanism. diff --git a/kernel/printk/printk_ringbuffer.h b/kernel/printk/printk_ringb= uffer.h index 52626d0f1fa3..bd2a892deac1 100644 --- a/kernel/printk/printk_ringbuffer.h +++ b/kernel/printk/printk_ringbuffer.h @@ -5,6 +5,8 @@ =20 #include #include +#include +#include =20 /* * Meta information about each stored message. --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 862B91C231D for ; Thu, 2 May 2024 21:38: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=1714685933; cv=none; b=bTYBoOvnwQldiIm+ymo8uRT/n6/TEG2BxYrMvXySefUgC6MDRNW1/MoswNHRVxBLROBx7aBXjLtJpgyvHBvG9TFd+EOayUWP3lwVE/Lge4AhV/kbIrPVyRWHTTz533ad9VxIWlADmvUbBGJFEvpmYdxKzj8uVmvwuRocNBi3XjE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685933; c=relaxed/simple; bh=9Grq66tYc52lVbzlMciVnJKn6pZT7/ppw1afD/fleRE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OYSZt+RUFivNqXrnqmz5jWfz512wALGWbiwa7fMrfMrQWoVJNZr+O4eyX0RK20DtV2eQQJlNAgtCta4362so5X6g/g75s4QFgrcvXxlklTJnpjycuQtjRFWadQ98mmMmzjWpjnTySBlVFIg1FHVcqzAzRXN+Anc+1qMHHzZkc9U= 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=nYH6AIbc; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0rPOLxf/; 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="nYH6AIbc"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0rPOLxf/" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685930; 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=Oias+1Ax4KrbbYorG2aEU5ZFjDcXbK21YV2QApk6lYk=; b=nYH6AIbcNA8QPlt0tOnDKiW/tsi+t7/yqOJT6sNmNitdzf3LSiVu2fclTkn68B3CEJniIg 3QL9gkvIpBXxjHaX735avSeIufQkOT+tDHmr0gM8Ld1qmCcLHJy+rI5wCJUhw7QGygUNUO q3G4FA9e8B4/8sVxzXXpAKadWAluYfOwbI4mHTyK7quNuopV7G0m8ToKHSrhZWvaH8NnJd a37lkIoyKCsdm7WcSw7uJvUBOic/EM5mosKGqU8I+uiWKfiN+dTOZiOtaytSbG1CKK9Ew9 dfBPy2L3j+XEfdS/JU6JICIKJmUzbYed/jrI6sDGRAAyBHHBaPnrj3CXTXinYw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685930; 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=Oias+1Ax4KrbbYorG2aEU5ZFjDcXbK21YV2QApk6lYk=; b=0rPOLxf/XfcOzqKBz3DqmUwP1Y9YIM/ER7etnQaRg7Qd0vSJDMwooZvHb2ygHcHcejkoY2 96snNTDsIP5DYuDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Randy Dunlap Subject: [PATCH printk v5 14/30] printk: nbcon: Fix kerneldoc for enums Date: Thu, 2 May 2024 23:44:23 +0206 Message-Id: <20240502213839.376636-15-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 3c20938f3676..71d0638aa818 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 299161C65E2 for ; Thu, 2 May 2024 21:38: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=1714685934; cv=none; b=AePFl/Yjh/ZB57th7UsOWImhDP6IwdP1ZrpaAFlzuG0gk8AAlRgUy5qr3QlnrNbZ3sD4bei7AQW86HmlgvJtIIduiiq10Mx/DRVSohXTfwyjNnpXW/KHPPoJxvPrqwuWhKnqzUxy2XYSp7mYy28Tajv1pWbGnJX4wqUli7xD20M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685934; c=relaxed/simple; bh=uz9PsDDM91Md3077crOcgUldGsieRbCgfcyBWemN+Z8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Vr+iGTBxIr1OriOe7Vrtgo0B9gFKT0ds8ufiO9lV6YeGA3qpVVVRr2hv5Z80Q3u3uSModXl7krfRA4f9ocPaYkJsqd80og3JLaa2IdjOBSoO384VqFq/W9456AZgj6x1ZGNYdtcuEbHXcW1H0GS4iA15X9TR6IVJ/BTabdSflbA= 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=olQiN3+1; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=HpcfrFDh; 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="olQiN3+1"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="HpcfrFDh" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685930; 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=QJyf1SS8ltJ91fkLBiBJOqIJq9MASgB7rwQdXq0c04g=; b=olQiN3+1G8WaRvFcr/nxEYJT07Lj3NFtv911hXhHk5/BGbPlhGgPfz1E4kXwevrv9kGK/p 9Tu0Zhno8uAv4yTX6ptpMoO8FfYZ0M/aivpVprngtemvSLhUnf22zIUYaR3ZuP6oSWgF0f f9rsdEz8ZrdpuhecH6B9L6m0BPdBS804ZbO+Oibdm3KQc3pHb5KwBUm7kE1icoz15RQKaJ CV8CnOivN/GwH5Hpc33N+RvRwXyrwWSdniCNDJ5F5QgLaQeS25nqNRCRLm7uGyTE+Q7PSQ VAN/C0PYTRQ48hbYroAqJP0QuHgkM/LBpYtGifPCEJ59B6o5LhBN5q8NTx/vww== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685930; 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=QJyf1SS8ltJ91fkLBiBJOqIJq9MASgB7rwQdXq0c04g=; b=HpcfrFDhbJz1GQqgnpyEtvBFfCNt7Tw8uqDTW7Jq3Bwo8bdCH63aalcimbEg/RH7Pad+4O HrdLtYCWbKu7DyAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 15/30] printk: Make console_is_usable() available to nbcon Date: Thu, 2 May 2024 23:44:24 +0206 Message-Id: <20240502213839.376636-16-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 6c1d01eabf3b..339563dd60bb 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, u64 init_seq); 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, u64 init_seq) { } 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 15d19d8461ed..b0e99ee472e1 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 01F851C233B for ; Thu, 2 May 2024 21:38: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=1714685933; cv=none; b=J6AqXmyMzuifBRQUpzcsaO4lx9y3KnMaggDe4W5/0lRNB9BucuMPYzWLGPoQcU9YY/SZSsu4Xah8p0ZqMIU6F3AIQt4RexiMN1f/FiX484GgfnfZtpQg3bgQ0s2/iZe/Zmi9IcOOomOt1X1D3rFVxdJtCYVAwR591sGS5xuLSFI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685933; c=relaxed/simple; bh=lwK7GcrDHEjSCVMCl6fH9fAyvwP+TuECoQz0KJrspIs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=swPlsRww6YpA9FwWaWN8WpvZbVGQqOL2TSgWsFc6ArTzWbrxw2c39m8UZ8KD3MJaUeHCtqvwN6+GPnIU+q7if/Awuf8E09v95mnujlUKvWi06h7q5nKYXABVzLqImkaNxBVNvi/mpl3JwMTGc2wcD8UDp6/hKC9EArk+1CC5QoU= 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=aRCUgySs; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=bBEygG+t; 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="aRCUgySs"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="bBEygG+t" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685930; 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=gIB/C0kn4U7fgTEgQsG7CoHVIr2JrRBBOwabsQzPu5A=; b=aRCUgySsgIMJPdkX8w83I6XtQKM9MpLdM6TI+yT9ThD4ktEvVJnqNLeguPv6YLzZUxxb+c Bztr3PrEnd4upA89FdjBv1Q9Rb85zJ8XRtUjzf0DHaKbxduHe/V3NxAM080s56WDnyvskl MUsPrX5WLHLX4FndYzXvdyegCdm6ysiEJ1KgYbTp37NRk7lfkRhOVLsTmQxuDYh/Np0mF4 49FlaFL3CgTM0ru92r5wfiYze7megajaQOcnfvK8x8LZFeMjCr/BqKy1zLfL3vHdYDqdts uIWTzKL2CAKcf5kCHKCyv28V2SurryoiRzSXcOyHds1UNrYORKhs8/JT7+grDA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685930; 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=gIB/C0kn4U7fgTEgQsG7CoHVIr2JrRBBOwabsQzPu5A=; b=bBEygG+tIrdX7FBVduKNWeDSAUMNudEJd1sOtzR6U/D3E4AIyEkPwtD52glTUiabrv3W0U Z8pUERObNzgqokCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 16/30] printk: Let console_is_usable() handle nbcon Date: Thu, 2 May 2024 23:44:25 +0206 Message-Id: <20240502213839.376636-17-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 339563dd60bb..943e8f083609 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C9B61C65EC for ; Thu, 2 May 2024 21:38: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=1714685934; cv=none; b=tjPSwg3iYgoYokrzCmRq3puDAgS3tv9Og0DHP+rypwhUrq28MvIhI3VZJ7aDmzJdVUp2eA+1tSqH3lifjmKAYZNkJh0ld1i3b8eeGjcioOBCjCtAmRgZPw6hiBcHe4FR6Bvw86ktuGGgxL8ufB7SkS4nSM5zSIrr7/YrvaAL548= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685934; c=relaxed/simple; bh=SNLZe25NvdawX74sPMNN2b32M9UWDQ0xBcf74EaYUGs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ez65Ed11GO0WXismU833mXESueJ+Xmv9hZHXCvsCd84uL0stkie2flCSU7WMhMGE1WRuDb2GsGg6h96jKnemW5SK2lQzBqL8yFQMT8OG7/B4iwz5oVvwl8F3frX6jft96hG1nhLVlFjbeSW5/raoLPRFomXUQ3yskToB+FXZVuA= 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=u0lnJc6O; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=QDGpdx7F; 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="u0lnJc6O"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="QDGpdx7F" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685931; 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=vHXFQ842EHAesP3cWbIS8s/eD5QSwLsV5qin0yhI+w4=; b=u0lnJc6OtkrxDt5k76e0WVIUprCNBu9MktrVs8rsgDncMTv7pyWfIDOygfzEIHCxiA3m1T mPa+et4CoUVTYN1PcDUbcIk5JpF/qc22Aul5LWW3OrFVCu/nBG5pfsAhkh4+yvhf83BQKr B7p4XvToB31OItxKQmKCD7DWDOzB/terNQw09PVz2YW0yArQ5ls6JYrVxHzHItQZCvDytc HvT2cnAEY+o5fuuakohSvqvBWv9fJGVvH2n219HYVsS1+8rv6oLL0h8PWeHqdX5rkUNmZX coWd1HRN8G/oCyoYYB0GwFLmoH9qbx3vgZ5UWkSW3SEdZ+43IouiNFBOL1sfQw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685931; 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=vHXFQ842EHAesP3cWbIS8s/eD5QSwLsV5qin0yhI+w4=; b=QDGpdx7F/MGax+73p1+r2KrWTywkx7Qu89uuyDkRbnj4YFv+60ndkyutIyN2BRzd8rmluK MEYKpLhNGHbIKYBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 17/30] printk: Add @flags argument for console_is_usable() Date: Thu, 2 May 2024 23:44:26 +0206 Message-Id: <20240502213839.376636-18-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 943e8f083609..e2de2d262db4 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, u64 init_seq) { } 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 b0e99ee472e1..9cdc110a46a2 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 @@ -3823,7 +3824,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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B2A751C660C for ; Thu, 2 May 2024 21:38: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=1714685934; cv=none; b=DEJsxzH7M3EfI5QRhHQkebayWnPLnMN0F/r5yw1dTHcj+LOstj36ZPcD3hanzuvlyn0ZBn3PbB+2CCZ31uJugIqF9rFroFcNA58kjbRPMp9PLccv0FTRmJHVTRg5NFnFY7ErOxM3qjPkH/oKVEXfj2nm+LFjSPSiCiOSSQvhagI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685934; c=relaxed/simple; bh=tOlb55GZsIaT9cynpsyj5UhAczLLhu/zc6LG7enM9MM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=RsdDEtblRzkSmHOmoWcH48dQeeCkuqGnek8A0IF8+rEgoYgDK7AInxRZ3kJEwSyAIf9LwuCf7okPMuxVzbHYTjVfLgO6FqQAV31/xetEwi4PbNkDMhOBvWfaZsxhN5J8COw3CsdSHlCy9jjSn0jxPNmoX4Umw86poe1atwgSX+M= 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=yLAB//lY; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=eUdzEyLe; 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="yLAB//lY"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="eUdzEyLe" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685931; 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=xZtMTVl1yOdxUwgi1uSJ5RX46gAn8gugProPBvxHrmg=; b=yLAB//lYOLxA2VBr+Fmj4UrmcxxotVzWo6MUxgq9r9uOU5+EB8PBuFZiseZ+Ug6U5CQ39f QlhvNuf+72txiBwIe08ugIsqv+IzCUzYNs+YDW9BH8hGxjHJwLXOWovzY4QXOvvuq6p0xk FaO9+sL4sqtl9qgZg62T8pUWR8mkzR1A5IBhdKp3bSj5NfzPHtZqiKdoh+8tyLY6CML7tQ g76jFSS2AVa6CXROmo6t3gqsIC/l0MDs9wLylZ1r8icHZYrwwFnJoO/knQH2fyvqN3qS2+ QoL8C020KapG9y4S9cVohx7dHm39/XUyMHVT0IfRWXHkAATKrGn/Rs+tSWBNtg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685931; 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=xZtMTVl1yOdxUwgi1uSJ5RX46gAn8gugProPBvxHrmg=; b=eUdzEyLepH2X4yDnXVKEbC3+f7t8Wp3naT1xcOCcJAY2ZHMAUqswcFhlm3BDJxnhGsj+5x XzZ/FmwwUzt5HKCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 18/30] printk: nbcon: Add helper to assign priority based on CPU state Date: Thu, 2 May 2024 23:44:27 +0206 Message-Id: <20240502213839.376636-19-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add a helper function to use the current state of the CPU to determine which priority to assign to the printing context. The EMERGENCY priority handling is added in a follow-up commit. It will use a per-CPU variable. Note: nbcon_driver_try_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 | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index e2de2d262db4..5826cd4eed58 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, u64 init_seq); void nbcon_free(struct console *con); +enum nbcon_prio nbcon_get_default_prio(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -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, u64 init_seq) { } static inline void nbcon_free(struct console *con) { } +static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } =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 c429f4cfbb2d..e37af5c72d38 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -937,6 +937,22 @@ static bool nbcon_emit_next_record(struct nbcon_write_= context *wctxt) return nbcon_context_exit_unsafe(ctxt); } =20 +/** + * nbcon_get_default_prio - The appropriate nbcon priority to use for nbcon + * printing on the current CPU + * + * Context: Any context 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_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 7807A1C689D for ; Thu, 2 May 2024 21:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685935; cv=none; b=Thjzal2YyegknY6QpUD7SBUhPlOraQUki6a6U79WBtzLj5skSD3ZwMz3pWcIdUz9v20F97Z9YXS+qfpN3TOvd3B8dG8MlwbIOntggczyKAMhkL5lRiOpLXAzpE+Vp/bX+eN6XmAC4oHMjHWnKMgXQ6a88IlRuWqB2BH0bI3BeaE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685935; c=relaxed/simple; bh=fTtchIpBovev6HupeGoYzelu3WRCB5YiDqZ96NyLiRY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=fTYtuXFaizDRY8s4W9/h5q1F4yrvamJ6PMDjR4qsUGPcfgcVFEukR5zzKLRs3ERqK+/zYmQlAC9bxaUp5TH7YjXIx0W6vDW8E0xnTY0RfHYBgMuHf9hjUuxiKjX7iIR1sLGMGP1lwqOAGoJEYeWW1/tR5K7PhIhZ14ADADKyMS8= 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=nvgm7a7T; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dfki5hnX; 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="nvgm7a7T"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dfki5hnX" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685931; 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=SyciDM9MuzozEvvOTWNllt97oz699Qam5LE37cOWrg8=; b=nvgm7a7T+GD1ZJ1GC+0zH14GX37sug/8SVerGFNix9ZzgIXN9P1hncurrt67ASH2x1K8hC j32yVmruYRmJHFtSoWp1gKgFT+K79ebMIB+sjhRVj90gWDr/HebxBxRCNT4WtITWLpLI5v 5hYtIpYCaozt8mcUCRsC5fqKibFygTX6VdhMWYl/0Xx4J/0IljLXpdSp5Jwfooy0rQy/TR XXlt2evw8rsAtd9DnRjE+iA/Jx1su3CjpPOp3l1MRFwoPoNiHF6Ni21qSaKBHNZ3LccAiD RD+wwTeouXfbS6iWITMtnKzQTNBWwgLwYHihKcbwCBLQJCyR3TqFIgLmO1hVXg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685931; 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=SyciDM9MuzozEvvOTWNllt97oz699Qam5LE37cOWrg8=; b=dfki5hnXKCeKcmraDHEWFg55Af6pZr6tjWUmGNptbv1SkrMFx7ANe4c4/CRJUTsH13M9o1 BAloyq54DGOufiBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 19/30] printk: nbcon: Provide function to flush using write_atomic() Date: Thu, 2 May 2024 23:44:28 +0206 Message-Id: <20240502213839.376636-20-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Thomas Gleixner Provide nbcon_atomic_flush_pending() to perform flushing of all registered nbcon consoles using their write_atomic() callback. Unlike console_flush_all(), nbcon_atomic_flush_pending() will only flush up through the newest record at the time of the call. This prevents a CPU from printing unbounded when other CPUs are adding records. If new records are added while flushing, it is expected that the dedicated printer threads will print those records. If the printer thread is not available (which is always the case at this point in the rework), nbcon_atomic_flush_pending() _will_ flush all records in the ringbuffer. Unlike console_flush_all(), nbcon_atomic_flush_pending() will fully flush one console before flushing the next. This helps to guarantee that a block of pending records (such as a stack trace in an emergency situation) can be printed atomically at once before releasing console ownership. nbcon_atomic_flush_pending() is safe in any context because it uses write_atomic() and acquires with unsafe_takeover disabled. 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. Also use it in nbcon_driver_release() to flush records added while the driver had the console locked to perform non-printing operations. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- kernel/printk/internal.h | 2 + kernel/printk/nbcon.c | 162 ++++++++++++++++++++++++++++++++++++++- kernel/printk/printk.c | 2 + 3 files changed, 163 insertions(+), 3 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 5826cd4eed58..5a93825871de 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -85,6 +85,7 @@ bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con, u64 init_seq); void nbcon_free(struct console *con); enum nbcon_prio nbcon_get_default_prio(void); +void nbcon_atomic_flush_pending(void); =20 /* * Check if the given console is currently capable and allowed to print @@ -140,6 +141,7 @@ static inline bool nbcon_alloc(struct console *con) { r= eturn false; } static inline void nbcon_init(struct console *con, u64 init_seq) { } static inline void nbcon_free(struct console *con) { } static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } +static inline void nbcon_atomic_flush_pending(void) { } =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 e37af5c72d38..7532eba55d15 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -850,7 +850,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); @@ -953,6 +952,148 @@ enum nbcon_prio nbcon_get_default_prio(void) return NBCON_PRIO_NORMAL; } =20 +/* + * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using = its + * write_atomic() callback + * @con: The nbcon console to flush + * @stop_seq: Flush up until this record + * + * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on + * failure. + * + * Errors: + * + * -EPERM: Unable to acquire console ownership. + * + * -EAGAIN: Another context took over ownership while printing. + * + * -ENOENT: A record before @stop_seq is not available. + * + * If flushing up to @stop_seq was not successful, it only makes sense for= the + * caller to try again when -EAGAIN was returned. When -EPERM is returned, + * this context is not allowed to acquire the console. When -ENOENT is + * returned, it cannot be expected that the unfinalized record will become + * available. + */ +static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_= seq) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + int err =3D 0; + + ctxt->console =3D con; + ctxt->spinwait_max_us =3D 2000; + ctxt->prio =3D nbcon_get_default_prio(); + + if (!nbcon_context_try_acquire(ctxt)) + return -EPERM; + + while (nbcon_seq_read(con) < stop_seq) { + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + */ + if (!nbcon_emit_next_record(&wctxt)) + return -EAGAIN; + + if (!ctxt->backlog) { + if (nbcon_seq_read(con) < stop_seq) + err =3D -ENOENT; + break; + } + } + + nbcon_context_release(ctxt); + return err; +} + +/** + * nbcon_atomic_flush_pending_con - Flush specified nbcon console using its + * write_atomic() callback + * @con: The nbcon console to flush + * @stop_seq: Flush up until this record + * + * This will stop flushing before @stop_seq if another context has ownersh= ip. + * That context is then responsible for the flushing. Likewise, if new rec= ords + * are added while this context was flushing and there is no other context + * to handle the printing, this context must also flush those records. + */ +static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq) +{ + unsigned long flags; + int err; + +again: + /* + * Atomic flushing does not use console driver synchronization (i.e. + * it does not hold the port lock for uart consoles). Therefore IRQs + * must be disabled to avoid being interrupted and then calling into + * a driver that will deadlock trying to acquire console ownership. + */ + local_irq_save(flags); + + err =3D __nbcon_atomic_flush_pending_con(con, stop_seq); + + local_irq_restore(flags); + + /* + * If flushing was successful but more records are available, this + * context must flush those remaining records because there is no + * other context that will do it. + */ + if (!err && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { + stop_seq =3D prb_next_reserve_seq(prb); + goto again; + } + + /* + * If there was a new owner, that context is responsible for + * completing the flush. + */ +} + +/** + * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their + * write_atomic() callback + * @stop_seq: Flush up until this record + */ +static void __nbcon_atomic_flush_pending(u64 stop_seq) +{ + struct console *con; + int cookie; + + cookie =3D console_srcu_read_lock(); + for_each_console_srcu(con) { + short flags =3D console_srcu_read_flags(con); + + if (!(flags & CON_NBCON)) + continue; + + if (!console_is_usable(con, flags)) + continue; + + if (nbcon_seq_read(con) >=3D stop_seq) + continue; + + nbcon_atomic_flush_pending_con(con, stop_seq); + } + console_srcu_read_unlock(cookie); +} + +/** + * nbcon_atomic_flush_pending - Flush all nbcon consoles using their + * write_atomic() callback + * + * Flush the backlog up through the currently newest record. Any new + * records added while flushing will not be flushed. 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 @@ -1064,8 +1205,23 @@ EXPORT_SYMBOL_GPL(nbcon_driver_try_acquire); void nbcon_driver_release(struct console *con) { struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(con, nbcon_driver_ctxt); + int cookie; =20 - if (nbcon_context_exit_unsafe(ctxt)) - nbcon_context_release(ctxt); + if (!nbcon_context_exit_unsafe(ctxt)) + return; + + nbcon_context_release(ctxt); + + /* + * This context must flush any new records added while the console + * was locked. The console_srcu_read_lock must be taken to ensure + * the console is usable throughout flushing. + */ + cookie =3D console_srcu_read_lock(); + if (console_is_usable(con, console_srcu_read_flags(con)) && + prb_read_valid(prb, nbcon_seq_read(con), NULL)) { + __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb)); + } + console_srcu_read_unlock(cookie); } EXPORT_SYMBOL_GPL(nbcon_driver_release); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 9cdc110a46a2..8c93ef6292ac 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 67F9B1C6898 for ; Thu, 2 May 2024 21:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685935; cv=none; b=o/7oWtLGbhTQZdSQtx0bNQ4AyyEJmfybXHEa1zbzK2ETj2QgW+bHPWaVNT09YxS6X3I0JsHPpe0Ie+Ih7T4DC4ShkvRXvlOysUGguduojIkAIzcPwDnMiT0RNKZN3eihlKLL3SVEchicOB3Z0//XjDehQjO/i55siwmSudqC9Go= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685935; c=relaxed/simple; bh=eBpAe0m3jkrciUtpy/gK/+A8yIKAuwbXb+fVieGeCys=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qr2kuATVtuhxsO/l1MMkpWfHDMQfcyYZWDa8ekMQt/NP15H1VtLHFp5o1YYeyeo5yjclQwkgAQecSl2/vXTcQHImLhW8JHXObf9PfaChLa3AqKS610yiC1W4fnq18TRJ9AGigjRdr8Un7Uc/jYYNy8GrQk63BlJyzmyvNmp2MVk= 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=KWmd+jZE; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=dhE/TxHH; 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="KWmd+jZE"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="dhE/TxHH" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685932; 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=B5pjl5Q+p+FjfYSMKXRHKDtD1jLA4O0osbygvWzIAZM=; b=KWmd+jZEX4WhqnFPiNK65pGiYeOZgEKMu5AN/162/WiCwuXYdbqJKLQD/QnKltRlnnAij0 izyHNlaCOX6J915DvELmtYN0aAR676WhEdcNF1vwDsQkUxG9eEXqCsrT+zboj5k1lsWarH dnMdcnUDhHxYGyKMuPareTD3HevuWnine+dmb/QpmGVuEyJ8Z9EAkqxki9Ok1tnQ7XJCGk MkJoAPw50nUdDLYQfJeB9tVboRhG3gyrjcRnzLtsRNtg3jAv15QCjm+MpeYuRUuZD2fZcE MbJRD3xxDzsOfELsOpPn/67uCARkb2ZaeuV7jMpV2CPfSTG1Cc56EuzgsU55gg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685932; 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=B5pjl5Q+p+FjfYSMKXRHKDtD1jLA4O0osbygvWzIAZM=; b=dhE/TxHHWlXSAOSf/vIr/B7q/57V05gpNTEy9NIuwTae6RV1k1g+RjVvd3jXRhhcwMV59k yweaT8HI61w/T1Bw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 20/30] printk: Track registered boot consoles Date: Thu, 2 May 2024 23:44:29 +0206 Message-Id: <20240502213839.376636-21-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 8c93ef6292ac..e1cf0f7d6ff2 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. */ @@ -3511,6 +3519,9 @@ void register_console(struct console *newcon) newcon->seq =3D init_seq; } =20 + if (newcon->flags & CON_BOOT) + have_boot_console =3D true; + /* * If another context is actively using the hardware of this new * console, it will not be aware of the nbcon synchronization. This @@ -3580,7 +3591,9 @@ 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; unsigned long flags; + struct console *c; int res; =20 lockdep_assert_console_list_lock_held(); @@ -3638,6 +3651,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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id E917C1C68BF for ; Thu, 2 May 2024 21:38:53 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685936; cv=none; b=M01xQm+yrIS6wK1F+m4BDUq7B+Qx4LfmIRO4+DrLtHzkr5V9EH694wrVjD5Y7bST+e2vQs9s6iLXWdeNjEN6nMKf0Wmp0GrKIYIkN79DczA5DSxLRiAo1NvgGOSWtmcctsce7E5Ovb4YsxyewdMhTSn99qS+NEtn9sl1woIfQ9w= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685936; c=relaxed/simple; bh=grpdJaz2UnvW0mDQQgbmHL68Ekyu1fs7+Qvt44D7P+8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=SYFpn6QxJP0LmYInAuP2K3FpKTIyolbLbfmNSJKRNFNv7GI6dowdFsJzX0ZEIdGBYM8iAnFQc+yu8J/uGTe+R9utjNjiyqEqoJnG/iOXf2EoIw8vMXa/+L7v6TV0yOKWsQloyt9wGlHEiBHBhQF6Ko7sSlLdsw/YtOGxkGzxNYE= 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=tRZfC0cA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=WlpJQE+e; 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="tRZfC0cA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="WlpJQE+e" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685932; 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=Sk0j1OodFRAFjoCizT58vRe3D/jMHkQlUpYUl6q8Mbc=; b=tRZfC0cAVDtRZP6zFbRixAbjMArOplm27oBGPNT1DlyYE1rk1weeKUcSL0QhtpHOaHnrBT II2p3mP/aJaE93HfuHWQEWmkWa2xEtnCSqZGeNa3JRhcOfhKibaEBbDR+nf+u+LQ+IEJdf F/q2fJzIz2ALlP1xvG5CCvfWWW9LIVmIxRsZD/OqGRtnp9lS4SoAFkNdQ5/0ZwbxUd1bq1 ysJ2dyEYowR27A3HZ5k7fvHnh588q3tqUUjmXaEfuAr8u6ObrlB0xj5fEu9nncNKYCtmvb H8IAm58Fj+RCbmbU5Xz25ZnUIYZoB5hW6yrXn95TF1lBx3Ee0vpU7Y8sFY+7mw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685932; 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=Sk0j1OodFRAFjoCizT58vRe3D/jMHkQlUpYUl6q8Mbc=; b=WlpJQE+e9Yv/UrAMasJShPvTyy0u96jmWqFIeERiFrdAVgUAg/+GL1tKu8xd8ypEvhNr2a ekgt+jXmD+JnBZAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 21/30] printk: nbcon: Use nbcon consoles in console_flush_all() Date: Thu, 2 May 2024 23:44:30 +0206 Message-Id: <20240502213839.376636-22-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Allow nbcon consoles to print messages in the legacy printk() caller context (printing via unlock) by integrating them into console_flush_all(). The write_atomic() callback is used for printing. Provide nbcon_legacy_emit_next_record(), which acts as the nbcon variant of console_emit_next_record(). Call this variant within console_flush_all() for nbcon consoles. Since nbcon consoles use their own @nbcon_seq variable to track the next record to print, this also must be appropriately handled in console_flush_all(). Note that the legacy printing logic uses @handover to detect handovers for printing all consoles. For nbcon consoles, handovers/takeovers occur on a per-console basis and thus do not cause the console_flush_all() loop to abort. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/internal.h | 6 +++ kernel/printk/nbcon.c | 87 ++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 17 +++++--- 3 files changed, 105 insertions(+), 5 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 5a93825871de..91bc0ee43f8d 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); @@ -86,6 +88,8 @@ void nbcon_init(struct console *con, u64 init_seq); void nbcon_free(struct console *con); enum nbcon_prio nbcon_get_default_prio(void); void nbcon_atomic_flush_pending(void); +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 @@ -142,6 +146,8 @@ static inline void nbcon_init(struct console *con, u64 = init_seq) { } static inline void nbcon_free(struct console *con) { } static inline enum nbcon_prio nbcon_get_default_prio(void) { return NBCON_= PRIO_NONE; } static inline void nbcon_atomic_flush_pending(void) { } +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 7532eba55d15..abd3835a3466 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -953,6 +953,93 @@ enum nbcon_prio nbcon_get_default_prio(void) } =20 /* + * nbcon_atomic_emit_one - Print one record for an nbcon console using the + * write_atomic() callback + * @wctxt: An initialized write context struct to use for this context + * + * Return: True, when a record has been printed and there are still + * pending records. The caller might want to continue flushing. + * + * False, when there is no pending record, or when the console + * context cannot be acquired, or the ownership has been lost. + * The caller should give up. Either the job is done, cannot be + * done, or will be handled by the owning context. + * + * This is an internal helper to handle the locking of the console before + * calling nbcon_emit_next_record(). + */ +static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + + if (!nbcon_context_try_acquire(ctxt)) + return false; + + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + * + * The higher priority printing context takes over responsibility + * to print the pending records. + */ + if (!nbcon_emit_next_record(wctxt)) + return false; + + nbcon_context_release(ctxt); + + return ctxt->backlog; +} + +/** + * nbcon_legacy_emit_next_record - Print one record for an nbcon console + * in legacy contexts + * @con: The console to print on + * @handover: Will be set to true if a printk waiter has taken over the + * console_lock, in which case the caller is no longer holding + * both the console_lock and the SRCU read lock. Otherwise it + * is set to false. + * @cookie: The cookie from the SRCU read lock. + * + * Context: Any context except NMI. + * Return: True, when a record has been printed and there are still + * pending records. The caller might want to continue flushing. + * + * False, when there is no pending record, or when the console + * context cannot be acquired, or the ownership has been lost. + * The caller should give up. Either the job is done, cannot be + * done, or will be handled by the owning context. + * + * This function is meant to be called by console_flush_all() to print rec= ords + * on nbcon consoles from legacy context (printing via console unlocking). + * Essentially it is the nbcon version of console_emit_next_record(). + */ +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie) +{ + struct nbcon_write_context wctxt =3D { }; + struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); + unsigned long flags; + bool progress; + + /* Use the same procedure as console_emit_next_record(). */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + stop_critical_timings(); + + ctxt->console =3D con; + ctxt->prio =3D nbcon_get_default_prio(); + + progress =3D nbcon_atomic_emit_one(&wctxt); + + start_critical_timings(); + *handover =3D console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + + return progress; +} + +/** * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using = its * write_atomic() callback * @con: The nbcon console to flush diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e1cf0f7d6ff2..d17b0e057e22 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 48C7A1C8FC0 for ; Thu, 2 May 2024 21:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685936; cv=none; b=EJt1Th0Rnd5I03kTT6kfzX7KMnNgWKdw9JUJn89MPjp11tVJ0HTnHY+ik4Qih7zYycD+hoKItaNjVMFu4QAuCzIhE5WmqP/N5uqw259702Po4bj6XMR6hi5EVRK3tgb9qGgKpvIaVnAMwlbTSGUxRcKliNQn1bTDHBYtq/II4/Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685936; c=relaxed/simple; bh=gARkG73eQRU8V9yDFnYjNtWOHoOUrDG/qMyvfb3FT5A=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Tjyk738mDNh2D6iJIxDvIxBbkbUW+yaGzzC2rzIDVslceDdUyHUo6+yoqFnAz3bhLylVEzfTg2vJZE3njDvBFQUSM+xaDEjeIvTFTjiHBvVvaE3PNnLYXWMc4HpFJEY+2oi281D9wNIhCwlw4osOhQYkaTs2qNgFrNlhvm+gfz0= 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=Si0FjGbT; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=xnV/4eWX; 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="Si0FjGbT"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="xnV/4eWX" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685933; 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=WjNzD/P9+1HhwBtJEEAC1hhuGU7StxseV5FY0ZUbzxw=; b=Si0FjGbTfDhLWv8n4XGqxMj3pt5CGJsUa7NODCfBt1fLhXdaksNkS4LIeZZhcjLPwJ9Qne ZA9dv7PlliiVAx0MpOsr4OA9zMAoeOdn2UlqDBqnV2CCFcj1P7vEKReGeYc/TtFRt5gY2A 3XkgZUUUBHcDPKrEeWm7/uxjSFxGkmiZR6siXvwc53JM9EyZsSdE6yHc9YJpjvq5H8f677 SqbhWDOZ4MRPqCpTCwDjcy60KCtA7XDu/eWXHzbW/cZmt/eB/x8+PWCnNTtP5XKSeppvR0 Bro+Mk4CCA3pTN0BnhEvBvOcTJnMnuIKcTQ4Si6KCajydApAC37JLokHd3SsYA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685933; 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=WjNzD/P9+1HhwBtJEEAC1hhuGU7StxseV5FY0ZUbzxw=; b=xnV/4eWXwKv4Woewc7X7tsHGY1gfoaKnp1aglMCfqd9UcUz5F4epk5o8Yo4P0ZfC4MMqRx jRD7gjfDsWunmdAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Lukas Wunner , Arnd Bergmann , Uros Bizjak , Kefeng Wang Subject: [PATCH printk v5 22/30] printk: nbcon: Add unsafe flushing on panic Date: Thu, 2 May 2024 23:44:31 +0206 Message-Id: <20240502213839.376636-23-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Add nbcon_atomic_flush_unsafe() to flush all nbcon consoles using the write_atomic() callback and allowing unsafe hostile takeovers. Call this at the end of panic() as a final attempt to flush any pending messages. Note that legacy consoles use unsafe methods for flushing from the beginning of panic (see bust_spinlocks()). Therefore, systems using both legacy and nbcon consoles may still fail to see panic messages due to unsafe legacy console usage. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/printk.h | 5 +++++ kernel/panic.c | 1 + kernel/printk/nbcon.c | 32 +++++++++++++++++++++++++------- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index d0a1106388d1..439decc792b6 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 bool nbcon_driver_try_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) @@ -286,6 +287,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 abd3835a3466..8f6b3858ab27 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -1044,6 +1044,7 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * Return: 0 if @con was flushed up to @stop_seq Otherwise, error code on * failure. @@ -1062,7 +1063,8 @@ bool nbcon_legacy_emit_next_record(struct console *co= n, bool *handover, * returned, it cannot be expected that the unfinalized record will become * available. */ -static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_= seq) +static int __nbcon_atomic_flush_pending_con(struct console *con, u64 stop_= seq, + bool allow_unsafe_takeover) { struct nbcon_write_context wctxt =3D { }; struct nbcon_context *ctxt =3D &ACCESS_PRIVATE(&wctxt, ctxt); @@ -1071,6 +1073,7 @@ static int __nbcon_atomic_flush_pending_con(struct co= nsole *con, u64 stop_seq) ctxt->console =3D con; ctxt->spinwait_max_us =3D 2000; ctxt->prio =3D nbcon_get_default_prio(); + ctxt->allow_unsafe_takeover =3D allow_unsafe_takeover; =20 if (!nbcon_context_try_acquire(ctxt)) return -EPERM; @@ -1100,13 +1103,15 @@ static int __nbcon_atomic_flush_pending_con(struct = console *con, u64 stop_seq) * write_atomic() callback * @con: The nbcon console to flush * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers * * This will stop flushing before @stop_seq if another context has ownersh= ip. * That context is then responsible for the flushing. Likewise, if new rec= ords * are added while this context was flushing and there is no other context * to handle the printing, this context must also flush those records. */ -static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq) +static void nbcon_atomic_flush_pending_con(struct console *con, u64 stop_s= eq, + bool allow_unsafe_takeover) { unsigned long flags; int err; @@ -1120,7 +1125,7 @@ static void nbcon_atomic_flush_pending_con(struct con= sole *con, u64 stop_seq) */ local_irq_save(flags); =20 - err =3D __nbcon_atomic_flush_pending_con(con, stop_seq); + err =3D __nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_take= over); =20 local_irq_restore(flags); =20 @@ -1144,8 +1149,9 @@ static void nbcon_atomic_flush_pending_con(struct con= sole *con, u64 stop_seq) * __nbcon_atomic_flush_pending - Flush all nbcon consoles using their * write_atomic() callback * @stop_seq: Flush up until this record + * @allow_unsafe_takeover: True, to allow unsafe hostile takeovers */ -static void __nbcon_atomic_flush_pending(u64 stop_seq) +static void __nbcon_atomic_flush_pending(u64 stop_seq, bool allow_unsafe_t= akeover) { struct console *con; int cookie; @@ -1163,7 +1169,7 @@ static void __nbcon_atomic_flush_pending(u64 stop_seq) if (nbcon_seq_read(con) >=3D stop_seq) continue; =20 - nbcon_atomic_flush_pending_con(con, stop_seq); + nbcon_atomic_flush_pending_con(con, stop_seq, allow_unsafe_takeover); } console_srcu_read_unlock(cookie); } @@ -1178,7 +1184,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 /** @@ -1307,7 +1325,7 @@ void nbcon_driver_release(struct console *con) cookie =3D console_srcu_read_lock(); if (console_is_usable(con, console_srcu_read_flags(con)) && prb_read_valid(prb, nbcon_seq_read(con), NULL)) { - __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb)); + __nbcon_atomic_flush_pending_con(con, prb_next_reserve_seq(prb), false); } console_srcu_read_unlock(cookie); } --=20 2.39.2 From nobody Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D97EC1C9EA4 for ; Thu, 2 May 2024 21:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685937; cv=none; b=B+JTCZd+sQgMfWaycxo64+D1J7NllGKWYUIf41VkfNAFeCchZZGOWRZSDJzOzbgDX/NJ11IztYj1tn+HP9S5BLw5lYjk9EbIAURQeGzwx4wndkVcbw9A/V0k6MnqiXXQbIzoo/6aJugl/E4sVPB+Kajv/MIgRIcEaPD1pr6yA/g= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685937; c=relaxed/simple; bh=aFuXsM0PQjxnhe9BJyWGcDKlPrKO7Y8pNeIWcZD7jCM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oOuRFUp9m2WvZSDcYayLTxEaVIsOWSXStlYFT1UScbpkVZFf1duTQpCNbLoAhRHJ9itqz0UUQQUGS3UDD08fXPTLzE84yMGeQ/PBpgzeTwBp/ZdOQ5ZhMkUd8TGnOf0i7uuLl/rjXa9zDY/TE6x4LMj8xTz7SsunzB9hy/hQ0gY= 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=McMfiB04; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AlqHHyq0; 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="McMfiB04"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AlqHHyq0" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685933; 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=+6sRFHbv8z6woNAyIM/Oyrbcgnr8C2ODc2QwVRv1j5w=; b=McMfiB04rn24IsAutxYT1GsJAzE3mzo3MXpqVQ85WBPXUaOAs9xiJmaq1cYu5CYOovtp0e 7QmBZRLuM7k+ZHDsXhmdQf8yak3u/U4rs7zsvri7FwN00GjUuoETe+uFA4fuQ4zPu6YBMd 7JrQx8d8wCUjWQpoyd7f7WYLGHudroR514Sxcfr+wvjnlA0LmzqGXpmJvVJOLlSteS1N8k f8aOi0pTKlBpHsCyA9HRfYiMoeqExMDDrWcoKRX1pM/fgNt6CgUCDk/RIrgmtEuVBT+Emy r8YnML6r+Iwf79KSAPZ3aIqs678NaEmhyRPKP2YMFlRraTFvDXA01JL42g6KWg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685933; 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=+6sRFHbv8z6woNAyIM/Oyrbcgnr8C2ODc2QwVRv1j5w=; b=AlqHHyq0TxBaYm71rt5dUQMlms4BRaHq6uqSG6hVzQ78vp/3ONJoVwZ2nV7AMDM+YwtVIg rOYaOeTe9CHGHJCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 23/30] printk: Avoid console_lock dance if no legacy or boot consoles Date: Thu, 2 May 2024 23:44:32 +0206 Message-Id: <20240502213839.376636-24-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 | 78 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d17b0e057e22..0cb6ae5fd7e3 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 /* @@ -3523,6 +3539,7 @@ void register_console(struct console *newcon) if (newcon->flags & CON_NBCON) { nbcon_init(newcon, init_seq); } else { + have_legacy_console =3D true; newcon->seq =3D init_seq; } =20 @@ -3598,6 +3615,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; unsigned long flags; struct console *c; @@ -3665,9 +3683,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; } @@ -3828,22 +3850,34 @@ 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; - - diff =3D 0; + bool use_console_lock =3D printing_via_unlock; =20 /* - * Hold the console_lock to guarantee safe access to - * console->seq. Releasing console_lock flushes more - * records in case @seq is still not printed on all - * usable consoles. + * Ensure the compiler does not optimize @use_console_lock to + * be @printing_via_unlock since the latter can change at any + * time. */ - console_lock(); + barrier(); + + diff =3D 0; + + if (use_console_lock) { + /* + * 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(); + } =20 cookie =3D console_srcu_read_lock(); for_each_console_srcu(c) { @@ -3863,6 +3897,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(!use_console_lock); printk_seq =3D c->seq; } =20 @@ -3874,7 +3909,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 (use_console_lock) + console_unlock(); =20 /* Note: @diff is 0 if there are no usable consoles. */ if (diff =3D=3D 0 || remaining_jiffies =3D=3D 0) @@ -3944,6 +3980,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. @@ -3955,11 +3992,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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 381C91C9EBD for ; Thu, 2 May 2024 21:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685937; cv=none; b=dz+TuXOtIbN81ysh6UqtDDQ85K8Q4rZ0kY6wum8zJu7eduaiClRAzrkI2CH4h8sijw4zMEcYaMDri2LnIeJ4fVZ79Ihw0hAhh/C73EjRIoLmBZXrnDWEFFQTLcQtMhqYl7VS6ykSrc49K/FHbIvFRn5u+zlBF7043h5Vu1qw5Ts= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685937; c=relaxed/simple; bh=LNUSX7frizCoQB/cUybOT78S5L8+BZsAsyiRjVvjQqI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Ft5Nqto9ugJd4Pk4O9eJzhJk0omZ3UGZV3vg4bVVX0y1avf5q+mMbgoYpd+muZwmrZG98nOsZlQlsHAAb587suwXjt43N1o/E4LVxB9MPApRqep3iYYidjyOMxdoAOvLISzGpsH03OzdGX1ZwLvEnRfynZbp/zyHrbeqO5qwqXQ= 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=u0HE7bHA; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AhmNS173; 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="u0HE7bHA"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AhmNS173" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685933; 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=zOxgJh7OowKlUgCULZJtMLD8sILLxBu5zJz7tzlhAxw=; b=u0HE7bHASx0ogSUS+II8HMVGUModVzXeROfR1tPN3VIsmaXSVEB/2yexQyupY1sJYdkfeA Ezt26vCxzgs9EsHTmxJCBTjH+sk1WT1Dr9f2lUA0pIMMs37M62VlBjai+KC1nWvPyHI9Cs SWS7/5bROrRKUJ0Wte/RpXrYvDNenQAjxJzDMOjwcKInoLi3oHZsFnWrnF3FIlGAaZdfXu DN3VBfF3bLQIh3YNa1ocWQK1j8ofVkemKCM07H5wakawUKzfITOTEF0j4G50HyKbmuM+Er fFdY6fiupMEETIFc0QZTj2rOYlWnzQ5vBD22uvPzrF23+SvFAIlyTFQHQUE6ZA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685933; 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=zOxgJh7OowKlUgCULZJtMLD8sILLxBu5zJz7tzlhAxw=; b=AhmNS173bXsFi0tZIhvdUKSKBjik/wVvsowX4RFhuueJirBLY6DGrkc27sjk17LJQwMJZZ xoSRfoOrrs0hvOCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 24/30] printk: Track nbcon consoles Date: Thu, 2 May 2024 23:44:33 +0206 Message-Id: <20240502213839.376636-25-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 0cb6ae5fd7e3..31ed4450d9a6 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 @@ -3537,6 +3542,7 @@ void register_console(struct console *newcon) init_seq =3D get_init_console_seq(newcon, bootcon_registered); =20 if (newcon->flags & CON_NBCON) { + have_nbcon_console =3D true; nbcon_init(newcon, init_seq); } else { have_legacy_console =3D true; @@ -3616,6 +3622,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; unsigned long flags; struct console *c; @@ -3683,13 +3690,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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B4F261CB304 for ; Thu, 2 May 2024 21:38:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; cv=none; b=WkAFKpMdonKbrJvKrIhAsP+vAyxsGvRixHpPvLRa3qassA3k7aG86zAvQtiaz2X40bXzhLT2hWfT3XaD6KcvQ1iadCxXe9IcShDKmQcMZ5o1etAByKSgtml8WkfILIvZ7dYHs1EWlN7UvXTm0D5UJUqQRM3d/biK2mruENE9IMc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; c=relaxed/simple; bh=wcVMUk8/oS73vu6qaEv5wUZlhxXlDPZSrnQyQGtkoVk=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=sAJnK8cgJt4VYqn1Uy+KFCFrCbDZYcWBmauQiRK9gWKksDWEpFeC5sbzngl2QsiATQmv2Ex5JLHzpnX3h7zEKctaLL+9bN9ed+ngmSdBGh/4I/+Pzu9GIrfPEffWN4YT+OJpgj+/SS9eH9965vdUD+KaKJjHvHifPGPpwVuuV9g= 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=kYyL5WEW; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=fhhfqQ3h; 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="kYyL5WEW"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="fhhfqQ3h" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685934; 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=15UC9Vkso+YE7CuUzdF4vgVCG9xL1mAkK0rp0AxZYSs=; b=kYyL5WEW+TDw2LCkbixGhFDkd9BDWgYaq+dGC/WZ70ExfEJaRooAIwq7HCA79HrSIwf96w +A2SF8d88ldg1oc+xEHhHUDODFjFgxDMElts/NrYB9rBXSM+N+PGhPzQaLsSq1vXI1dij2 XSBG05TqxxvC7b8BhwACmt+CEYDmh60BDT8LXZRZUHDT4HjDzsKfYenQYJB/FW7nCK2Psf vTGb9Aogij/gAm0rluTT8LvxxJYtsAp17z8faklVxs0fhlpKycoGispaPQfB6064AsDipJ SB1SzSKOMjGt9hVKm74epaVH8agJzLHDJfQ2s263u8ptKQONqseAr5ei2ICp6A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685934; 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=15UC9Vkso+YE7CuUzdF4vgVCG9xL1mAkK0rp0AxZYSs=; b=fhhfqQ3hudaUV0+lBsNeAy6ORFGolYaLe0+3/MCAUKDw5PVo3CTW1K9C2l4q1ONPoO8Av3 p9sKk6m8j4yOXdCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Arnd Bergmann , Kefeng Wang , Uros Bizjak , Lukas Wunner Subject: [PATCH printk v5 25/30] printk: Coordinate direct printing in panic Date: Thu, 2 May 2024 23:44:34 +0206 Message-Id: <20240502213839.376636-26-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 439decc792b6..bbd00a310f6b 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 bool nbcon_driver_try_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 bool nbcon_driver_try_acquire(struct console *con) { return false; 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 31ed4450d9a6..eb1d7f0b10bf 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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BD0711CB30A for ; Thu, 2 May 2024 21:38:55 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; cv=none; b=t3gu2W+bHBz2ojVf4RsbTrDrotpYojLtmq6oUrwKGW4P1l0nW7n6vY8f6bTthUgRpbiKs/Gn8rfVpNb1nsqSdr4/29XRmjvfmUND5Q/XC4zMQ/9XfHH+UfU38imESHy7XCk+Q1ej6dOuktFEcjs+5A2bUF0+DFvjuqDqiiRRR7Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; c=relaxed/simple; bh=ngbrYVMExdcOV1ngJsRT+LpRpt5L9EbM6fx8Jr4AdgI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WF+jVtneLdlYtcRdvKUoaVTfTmgdMNQOaGWZrPX8NkpVr0162C6QaF2xaYtdxPcg1U8WZ+z4RsHDiwszbC1xfj3x9maw9HAs0HgObbT7r7IIJ7EpAxR0OBv4fKhchRAfqBBqXGmVUzrlko/EjoUb8lad0KukPAmQTKwoy0s4op0= 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=fqRnxaYU; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=cuMCWKgm; 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="fqRnxaYU"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="cuMCWKgm" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685934; 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=2eq+dcRV+xTsqG/y9TJ22b7EZvwkA/4uLrtusf4n1LA=; b=fqRnxaYUq3tSAbxeQzt661qvGpLBsFdSJxYYn2iiKEKmPmX4GTVVgVbP/EIaWr7u5dUjff Eiso9sC3XOzMUZd1BA65rwk++YkMbWnW++IuFh0e5o/sa7/Kp19wDF/mYT49OViNYlJs35 cwlrAgZ2Z4+hXYH7VCPaw0nEW+L3PhKm9f0dd8kkd0GeCPzf1JFtX4M8gayw0LJCZIkLtS OGeiu6Ke/tuBAwAk2QJzrsqmuB5P0YLl22xyvxbrN/FL33V2QW4SmQ2iDt0BBreHk6TjHv kYm5XeiMYSTWC2GyvLOmd4ywLKKzwGlw/KgOIajaN/9AAt1a/ECWXE1dy8p3Rw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685934; 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=2eq+dcRV+xTsqG/y9TJ22b7EZvwkA/4uLrtusf4n1LA=; b=cuMCWKgmJjt6HcxyRHAO+HrnpG6P/xitqnMtdVA52w/SLC4wAVwMhvU/cq3oHCPZIF7d4i JhnVhvhN9GZq5yDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v5 26/30] printk: nbcon: Implement emergency sections Date: Thu, 2 May 2024 23:44:35 +0206 Message-Id: <20240502213839.376636-27-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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) --- include/linux/console.h | 6 ++ kernel/printk/internal.h | 11 ++++ kernel/printk/nbcon.c | 116 +++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 25 +++++---- 4 files changed, 146 insertions(+), 12 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 71d0638aa818..1b25eda4e7c2 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -535,10 +535,16 @@ 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 void nbcon_cpu_emergency_flush(void); extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); #else +static inline void nbcon_cpu_emergency_enter(void) { } +static inline void nbcon_cpu_emergency_exit(void) { } +static inline void nbcon_cpu_emergency_flush(void) { } static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { = return false; } static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) {= return false; } static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { = return false; } diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 91bc0ee43f8d..5519239fa5d2 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -153,6 +153,17 @@ static inline bool console_is_usable(struct console *c= on, short flags) { return =20 #endif /* CONFIG_PRINTK */ =20 +extern bool have_boot_console; +extern bool have_legacy_console; + +/* + * Specifies if the console lock/unlock dance is needed for console + * printing. If @have_boot_console is true, the nbcon consoles will + * be printed serially along with the legacy consoles because nbcon + * consoles cannot print simultaneously with boot consoles. + */ +#define printing_via_unlock (have_legacy_console || have_boot_console) + extern struct printk_buffers printk_shared_pbufs; =20 /** diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 8f6b3858ab27..991e2702915c 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_get_default_prio - The appropriate nbcon priority to use for nbcon * printing on the current CPU @@ -946,9 +969,15 @@ static bool nbcon_emit_next_record(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 @@ -1199,6 +1228,93 @@ 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(); + + /* + * Flush the messages before enabling preemtion to see them ASAP. + * + * Reduce the risk of potential softlockup by using the + * flush_pending() variant which ignores messages added later. It is + * called before decrementing the counter so that the printing context + * for the emergency messages is NBCON_PRIO_EMERGENCY. + */ + if (*cpu_emergency_nesting =3D=3D 1) { + nbcon_atomic_flush_pending(); + do_trigger_flush =3D true; + } + + (*cpu_emergency_nesting)--; + + if (WARN_ON_ONCE(*cpu_emergency_nesting < 0)) + *cpu_emergency_nesting =3D 0; + + preempt_enable(); + + if (do_trigger_flush) + printk_trigger_flush(); +} + +/** + * nbcon_cpu_emergency_flush - Explicitly flush consoles while + * within emergency context + * + * Both nbcon and legacy consoles are flushed. + * + * It should be used only when there are too many messages printed + * in emergency context, for example, printing backtraces of all + * CPUs or processes. It is typically needed when the watchdogs + * need to be touched as well. + */ +void nbcon_cpu_emergency_flush(void) +{ + /* The explicit flush is needed only in the emergency context. */ + if (*(nbcon_get_cpu_emergency_nesting()) =3D=3D 0) + return; + + nbcon_atomic_flush_pending(); + + if (printing_via_unlock && !in_nmi()) { + if (console_trylock()) + console_unlock(); + } +} + /** * 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 eb1d7f0b10bf..487742eeacf3 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -468,7 +468,7 @@ static DEFINE_MUTEX(syslog_lock); * present, it is necessary to perform the console lock/unlock dance * whenever console flushing should occur. */ -static bool have_legacy_console; +bool have_legacy_console; =20 /* * Specifies if an nbcon console is registered. If nbcon consoles are pres= ent, @@ -483,15 +483,7 @@ static bool have_nbcon_console; * the console lock. This is because boot consoles and nbcon consoles may * have mapped the same hardware. */ -static bool have_boot_console; - -/* - * Specifies if the console lock/unlock dance is needed for console - * printing. If @have_boot_console is true, the nbcon consoles will - * be printed serially along with the legacy consoles because nbcon - * consoles cannot print simultaneously with boot consoles. - */ -#define printing_via_unlock (have_legacy_console || have_boot_console) +bool have_boot_console; =20 #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); @@ -2412,16 +2404,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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 4A1A01CB334 for ; Thu, 2 May 2024 21:38:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; cv=none; b=S0m5lfC0qFKU/r4TCtMqFSziJvdb6Vk2AltgrBpOe+XG0K29kOfQyGRLzXt15ZISiPFc9KNDVj2iNItUSwSRLNU8SE0lMoM3BaJqUwjyZGD2rI9nZyhRR+X3dqFFDXSAAI/328oatDq6BzMkEbc5/I8qbYjyGFi1Ekmq5vSDLio= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; c=relaxed/simple; bh=sIDwBVtEZ9II9z34FVW/arDvGAYqJE9qC2xSXnoiiY8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cD8SKoqkbIA5PxtWnl1BU/kQHl2QvhfSVH1BRkDvnVxZbPP8bawyhoAsMfzmr5IhyXcxrP8PKYOEm4VgVDrR4v2eV0/ldMGrAW248ouaQ/9AfozWFhdbtvLgGPHEAO4blTS13qAors3Gl3La5EfPNthWP/OexCoP/57vD7WeaOs= 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=a26noFQ9; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=UB4dx3u/; 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="a26noFQ9"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="UB4dx3u/" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685934; 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=02mlGKrKUI3gQEB07A6y9zFTpJYkffg7XWV4RkY45mg=; b=a26noFQ9VHSBNGAz/Zf3ay3EJNiHrzvKdrHmERLizb1irxGUdVyR/YY3YMueIhuoxFqKQ0 IJHSjYGaYGOA4XUYSeOumtVWycJKnO7KWQx8LubIxcab8aqXO96D3pUMa0ImyDAf/pbWkN P8yNWOEXNM8GXK3UjEZR1y9ssUhH3SloV4UGq/415wm5ONyYmqpqRUByR96+U7ZNpK8mxv G+VVivHdhVgaqIN/0Uq8aKwx5euxEedx96o39X+12B7IHtD9xAl3p6Agmxkz15Q538JFsp /Q55TQ0HjwTDD7pnKaXbuFAeib1YEAyvPwYWPQu6cLz66ExorQWqNKrZv0D+lw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685934; 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=02mlGKrKUI3gQEB07A6y9zFTpJYkffg7XWV4RkY45mg=; b=UB4dx3u/PO58OnwfwIPQI/9jr+UCmR8vKb2hurmxCwEkii5uFJJLOtxgRyVLRv7rLnXcaI JZotGhzCIm1HLOBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Kefeng Wang , Uros Bizjak , Lukas Wunner , Arnd Bergmann Subject: [PATCH printk v5 27/30] panic: Mark emergency section in warn Date: Thu, 2 May 2024 23:44:36 +0206 Message-Id: <20240502213839.376636-28-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0EAAC200127 for ; Thu, 2 May 2024 21:38:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; cv=none; b=ZVwGegnv/giP43j14ZPxhGVErHKx57QUP928SIZpS+rzoUbCHmVZjIkritbh4TqY4TZN2Q/R9zP8U+mZKx4NUNgyXxK6bCasVmU0WnWS+u6uX0weBvNdp78Apxq2zzYl4dv80Q4DpaVMB1BzKAEms3wclHKaFBU7y+NPATXbQzU= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685938; c=relaxed/simple; bh=ApV3BrzIy3FzvWp+ns3WoghCqTJI0MOJzDStPgz0cJY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=gUvxLGUuMOPDhf9zUNvL6NBoBAW+1J2fYrD1BIVOXu1AM5yoIk2ShWxfnJ+kSVEdbvvlOk3ri7oatIBiFkRTyNtzp4R6UWiFSISLsyzXbu+rvxF6KAtIVextN5ir1hL18l9JK1/uYpqkWQNLK8DxEdhW++CKg4rWHeatg+BaSes= 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=cXQO7XfS; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=n88rBhwC; 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="cXQO7XfS"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="n88rBhwC" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685935; 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=Ypq720fklC/HaR4UTxfPN0fK1+MPpff79Yq5uFGIqB0=; b=cXQO7XfSN9tVIahWJBnVRk9/NuzKFtYcODXtqO97f0RdomlvjPrGKpT64jFJn8d9oi+KzB +OsIlc71W61ovDYtJfBKxHBDLhFGENjN+VZz1mXE/ZnkfweudCF2tzHIpMkzscb3QjU30q 1YM2VVqn3bTq8FRZFxRigp6A1cI9oUo8kkr6IHaOsEP0ST449o91o/dArVNQYk+gb0hGzR Ou7X+VNxWz4KbEgwMwqNr4ejwRloq2IwPOu40t2TkcprKJgaUUMrjYSwMhDkILebpOwed8 pJ+4sfSj6y/MUNwhdiUyBT5z9Efg2loXf4LDfVM6ZNtwAdxkEs33uvWlnF3ZrA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685935; 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=Ypq720fklC/HaR4UTxfPN0fK1+MPpff79Yq5uFGIqB0=; b=n88rBhwCRrPVvc8GBpeigqEBKog+cA8zT/E2ErrYVLMiuFwxqQMV8NERGVIDDyHHRK/P4S WpKd5mkQtLalzxDg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Lukas Wunner , Uros Bizjak , Kefeng Wang , Arnd Bergmann Subject: [PATCH printk v5 28/30] panic: Mark emergency section in oops Date: Thu, 2 May 2024 23:44:37 +0206 Message-Id: <20240502213839.376636-29-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78CAC201246; Thu, 2 May 2024 21:38:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685939; cv=none; b=atZWS6ZYjSzLnuCzuciMe9lXrqdw17NPVMFHZXz5fl4Ri5G6Kl9khzxeZYp9DBLrSOj85AvQ+B1o/emdqrL90dcIwsyrI60eNRy430/nbxripwzp9mg77ln8z02KGGq0usdW/4S/OuPeXLbok7t6GKLctpPiii4ZqLeQNJynU3Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685939; c=relaxed/simple; bh=X5bm+DVovRz/4U93EleiMnGhMnScrQBwEa6gswrUflI=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=NvXz91pToiFiHBbLE72hLwGPPOxNhKvMWrB4Kvvcd8EcLwUSrhIhYwe9cv46ncHmCgeEvkugP/bMntzDw7Vr6aX1pxAM5ApIWaloSDk4ltutUqNaTK/TXKNE3LA9FRYfwslQD/p9nxzDL3plsZnB9ZMVns52GqtBZku2T/CV1rM= 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=eyyIpJRj; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=BAAF8PCP; 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="eyyIpJRj"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="BAAF8PCP" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685936; 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=rswmw9AW06GyxQ8xxVykW80fBxNBsbL6ay1ZU2arScE=; b=eyyIpJRj2k6OAGQqPcWE8HWpj/l1btrxft8GjvD+u49PYf5YL2YpTLqXRpCoP33oV3xbrl P2ME7Yp4dVMNMJ3Dw+0qr8I88OqovOICdVjaLTNRdyHS7fYOyxHY4b7h1H9EVQTDzNWQcw wXll31IMZPoFW5zm22FM2bLiFsPl6fLGWMqrV/2EHYDmkIy5NEKIC9JXu4Zo7KwUpUM0eq OGLZPfymO47btdbsjmOR2A8XY+bxfIO+GOOxCu/1LV832ulzTInoNAw2hN0yMIcEhzAEwT P4yzQ/VE/4BxrNj4QSqcxX+QZiCxAwynugN/EWqaASX5SvTIvJXcooozOhYvfg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685936; 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=rswmw9AW06GyxQ8xxVykW80fBxNBsbL6ay1ZU2arScE=; b=BAAF8PCP1oOMbgV0y8jJcZTIoszqspZ0lj8a5Usa0cvneO62fhXLTQVyzKBzdKLDaayBwb RH9kzRHrvA0b3MAg== 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 v5 29/30] rcu: Mark emergency sections in rcu stalls Date: Thu, 2 May 2024 23:44:38 +0206 Message-Id: <20240502213839.376636-30-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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 --- kernel/rcu/tree_exp.h | 9 +++++++++ kernel/rcu/tree_stall.h | 11 +++++++++++ 2 files changed, 20 insertions(+) diff --git a/kernel/rcu/tree_exp.h b/kernel/rcu/tree_exp.h index 6b83537480b1..bc1d8733c08f 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")); @@ -612,6 +616,7 @@ static void synchronize_rcu_expedited_wait(void) } pr_cont("\n"); } + nbcon_cpu_emergency_flush(); rcu_for_each_leaf_node(rnp) { for_each_leaf_node_possible_cpu(rnp, cpu) { mask =3D leaf_node_cpu_bit(rnp, cpu); @@ -624,6 +629,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(); } } @@ -792,6 +800,7 @@ static void rcu_exp_print_detail_task_stall_rnp(struct = rcu_node *rnp) */ touch_nmi_watchdog(); sched_show_task(t); + nbcon_cpu_emergency_flush(); } raw_spin_unlock_irqrestore_rcu_node(rnp, flags); } diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h index 5d666428546b..1ca0826545c1 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 @@ -260,6 +261,7 @@ static void rcu_print_detail_task_stall_rnp(struct rcu_= node *rnp) */ touch_nmi_watchdog(); sched_show_task(t); + nbcon_cpu_emergency_flush(); } raw_spin_unlock_irqrestore_rcu_node(rnp, flags); } @@ -522,6 +524,7 @@ static void print_cpu_stall_info(int cpu) falsepositive ? " (false positive?)" : ""); =20 print_cpu_stat_info(cpu); + nbcon_cpu_emergency_flush(); } =20 /* Complain about starvation of grace-period kthread. */ @@ -604,6 +607,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 +660,8 @@ static void print_other_cpu_stall(unsigned long gp_seq,= unsigned long gps) rcu_check_gp_kthread_expired_fqs_timer(); rcu_check_gp_kthread_starvation(); =20 + nbcon_cpu_emergency_exit(); + panic_on_rcu_stall(); =20 rcu_force_quiescent_state(); /* Kick them all. */ @@ -675,6 +682,8 @@ static void print_cpu_stall(unsigned long gps) if (rcu_stall_is_suppressed()) return; =20 + nbcon_cpu_emergency_enter(); + /* * OK, time to rat on ourselves... * See Documentation/RCU/stallwarn.rst for info on how to debug @@ -703,6 +712,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 Wed Dec 17 20:50:16 2025 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 373E11C0DC7 for ; Thu, 2 May 2024 21:38:57 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685940; cv=none; b=CkY8yShpQv/Dbxqlk+feZk9bj9pvEG2X1lDH0jFfFGs7LaZGSl09/dyCYIlhZec4CsHvCcF3NNe2+Bj+h2q0H+c4eq60bEiKCALzvjg2K6frDlVh3bkdzpxL78tW8vOTc2E1KmJK9gQnv7ux/dBJSvPe8BWcMP6aA+coTCnZ/jc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685940; c=relaxed/simple; bh=K1/lCXHyQpAp0Tu54jz9wFsc8aCz6pDHL8XjkigfS3U=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=ogZeo43k6aJ2Bg5G7EYu2axfKQ3fIPF5rdHz7hEj4jJjfPI5LM3pELAAvnOhtRQ+Qne+zUaGMb+rStpkmqsfm8X3jKO3P8rmFAokqbgIqAkWTCu0TlC4P5zBTN8kSwsxC3UP2dsWvDSq2zKaCYurIGZKeRzGafU5HTv3luSHuoM= 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=jOznvsx4; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=5F/ur9br; 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="jOznvsx4"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="5F/ur9br" From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685936; 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=ZrVBb0WZqvMLfbSBp664cPAoDOu5ekw1P/3pAX1VS7A=; b=jOznvsx42cx2QWcytV7u68JhqOPr6X0eErznhNZyI6ZMUxOQnUM8VmW652LD/17E6NdwwY MiYr84Gatyw8DV+jGHg9Ml0uKNm5UN6c1+TEY7dRxnuY9OBfVeTjkqFa4C/rZhXHatzaEx rfBWkK8l2c+psRlIOTo2qWLXkp8xyvPjKNj0lI75/32E1SOQ2BguXvAajiyo8XvN+oQIMr YMORKzt2ihT4pzc+jRmeA3g7OIiu7xxXxByUucq38Y9xGbrosdzk3/HHQg+pjKrWKc+KtH E8gKIQGbUf3cXDvtutMR7758jEroO1fFHfoCEIymQTkLV6FIAOi0i9biqhZlZQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685936; 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=ZrVBb0WZqvMLfbSBp664cPAoDOu5ekw1P/3pAX1VS7A=; b=5F/ur9brPUprz3iV/wCUaVZBlqi5yUccBmWAv0IRI+3nnUkkFA9Yi7GfFzJ/KuK8p02KCW thSUIKjgfC7mX4Bw== 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 v5 30/30] lockdep: Mark emergency sections in lockdep splats Date: Thu, 2 May 2024 23:44:39 +0206 Message-Id: <20240502213839.376636-31-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-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. Note that debug_show_all_locks() and lockdep_print_held_locks() rely on their callers to enter the emergency section. This is because these functions can also be called in non-emergency situations (such as sysrq). Signed-off-by: John Ogness --- kernel/locking/lockdep.c | 84 +++++++++++++++++++++++++++++++++++++++- 1 file changed, 82 insertions(+), 2 deletions(-) diff --git a/kernel/locking/lockdep.c b/kernel/locking/lockdep.c index 151bd3de5936..c06842e037d8 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; } @@ -888,11 +891,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 +974,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 +1324,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 +1359,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 +1404,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 +2053,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 +2073,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 +2587,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 +2638,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 +3009,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 +3033,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 +3632,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 +3650,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 +3742,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 +3762,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 +4004,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 +4034,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 +4070,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 +4112,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 +4195,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 +4210,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 +4732,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 +4778,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 +4799,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 +5005,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 +5027,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 +5074,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 +5207,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 +5215,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 +5235,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 +5253,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 +5958,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 +5976,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 +6591,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 +6605,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 +6653,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 +6664,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) @@ -6616,6 +6691,7 @@ void debug_show_all_locks(void) if (!p->lockdep_depth) continue; lockdep_print_held_locks(p); + nbcon_cpu_emergency_flush(); touch_nmi_watchdog(); touch_all_softlockup_watchdogs(); } @@ -6648,6 +6724,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 +6733,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 +6750,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 +6789,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