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 96DBDC3A589 for ; Wed, 30 Mar 2022 12:06:55 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1345869AbiC3MEX (ORCPT ); Wed, 30 Mar 2022 08:04:23 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35806 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345323AbiC3LyX (ORCPT ); Wed, 30 Mar 2022 07:54:23 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2D2341116D; Wed, 30 Mar 2022 04:50:56 -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 ams.source.kernel.org (Postfix) with ESMTPS id 71D45B81ACC; Wed, 30 Mar 2022 11:50:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 67666C36AE3; Wed, 30 Mar 2022 11:50:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1648641053; bh=G22sE1NkGEgwqMcjpTZycFRhDcAIrBpWYxYkC+AtI0U=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UnRKIJ775NWcVvof4M+mnTCxLwagLdMtNGpw8FvpfqtVGArcwE+oX0ftyIo5CSA5+ fAjNOsjr+dAgIKiN4q90Q6N8fZNPzbynH6EjMud+yvtAuxWYpZ9lp2E0ECSpuYszeD ud+27ueq1yROFRLLMdG7/c+uyGbmMoHSj8Ydvf3vZ7xjqL2KxMQApxrSlbfskXIhdF 4OTYiFJfYsNilnBEQrURBbQbX3CpVuNLA599iNmE/D68yvuOpPYDz3Mxm0YSe/d+ZD EY9SRZSrDKmtSXVOMckGxmUHpnHp6+yXJhYf/2lkRwDatd3eJ6e75gPV5we+BM2U6n DxXV9XNqLH2jg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Stephen Brennan , Petr Mladek , Sergey Senozhatsky , Sasha Levin , linux@roeck-us.net, gregkh@linuxfoundation.org Subject: [PATCH AUTOSEL 5.15 31/50] printk: Drop console_sem during panic Date: Wed, 30 Mar 2022 07:49:45 -0400 Message-Id: <20220330115005.1671090-31-sashal@kernel.org> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20220330115005.1671090-1-sashal@kernel.org> References: <20220330115005.1671090-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 8ebc476fd51e6c0fd3174ec1959a20ba99d4c5e5 ] If another CPU is in panic, we are about to be halted. Try to gracefully abandon the console_sem, leaving it free for the panic CPU to grab. Suggested-by: Petr Mladek Signed-off-by: Stephen Brennan Reviewed-by: Petr Mladek Reviewed-by: Sergey Senozhatsky Signed-off-by: Petr Mladek Link: https://lore.kernel.org/r/20220202171821.179394-5-stephen.s.brennan@oracle.com Signed-off-by: Sasha Levin --- kernel/printk/printk.c | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index f37889b6cf9f..34bdc51f4cb3 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2587,6 +2587,25 @@ static int have_callable_console(void) return 0; } +/* + * Return true when this CPU should unlock console_sem without pushing all + * messages to the console. This reduces the chance that the console is + * locked when the panic CPU tries to use it. + */ +static bool abandon_console_lock_in_panic(void) +{ + if (!panic_in_progress()) + return false; + + /* + * We can use raw_smp_processor_id() here because it is impossible for + * the task to be migrated to the panic_cpu, or away from it. If + * panic_cpu has already been set, and we're not currently executing on + * that CPU, then we never will be. + */ + return atomic_read(&panic_cpu) != raw_smp_processor_id(); +} + /* * Can we actually use the console at this time on this cpu? * @@ -2735,6 +2754,10 @@ void console_unlock(void) if (handover) return; + /* Allow panic_cpu to take over the consoles safely */ + if (abandon_console_lock_in_panic()) + break; + if (do_cond_resched) cond_resched(); } @@ -2752,7 +2775,7 @@ void console_unlock(void) * flush, no worries. */ retry = prb_read_valid(prb, next_seq, NULL); - if (retry && console_trylock()) + if (retry && !abandon_console_lock_in_panic() && console_trylock()) goto again; } EXPORT_SYMBOL(console_unlock); -- 2.34.1