Received: by 2002:a05:6358:16cc:b0:ea:6187:17c9 with SMTP id r12csp720223rwl; Thu, 5 Jan 2023 03:43:55 -0800 (PST) X-Google-Smtp-Source: AMrXdXtn0h+sn3dwE1VPMOR19Sh6bd9bi0ivZyVSy33IVhpIW2LWUwRn9rDYKoih3qXRnGJdt7nD X-Received: by 2002:a05:6402:2296:b0:478:b87d:94d0 with SMTP id cw22-20020a056402229600b00478b87d94d0mr42142316edb.17.1672919035316; Thu, 05 Jan 2023 03:43:55 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672919035; cv=none; d=google.com; s=arc-20160816; b=YOC17iQwVxz1Xer1vZYM/asMfKGvcKlG9lrIJSr+7Vu1wNAfyIayID9Akagxl634Xn Ttg+N+J4tLxnPyIE8yc4iVUeGWceUB5yJnhti9qT5TMWJMOKuIfM8G93FkKMt3t9Sl2n fP+oCDLKtlG+nwTDLQULL+Uctl2z0fXP8+PW1V2ODcAjB1iD5A3QN84bTiPtn7fEOGBt xp9kSeJ4psT0qOKnwci4MUqpmEZ0fR5CxWPLGRBfpe2qdS7TzYRWN9Ff9MeJqzFT7ZvI nbq1bnoRCSFKWoZ4reAiVmJWqAu8/pblxwLM7b5NNXrbBtW37TI9nkCPR9LjA1jjHLdU QsTA== 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=K6Zk/xyTkHlZSXedWM+pYMQGuYWeTvriHXM5QqKd1Wc=; b=d7soKHErsABoTxv19UHizCWppQsvPv+EmvMxe8CM/4kFeFXx3DxH2/GBYZ2mK2I9zK 7k8M0Wlh9Hp8231f8/3vY+r5d610oN13ulpyHcUOvF2e6eoNo/z8J5GH3YILQ+BkHfrg 2JjqklCKbSVy7GsqXnY1AjRGePvEz1rEKQjLRK1ABMqcA0+PLDccEwCu0eD/AzIr+Nnz PjFw31/YDD7uxpNrJGhhEIjv5lgHQla/x9wA0KBzGdyW5JecM+N3s5aSZtOqNddhjJ5j pt8ExX44Q8TpiXUGqqDHscndU8uKPfGa5kOy6pVwenKMP4k65F/XPc2UKoKSC+3CC6HD FCSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=3ifzsD2V; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j4-20020a508a84000000b0047e9f550ab7si32762617edj.346.2023.01.05.03.43.42; Thu, 05 Jan 2023 03:43:55 -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=3ifzsD2V; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 S233139AbjAEKiv (ORCPT + 55 others); Thu, 5 Jan 2023 05:38:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59346 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233074AbjAEKiT (ORCPT ); Thu, 5 Jan 2023 05:38:19 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 75D97395DF for ; Thu, 5 Jan 2023 02:38:18 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1672915096; 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=K6Zk/xyTkHlZSXedWM+pYMQGuYWeTvriHXM5QqKd1Wc=; b=3ifzsD2VLb0UnRUZmc2jd0Kxf8DOYPfIyi/qil780zAs/3nncS0rqmUFvGTLEjRM1QQo6R tcUb+sWumldOTsVV+uCRHEm77N+TdAL+oxOijC7mp00YsB3hlFVsNcRkMWPBBu7PrkmFaf rW9c9yekuhG0Qp/z4yf8AohrDjNXT3ojBuePxlzBfl5vFfV3j/NE9QdnIMcA/D1dg50hZR slgCYk8rU6P6SHevvG2M276uMqLU4BixjqnCX7v38AJ+V0JOWDpwPqtHvQYTJtls4c5p1m Jm9bAwvGEvWfPUdEsn/x2s8Av5N3o2asS9mjhmvMACFJw0GOKUihqbArkflriQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1672915096; 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=K6Zk/xyTkHlZSXedWM+pYMQGuYWeTvriHXM5QqKd1Wc=; b=8ptkBRZIkXSNC7TtUxZZmmlo3sejsNalSNJqYR6L5LR7ES7jDazDpEQjzwRVNa/HiOx/y8 wSkdFwleCcEH5EAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 5/8] printk: introduce printk_get_next_message() and printk_message Date: Thu, 5 Jan 2023 11:43:32 +0106 Message-Id: <20230105103735.880956-6-john.ogness@linutronix.de> In-Reply-To: <20230105103735.880956-1-john.ogness@linutronix.de> References: <20230105103735.880956-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Code for performing the console output is intermixed with code that is formatting the output for that console. Introduce a new helper function printk_get_next_message() to handle the reading and formatting of the printk text. The helper does not require any locking so that in the future it can be used for other printing contexts as well. This also introduces a new struct printk_message to wrap the struct printk_buffers, adding metadata about its contents. This allows users of printk_get_next_message() to receive all relevant information about the message that was read and formatted. Why is struct printk_message a wrapper struct? It is intentional that a wrapper struct is introduced instead of adding the metadata directly to struct printk_buffers. The upcoming atomic consoles support multiple printing contexts per CPU. This means that while a CPU is formatting a message, it can be interrupted and the interrupting context may also format a (possibly different) message. Since the printk buffers are rather large, there will only be one struct printk_buffers per CPU and it must be shared by the possible contexts of that CPU. If the metadata was part of struct printk_buffers, interrupting contexts would clobber the metadata being prepared by the interrupted context. This could be handled by robustifying the message formatting functions to cope with metadata unexpectedly changing. However, this would require significant amounts of extra data copying, also adding significant complexity to the code. Instead, the metadata can live on the stack of the formatting context and the message formatting functions do not need to be concerned about the metadata changing underneath them. Note that the message formatting functions can handle unexpected text buffer changes. So it is perfectly OK if a shared text buffer is clobbered by an interrupting context. The atomic console implementation will recognize the interruption and avoid printing the (probably garbage) text buffer. Signed-off-by: John Ogness --- kernel/printk/internal.h | 16 ++++++ kernel/printk/printk.c | 115 +++++++++++++++++++++++++++------------ 2 files changed, 96 insertions(+), 35 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index 6080d289a342..c9bb0cd86372 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -92,3 +92,19 @@ struct printk_buffers { char outbuf[CONSOLE_EXT_LOG_MAX]; char scratchbuf[LOG_LINE_MAX]; }; + +/** + * struct printk_message - Container for a prepared printk message. + * @pbufs: printk buffers used to prepare the message. + * @outbuf_len: The length of prepared text in @pbufs->outbuf to output. This + * does not count the terminator. A value of 0 means there is + * nothing to output and this record should be skipped. + * @seq: The sequence number of the record used for @pbufs->outbuf. + * @dropped: The number of dropped records from reading @seq. + */ +struct printk_message { + struct printk_buffers *pbufs; + unsigned int outbuf_len; + u64 seq; + unsigned long dropped; +}; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index bc5d4574c459..6e2a6d5548e9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2725,35 +2725,35 @@ static void __console_unlock(void) } /* - * Print one record for the given console. The record printed is whatever - * record is the next available record for the given console. + * Read and format the specified record (or a later record if the specified + * record is not available). * - * @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. + * @pmsg will contain the formatted result. @pmsg->pbufs must point to a + * struct printk_buffers. * - * @cookie is the cookie from the SRCU read lock. + * @seq is the record to read and format. If it is not available, the next + * valid record is read. * - * Returns false if the given console has no next record to print, otherwise - * true. + * @is_extended specifies if the message should be formatted for extended + * console output. * - * Requires the console_lock and the SRCU read lock. + * Returns false if no record is available. Otherwise true and all fields + * of @pmsg are valid. (See the documentation of struct printk_message + * for information about the @pmsg fields.) */ -static bool console_emit_next_record(struct console *con, bool *handover, int cookie) +static bool printk_get_next_message(struct printk_message *pmsg, u64 seq, + bool is_extended) { - static char dropped_text[DROPPED_TEXT_MAX]; - static struct printk_buffers pbufs; static int panic_console_dropped; - bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED; - const size_t scratchbuf_sz = sizeof(pbufs.scratchbuf); - const size_t outbuf_sz = sizeof(pbufs.outbuf); - char *scratchbuf = &pbufs.scratchbuf[0]; - char *outbuf = &pbufs.outbuf[0]; + struct printk_buffers *pbufs = pmsg->pbufs; + const size_t scratchbuf_sz = sizeof(pbufs->scratchbuf); + const size_t outbuf_sz = sizeof(pbufs->outbuf); + char *scratchbuf = &pbufs->scratchbuf[0]; + char *outbuf = &pbufs->outbuf[0]; struct printk_info info; struct printk_record r; - unsigned long flags; - size_t len; + size_t len = 0; /* * Formatting extended messages requires a separate buffer, so use the @@ -2767,25 +2767,26 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co else prb_rec_init_rd(&r, &info, outbuf, outbuf_sz); - *handover = false; - - if (!prb_read_valid(prb, con->seq, &r)) + if (!prb_read_valid(prb, seq, &r)) return false; - if (con->seq != r.info->seq) { - con->dropped += r.info->seq - con->seq; - con->seq = r.info->seq; - if (panic_in_progress() && panic_console_dropped++ > 10) { - suppress_panic_printk = 1; - pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); - } + pmsg->seq = r.info->seq; + pmsg->dropped = r.info->seq - seq; + + /* + * Check for dropped messages in panic here so that printk + * suppression can occur as early as possible if necessary. + */ + if (pmsg->dropped && + panic_in_progress() && + panic_console_dropped++ > 10) { + suppress_panic_printk = 1; + pr_warn_once("Too many dropped messages. Suppress messages on non-panic CPUs to prevent livelock.\n"); } /* Skip record that has level above the console loglevel. */ - if (suppress_message_printing(r.info->level)) { - con->seq++; - goto skip; - } + if (suppress_message_printing(r.info->level)) + goto out; if (is_extended) { len = info_print_ext_header(outbuf, outbuf_sz, r.info); @@ -2794,6 +2795,50 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co } else { len = record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, printk_time); } +out: + pmsg->outbuf_len = len; + return true; +} + +/* + * Print one record for the given console. The record printed is whatever + * record is the next available record for the given console. + * + * @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 is the cookie from the SRCU read lock. + * + * Returns false if the given console has no next record to print, otherwise + * true. + * + * Requires the console_lock and the SRCU read lock. + */ +static bool console_emit_next_record(struct console *con, bool *handover, int cookie) +{ + static char dropped_text[DROPPED_TEXT_MAX]; + static struct printk_buffers pbufs; + + bool is_extended = console_srcu_read_flags(con) & CON_EXTENDED; + char *outbuf = &pbufs.outbuf[0]; + struct printk_message pmsg = { + .pbufs = &pbufs, + }; + unsigned long flags; + + *handover = false; + + if (!printk_get_next_message(&pmsg, con->seq, is_extended)) + return false; + + con->dropped += pmsg.dropped; + + /* Skip messages of formatted length 0. */ + if (pmsg.outbuf_len == 0) { + con->seq = pmsg.seq + 1; + goto skip; + } /* * While actively printing out messages, if another printk() @@ -2809,11 +2854,11 @@ static bool console_emit_next_record(struct console *con, bool *handover, int co console_lock_spinning_enable(); stop_critical_timings(); /* don't trace print latency */ - call_console_driver(con, outbuf, len, + call_console_driver(con, outbuf, pmsg.outbuf_len, is_extended ? NULL : dropped_text); start_critical_timings(); - con->seq++; + con->seq = pmsg.seq + 1; *handover = console_lock_spinning_disable_and_check(cookie); printk_safe_exit_irqrestore(flags); -- 2.30.2