Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2402656rdf; Mon, 6 Nov 2023 13:09:17 -0800 (PST) X-Google-Smtp-Source: AGHT+IGFvjifKFDw620JmidkzCOxcmWkh5GhFBHSAGIsYqCK6shIe7d7ZKCNlfkhbdq1k3ybcQCw X-Received: by 2002:a05:6a00:1883:b0:6bd:d884:df00 with SMTP id x3-20020a056a00188300b006bdd884df00mr34278276pfh.9.1699304957359; Mon, 06 Nov 2023 13:09:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699304957; cv=none; d=google.com; s=arc-20160816; b=BydtRuZKDJxVWcTjQYSkkM6egh/4axAeqq5lzVTcwn4QJYjw5D3GhkpTZD4qnmbnYS ny026Jj38Q1yNFAAKFySJRxpgTaxfjUtVR21RJ87I3gPFbxW566rWK+gidV+xhu4EeOX 1ambMNKxiIg8Y+8AtcOprv8T2bshHHA85I5dLI18oCmotB6W6vLFtgo1UBTZNrtZdQ0c MZ/wN/Ygp8jDCiwpqi85r0wZs96h9or8YZaTRm9PccAKkn+Mb5d8pMCfG7dZfcU4cwiE D+O20/yk0QLuyO4/xDi7OraICG48X1j+D4y7AzmKZ/onQW925NKlMG0RaVT73xqEqUpo nhKA== 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=W3IXvUuXJ9pnznvJZ8clgdIjVGMNk75/C1eUuuTRsrs=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=QjC+nLn3eBxCTlbqGa6Ghl1VtvoPcA5qU7pSH4CBM/ytibggaITl0ZV4CLIvUvpO88 NnST25FnednnWS1IpHiGMdRHVRjOmRysKpyLTiqECGSzPhx96bi6dsyn4LzhgSwouqJU CHkh/s3etlWC11FIKdC2yWxbIQMXOZ18BnwvjXqY+LIDeMzqk0YrdTgx8t2wZT3Id96V wTPb3g9UKIwjTmkte5HiYs3iG+/K+FsOwShtIWa6uFgkETbM61lOiWUAy62a+Ds/RbqR 7ad+WovCB0u9o4a1pJXXhWf9q0lqANOrG8Khd6Dmli7/cyuLCOthpoB6tiLcB643Oyx8 IqfA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hudJ+tTq; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 groat.vger.email (groat.vger.email. [23.128.96.35]) by mx.google.com with ESMTPS id s17-20020a056a0008d100b006be264b2734si9391468pfu.29.2023.11.06.13.09.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 13:09:17 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 as permitted sender) client-ip=23.128.96.35; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=hudJ+tTq; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.35 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 (depot.vger.email [IPv6:2620:137:e000::3:0]) by groat.vger.email (Postfix) with ESMTP id BFDD08048D84; Mon, 6 Nov 2023 13:09:08 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at groat.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233178AbjKFVHy (ORCPT + 99 others); Mon, 6 Nov 2023 16:07:54 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233111AbjKFVHk (ORCPT ); Mon, 6 Nov 2023 16:07:40 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 36C09D51 for ; Mon, 6 Nov 2023 13:07:38 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1699304855; 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=W3IXvUuXJ9pnznvJZ8clgdIjVGMNk75/C1eUuuTRsrs=; b=hudJ+tTqn5YPwaMErxVwIfT/yrE7Dn2MSwLeYnJsOMmetVf6LDrr8WCcqaxk6UkKagtcbe aCMNzerz9ohd70fVZAY4jftufPp+4B5fwQllqUxfmMPPVwBCuKwwRpm/57s7uCevGCX5T9 K+y4KZQvcbucKwB6WSgmwI5GNnSngkSVAbkBlRTNZnVQ8swSLVEYYLnzNZUHHq3NhBbRVO 1918m9QZsvgZg9F8l/S9DUnpx9nMBVdaD0h/ORFw6WIKqyxpG0qZ+4x5SgqSjGDpxe1kD3 UEOQeXkEhefuiuyRLtZiRdPplRdp+EXQzGO7XPoRfWt3LRMAhygWL9hqkcM7cA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1699304855; 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=W3IXvUuXJ9pnznvJZ8clgdIjVGMNk75/C1eUuuTRsrs=; b=LXqdXzlKnXhpsVg5KAQF+V3ELg6J3Er0v/dgs1pFYn6MvYNwFoi5VvAh/p2ZD8+mm1jBgA hN8+QAI+pn1msFBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v2 8/9] printk: Disable passing console lock owner completely during panic() Date: Mon, 6 Nov 2023 22:13:29 +0106 Message-Id: <20231106210730.115192-9-john.ogness@linutronix.de> In-Reply-To: <20231106210730.115192-1-john.ogness@linutronix.de> References: <20231106210730.115192-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-0.2 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INVALID_DATE_TZ_ABSURD, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on groat.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (groat.vger.email [0.0.0.0]); Mon, 06 Nov 2023 13:09:09 -0800 (PST) From: Petr Mladek The commit d51507098ff91 ("printk: disable optimistic spin during panic") added checks to avoid becoming a console waiter if a panic is in progress. However, the transition to panic can occur while there is already a waiter. The current owner should not pass the lock to the waiter because it might get stopped or blocked anytime. Also the panic context might pass the console lock owner to an already stopped waiter by mistake. It might happen when console_flush_on_panic() ignores the current lock owner, for example: CPU0 CPU1 ---- ---- console_lock_spinning_enable() console_trylock_spinning() [CPU1 now console waiter] NMI: panic() panic_other_cpus_shutdown() [stopped as console waiter] console_flush_on_panic() console_lock_spinning_enable() [print 1 record] console_lock_spinning_disable_and_check() [handover to stopped CPU1] This results in panic() not flushing the panic messages. Fix these problems by disabling all spinning operations completely during panic(). Another advantage is that it prevents possible deadlocks caused by "console_owner_lock". The panic() context does not need to take it any longer. The lockless checks are safe because the functions become NOPs when they see the panic in progress. All operations manipulating the state are still synchronized by the lock even when non-panic CPUs would notice the panic synchronously. The current owner might stay spinning. But non-panic() CPUs would get stopped anyway and the panic context will never start spinning. Fixes: dbdda842fe96 ("printk: Add console owner and waiter logic to load balance console writes") Signed-off-by: Petr Mladek Reviewed-by: John Ogness --- kernel/printk/printk.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index f3a7f5a6f6f8..cb99c854a648 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1869,10 +1869,23 @@ static bool console_waiter; */ static void console_lock_spinning_enable(void) { + /* + * Do not use spinning in panic(). The panic CPU wants to keep the lock. + * Non-panic CPUs abandon the flush anyway. + * + * Just keep the lockdep annotation. The panic-CPU should avoid + * taking console_owner_lock because it might cause a deadlock. + * This looks like the easiest way how to prevent false lockdep + * reports without handling races a lockless way. + */ + if (panic_in_progress()) + goto lockdep; + raw_spin_lock(&console_owner_lock); console_owner = current; raw_spin_unlock(&console_owner_lock); +lockdep: /* The waiter may spin on us after setting console_owner */ spin_acquire(&console_owner_dep_map, 0, 0, _THIS_IP_); } @@ -1897,6 +1910,22 @@ static int console_lock_spinning_disable_and_check(int cookie) { int waiter; + /* + * Ignore spinning waiters during panic() because they might get stopped + * or blocked at any time, + * + * It is safe because nobody is allowed to start spinning during panic + * in the first place. If there has been a waiter then non panic CPUs + * might stay spinning. They would get stopped anyway. The panic context + * will never start spinning and an interrupted spin on panic CPU will + * never continue. + */ + if (panic_in_progress()) { + /* Keep lockdep happy. */ + spin_release(&console_owner_dep_map, _THIS_IP_); + return 0; + } + raw_spin_lock(&console_owner_lock); waiter = READ_ONCE(console_waiter); console_owner = NULL; -- 2.39.2