Received: by 2002:ab2:60d1:0:b0:1f7:5705:b850 with SMTP id i17csp1426287lqm; Thu, 2 May 2024 14:43:03 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCWBThzA0/c63h3cy8k6XrcoOC7ipLS//0dHRabytT7w/lAo9anGnPdOTce9l8/zWH9jtF/I691U7TrIX6BmydyXAgWQOKuIFe+8zPChUw== X-Google-Smtp-Source: AGHT+IEW2cG6tGGvmVb5kV0+izK3PqwSvlWvcGSI7g7wxEElHoiMu5M8CGS578FXF0hqeMAqqFMg X-Received: by 2002:a81:a143:0:b0:61b:3402:805f with SMTP id y64-20020a81a143000000b0061b3402805fmr843465ywg.10.1714686182993; Thu, 02 May 2024 14:43:02 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714686182; cv=pass; d=google.com; s=arc-20160816; b=RU6A59WVCogailERHp+rXBV2WrorOx2nnY8GAmYRSJxOSF1G24G2zSeb7PX/psdk8T Fmwj+bW8M1Psu5hzJsF8oxYBiNPqWIQLu4U2hd9Was/RTgGjjwis+Zk0Sha9/Ipn5z7W 6pTOXJMArSpFy5MC4vqjKItB5XRLf/1Ej8rBhF56ixgphvYeOadXeUUnzgwIPMuok69A dmV3+biFRTzVrOtzHNbZNUp4otvlUpFga1JUtjxEqj6UdznbN6C4o7xeOoqoCw2nr0iu a/V6vbs1pJq4kzG1ZFm+o5SGUKGA+mtTF8/PUg0Ig252FcuNsqSMWdBvyrSkj7u+pVRv rVEQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=+6sRFHbv8z6woNAyIM/Oyrbcgnr8C2ODc2QwVRv1j5w=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=jZ4o0hDw//jnoGcDx/bwWhZK+E0it/+ZUGHIlERxnkHcpWX7apSrAC996yA0np3C7T 5a9V/H/Zj+DounacPdpebjvlvieXVkjDWdV0LAutgVSiuV//ep/jB0XJJLNiTEdssfyl N0AjhSNWWOxPV7EhP5mQhTTJ/wm2crpiThODKMW4Z128hltAcPE38SREjOrODgUF4SSb FdHcKbYrh4EA4dwfB2IDH9P1IuMXYwUb//MQ1gdMUBuj7TtpLw42RFI7aL7BrdqW/qLe rHm4vhlW1BQ5I/Z2VJ/lfslkAxD+BZ61cl6qLpVJuMLSS4BYat9JVug4lUayfFK3fdP2 VKhg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=McMfiB04; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-166975-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166975-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id i15-20020ac85e4f000000b00434e29e93c6si1924065qtx.519.2024.05.02.14.43.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 02 May 2024 14:43:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-166975-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=McMfiB04; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-166975-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-166975-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id A4FC51C21C94 for ; Thu, 2 May 2024 21:43:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E9D1920FA8F; Thu, 2 May 2024 21:38:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="McMfiB04"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="AlqHHyq0" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D97EC1C9EA4 for ; Thu, 2 May 2024 21:38:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685937; cv=none; b=B+JTCZd+sQgMfWaycxo64+D1J7NllGKWYUIf41VkfNAFeCchZZGOWRZSDJzOzbgDX/NJ11IztYj1tn+HP9S5BLw5lYjk9EbIAURQeGzwx4wndkVcbw9A/V0k6MnqiXXQbIzoo/6aJugl/E4sVPB+Kajv/MIgRIcEaPD1pr6yA/g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714685937; c=relaxed/simple; bh=aFuXsM0PQjxnhe9BJyWGcDKlPrKO7Y8pNeIWcZD7jCM=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=oOuRFUp9m2WvZSDcYayLTxEaVIsOWSXStlYFT1UScbpkVZFf1duTQpCNbLoAhRHJ9itqz0UUQQUGS3UDD08fXPTLzE84yMGeQ/PBpgzeTwBp/ZdOQ5ZhMkUd8TGnOf0i7uuLl/rjXa9zDY/TE6x4LMj8xTz7SsunzB9hy/hQ0gY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=McMfiB04; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=AlqHHyq0; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1714685933; 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=+6sRFHbv8z6woNAyIM/Oyrbcgnr8C2ODc2QwVRv1j5w=; b=McMfiB04rn24IsAutxYT1GsJAzE3mzo3MXpqVQ85WBPXUaOAs9xiJmaq1cYu5CYOovtp0e 7QmBZRLuM7k+ZHDsXhmdQf8yak3u/U4rs7zsvri7FwN00GjUuoETe+uFA4fuQ4zPu6YBMd 7JrQx8d8wCUjWQpoyd7f7WYLGHudroR514Sxcfr+wvjnlA0LmzqGXpmJvVJOLlSteS1N8k f8aOi0pTKlBpHsCyA9HRfYiMoeqExMDDrWcoKRX1pM/fgNt6CgUCDk/RIrgmtEuVBT+Emy r8YnML6r+Iwf79KSAPZ3aIqs678NaEmhyRPKP2YMFlRraTFvDXA01JL42g6KWg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1714685933; 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=+6sRFHbv8z6woNAyIM/Oyrbcgnr8C2ODc2QwVRv1j5w=; b=AlqHHyq0TxBaYm71rt5dUQMlms4BRaHq6uqSG6hVzQ78vp/3ONJoVwZ2nV7AMDM+YwtVIg rOYaOeTe9CHGHJCA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v5 23/30] printk: Avoid console_lock dance if no legacy or boot consoles Date: Thu, 2 May 2024 23:44:32 +0206 Message-Id: <20240502213839.376636-24-john.ogness@linutronix.de> In-Reply-To: <20240502213839.376636-1-john.ogness@linutronix.de> References: <20240502213839.376636-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Currently the console lock is used to attempt legacy-type printing even if there are no legacy or boot consoles registered. If no such consoles are registered, the console lock does not need to be taken. Add tracking of legacy console registration and use it with boot console tracking to avoid unnecessary code paths, i.e. do not use the console lock if there are no boot consoles and no legacy consoles. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 78 ++++++++++++++++++++++++++++++++++-------- 1 file changed, 63 insertions(+), 15 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d17b0e057e22..0cb6ae5fd7e3 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -463,6 +463,13 @@ static int console_msg_format = MSG_FORMAT_DEFAULT; /* syslog_lock protects syslog_* variables and write access to clear_seq. */ static DEFINE_MUTEX(syslog_lock); +/* + * Specifies if a legacy console is registered. If legacy consoles are + * present, it is necessary to perform the console lock/unlock dance + * whenever console flushing should occur. + */ +static bool have_legacy_console; + /* * Specifies if a boot console is registered. If boot consoles are present, * nbcon consoles cannot print simultaneously and must be synchronized by @@ -471,6 +478,14 @@ static DEFINE_MUTEX(syslog_lock); */ static bool have_boot_console; +/* + * Specifies if the console lock/unlock dance is needed for console + * printing. If @have_boot_console is true, the nbcon consoles will + * be printed serially along with the legacy consoles because nbcon + * consoles cannot print simultaneously with boot consoles. + */ +#define printing_via_unlock (have_legacy_console || have_boot_console) + #ifdef CONFIG_PRINTK DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ @@ -2339,7 +2354,7 @@ asmlinkage int vprintk_emit(int facility, int level, printed_len = vprintk_store(facility, level, dev_info, fmt, args); /* If called from the scheduler, we can not call up(). */ - if (!in_sched) { + if (!in_sched && printing_via_unlock) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2648,7 +2663,7 @@ void resume_console(void) */ static int console_cpu_notify(unsigned int cpu) { - if (!cpuhp_tasks_frozen) { + if (!cpuhp_tasks_frozen && printing_via_unlock) { /* If trylock fails, someone else is doing the printing */ if (console_trylock()) console_unlock(); @@ -3189,7 +3204,8 @@ void console_flush_on_panic(enum con_flush_mode mode) nbcon_atomic_flush_pending(); - console_flush_all(false, &next_seq, &handover); + if (printing_via_unlock) + console_flush_all(false, &next_seq, &handover); } /* @@ -3523,6 +3539,7 @@ void register_console(struct console *newcon) if (newcon->flags & CON_NBCON) { nbcon_init(newcon, init_seq); } else { + have_legacy_console = true; newcon->seq = init_seq; } @@ -3598,6 +3615,7 @@ EXPORT_SYMBOL(register_console); /* Must be called under console_list_lock(). */ static int unregister_console_locked(struct console *console) { + bool found_legacy_con = false; bool found_boot_con = false; unsigned long flags; struct console *c; @@ -3665,9 +3683,13 @@ static int unregister_console_locked(struct console *console) for_each_console(c) { if (c->flags & CON_BOOT) found_boot_con = true; + if (!(c->flags & CON_NBCON)) + found_legacy_con = true; } if (!found_boot_con) have_boot_console = found_boot_con; + if (!found_legacy_con) + have_legacy_console = found_legacy_con; return res; } @@ -3828,22 +3850,34 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre seq = prb_next_reserve_seq(prb); /* Flush the consoles so that records up to @seq are printed. */ - console_lock(); - console_unlock(); + if (printing_via_unlock) { + console_lock(); + console_unlock(); + } for (;;) { unsigned long begin_jiffies; unsigned long slept_jiffies; - - diff = 0; + bool use_console_lock = printing_via_unlock; /* - * Hold the console_lock to guarantee safe access to - * console->seq. Releasing console_lock flushes more - * records in case @seq is still not printed on all - * usable consoles. + * Ensure the compiler does not optimize @use_console_lock to + * be @printing_via_unlock since the latter can change at any + * time. */ - console_lock(); + barrier(); + + diff = 0; + + if (use_console_lock) { + /* + * Hold the console_lock to guarantee safe access to + * console->seq. Releasing console_lock flushes more + * records in case @seq is still not printed on all + * usable consoles. + */ + console_lock(); + } cookie = console_srcu_read_lock(); for_each_console_srcu(c) { @@ -3863,6 +3897,7 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre if (flags & CON_NBCON) { printk_seq = nbcon_seq_read(c); } else { + WARN_ON_ONCE(!use_console_lock); printk_seq = c->seq; } @@ -3874,7 +3909,8 @@ static bool __pr_flush(struct console *con, int timeout_ms, bool reset_on_progre if (diff != last_diff && reset_on_progress) remaining_jiffies = timeout_jiffies; - console_unlock(); + if (use_console_lock) + console_unlock(); /* Note: @diff is 0 if there are no usable consoles. */ if (diff == 0 || remaining_jiffies == 0) @@ -3944,6 +3980,7 @@ static void __wake_up_klogd(int val) return; preempt_disable(); + /* * Guarantee any new records can be seen by tasks preparing to wait * before this context checks if the wait queue is empty. @@ -3955,11 +3992,22 @@ static void __wake_up_klogd(int val) * * This pairs with devkmsg_read:A and syslog_print:A. */ - if (wq_has_sleeper(&log_wait) || /* LMM(__wake_up_klogd:A) */ - (val & PRINTK_PENDING_OUTPUT)) { + if (!wq_has_sleeper(&log_wait)) /* LMM(__wake_up_klogd:A) */ + val &= ~PRINTK_PENDING_WAKEUP; + + /* + * Simple read is safe. register_console() would flush a newly + * registered legacy console when writing the message about it + * being enabled. + */ + if (!printing_via_unlock) + val &= ~PRINTK_PENDING_OUTPUT; + + if (val) { this_cpu_or(printk_pending, val); irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); } + preempt_enable(); } -- 2.39.2