From patchwork Wed Nov 16 16:21:18 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 21138 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp232342wru; Wed, 16 Nov 2022 08:23:29 -0800 (PST) X-Google-Smtp-Source: AA0mqf7aKdS9J19NaExVj4xD/7FgBOXt9IP5W3StNW+8KQko/plkFm7nrTjV2qgvY8X02pyo9Bll X-Received: by 2002:a63:5d15:0:b0:434:7885:6ade with SMTP id r21-20020a635d15000000b0043478856ademr20904559pgb.243.1668615809242; Wed, 16 Nov 2022 08:23:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668615809; cv=none; d=google.com; s=arc-20160816; b=r/xXho9K7QgFvWOhnOe6EtnLEojowguB9o1qle8RlKDk4mV1HniRVnYMwQPvYspWQP GiGBoOtOgeFo7ws4kUftrz8EpYXktc0LIFz84+fCtvaUJxUsaUQN1bfTZdMpuWINXc10 Lmz856SHThLyywfoT2YAeJAVFC/Icwx1OGShu9B8bN1vGbmc6C1A3VZiAhgqZ2HTqwO0 78yc5a6lkFdQxI7VQF9Ryfq+LfRGHMdG0lyBk4kSOI5mtZH0ayMvQG42uAFrLHi79NvQ Viw7Nt8573XzLC2xLEw30UidxkAOfGNl/a63hAEmEHnBdphkbcquvt7y+DetQdyCU1GY 6WJQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=EXztAQTlLoZJohd2YWLlNMXfDo93BSWHA8w8Mma17HQ=; b=q0sJXbsbXZSL0Vr2wlqzXVF49L50zRtFdFUmhSFh4WyUUfYExpSwT0evJNg/UDhA0I McPKPrpPkfbIMOCX0jJ2q6kdzBbG2hYy0Hh71VKi3JeJpqb6zyFkrqk1DuNBU1OUXF1M P2/7hgaFiVdswsMZefmXNSUyvchkJXAJSfPtgN6aijA0WY5KtJM5iSCD3tpG75IgDAlQ 61vnIYYWnUwzJIcwGf6vaf+G8vwoNgD+kC+bswUmzS4JSBOTeA26AWNK2eTEKVMPimrx 0c2WaQC0qmDj/0BR9W2thJBagsyJ7HJ5gK/SaAitJavi2ERBUVyu3bT7oTcf5+E7sGpy exiw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=yAhjS6EM; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id mw16-20020a17090b4d1000b00218616d4f78si1140221pjb.36.2022.11.16.08.23.16; Wed, 16 Nov 2022 08:23:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=yAhjS6EM; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234653AbiKPQW3 (ORCPT + 99 others); Wed, 16 Nov 2022 11:22:29 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58638 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233489AbiKPQWD (ORCPT ); Wed, 16 Nov 2022 11:22:03 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76B6D26495 for ; Wed, 16 Nov 2022 08:22:02 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668615720; 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=EXztAQTlLoZJohd2YWLlNMXfDo93BSWHA8w8Mma17HQ=; b=yAhjS6EMHDwR29tHfXch75ulrWxpc1Dllv4e1A6L/CRuj3qaC6My52J05yB5LS9GYDty3S YbJygu6L8FxPUC3xceTSgU17KYRVnrlWuB69bhLpPC403se7hEx4Y0zChaqnPgAjnfE3oW Cr/u+gUpRuLp3lgAm6HWu80ooQi1vkv9JOU4lFDi94Hh4ol3jEk7tlMEzm3JYwypNq3QvI ZZEEWOZ3w5ptvZASYNKSNKyz7XCQb8XSV3fsXq+BPK9moc2EAkkxEE8VuOtAMaVnvNRPZv rSa3L1G9AM+0MYYCxgUVlc+6a/gZgm8lmIXcpscIaLPQ4b5XlBe7RIvfGCgKOg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668615720; 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=EXztAQTlLoZJohd2YWLlNMXfDo93BSWHA8w8Mma17HQ=; b=J45KvR1CKJPOFPEjopi9x5va7WC/8qt7O7J18LAFBh1vT0vVGDJioBw26YKvI+8Sp5ai6K rFudH5T5A87CiaAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 06/40] printk: fix setting first seq for consoles Date: Wed, 16 Nov 2022 17:27:18 +0106 Message-Id: <20221116162152.193147-7-john.ogness@linutronix.de> In-Reply-To: <20221116162152.193147-1-john.ogness@linutronix.de> References: <20221116162152.193147-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749670490585026332?= X-GMAIL-MSGID: =?utf-8?q?1749670490585026332?= It used to be that all consoles were synchronized with respect to which message they were printing. After commit a699449bb13b ("printk: refactor and rework printing logic"), all consoles have their own @seq for tracking which message they are on. That commit also changed how the initial sequence number was chosen. Instead of choosing the next non-printed message, it chose the sequence number of the next message that will be added to the ringbuffer. That change created a possibility that a non-boot console taking over for a boot console might skip messages if the boot console was behind and did not have a chance to catch up before being unregistered. Since it is not known which boot console is the same device, flush all consoles and, if necessary, start with the message of the enabled boot console that is the furthest behind. If no boot consoles are enabled, begin with the next message that will be added to the ringbuffer. Also, since boot consoles are meant to be used at boot time, handle them the same as CON_PRINTBUFFER to ensure that no initial messages are skipped. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 50 +++++++++++++++++++++++++++++++++++++----- 1 file changed, 45 insertions(+), 5 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 31d9d1cf8682..c84654444a02 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3131,16 +3131,56 @@ static void try_enable_default_console(struct console *newcon) (con->flags & CON_BOOT) ? "boot" : "", \ con->name, con->index, ##__VA_ARGS__) -static void console_init_seq(struct console *newcon) +static void console_init_seq(struct console *newcon, bool bootcon_registered) { - if (newcon->flags & CON_PRINTBUFFER) { + struct console *con; + bool handover; + + if (newcon->flags & (CON_PRINTBUFFER | CON_BOOT)) { /* Get a consistent copy of @syslog_seq. */ mutex_lock(&syslog_lock); newcon->seq = syslog_seq; mutex_unlock(&syslog_lock); } else { - /* Begin with next message. */ + /* Begin with next message added to ringbuffer. */ newcon->seq = prb_next_seq(prb); + + /* + * If any enabled boot consoles are due to be unregistered + * shortly, some may not be caught up and may be the same + * device as @newcon. Since it is not known which boot console + * is the same device, flush all consoles and, if necessary, + * start with the message of the enabled boot console that is + * the furthest behind. + */ + if (bootcon_registered && !keep_bootcon) { + /* + * Flush all consoles and set the console to start at + * the next unprinted sequence number. + */ + if (!console_flush_all(true, &newcon->seq, &handover)) { + /* + * Flushing failed. Just choose the lowest + * sequence of the enabled boot consoles. + */ + + /* + * If there was a handover, this context no + * longer holds the console_lock. + */ + if (handover) + console_lock(); + + newcon->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; + } + } + } + } } } @@ -3235,13 +3275,13 @@ void register_console(struct console *newcon) } newcon->dropped = 0; - console_init_seq(newcon); + console_lock(); + console_init_seq(newcon, bootcon_registered); /* * Put this console in the list - keep the * preferred driver at the head of the list. */ - console_lock(); if (hlist_empty(&console_list)) { /* Ensure CON_CONSDEV is always set for the head. */ newcon->flags |= CON_CONSDEV;