Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp888991pxb; Fri, 22 Apr 2022 13:31:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyjis1tiT+T5p7bQ5pniB3VPr57DmjnE7rkvRtaTf7ySzgvXifElLbpi4qmMwM8jWT2eg6G X-Received: by 2002:a17:902:c202:b0:15a:2af6:7e06 with SMTP id 2-20020a170902c20200b0015a2af67e06mr6095250pll.76.1650659472487; Fri, 22 Apr 2022 13:31:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650659472; cv=none; d=google.com; s=arc-20160816; b=hZV4l6j0+MbcDq5GosBrYY86sq53V5MQNk9k1WNHpUBCE1WkGHwEmvkYeNEPTflY3l jKrovTWrUbyfHXKSLwple+gUlhhjBheg+xS7wd2KETSHV9lJLWxXDoX/vMbWEjGetmxQ Hmt5IBEIdImHPp8Qn4KrKoRy3Zdr3jaN+jAQeeS1Tr6aLM3iwV5FtxijZzQLNuNw180B bKPN7KUldWC7h17gMjRK2OZV7OD11sGOkdbXUevosl/XGhg9OONhSIcZWjjQpqQAas1w a/KEteegPBQPPKH4xGUlaPAhtjLlav1z/Z+qwR5m1jlCS+J3yodtx0Wf3mUwnh3jaMDS aAKw== 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=HOncsmW3LCPWpiCUYH+mSUEQgA/CB+Uw1krMPbMPqTg=; b=upqDQ8vmHAqoBYlafcPJmFvjDAJbthpXjJg30+7Y9yyOT4JQvxIYU6NOOkSaVGVPZJ Win8FCvPaALH2my1x2xzpRMxJ17YI86y1chpHf4bs9rRdEwLp3vR0hPfunVW5I5+udR8 FoqYAaOiCzKUg+mGf6v7iKdwEZS+4B/XwaH3GnTS9hL/8SslQt1YPIE2uE4Cm+KNnQBm BDdzAA91FDWAH/skfd33aehAFxadvh2vEFbQ1qPCkjLqWzK0jm8R2DfFXXH3J4HxbqKV dIc/KgpED/xAVYAyjTsL+yoialw0GMXffZ3l9CMJLr3m5Xtew/Sie4qZ3NdAscjBMvz2 PYhQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LosGKig8; dkim=neutral (no key) header.i=@linutronix.de header.b=Dq5vpvAY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id l15-20020a17090ac58f00b001c9fe7a87cesi10662093pjt.105.2022.04.22.13.31.12 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 22 Apr 2022 13:31:12 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=LosGKig8; dkim=neutral (no key) header.i=@linutronix.de header.b=Dq5vpvAY; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 329A828917D; Fri, 22 Apr 2022 12:18:14 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1358180AbiDSXuC (ORCPT + 99 others); Tue, 19 Apr 2022 19:50:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39448 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358210AbiDSXt2 (ORCPT ); Tue, 19 Apr 2022 19:49:28 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 9C7A821836 for ; Tue, 19 Apr 2022 16:46:44 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1650412003; 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=HOncsmW3LCPWpiCUYH+mSUEQgA/CB+Uw1krMPbMPqTg=; b=LosGKig83z4JGHQNts4NqafmKQp0FJ7TPsQfIOsshMYKjy9MH9oND6wIH5Q6FJH4vRWdKz ZupQLldhcs0aIllGjLrws9WxaaJSxtlj3O29a9PpN/MezU+S5zad4R10/mDMSLf9pYKU1i KpGoqIwz3+IK1AjHgmDPLjFOHCmBMpJXiL8OofOJ8dpC0cZFF+x1/rqbP0gqSTklyZ1CdO xSbFi/rMDDG6Cu6S+6hxGAnrlxE6fwIFI0JyoiQ0K+x6EDdS14InpgFCzDM/Pi5+4RlHpc kH4eHFumTe37N/qk8E5U9NIDw82Q5pFtHTDoLcKm3q156wKIQY6k9ytm519f9g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1650412003; 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=HOncsmW3LCPWpiCUYH+mSUEQgA/CB+Uw1krMPbMPqTg=; b=Dq5vpvAYOe9pDW+Yv181mqaiUZdsgnUisSiLa23AdNTcwNt3Vm0+GcvcRQTMYVp9yX31hb D7vX55hAtPXhfdBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 11/15] printk: add pr_flush() Date: Wed, 20 Apr 2022 01:52:33 +0206 Message-Id: <20220419234637.357112-12-john.ogness@linutronix.de> In-Reply-To: <20220419234637.357112-1-john.ogness@linutronix.de> References: <20220419234637.357112-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.5 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INVALID_DATE_TZ_ABSURD,MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=no 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 Provide a might-sleep function to allow waiting for console printers to catch up to the latest logged message. Use pr_flush() whenever it is desirable to get buffered messages printed before continuing: suspend_console(), resume_console(), console_stop(), console_start(), console_unblank(). Signed-off-by: John Ogness --- include/linux/printk.h | 7 ++++ kernel/printk/printk.c | 83 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/include/linux/printk.h b/include/linux/printk.h index b70a42f94031..091fba7283e1 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -170,6 +170,8 @@ extern void __printk_safe_exit(void); #define printk_deferred_enter __printk_safe_enter #define printk_deferred_exit __printk_safe_exit +extern bool pr_flush(int timeout_ms, bool reset_on_progress); + /* * Please don't use printk_ratelimit(), because it shares ratelimiting state * with all other unrelated printk_ratelimit() callsites. Instead use @@ -220,6 +222,11 @@ static inline void printk_deferred_exit(void) { } +static inline bool pr_flush(int timeout_ms, bool reset_on_progress) +{ + return true; +} + static inline int printk_ratelimit(void) { return 0; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index dec5355c5b5b..a06999d55278 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2296,6 +2296,8 @@ asmlinkage __visible int _printk(const char *fmt, ...) } EXPORT_SYMBOL(_printk); +static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress); + #else /* CONFIG_PRINTK */ #define CONSOLE_LOG_MAX 0 @@ -2328,6 +2330,7 @@ static void call_console_driver(struct console *con, const char *text, size_t le { } static bool suppress_message_printing(int level) { return false; } +static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) { return true; } #endif /* CONFIG_PRINTK */ @@ -2515,6 +2518,7 @@ void suspend_console(void) if (!console_suspend_enabled) return; pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); + pr_flush(1000, true); console_lock(); console_suspended = 1; up_console_sem(); @@ -2527,6 +2531,7 @@ void resume_console(void) down_console_sem(); console_suspended = 0; console_unlock(); + pr_flush(1000, true); } /** @@ -2912,6 +2917,9 @@ void console_unblank(void) if ((c->flags & CON_ENABLED) && c->unblank) c->unblank(); console_unlock(); + + if (!oops_in_progress) + pr_flush(1000, true); } /** @@ -2970,6 +2978,7 @@ struct tty_driver *console_device(int *index) */ void console_stop(struct console *console) { + __pr_flush(console, 1000, true); console_lock(); console->flags &= ~CON_ENABLED; console_unlock(); @@ -2981,6 +2990,7 @@ void console_start(struct console *console) console_lock(); console->flags |= CON_ENABLED; console_unlock(); + __pr_flush(console, 1000, true); } EXPORT_SYMBOL(console_start); @@ -3352,6 +3362,79 @@ static int __init printk_late_init(void) late_initcall(printk_late_init); #if defined CONFIG_PRINTK +/* If @con is specified, only wait for that console. Otherwise wait for all. */ +static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progress) +{ + int remaining = timeout_ms; + struct console *c; + u64 last_diff = 0; + u64 printk_seq; + u64 diff; + u64 seq; + + might_sleep(); + + seq = prb_next_seq(prb); + + for (;;) { + diff = 0; + + console_lock(); + for_each_console(c) { + if (con && con != c) + continue; + if (!console_is_usable(c)) + continue; + printk_seq = c->seq; + if (printk_seq < seq) + diff += seq - printk_seq; + } + console_unlock(); + + if (diff != last_diff && reset_on_progress) + remaining = timeout_ms; + + if (diff == 0 || remaining == 0) + break; + + if (remaining < 0) { + /* no timeout limit */ + msleep(100); + } else if (remaining < 100) { + msleep(remaining); + remaining = 0; + } else { + msleep(100); + remaining -= 100; + } + + last_diff = diff; + } + + return (diff == 0); +} + +/** + * pr_flush() - Wait for printing threads to catch up. + * + * @timeout_ms: The maximum time (in ms) to wait. + * @reset_on_progress: Reset the timeout if forward progress is seen. + * + * A value of 0 for @timeout_ms means no waiting will occur. A value of -1 + * represents infinite waiting. + * + * If @reset_on_progress is true, the timeout will be reset whenever any + * printer has been seen to make some forward progress. + * + * Context: Process context. May sleep while acquiring console lock. + * Return: true if all enabled printers are caught up. + */ +bool pr_flush(int timeout_ms, bool reset_on_progress) +{ + return __pr_flush(NULL, timeout_ms, reset_on_progress); +} +EXPORT_SYMBOL(pr_flush); + /* * Delayed printk version, for scheduler-internal messages: */ -- 2.30.2