Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp5991378rwp; Mon, 17 Jul 2023 12:55:58 -0700 (PDT) X-Google-Smtp-Source: APBJJlH2+5FX41tFOMnMdfSrqX8DHbTUvGByL8tQvNQRFaCdKUi8UvoFgCQyrPlcvGGAZAFyK8ML X-Received: by 2002:a05:6a20:a10f:b0:133:5da8:2fa7 with SMTP id q15-20020a056a20a10f00b001335da82fa7mr13039853pzk.25.1689623757694; Mon, 17 Jul 2023 12:55:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689623757; cv=none; d=google.com; s=arc-20160816; b=JmLA3Tu3xOSG2Z/tlo06utdsRl+KVtq9TEsFZL7VQVj+fiSjIbrP/ywVIiBnuDmqxk 7z8dcXk0bauZoSGg7TgPTtuU/WB38eeAC0ice6IxEZc/4lnMNQB2t58JsBwlEfPOwZmv BEPYTY28SKb7rz2GnoxyxS+0XkrseFp+vSgaWxM2Fxprqxh6ly1SR8NqXo0MiH6fY9uO mkJ3j46SstP6rJW/TFuP5DyMZyXDV5CpkIaIYp7Ba1ITrfEzRqMQnoPnWh+j/H5UR9VK /daNP/fkjuBsqqlLEDIH5fWH/ftLeTww64P/0z+JHfs2Pz4CnJNImSyKmLVTR/bpgQLN C1wg== 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=ebq6l34l2HQ4NpuglsKT6EmyQrUsETfJZCfMOAes7xM=; fh=HVRBJStvoKWbFco6Jta18xe5UAVCwWVs9OvJstciom4=; b=DU311h/d+30UK2QrrSMXpCWKeFJD7TW3b60kT+elWdMFPeFAbL+CE9XAvsoACtEkoF HFl2dGBj0EdspjPpJrHfmP69NnWNp/GAugURcseyITcmMDbFTF66i/VVsoCX7wOV0lxM 1cLNOaMaFTja5sTzPGggUYIJYo4tbOgwTknJs1/F3KEcWu9IQ/zH9IQeMvBvQzN5Wdmb STzzjMolEktb2dKprBciMFAHsvQVeBkz/sNAPSZlprb9L2tWyxS6nBZuuzU9KpLLiWIH D6LkNT78Aw+wjx7Y87lEuA3oj/i8PIEZFOu227nPXAHTGhEruOHeKm1p8OL8ntan/lq8 qrbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=h2Phg3vq; 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 q83-20020a632a56000000b0055793097dbesi284400pgq.469.2023.07.17.12.55.45; Mon, 17 Jul 2023 12:55:57 -0700 (PDT) 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=h2Phg3vq; 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 S231273AbjGQTqU (ORCPT + 99 others); Mon, 17 Jul 2023 15:46:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41532 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230139AbjGQTqN (ORCPT ); Mon, 17 Jul 2023 15:46:13 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 83738134 for ; Mon, 17 Jul 2023 12:46:12 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1689623171; 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=ebq6l34l2HQ4NpuglsKT6EmyQrUsETfJZCfMOAes7xM=; b=h2Phg3vq9RX6pbtYd4TTqsothRQPA9cc8OMkVJvDNv6uVhzae5XPjK+j36HSgHgdDcVbqX +Nw5zHr++J17VgrVcj+UOJZtQ/0sKuPbkA7SAZwEbRxp2+Ns9ymjmo8uj6Kva+JkAfjxGm 5NdT5www9+FFuGh4qLmcagc89ij/rm8xFnRZuDWpQbonxdnIxJIbqR0fOT34vUPGtrTQcZ kT7jKuqR3jochM9fqOBtX0VOKBMULGyiYCRUlGvNe43NEoYjqca7RQXbm8Q5MQe16S1KiB SCZjYGV0V7fVIzJBOZ1hV0+wlrKjzj29nMfVxg0ZigmdRtPy6lRFP6pOpalsgw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1689623171; 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=ebq6l34l2HQ4NpuglsKT6EmyQrUsETfJZCfMOAes7xM=; b=jVReI8sdiuzF8KIatWI0DIlhvH1JYFe3Vod7rkgII32uVQ4VfjYCzXDuD0uC65i+ahX0vC PQn2VAozBSXtCvBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v3 3/7] printk: Keep non-panic-CPUs out of console lock Date: Mon, 17 Jul 2023 21:52:03 +0206 Message-Id: <20230717194607.145135-4-john.ogness@linutronix.de> In-Reply-To: <20230717194607.145135-1-john.ogness@linutronix.de> References: <20230717194607.145135-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.6 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_BLOCKED,SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 When in a panic situation, non-panic CPUs should avoid holding the console lock so as not to contend with the panic CPU. This is already implemented with abandon_console_lock_in_panic(), which is checked after each printed line. However, non-panic CPUs should also avoid trying to acquire the console lock during a panic. Modify console_trylock() to fail and console_lock() to block() when called from a non-panic CPU during a panic. Signed-off-by: John Ogness --- kernel/printk/printk.c | 45 ++++++++++++++++++++++++------------------ 1 file changed, 26 insertions(+), 19 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 7aa9dbee12e8..7219991885e6 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2583,6 +2583,25 @@ static int console_cpu_notify(unsigned int cpu) 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(); +} + /** * console_lock - block the console subsystem from printing * @@ -2595,6 +2614,10 @@ void console_lock(void) { might_sleep(); + /* On panic, the console_lock must be left to the panic cpu. */ + while (abandon_console_lock_in_panic()) + msleep(1000); + down_console_sem(); if (console_suspended) return; @@ -2613,6 +2636,9 @@ EXPORT_SYMBOL(console_lock); */ int console_trylock(void) { + /* On panic, the console_lock must be left to the panic cpu. */ + if (abandon_console_lock_in_panic()) + return 0; if (down_trylock_console_sem()) return 0; if (console_suspended) { @@ -2631,25 +2657,6 @@ int is_console_locked(void) } EXPORT_SYMBOL(is_console_locked); -/* - * 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(); -} - /* * Check if the given console is currently capable and allowed to print * records. -- 2.30.2