Received: by 2002:ab2:7903:0:b0:1fb:b500:807b with SMTP id a3csp1277180lqj; Mon, 3 Jun 2024 16:33:53 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCVoNrXQCAWYWGXu1PViRQxL0SdsPuMxoKL9mRC3EM5pTxdMwKOVFWPfUuB4Xm8hlBWy5tvPBDB5egJZ7VbFc8w0tp4WKMrFv7aYDXDmJQ== X-Google-Smtp-Source: AGHT+IGhseZb8zSMi67iB/0AKfI9BPKayUueGuSTHq7iyG+/gkOhwbpL+JF2yxWs/Mwe8IuwwQAL X-Received: by 2002:adf:cd06:0:b0:354:e775:19fc with SMTP id ffacd0b85a97d-35e0f2aacc6mr6587074f8f.41.1717457633737; Mon, 03 Jun 2024 16:33:53 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1717457633; cv=pass; d=google.com; s=arc-20160816; b=zjwmsc2g9dzf5cGkUdga4+W7JfI8DtdG3ZxjmYouvaR5W8/h18REYF6PzUcN2JMVpp AKGRekCdtywJ2APoZzDatT8eqBd9d2HiUAFxstzOmRzMuG2qq5rYkXBphcPmMvDzUDjr ipckHXWGRhJsPZ1uY+XDktZyh+XdjC0PPJfP2+r6L1c5UA1dm2MCQQXwThrYqVBmgrnY xBxqzPqwaEW78i1oh9mgJJgKVEOtBOM3vAPYAqxf1cVw0B9vuQ5cjThc8qxHP3mT5hmr fJecUMPp9ssX5Bo3evzfyecVF6Y3UnmUZO+W8dXz1XG0kbhM6I4FA3Kf4xHIFnKIoCaz Fy9g== 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=HSPbGXrcPCHiCR0e1KtRGjSW0V56pt20H3Bnio+pcfI=; fh=/NZX815RJy/ecOe1WbZ023sSZUJKZnpaeIZwQG5Ym7U=; b=NNLH6QKxkfBFvs9KvkAFPsdRlSgxUk+e+qplr1FFvEEOoW9/AsmjzHwrLL841nhb0X FLz3c12QzDFUPzpSvU82rvrXUDsGyladxWtZ6/+l1ptyhHWCzPaAxtxKSdBkHjvZSsw/ XXYBBht9yn3PGFEtAC/VIPxjipwkIFsqf+KCA5I3JZJMAyxZU7RY2ndpw4udOo4vRE/G lOlOLoYZv1tpvkOqQNnIS2tCOLfWPhsEKyEiyau6dg32aiyCNTC1jkXYdVahbYmP/yed ywN4up6oUpWSjmdyZhQejQ09gkSRSPynnBdsARSdfPcImdupQcGlBvaOVta1PT/3r2e/ +VwQ==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=DHV8Zwi6; 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-199830-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199830-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id a640c23a62f3a-a68f101eb84si208886266b.595.2024.06.03.16.33.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 03 Jun 2024 16:33:53 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-199830-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=DHV8Zwi6; 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-199830-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-199830-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 238681F274D9 for ; Mon, 3 Jun 2024 23:26:50 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B39B13E889; Mon, 3 Jun 2024 23:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="DHV8Zwi6"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="unMpXQQf" 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 B7BF913D606 for ; Mon, 3 Jun 2024 23:25:00 +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=1717457102; cv=none; b=sf7j+WkxXUX/c6I+PhNelzMV5V+BtFPkNl3Do3htRYapE3lghbyDYO46YzJFZI2zZmndRKpgsTdb0dZ+1KoL622PE51BkxQcbY/Ck97X4C+SxpQGrx5pcfz11C89XJZV8YqBkrEftCPIvfdstcudyPHDoKw4ndB3VEJ5kIcObWw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1717457102; c=relaxed/simple; bh=zCdEe8+RH4XlG7eBx9a3rSWZHIqYz7vbbkZgHROBGK8=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=cJv0baMvbPGlg7QNeWp1aIs+pambcAjZXS6UZSi8jTo9Ri2x3RHCC/bs3HTv0gUrwMf3KBIq0n+Q0ii/19k2dv5rTA+cXhR+H7F6bWwb/beJI9101Bo8kOlrboo6WeiQFEb8cpygWNN2dnS271N7u89xdV5ont9yTwY2d/tqv5s= 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=DHV8Zwi6; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=unMpXQQf; 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=1717457099; 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=HSPbGXrcPCHiCR0e1KtRGjSW0V56pt20H3Bnio+pcfI=; b=DHV8Zwi6y4rTdb9NOOkhROyJ4R4g2iCWZHE83mUWDGdEAN87ebE3cnzLGhO8hHWJGrzl6K 7mvj17Gu10Thx+hvdO4vkkam24FDvzYgnQm47mO+xKKzTQwf+/to7uYDTlHAFiSK8EqeLp 8yWsPk4lCq/7nLlkHRoATl5G9mIyRyQRySKwngeYSqay+nF1YjNyWBm2FRV7KH/vwlstnc S3o3sIotQk+NhUZsZPKteTPlxX98GtTjxjOLLDMNaHTRbpo2eeJHwRQCu1bILU5W0nfWGb AeeWbSMxQTCpPco2KLF7OfRCIwM9Juqr9Eee7nq4HIAB0tPrJDTpXFWF0Z/8rQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1717457099; 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=HSPbGXrcPCHiCR0e1KtRGjSW0V56pt20H3Bnio+pcfI=; b=unMpXQQf6Rr8AddTHoAqNxnxTkclhT2pnume2VqwfVqUZTbKWj/ltvZQFRQek6vYXqTH7M TReXtirtTJ5bBRDw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v2 11/18] printk: nbcon: Show replay message on takeover Date: Tue, 4 Jun 2024 01:30:46 +0206 Message-Id: <20240603232453.33992-12-john.ogness@linutronix.de> In-Reply-To: <20240603232453.33992-1-john.ogness@linutronix.de> References: <20240603232453.33992-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 An emergency or panic context can takeover console ownership while the current owner was printing a printk message. The atomic printer will re-print the message that the previous owner was printing. However, this can look confusing to the user and may even seem as though a message was lost. [3430014.1 [3430014.181123] usb 1-2: Product: USB Audio Add a new field @nbcon_prev_seq to struct console to track the sequence number to print that was assigned to the previous console owner. If this matches the sequence number to print that the current owner is assigned, then a takeover must have occurred. In this case, print an additional message to inform the user that the previous message is being printed again. [3430014.1 ** replaying previous printk message ** [3430014.181123] usb 1-2: Product: USB Audio Signed-off-by: John Ogness --- include/linux/console.h | 3 +++ kernel/printk/internal.h | 1 + kernel/printk/nbcon.c | 24 ++++++++++++++++++++++++ kernel/printk/printk.c | 19 +++++++++++++++++++ 4 files changed, 47 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index adcc1a74aeee..9927f08ac054 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -325,6 +325,7 @@ struct nbcon_write_context { * @nbcon_state: State for nbcon consoles * @nbcon_seq: Sequence number of the next record for nbcon to print * @nbcon_device_ctxt: Context available for non-printing operations + * @nbcon_prev_seq: Seq num the previous nbcon owner was assigned to print * @pbufs: Pointer to nbcon private buffer * @kthread: Printer kthread for this console * @rcuwait: RCU-safe wait object for @kthread waking @@ -445,6 +446,8 @@ struct console { atomic_t __private nbcon_state; atomic_long_t __private nbcon_seq; struct nbcon_context __private nbcon_device_ctxt; + atomic_long_t __private nbcon_prev_seq; + struct printk_buffers *pbufs; struct task_struct *kthread; struct rcuwait rcuwait; diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 5d4722310850..9e027e08918d 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -233,4 +233,5 @@ bool printk_get_next_message(struct printk_message *pmsg, u64 seq, #ifdef CONFIG_PRINTK void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped); +void console_prepend_replay(struct printk_message *pmsg); #endif diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index 480a0ced2708..8c92b076c745 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -866,6 +866,7 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool use_a unsigned long con_dropped; struct nbcon_state cur; unsigned long dropped; + unsigned long ulseq; /* * The printk buffers are filled within an unsafe section. This @@ -891,6 +892,28 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt, bool use_a if (dropped && !is_extended) console_prepend_dropped(&pmsg, dropped); + /* + * If the previous owner was assigned the same record, this context + * has taken over ownership and is replaying the record. Prepend a + * message to let the user know the record is replayed. + */ + ulseq = atomic_long_read(&ACCESS_PRIVATE(con, nbcon_prev_seq)); + if (__ulseq_to_u64seq(prb, ulseq) == pmsg.seq) { + console_prepend_replay(&pmsg); + } else { + /* + * Ensure this context is still the owner before trying to + * update @nbcon_prev_seq. Otherwise the value in @ulseq may + * not be from the previous owner. + */ + nbcon_state_read(con, &cur); + if (!nbcon_context_can_proceed(ctxt, &cur)) + return false; + + atomic_long_try_cmpxchg(&ACCESS_PRIVATE(con, nbcon_prev_seq), &ulseq, + __u64seq_to_ulseq(pmsg.seq)); + } + if (!nbcon_context_exit_unsafe(ctxt)) return false; @@ -1631,6 +1654,7 @@ void nbcon_init(struct console *con, u64 init_seq) rcuwait_init(&con->rcuwait); init_irq_work(&con->irq_work, nbcon_irq_work); nbcon_seq_force(con, init_seq); + atomic_long_set(&ACCESS_PRIVATE(con, nbcon_prev_seq), -1UL); nbcon_state_set(con, &state); nbcon_kthread_create(con); } diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fcbf794a0aaf..cf0d612329bf 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2867,6 +2867,25 @@ void console_prepend_dropped(struct printk_message *pmsg, unsigned long dropped) __console_prepend_scratch(pmsg, len); } +/* + * Prepend the message in @pmsg->pbufs->outbuf with a "replay message". + * @pmsg->outbuf_len is updated appropriately. + * + * @pmsg is the printk message to prepend. + */ +void console_prepend_replay(struct printk_message *pmsg) +{ + struct printk_buffers *pbufs = pmsg->pbufs; + const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); + char *scratchbuf = &pbufs->scratchbuf[0]; + size_t len; + + len = scnprintf(scratchbuf, scratchbuf_sz, + "** replaying previous printk message **\n"); + + __console_prepend_scratch(pmsg, len); +} + /* * Read and format the specified record (or a later record if the specified * record is not available). -- 2.39.2