From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 45A7BC433FE for ; Wed, 30 Mar 2022 11:49:21 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344130AbiC3LvD (ORCPT ); Wed, 30 Mar 2022 07:51:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59174 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344042AbiC3LuO (ORCPT ); Wed, 30 Mar 2022 07:50:14 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92A7E26E557; Wed, 30 Mar 2022 04:47:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id C4437615F3; Wed, 30 Mar 2022 11:47:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 6B869C34112; Wed, 30 Mar 2022 11:47:35 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1648640856; bh=oYDI8w8KmL+3q46IW8SiSGl3fitEO8H7Wb6JZeU7lG8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=K4sQJUgLQ17WLkS1ij6BpKnLW/uLgYYg5kp2j5CPFy/4ovTRGmaJf0+3+qQ4azAGc 123XoSGcuUgZvvmq8IRnzueybLbR5aqEIJ79uUQGtXj55IE4BKyQCrc07DJZO73Y9E MhYOMSoM8nY3g46Z+Cf89IUi6NA+gJ9aikcgsg+9JUj6VLFFPDqm28NoZYSsnH6OZ0 zynL2SeeFw073KonJDwwkWP5qk894NosSPuSNxK13GmRbyV/Kzj/Y5gGwZpQmKjYKA IQv/HSi+sWbkR8QhUZP/dFCxuzzx2nl/ouebP5bZ8mGUsTR7SMK+MoUUXYBdyDsncI wCp+U4LopZCMA== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Stephen Brennan , Petr Mladek , Sergey Senozhatsky , Sasha Levin , gregkh@linuxfoundation.org Subject: [PATCH AUTOSEL 5.17 32/66] printk: Avoid livelock with heavy printk during panic Date: Wed, 30 Mar 2022 07:46:11 -0400 Message-Id: <20220330114646.1669334-32-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220330114646.1669334-1-sashal@kernel.org> References: <20220330114646.1669334-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Stephen Brennan [ Upstream commit 13fb0f74d7029df3b8137f11ef955e578a4a4a60 ] During panic(), if another CPU is writing heavily the kernel log (e.g. via /dev/kmsg), then the panic CPU may livelock writing out its messages to the console. Note when too many messages are dropped during panic and suppress further printk, except from the panic CPU. This could result in some important messages being dropped. However, messages are already being dropped, so this approach at least prevents a livelock. Reviewed-by: Petr Mladek Signed-off-by: Stephen Brennan Reviewed-by: Sergey Senozhatsky Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20220202171821.179394-4-stephen.s.brennan@oracle.com Signed-off-by: Sasha Levin --- kernel/printk/printk.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 0f8918f5f22a..4f31290256ad 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -93,6 +93,12 @@ EXPORT_SYMBOL_GPL(console_drivers); */ int __read_mostly suppress_printk; +/* + * During panic, heavy printk by other CPUs can delay the + * panic and risk deadlock on console resources. + */ +int __read_mostly suppress_panic_printk; + #ifdef CONFIG_LOCKDEP static struct lockdep_map console_lock_dep_map = { .name = "console_lock" @@ -2223,6 +2229,10 @@ asmlinkage int vprintk_emit(int facility, int level, if (unlikely(suppress_printk)) return 0; + if (unlikely(suppress_panic_printk) && + atomic_read(&panic_cpu) != raw_smp_processor_id()) + return 0; + if (level == LOGLEVEL_SCHED) { level = LOGLEVEL_DEFAULT; in_sched = true; @@ -2608,6 +2618,7 @@ void console_unlock(void) { static char ext_text[CONSOLE_EXT_LOG_MAX]; static char text[CONSOLE_LOG_MAX]; + static int panic_console_dropped; unsigned long flags; bool do_cond_resched, retry; struct printk_info info; @@ -2662,6 +2673,10 @@ void console_unlock(void) if (console_seq != r.info->seq) { console_dropped += r.info->seq - console_seq; console_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"); + } } if (suppress_message_printing(r.info->level)) { -- 2.34.1