Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp1876196lqb; Sun, 26 May 2024 23:38:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCW2AgI6/t0/3ztn4s44rmQKL5gHWXCYvi54BygXL+Sqwa6yQCUYpF2Iuhjw3XbkRPTJOoqWHPNCBCcR8lGjMUpk76P/QXoFM1xctvysCw== X-Google-Smtp-Source: AGHT+IFuk93mCdRMS7YSM6nxFgp3usQDSH3RRkn4ZJBaGp//wz2iwB9wUsT9bi6PU+qCwWb5uRVA X-Received: by 2002:a17:906:84a:b0:a5c:fe8e:cf6f with SMTP id a640c23a62f3a-a6264f0efb0mr566782466b.56.1716791919016; Sun, 26 May 2024 23:38:39 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716791919; cv=pass; d=google.com; s=arc-20160816; b=QgDM2vxx0zQZDj9SfJ76XfXLkKWSj53Hc4XJB9tpqH3o5cZe7PibUtnV0UD0oRx3ln rZInUxXTJ9ao2A8j6cN8cIZ9ufs3gxF+dEy3Rko6Sidn+UjCyc10kQqB0gDVEwJRLgXR MbvVXfkBqXmRiUPUwh7x2J+3k7mDv79qGz6hX2Z3IWx6Vmdy68rl+hMEIoDvdHY5aYGy vU0vxteakYt6hOjprBLkK9jnAS73z31FEyQQT+Hy5ZwO8c85KBbxjt9KsFz76x5ZmS0x ZNMtc9+ABTH0wUz8glHvsU36DNvQjBOF0GSeiohquP7228ckR31Tn/o7WpZrlA6OM5QZ SCcA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=vvvXcgZvXxemT9kjJC+HVcrx0f7yfYMwgbRbPkHz9+k=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=QEt8vcF3wB+a4xDqjw5YtkH2s6Xr/p3n5Y8uIZrxAhvrK7KQTInnIoApNM6hgLVW3B SetN7L1oR7rh+JT27YxIFpO4kX0/5kj9sz9dKEekds9QvzXpBvAgBdfxOsuI2EtcD1w0 RPLMTPAzp3IeTxFqhZ5rAeKiX2S/6H0XTbBl3vyomwr1acNjW9G+6uW4jcMHALlVJmy4 vWZbQLF7elnK8MBrQy3FqucdolqF4hQvk5Jh7eupXG8fPzbb+G7rFX6WC12oURKcDDMu YTDH/3r194pSXlJvnbRpCklCFYyLm52SP53t070kRqHBx0XPviw+3C4aoYwX+qfKJPsX CSSw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=qjZCHn5U; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-190075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190075-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id a640c23a62f3a-a626cdd070asi335981466b.973.2024.05.26.23.38.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 26 May 2024 23:38:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-190075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=qjZCHn5U; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-190075-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-190075-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 929B21F2127D for ; Mon, 27 May 2024 06:38:38 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 8E40F17BD5; Mon, 27 May 2024 06:38:21 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="qjZCHn5U"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="0KYfIqMw" 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 C5FF12032A for ; Mon, 27 May 2024 06:38:18 +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=1716791900; cv=none; b=BM8rlMm72zARf1V/lgi63YlgJC95AEFHUASGdCFAljPQW4tHl/FHHIzZYXh2MDcNDRmZEbVVBbKwtEux/3gyATzOu/0vko/s6fp+uHAkyoa9rpW09NxgHloCkxrc2JS8YLR6HlbReaNjxpIr7RAzkitSMjl2Z/c/i03Y0bpyQ3s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716791900; c=relaxed/simple; bh=uJo1oELvyDQOhiRu2SO/n1grPBTW50wUxfvcqcbXfEE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=h9smRKgxfMwAIKdUzfHXyEAWr0UpR2Vp37GOBwAfVhnpgFUir85mrV8asM0iUh2eRctMtbjzzUbPGb3b7KWTgLQyFDRNDU6ai2ElBZs05l3ja7HWU75uh/wBe9qvbHCN9DraKIWikWC5c+9zzXYdsS2q83EGx2pJDI/vRANI0VM= 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=qjZCHn5U; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=0KYfIqMw; 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 From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1716791897; 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=vvvXcgZvXxemT9kjJC+HVcrx0f7yfYMwgbRbPkHz9+k=; b=qjZCHn5U7M1e/aS5MXcWPWaLga2DddaMPm5v2wAL53N2CSM0NHGOc8ptV/MstuzjR3q1MV 2EPpHBIKbHgN3DVAQ5dIPT1IQzD289wV15dHHfKh1bM19GmTzYFRZOVdVwnTGw/wZb0F4w u0Mu2qUlHMEzQyh7BxtsNc4k1UG3Em/I5y4fKzh9m8tMnLtWnerbyWmX5Vfe3CJihIU+nS XGI99yVXYN9L417Edk3Peq63BjzNEBSY35uvDR1o7OZs7WSC+P0JcUMT4O+sPoGexaBgTS CEkQ8r+PgjQQO8dfDvIaW1X8Q0JEnj0QJPKrTvkYbx9eKvvCeX2SJ0DnOTsRsQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1716791897; 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=vvvXcgZvXxemT9kjJC+HVcrx0f7yfYMwgbRbPkHz9+k=; b=0KYfIqMwqM1NEMGyZWAG/39WznOWARAFCcBbQper6Q+xOgOXXOlMPy882iwk3TYtbf5m4/ Oz1GhITWHsjINgCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v6 02/30] printk: Properly deal with nbcon consoles on seq init Date: Mon, 27 May 2024 08:43:21 +0206 Message-Id: <20240527063749.391035-3-john.ogness@linutronix.de> In-Reply-To: <20240527063749.391035-1-john.ogness@linutronix.de> References: <20240527063749.391035-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: 8bit 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); #else @@ -96,7 +96,7 @@ static inline bool printk_percpu_data_ready(void) { return false; } static inline u64 nbcon_seq_read(struct console *con) { return 0; } static inline void nbcon_seq_force(struct console *con, u64 seq) { } static inline bool nbcon_alloc(struct console *con) { return false; } -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) { } #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 = max_t(u64, seq, prb_first_valid_seq(prb)); 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 = 0; } /** @@ -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 = { }; /* nbcon_alloc() must have been called and successful! */ BUG_ON(!con->pbufs); - nbcon_seq_force(con, con->seq); + nbcon_seq_force(con, init_seq); nbcon_state_set(con, &state); } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 6efb629fd976..4480ad2f198e 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3348,19 +3348,21 @@ static void try_enable_default_console(struct console *newcon) newcon->flags |= CON_CONSDEV; } -static void console_init_seq(struct console *newcon, bool bootcon_registered) +/* Return the starting sequence number for a newly registered console. */ +static u64 get_init_console_seq(struct console *newcon, bool bootcon_registered) { struct console *con; bool handover; + u64 init_seq; if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) { /* Get a consistent copy of @syslog_seq. */ mutex_lock(&syslog_lock); - newcon->seq = syslog_seq; + init_seq = syslog_seq; mutex_unlock(&syslog_lock); } else { /* Begin with next message added to ringbuffer. */ - newcon->seq = prb_next_seq(prb); + init_seq = prb_next_seq(prb); /* * 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(); - newcon->seq = prb_next_seq(prb); + init_seq = prb_next_seq(prb); for_each_console(con) { - if ((con->flags & CON_BOOT) && - (con->flags & CON_ENABLED) && - con->seq < newcon->seq) { - newcon->seq = con->seq; + u64 seq; + + if (!(con->flags & CON_BOOT) || + !(con->flags & CON_ENABLED)) { + continue; } + + if (con->flags & CON_NBCON) + seq = nbcon_seq_read(con); + else + seq = con->seq; + + if (seq < init_seq) + init_seq = seq; } } console_unlock(); } } + + return init_seq; } #define console_first() \ @@ -3438,6 +3451,7 @@ void register_console(struct console *newcon) struct console *con; bool bootcon_registered = false; bool realcon_registered = false; + u64 init_seq; int err; console_list_lock(); @@ -3515,10 +3529,13 @@ void register_console(struct console *newcon) } newcon->dropped = 0; - console_init_seq(newcon, bootcon_registered); + init_seq = get_init_console_seq(newcon, bootcon_registered); - if (newcon->flags & CON_NBCON) - nbcon_init(newcon); + if (newcon->flags & CON_NBCON) { + nbcon_init(newcon, init_seq); + } else { + newcon->seq = init_seq; + } /* * Put this console in the list - keep the -- 2.39.2