From nobody Mon Jun 29 15:59:55 2026 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 6DADEC433FE for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240162AbiBGTo7 (ORCPT ); Mon, 7 Feb 2022 14:44:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59254 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240424AbiBGTnb (ORCPT ); Mon, 7 Feb 2022 14:43:31 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0ADDEC0401DA for ; Mon, 7 Feb 2022 11:43:30 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263005; 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=hz3UXucfv0ioQaqY71ZN2swXWDkXsVf8PhgiFykDXVQ=; b=jnmxtF99g+RncyfAOjYWY0YELRfAyffebVVoswd00n15/9gtp2OK+6bcJuxkfmCnYJQCm8 zfxNuWN/eM4bZ4d6bjC0QsYtjRr4ASkBbgwn44fnhW/R7pWComemqzwtsADRqKTv0bKRNb Z3MWHcBXyJUtmwQUn5TlBOTW59oiGkT4m294JLFi+BWZxlKB1fmdJ8CY2620tIB9plOEZ7 Z0sp6CzmEQMicp4yocMNduE2WfWxyww9kN3ge0BCvqFrUp13p5ryryFWwDJM75qC7V0JA4 ykPe22+CSheKoZJa9pAFK0Xy6O4DAkl5h7OtA7A7FKTOPqrOGwZPghp66jp4zg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263005; 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=hz3UXucfv0ioQaqY71ZN2swXWDkXsVf8PhgiFykDXVQ=; b=2BvUEXbMchw8vbWhjBWm4o3nYmakMLi1URZWQ+/T/cGXSf0IdJx61L80Kw5mu02xDGh/ze xDa3jCj1F4BWY2Ag== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Marco Elver , Stephen Boyd , Alexander Potapenko , Randy Dunlap , Nicholas Piggin Subject: [PATCH printk v1 01/13] printk: rename cpulock functions Date: Mon, 7 Feb 2022 20:49:11 +0106 Message-Id: <20220207194323.273637-2-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Since the printk cpulock is CPU-reentrant and since it is used in all contexts, its usage must be carefully considered and most likely will require programming locklessly. To avoid mistaking the printk cpulock as a typical lock, rename it to cpu_sync. The main functions then become: printk_cpu_sync_get_irqsave(flags); printk_cpu_sync_put_irqrestore(flags); Signed-off-by: John Ogness --- include/linux/printk.h | 42 ++++++++++++------------- kernel/printk/printk.c | 71 +++++++++++++++++++++--------------------- lib/dump_stack.c | 4 +-- lib/nmi_backtrace.c | 4 +-- 4 files changed, 61 insertions(+), 60 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 9497f6b98339..780408b29c13 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -281,43 +281,43 @@ static inline void printk_trigger_flush(void) #endif =20 #ifdef CONFIG_SMP -extern int __printk_cpu_trylock(void); -extern void __printk_wait_on_cpu_lock(void); -extern void __printk_cpu_unlock(void); +extern int __printk_cpu_sync_try_get(void); +extern void __printk_cpu_sync_wait(void); +extern void __printk_cpu_sync_put(void); =20 /** - * printk_cpu_lock_irqsave() - Acquire the printk cpu-reentrant spinning - * lock and disable interrupts. + * printk_cpu_sync_get_irqsave() - Acquire the printk cpu-reentrant spinni= ng + * lock and disable interrupts. * @flags: Stack-allocated storage for saving local interrupt state, - * to be passed to printk_cpu_unlock_irqrestore(). + * to be passed to printk_cpu_sync_put_irqrestore(). * * If the lock is owned by another CPU, spin until it becomes available. * Interrupts are restored while spinning. */ -#define printk_cpu_lock_irqsave(flags) \ - for (;;) { \ - local_irq_save(flags); \ - if (__printk_cpu_trylock()) \ - break; \ - local_irq_restore(flags); \ - __printk_wait_on_cpu_lock(); \ +#define printk_cpu_sync_get_irqsave(flags) \ + for (;;) { \ + local_irq_save(flags); \ + if (__printk_cpu_sync_try_get()) \ + break; \ + local_irq_restore(flags); \ + __printk_cpu_sync_wait(); \ } =20 /** - * printk_cpu_unlock_irqrestore() - Release the printk cpu-reentrant spinn= ing - * lock and restore interrupts. - * @flags: Caller's saved interrupt state, from printk_cpu_lock_irqsave(). + * printk_cpu_sync_put_irqrestore() - Release the printk cpu-reentrant spi= nning + * lock and restore interrupts. + * @flags: Caller's saved interrupt state, from printk_cpu_sync_get_irqsav= e(). */ -#define printk_cpu_unlock_irqrestore(flags) \ +#define printk_cpu_sync_put_irqrestore(flags) \ do { \ - __printk_cpu_unlock(); \ + __printk_cpu_sync_put(); \ local_irq_restore(flags); \ - } while (0) \ + } while (0) =20 #else =20 -#define printk_cpu_lock_irqsave(flags) ((void)flags) -#define printk_cpu_unlock_irqrestore(flags) ((void)flags) +#define printk_cpu_sync_get_irqsave(flags) ((void)flags) +#define printk_cpu_sync_put_irqrestore(flags) ((void)flags) =20 #endif /* CONFIG_SMP */ =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 155229f0cf0f..d1b773823d63 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3598,26 +3598,26 @@ EXPORT_SYMBOL_GPL(kmsg_dump_rewind); #endif =20 #ifdef CONFIG_SMP -static atomic_t printk_cpulock_owner =3D ATOMIC_INIT(-1); -static atomic_t printk_cpulock_nested =3D ATOMIC_INIT(0); +static atomic_t printk_cpu_sync_owner =3D ATOMIC_INIT(-1); +static atomic_t printk_cpu_sync_nested =3D ATOMIC_INIT(0); =20 /** - * __printk_wait_on_cpu_lock() - Busy wait until the printk cpu-reentrant - * spinning lock is not owned by any CPU. + * __printk_cpu_sync_wait() - Busy wait until the printk cpu-reentrant + * spinning lock is not owned by any CPU. * * Context: Any context. */ -void __printk_wait_on_cpu_lock(void) +void __printk_cpu_sync_wait(void) { do { cpu_relax(); - } while (atomic_read(&printk_cpulock_owner) !=3D -1); + } while (atomic_read(&printk_cpu_sync_owner) !=3D -1); } -EXPORT_SYMBOL(__printk_wait_on_cpu_lock); +EXPORT_SYMBOL(__printk_cpu_sync_wait); =20 /** - * __printk_cpu_trylock() - Try to acquire the printk cpu-reentrant - * spinning lock. + * __printk_cpu_sync_try_get() - Try to acquire the printk cpu-reentrant + * spinning lock. * * If no processor has the lock, the calling processor takes the lock and * becomes the owner. If the calling processor is already the owner of the @@ -3626,7 +3626,7 @@ EXPORT_SYMBOL(__printk_wait_on_cpu_lock); * Context: Any context. Expects interrupts to be disabled. * Return: 1 on success, otherwise 0. */ -int __printk_cpu_trylock(void) +int __printk_cpu_sync_try_get(void) { int cpu; int old; @@ -3636,79 +3636,80 @@ int __printk_cpu_trylock(void) /* * Guarantee loads and stores from this CPU when it is the lock owner * are _not_ visible to the previous lock owner. This pairs with - * __printk_cpu_unlock:B. + * __printk_cpu_sync_put:B. * * Memory barrier involvement: * - * If __printk_cpu_trylock:A reads from __printk_cpu_unlock:B, then - * __printk_cpu_unlock:A can never read from __printk_cpu_trylock:B. + * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B, + * then __printk_cpu_sync_put:A can never read from + * __printk_cpu_sync_try_get:B. * * Relies on: * - * RELEASE from __printk_cpu_unlock:A to __printk_cpu_unlock:B + * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B * of the previous CPU * matching - * ACQUIRE from __printk_cpu_trylock:A to __printk_cpu_trylock:B - * of this CPU + * ACQUIRE from __printk_cpu_sync_try_get:A to + * __printk_cpu_sync_try_get:B of this CPU */ - old =3D atomic_cmpxchg_acquire(&printk_cpulock_owner, -1, - cpu); /* LMM(__printk_cpu_trylock:A) */ + old =3D atomic_cmpxchg_acquire(&printk_cpu_sync_owner, -1, + cpu); /* LMM(__printk_cpu_sync_try_get:A) */ if (old =3D=3D -1) { /* * This CPU is now the owner and begins loading/storing - * data: LMM(__printk_cpu_trylock:B) + * data: LMM(__printk_cpu_sync_try_get:B) */ return 1; =20 } else if (old =3D=3D cpu) { /* This CPU is already the owner. */ - atomic_inc(&printk_cpulock_nested); + atomic_inc(&printk_cpu_sync_nested); return 1; } =20 return 0; } -EXPORT_SYMBOL(__printk_cpu_trylock); +EXPORT_SYMBOL(__printk_cpu_sync_try_get); =20 /** - * __printk_cpu_unlock() - Release the printk cpu-reentrant spinning lock. + * __printk_cpu_sync_put() - Release the printk cpu-reentrant spinning loc= k. * * The calling processor must be the owner of the lock. * * Context: Any context. Expects interrupts to be disabled. */ -void __printk_cpu_unlock(void) +void __printk_cpu_sync_put(void) { - if (atomic_read(&printk_cpulock_nested)) { - atomic_dec(&printk_cpulock_nested); + if (atomic_read(&printk_cpu_sync_nested)) { + atomic_dec(&printk_cpu_sync_nested); return; } =20 /* * This CPU is finished loading/storing data: - * LMM(__printk_cpu_unlock:A) + * LMM(__printk_cpu_sync_put:A) */ =20 /* * Guarantee loads and stores from this CPU when it was the * lock owner are visible to the next lock owner. This pairs - * with __printk_cpu_trylock:A. + * with __printk_cpu_sync_try_get:A. * * Memory barrier involvement: * - * If __printk_cpu_trylock:A reads from __printk_cpu_unlock:B, - * then __printk_cpu_trylock:B reads from __printk_cpu_unlock:A. + * If __printk_cpu_sync_try_get:A reads from __printk_cpu_sync_put:B, + * then __printk_cpu_sync_try_get:B reads from __printk_cpu_sync_put:A. * * Relies on: * - * RELEASE from __printk_cpu_unlock:A to __printk_cpu_unlock:B + * RELEASE from __printk_cpu_sync_put:A to __printk_cpu_sync_put:B * of this CPU * matching - * ACQUIRE from __printk_cpu_trylock:A to __printk_cpu_trylock:B - * of the next CPU + * ACQUIRE from __printk_cpu_sync_try_get:A to + * __printk_cpu_sync_try_get:B of the next CPU */ - atomic_set_release(&printk_cpulock_owner, - -1); /* LMM(__printk_cpu_unlock:B) */ + atomic_set_release(&printk_cpu_sync_owner, + -1); /* LMM(__printk_cpu_sync_put:B) */ } -EXPORT_SYMBOL(__printk_cpu_unlock); +EXPORT_SYMBOL(__printk_cpu_sync_put); #endif /* CONFIG_SMP */ diff --git a/lib/dump_stack.c b/lib/dump_stack.c index 6b7f1bf6715d..83471e81501a 100644 --- a/lib/dump_stack.c +++ b/lib/dump_stack.c @@ -102,9 +102,9 @@ asmlinkage __visible void dump_stack_lvl(const char *lo= g_lvl) * Permit this cpu to perform nested stack dumps while serialising * against other CPUs */ - printk_cpu_lock_irqsave(flags); + printk_cpu_sync_get_irqsave(flags); __dump_stack(log_lvl); - printk_cpu_unlock_irqrestore(flags); + printk_cpu_sync_put_irqrestore(flags); } EXPORT_SYMBOL(dump_stack_lvl); =20 diff --git a/lib/nmi_backtrace.c b/lib/nmi_backtrace.c index 199ab201d501..d01aec6ae15c 100644 --- a/lib/nmi_backtrace.c +++ b/lib/nmi_backtrace.c @@ -99,7 +99,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs) * Allow nested NMI backtraces while serializing * against other CPUs. */ - printk_cpu_lock_irqsave(flags); + printk_cpu_sync_get_irqsave(flags); if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointe= r(regs))) { pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n", cpu, (void *)instruction_pointer(regs)); @@ -110,7 +110,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs) else dump_stack(); } - printk_cpu_unlock_irqrestore(flags); + printk_cpu_sync_put_irqrestore(flags); cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); return true; } --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 2D918C41535 for ; Mon, 7 Feb 2022 19:46:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240906AbiBGTpS (ORCPT ); Mon, 7 Feb 2022 14:45:18 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59216 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239957AbiBGTn3 (ORCPT ); Mon, 7 Feb 2022 14:43:29 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8BD63C0401E4 for ; Mon, 7 Feb 2022 11:43:28 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263006; 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=YeoQK9icCZQHqxjTMXztaF3pNjld+IRlJPuqBw66sL4=; b=hxkII20lpD34X3CtBuHp0CCV19hqTdEWFnxIE5rW4TLI34jRq/J67CNh5e4+3zDXTBLSKX ZGvj+VV6gesck38MmRndRSBkyFNywpPs4q0oJpdExUBfdTKSpOGEdN52XBeRDmehsVKx+A n451bPzMFczXFfkN5j4OCPOXRgMJ1xv9CrQMIxejRmtoaMTYIuz+DHiC0wlc5VGxDlC7fs nUutZecadPcS0xzYsrZawVhhTontfdlHboTCl6zinMlYZTzb6CkP6iP6zHuRJzs7wfI6Af R42Sf06HJt/gS/xMEXFABJGYI7IEUwBBsqxBnfXKmXGxzu4kDpWYofX+kkzcoA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263006; 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=YeoQK9icCZQHqxjTMXztaF3pNjld+IRlJPuqBw66sL4=; b=nVSx6zvM/ianRuZFzFAUSAoY9g0kHDH5KgIleQqCyOf3LLx4Zlas+Cwvz+Wb0pPCQkZFHu sJpkZzB1vWIqg9CA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v1 02/13] printk: cpu sync always disable interrupts Date: Mon, 7 Feb 2022 20:49:12 +0106 Message-Id: <20220207194323.273637-3-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The CPU sync functions are a NOP for !CONFIG_SMP. But for !CONFIG_SMP they still need to disable interrupts in order to preserve context within the CPU sync sections. Signed-off-by: John Ogness Reviewed-by: Petr Mladek Reviewed-by: Sergey Senozhatsky --- include/linux/printk.h | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index 780408b29c13..cd8192e611a1 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -285,9 +285,16 @@ extern int __printk_cpu_sync_try_get(void); extern void __printk_cpu_sync_wait(void); extern void __printk_cpu_sync_put(void); =20 +#else + +#define __printk_cpu_sync_try_get() true +#define __printk_cpu_sync_wait() +#define __printk_cpu_sync_put() +#endif /* CONFIG_SMP */ + /** - * printk_cpu_sync_get_irqsave() - Acquire the printk cpu-reentrant spinni= ng - * lock and disable interrupts. + * printk_cpu_sync_get_irqsave() - Disable interrupts and acquire the prin= tk + * cpu-reentrant spinning lock. * @flags: Stack-allocated storage for saving local interrupt state, * to be passed to printk_cpu_sync_put_irqrestore(). * @@ -314,13 +321,6 @@ extern void __printk_cpu_sync_put(void); local_irq_restore(flags); \ } while (0) =20 -#else - -#define printk_cpu_sync_get_irqsave(flags) ((void)flags) -#define printk_cpu_sync_put_irqrestore(flags) ((void)flags) - -#endif /* CONFIG_SMP */ - extern int kptr_restrict; =20 /** --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 D5F4AC43219 for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240694AbiBGTpL (ORCPT ); Mon, 7 Feb 2022 14:45:11 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59206 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239915AbiBGTn3 (ORCPT ); Mon, 7 Feb 2022 14:43:29 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81297C0401DA for ; Mon, 7 Feb 2022 11:43:28 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263006; 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=m/cq4VzUVWEBg0IHZiL8z1jRPI1TZUzZwhqXEkTRhR4=; b=kJIVZ7eXjfCsw1hGcf4RnzGwUEkDZZJA847eIS6sqDXPBKLepIPub/Y9z+iAa72UNcmGPZ pErQAYArCGqCysUWHEM+Sw77R6GN8xURbcnMW8sZuEauIYE2JxlqO0Yvc+uSXESE3eGQSO /zn3WI8fhxb8RqfQ+AKHyCvSrV9yBaGzdhrXO7FWlft8+YS5WwgBee+pYN2P9moBGkPa3D fP/GbRKUy23SSCZXSkjflV9os+f/LIe0x538MCexanLtBjayLtLTRZRUrQ2yVEeOYnkkQB xbaMeq+HFwBqR7JqxUf4soH5RrmAd2VNQO0JNIzUZZJcBdEc1PV+f37Jl6mU5w== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263006; 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=m/cq4VzUVWEBg0IHZiL8z1jRPI1TZUzZwhqXEkTRhR4=; b=APjx/qn9rMyCp3tv+25yde5jq4b4OrPAAeuDDnU3GuuPZhcUgp1S67tMVxaJi85t5AAwLA O5PfUYCD2P9eHJCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v1 03/13] printk: use percpu flag instead of cpu_online() Date: Mon, 7 Feb 2022 20:49:13 +0106 Message-Id: <20220207194323.273637-4-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The CON_ANYTIME console flag is used to label consoles that will work correctly before percpu resources are allocated. To check the condition, cpu_online(raw_smp_processor_id()) was used. However, this is odd because CPUs can go offline at a later point. Also, the function is forced to use the raw_ variant because migration is not disabled. Since commit ab6f762f0f53 ("printk: queue wake_up_klogd irq_work only if per-CPU areas are ready") there is a variable to identify if percpu resources have been allocated. Use that variable instead of cpu_online(raw_smp_processor_id()). Signed-off-by: John Ogness --- include/linux/console.h | 2 +- kernel/printk/printk.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 7cd758a4f44e..aa4d3fb4c1c5 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -133,7 +133,7 @@ static inline int con_debug_leave(void) #define CON_CONSDEV (2) /* Preferred console, /dev/console */ #define CON_ENABLED (4) #define CON_BOOT (8) -#define CON_ANYTIME (16) /* Safe to call when cpu is offline */ +#define CON_ANYTIME (16) /* Safe to call before per-cpu resources ready */ #define CON_BRL (32) /* Used for a braille device */ #define CON_EXTENDED (64) /* Use the extended output format a la /dev/kmsg= */ =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index d1b773823d63..b346e60e9e51 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2577,11 +2577,11 @@ static int have_callable_console(void) * * Console drivers may assume that per-cpu resources have been allocated. = So * unless they're explicitly marked as being able to cope (CON_ANYTIME) do= n't - * call them until this CPU is officially up. + * call them until per-cpu resources have been allocated. */ static inline int can_use_console(void) { - return cpu_online(raw_smp_processor_id()) || have_callable_console(); + return (printk_percpu_data_ready() || have_callable_console()); } =20 /** --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 8E119C4332F for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240351AbiBGTpE (ORCPT ); Mon, 7 Feb 2022 14:45:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59214 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S239950AbiBGTn3 (ORCPT ); Mon, 7 Feb 2022 14:43:29 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 81527C0401E2 for ; Mon, 7 Feb 2022 11:43:28 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263006; 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=lZJXPJIT5SmBR9g67hmZQXBDjPIFa3gpior2KE3O7e0=; b=CqMCWvWy1KAyyVjByXMA0ujM9u6w+ZCFyMZNSYyn63S7b6CmLL3lXerptxJwIwQZTbKovg QuAo1sucTKPf8QGzb57657VkUKX2u5yCm80bRqBjSMFHyaEfzLi98AtxwiKDpVrQqJM28M hq3eKqj2+wG+VLBzV8Wbq8c7snZ67mVpD+qdlqJgu+8qi703w+0jXkOkiwEz2qSnCmpCDZ vwCahncH456hpG4NNHrcI2Tx+oGT0m/vUPbbQM3cJn2xoTFfqYQ/1hS0IaV+scnW9gyeyM lEXPxg5cJyPtiUFQs32dubWIpXS7Mrc8k+pG3mGrT7aoyaW0ER9ShEZ8UYfEMw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263006; 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=lZJXPJIT5SmBR9g67hmZQXBDjPIFa3gpior2KE3O7e0=; b=LAPJ8HRccNNN76RivwGemLEwfDBT7AeBoRlGNXX2uuCaUe5FcyV87ss5MklQKiFFO6/pAr df6GOvuLcOvpMPBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v1 04/13] printk: get caller_id/timestamp after migration disable Date: Mon, 7 Feb 2022 20:49:14 +0106 Message-Id: <20220207194323.273637-5-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Currently the local CPU timestamp and caller_id for the record are collected while migration is enabled. Since this information is CPU-specific, it should be collected with migration disabled. Migration is disabled immediately after collecting this information anyway, so just move the information collection to after the migration disabling. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index b346e60e9e51..206405f349d0 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2017,7 +2017,7 @@ static inline void printk_delay(void) static inline u32 printk_caller_id(void) { return in_task() ? task_pid_nr(current) : - 0x80000000 + raw_smp_processor_id(); + 0x80000000 + smp_processor_id(); } =20 /** @@ -2099,7 +2099,6 @@ int vprintk_store(int facility, int level, const struct dev_printk_info *dev_info, const char *fmt, va_list args) { - const u32 caller_id =3D printk_caller_id(); struct prb_reserved_entry e; enum printk_info_flags flags =3D 0; struct printk_record r; @@ -2109,10 +2108,14 @@ int vprintk_store(int facility, int level, u8 *recursion_ptr; u16 reserve_size; va_list args2; + u32 caller_id; u16 text_len; int ret =3D 0; u64 ts_nsec; =20 + if (!printk_enter_irqsave(recursion_ptr, irqflags)) + return 0; + /* * Since the duration of printk() can vary depending on the message * and state of the ringbuffer, grab the timestamp now so that it is @@ -2121,8 +2124,7 @@ int vprintk_store(int facility, int level, */ ts_nsec =3D local_clock(); =20 - if (!printk_enter_irqsave(recursion_ptr, irqflags)) - return 0; + caller_id =3D printk_caller_id(); =20 /* * The sprintf needs to come first since the syslog prefix might be --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 3D483C3526C for ; Mon, 7 Feb 2022 19:46:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240954AbiBGTpU (ORCPT ); Mon, 7 Feb 2022 14:45:20 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59256 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240413AbiBGTnb (ORCPT ); Mon, 7 Feb 2022 14:43:31 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 92E40C0401E1 for ; Mon, 7 Feb 2022 11:43:30 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263007; 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=CcJAS7OHmwiIaC/n5sQLSDHnPEzEBeUZ3m1d4fP3mjQ=; b=yyO3GMreuW1+E5VBxVctTVFluNm8xtt5rxmastM+uZ/XRECiE/2lneXr1z2y+a1izvEnCq abPeFfwZ60L6SwnaSCcrFJ8jcLSdZXsB5nLV3hcigF6xO1w5PL+yj033xwyvTIvEIUDyOf E5V3L39EJZzvdizD+DzxPVKzVfvHFALNJs+LSgdd4jNJ0sMchRJwf+G8kU16cjBJWfwKpO WqbdqC7v4VbSHOvHM4g05J5yJIOK9DTVHlzhjXK+RGlQbJf7NSkyd9w1dyslwRQ6kTMKfU d/0q3L1ci9vmKaUBYAIGt2KyrOevUG+nowFF749PGMGWTJ1HXY4Y/PulWHGN5g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263007; 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=CcJAS7OHmwiIaC/n5sQLSDHnPEzEBeUZ3m1d4fP3mjQ=; b=R9l7ob6ZMwful/kMPl9aGn6l6U0+mxKAzj0UhtVw+mk58nrAKzJblGJfrBGTha8jPdnDY1 vv9GJwFmccymSaDA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v1 05/13] printk: call boot_delay_msec() in printk_delay() Date: Mon, 7 Feb 2022 20:49:15 +0106 Message-Id: <20220207194323.273637-6-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" boot_delay_msec() is always called immediately before printk_delay() so just call it from within printk_delay(). Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 206405f349d0..8c9a89c60989 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2002,8 +2002,10 @@ static u8 *__printk_recursion_counter(void) =20 int printk_delay_msec __read_mostly; =20 -static inline void printk_delay(void) +static inline void printk_delay(int level) { + boot_delay_msec(level); + if (unlikely(printk_delay_msec)) { int m =3D printk_delay_msec; =20 @@ -2224,8 +2226,7 @@ asmlinkage int vprintk_emit(int facility, int level, in_sched =3D true; } =20 - boot_delay_msec(level); - printk_delay(); + printk_delay(level); =20 printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 7DED0C433F5 for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240233AbiBGTpD (ORCPT ); Mon, 7 Feb 2022 14:45:03 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59294 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240732AbiBGTnd (ORCPT ); Mon, 7 Feb 2022 14:43:33 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4176AC0401E5 for ; Mon, 7 Feb 2022 11:43:32 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263007; 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=n/oB8NBslNwIIRp03p17Mla9dUOK/1FizalS0qQxwJI=; b=MmiiERkSw8mNyuss88JakLupxoYGjq8GZsj5XIRgWr4EzD/koyujldxld4wjItkkFd90/g sHu4AUbHnO0Z0lZ4KPjK0x3I6yEGNEovHRRIuyIvmlPw+8NV1u2LYFZOhUBWxk48QAMe9H qsjgQL/cLkeVnXDtLdZPdri7hJ+cwSgOCz1gNdr1dWtmHG6hbOd4Mibjyy+kwS90Ezy8vU ikcPNvNLk00ugPIgUXN+0jROjJ4zwDcEtmWE7J2YhPKwqHerrIZCnUw5+3D0imLaoyWbgW yVzF1wf4qCyifwWXAXMbD4Xt+iso9E83K2gaaeK+KbHDDgoHKy4pA+ZIZPBVNA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263007; 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=n/oB8NBslNwIIRp03p17Mla9dUOK/1FizalS0qQxwJI=; b=bDpr6WwhPQ0lrIsRTyB2NkTrKFPaG2btd8IeQ3sj49u8KwMHOAFORQe9ENZvlZNOiVObG7 sJsGel5BMKWL45Aw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v1 06/13] printk: refactor and rework printing logic Date: Mon, 7 Feb 2022 20:49:16 +0106 Message-Id: <20220207194323.273637-7-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Refactor/rework printing logic in order to prepare for moving to threaded console printing. - Move @console_seq into struct console so that the current "position" of each console can be tracked individually. - Move @console_dropped into struct console so that the current drop count of each console can be tracked individually. - Modify printing logic so that each console independently loads, prepares, prints, and delays its next record. - Remove exclusive_console logic. Since console positions are handled independently, replaying past records occurs naturally. Signed-off-by: John Ogness --- include/linux/console.h | 2 + kernel/printk/printk.c | 388 ++++++++++++++++++++-------------------- 2 files changed, 198 insertions(+), 192 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index aa4d3fb4c1c5..56a6669471a6 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -151,6 +151,8 @@ struct console { int cflag; uint ispeed; uint ospeed; + u64 seq; + unsigned long dropped; void *data; struct console *next; }; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 8c9a89c60989..822b7b6ad6d1 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -266,11 +266,6 @@ static void __up_console_sem(unsigned long ip) */ static int console_locked, console_suspended; =20 -/* - * If exclusive_console is non-NULL then only this console is to be printe= d to. - */ -static struct console *exclusive_console; - /* * Array of consoles built from command line options (console=3D) */ @@ -360,12 +355,6 @@ static u64 syslog_seq; static size_t syslog_partial; static bool syslog_time; =20 -/* All 3 protected by @console_sem. */ -/* the next printk record to write to the console */ -static u64 console_seq; -static u64 exclusive_console_stop_seq; -static unsigned long console_dropped; - struct latched_seq { seqcount_latch_t latch; u64 val[2]; @@ -1887,47 +1876,26 @@ static int console_trylock_spinning(void) } =20 /* - * Call the console drivers, asking them to write out - * log_buf[start] to log_buf[end - 1]. - * The console_lock must be held. + * Call the specified console driver, asking it to write out the specified + * text and length. For non-extended consoles, if any records have been + * dropped, a dropped message will be written out first. */ -static void call_console_drivers(const char *ext_text, size_t ext_len, - const char *text, size_t len) +static void call_console_driver(struct console *con, const char *text, siz= e_t len) { static char dropped_text[64]; - size_t dropped_len =3D 0; - struct console *con; + size_t dropped_len; =20 trace_console_rcuidle(text, len); =20 - if (!console_drivers) - return; - - if (console_dropped) { + if (con->dropped && !(con->flags & CON_EXTENDED)) { dropped_len =3D snprintf(dropped_text, sizeof(dropped_text), "** %lu printk messages dropped **\n", - console_dropped); - console_dropped =3D 0; + con->dropped); + con->dropped =3D 0; + con->write(con, dropped_text, dropped_len); } =20 - for_each_console(con) { - if (exclusive_console && con !=3D exclusive_console) - continue; - if (!(con->flags & CON_ENABLED)) - continue; - if (!con->write) - continue; - if (!cpu_online(smp_processor_id()) && - !(con->flags & CON_ANYTIME)) - continue; - if (con->flags & CON_EXTENDED) - con->write(con, ext_text, ext_len); - else { - if (dropped_len) - con->write(con, dropped_text, dropped_len); - con->write(con, text, len); - } - } + con->write(con, text, len); } =20 /* @@ -2226,8 +2194,6 @@ asmlinkage int vprintk_emit(int facility, int level, in_sched =3D true; } =20 - printk_delay(level); - printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 /* If called from the scheduler, we can not call up(). */ @@ -2279,11 +2245,9 @@ EXPORT_SYMBOL(_printk); =20 #define prb_read_valid(rb, seq, r) false #define prb_first_valid_seq(rb) 0 +#define prb_next_seq(rb) 0 =20 static u64 syslog_seq; -static u64 console_seq; -static u64 exclusive_console_stop_seq; -static unsigned long console_dropped; =20 static size_t record_print_text(const struct printk_record *r, bool syslog, bool time) @@ -2300,9 +2264,9 @@ static ssize_t msg_print_ext_body(char *buf, size_t s= ize, struct dev_printk_info *dev_info) { return 0; } static void console_lock_spinning_enable(void) { } static int console_lock_spinning_disable_and_check(void) { return 0; } -static void call_console_drivers(const char *ext_text, size_t ext_len, - const char *text, size_t len) {} +static void call_console_driver(struct console *con, const char *text, siz= e_t len) {} static bool suppress_message_printing(int level) { return false; } +static void printk_delay(int level) {} =20 #endif /* CONFIG_PRINTK */ =20 @@ -2560,31 +2524,167 @@ int is_console_locked(void) EXPORT_SYMBOL(is_console_locked); =20 /* - * Check if we have any console that is capable of printing while cpu is - * booting or shutting down. Requires console_sem. + * Check if the given console is currently capable and allowed to print + * records. + * + * Requires the console_lock. */ -static int have_callable_console(void) +static inline bool console_is_usable(struct console *con) { - struct console *con; + if (!(con->flags & CON_ENABLED)) + return false; =20 - for_each_console(con) - if ((con->flags & CON_ENABLED) && - (con->flags & CON_ANYTIME)) - return 1; + if (!con->write) + return false; =20 - return 0; + /* + * Console drivers may assume that per-cpu resources have been + * allocated. So unless they're explicitly marked as being able to + * cope (CON_ANYTIME) don't call them until per-cpu resources have + * been allocated. + */ + if (!printk_percpu_data_ready() && + !(con->flags & CON_ANYTIME)) + return false; + + return true; +} + +static void __console_unlock(void) +{ + console_locked =3D 0; + up_console_sem(); +} + +/* + * Print one record for the given console. The record printed is whatever + * record is the next available record for the given console. + * + * Requires the console_lock. + * + * Returns false if the given console has no next record to print, otherwi= se + * true. + * + * @handover will be set to true if a printk waiter has taken over the + * console_lock, in which case the caller is no longer holding the + * console_lock. Otherwise it is set to false. + */ +static bool console_emit_next_record(struct console *con, bool *handover) +{ + static char ext_text[CONSOLE_EXT_LOG_MAX]; + static char text[CONSOLE_LOG_MAX]; + struct printk_info info; + struct printk_record r; + unsigned long flags; + char *write_text; + size_t len; + + prb_rec_init_rd(&r, &info, text, sizeof(text)); + + *handover =3D false; + + if (!prb_read_valid(prb, con->seq, &r)) + return false; + + if (con->seq !=3D r.info->seq) { + con->dropped +=3D r.info->seq - con->seq; + con->seq =3D r.info->seq; + } + + /* Skip record that has level above the console loglevel. */ + if (suppress_message_printing(r.info->level)) { + con->seq++; + goto skip; + } + + if (con->flags & CON_EXTENDED) { + write_text =3D &ext_text[0]; + len =3D info_print_ext_header(ext_text, sizeof(ext_text), r.info); + len +=3D msg_print_ext_body(ext_text + len, sizeof(ext_text) - len, + &r.text_buf[0], r.info->text_len, &r.info->dev_info); + } else { + write_text =3D &text[0]; + len =3D record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, pr= intk_time); + } + + /* + * While actively printing out messages, if another printk() + * were to occur on another CPU, it may wait for this one to + * finish. This task can not be preempted if there is a + * waiter waiting to take over. + * + * Interrupts are disabled because the hand over to a waiter + * must not be interrupted until the hand over is completed + * (@console_waiter is cleared). + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + + stop_critical_timings(); /* don't trace print latency */ + call_console_driver(con, write_text, len); + start_critical_timings(); + + con->seq++; + + *handover =3D console_lock_spinning_disable_and_check(); + printk_safe_exit_irqrestore(flags); + + printk_delay(r.info->level); +skip: + return true; } =20 /* - * Can we actually use the console at this time on this cpu? + * Print out all remaining records to all consoles. + * + * Requires the console_lock. + * + * Returns true if a console was available for flushing, otherwise false. * - * Console drivers may assume that per-cpu resources have been allocated. = So - * unless they're explicitly marked as being able to cope (CON_ANYTIME) do= n't - * call them until per-cpu resources have been allocated. + * @next_seq is set to the highest sequence number of all of the consoles = that + * were flushed. + * + * @handover will be set to true if a printk waiter has taken over the + * console_lock, in which case the caller is no longer holding the + * console_lock. Otherwise it is set to false. */ -static inline int can_use_console(void) +static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *h= andover) { - return (printk_percpu_data_ready() || have_callable_console()); + bool any_usable =3D false; + struct console *con; + bool any_progress; + + *next_seq =3D 0; + *handover =3D false; + + do { + any_progress =3D false; + + for_each_console(con) { + bool progress; + + if (!console_is_usable(con)) + continue; + any_usable =3D true; + + progress =3D console_emit_next_record(con, handover); + if (*handover) + return true; + + /* Track the highest seq flushed. */ + if (con->seq > *next_seq) + *next_seq =3D con->seq; + + if (!progress) + continue; + any_progress =3D true; + + if (do_cond_resched) + cond_resched(); + } + } while (any_progress); + + return any_usable; } =20 /** @@ -2603,21 +2703,16 @@ static inline int can_use_console(void) */ void console_unlock(void) { - static char ext_text[CONSOLE_EXT_LOG_MAX]; - static char text[CONSOLE_LOG_MAX]; - unsigned long flags; - bool do_cond_resched, retry; - struct printk_info info; - struct printk_record r; - u64 __maybe_unused next_seq; + bool do_cond_resched; + bool handover; + bool flushed; + u64 next_seq; =20 if (console_suspended) { up_console_sem(); return; } =20 - prb_rec_init_rd(&r, &info, text, sizeof(text)); - /* * Console drivers are called with interrupts disabled, so * @console_may_schedule should be cleared before; however, we may @@ -2626,117 +2721,31 @@ void console_unlock(void) * between lines if allowable. Not doing so can cause a very long * scheduling stall on a slow console leading to RCU stall and * softlockup warnings which exacerbate the issue with more - * messages practically incapacitating the system. - * - * console_trylock() is not able to detect the preemptive - * context reliably. Therefore the value must be stored before - * and cleared after the "again" goto label. + * messages practically incapacitating the system. Therefore, create + * a local to use for the printing loop. */ do_cond_resched =3D console_may_schedule; -again: - console_may_schedule =3D 0; =20 - /* - * We released the console_sem lock, so we need to recheck if - * cpu is online and (if not) is there at least one CON_ANYTIME - * console. - */ - if (!can_use_console()) { - console_locked =3D 0; - up_console_sem(); - return; - } - - for (;;) { - size_t ext_len =3D 0; - int handover; - size_t len; + do { + console_may_schedule =3D 0; =20 -skip: - if (!prb_read_valid(prb, console_seq, &r)) + flushed =3D console_flush_all(do_cond_resched, &next_seq, &handover); + if (handover) break; =20 - if (console_seq !=3D r.info->seq) { - console_dropped +=3D r.info->seq - console_seq; - console_seq =3D r.info->seq; - } + __console_unlock(); =20 - if (suppress_message_printing(r.info->level)) { - /* - * Skip record we have buffered and already printed - * directly to the console when we received it, and - * record that has level above the console loglevel. - */ - console_seq++; - goto skip; - } - - /* Output to all consoles once old messages replayed. */ - if (unlikely(exclusive_console && - console_seq >=3D exclusive_console_stop_seq)) { - exclusive_console =3D NULL; - } - - /* - * Handle extended console text first because later - * record_print_text() will modify the record buffer in-place. - */ - if (nr_ext_console_drivers) { - ext_len =3D info_print_ext_header(ext_text, - sizeof(ext_text), - r.info); - ext_len +=3D msg_print_ext_body(ext_text + ext_len, - sizeof(ext_text) - ext_len, - &r.text_buf[0], - r.info->text_len, - &r.info->dev_info); - } - len =3D record_print_text(&r, - console_msg_format & MSG_FORMAT_SYSLOG, - printk_time); - console_seq++; + /* Were there any consoles available for flushing? */ + if (!flushed) + break; =20 /* - * While actively printing out messages, if another printk() - * were to occur on another CPU, it may wait for this one to - * finish. This task can not be preempted if there is a - * waiter waiting to take over. - * - * Interrupts are disabled because the hand over to a waiter - * must not be interrupted until the hand over is completed - * (@console_waiter is cleared). + * Some context may have added new records after + * console_flush_all() but before unlocking the console. + * Re-check if there is a new record to flush. If the trylock + * fails, another context is already handling the printing. */ - printk_safe_enter_irqsave(flags); - console_lock_spinning_enable(); - - stop_critical_timings(); /* don't trace print latency */ - call_console_drivers(ext_text, ext_len, text, len); - start_critical_timings(); - - handover =3D console_lock_spinning_disable_and_check(); - printk_safe_exit_irqrestore(flags); - if (handover) - return; - - if (do_cond_resched) - cond_resched(); - } - - /* Get consistent value of the next-to-be-used sequence number. */ - next_seq =3D console_seq; - - console_locked =3D 0; - up_console_sem(); - - /* - * Someone could have filled up the buffer again, so re-check if there's - * something to flush. In case we cannot trylock the console_sem again, - * there's a new owner and the console_unlock() from them will do the - * flush, no worries. - */ - retry =3D prb_read_valid(prb, next_seq, NULL); - if (retry && console_trylock()) - goto again; + } while (prb_read_valid(prb, next_seq, NULL) && console_trylock()); } EXPORT_SYMBOL(console_unlock); =20 @@ -2796,8 +2805,14 @@ void console_flush_on_panic(enum con_flush_mode mode) console_trylock(); console_may_schedule =3D 0; =20 - if (mode =3D=3D CONSOLE_REPLAY_ALL) - console_seq =3D prb_first_valid_seq(prb); + if (mode =3D=3D CONSOLE_REPLAY_ALL) { + struct console *c; + u64 seq; + + seq =3D prb_first_valid_seq(prb); + for_each_console(c) + c->seq =3D seq; + } console_unlock(); } =20 @@ -3032,26 +3047,15 @@ void register_console(struct console *newcon) if (newcon->flags & CON_EXTENDED) nr_ext_console_drivers++; =20 + newcon->dropped =3D 0; if (newcon->flags & CON_PRINTBUFFER) { - /* - * console_unlock(); will print out the buffered messages - * for us. - * - * We're about to replay the log buffer. Only do this to the - * just-registered console to avoid excessive message spam to - * the already-registered consoles. - * - * Set exclusive_console with disabled interrupts to reduce - * race window with eventual console_flush_on_panic() that - * ignores console_lock. - */ - exclusive_console =3D newcon; - exclusive_console_stop_seq =3D console_seq; - /* Get a consistent copy of @syslog_seq. */ mutex_lock(&syslog_lock); - console_seq =3D syslog_seq; + newcon->seq =3D syslog_seq; mutex_unlock(&syslog_lock); + } else { + /* Begin with next message. */ + newcon->seq =3D prb_next_seq(prb); } console_unlock(); console_sysfs_notify(); --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 5D76FC433EF for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240032AbiBGTo5 (ORCPT ); Mon, 7 Feb 2022 14:44:57 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59264 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240498AbiBGTnb (ORCPT ); Mon, 7 Feb 2022 14:43:31 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DFC43C0401E2 for ; Mon, 7 Feb 2022 11:43:30 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263008; 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=7hqu9l5kqJCuQTlbnhEqTk9n+e1ZXESo2zvx1X+Jm9U=; b=KCEZ9QqCoglJ6rbCvkG2Lc4Ar8XzRF7VZjI4QZuptkHkDzaYQAEnjgTLYFZNAlbnz2/oVW eBoYR16s+ED6bJSkfoLy08MNL6FOZ62lhkxWBkEtv41VcAfdPwNlv8d8nqIkfSpx4+jKgE UBD+2Gf1n+d+YAZkHejYyErscW7yZqSRCRuIMh7rcFK9NexKuyJBMjZcK5MBuSHewNYKPP c98/jQqwIZ17+pjVG5Dl5npTak+lHZYkoVLFWEO5gqNGkUR7xHAxAIC9nz6AuBqr8MMAWz VaiTyMKi80a92MaLK/VD9E7aisaR6pt+FiSul3oWKOn3EY13u0oqMWLmk+vlNw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263008; 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=7hqu9l5kqJCuQTlbnhEqTk9n+e1ZXESo2zvx1X+Jm9U=; b=VVfxGjuen9ls0fccEf4kFtzhuyQPHueMvtv8Bjr1fNjthZWa5VvhCexTVADPGnizGOyLCM vzUHA65jiZhZDABw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v1 07/13] printk: move buffer definitions into console_emit_next_record() caller Date: Mon, 7 Feb 2022 20:49:17 +0106 Message-Id: <20220207194323.273637-8-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Extended consoles print extended messages and do not print messages about dropped records. Non-extended consoles print "normal" messages as well as extra messages about dropped records. Currently the buffers for these various message types are defined within the functions that might use them and their usage is based upon the CON_EXTENDED flag. This will be a problem when moving to kthread printers because each printer must be able to provide its own buffers. Move all the message buffer definitions outside of console_emit_next_record(). The caller knows if extended or dropped messages should be printed and can specify the appropriate buffers to use. The console_emit_next_record() and call_console_driver() functions can know what to print based on whether specified buffers are non-NULL. With this change, buffer definition/allocation/specification is separated from the code that does the various types of string printing. Signed-off-by: John Ogness --- kernel/printk/printk.c | 60 ++++++++++++++++++++++++++++++------------ 1 file changed, 43 insertions(+), 17 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 822b7b6ad6d1..02bde45c1149 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -380,6 +380,9 @@ static struct latched_seq clear_seq =3D { /* the maximum size of a formatted record (i.e. with prefix added per line= ) */ #define CONSOLE_LOG_MAX 1024 =20 +/* the maximum size for a dropped text message */ +#define DROPPED_TEXT_MAX 64 + /* the maximum size allowed to be reserved for a record */ #define LOG_LINE_MAX (CONSOLE_LOG_MAX - PREFIX_MAX) =20 @@ -1877,18 +1880,18 @@ static int console_trylock_spinning(void) =20 /* * Call the specified console driver, asking it to write out the specified - * text and length. For non-extended consoles, if any records have been + * text and length. If @dropped_text is non-NULL and any records have been * dropped, a dropped message will be written out first. */ -static void call_console_driver(struct console *con, const char *text, siz= e_t len) +static void call_console_driver(struct console *con, const char *text, siz= e_t len, + char *dropped_text) { - static char dropped_text[64]; size_t dropped_len; =20 trace_console_rcuidle(text, len); =20 - if (con->dropped && !(con->flags & CON_EXTENDED)) { - dropped_len =3D snprintf(dropped_text, sizeof(dropped_text), + if (con->dropped && dropped_text) { + dropped_len =3D snprintf(dropped_text, DROPPED_TEXT_MAX, "** %lu printk messages dropped **\n", con->dropped); con->dropped =3D 0; @@ -2241,6 +2244,7 @@ EXPORT_SYMBOL(_printk); #else /* CONFIG_PRINTK */ =20 #define CONSOLE_LOG_MAX 0 +#define DROPPED_TEXT_MAX 0 #define printk_time false =20 #define prb_read_valid(rb, seq, r) false @@ -2264,7 +2268,10 @@ static ssize_t msg_print_ext_body(char *buf, size_t = size, struct dev_printk_info *dev_info) { return 0; } static void console_lock_spinning_enable(void) { } static int console_lock_spinning_disable_and_check(void) { return 0; } -static void call_console_driver(struct console *con, const char *text, siz= e_t len) {} +static void call_console_driver(struct console *con, const char *text, siz= e_t len, + char *dropped_text) +{ +} static bool suppress_message_printing(int level) { return false; } static void printk_delay(int level) {} =20 @@ -2560,6 +2567,14 @@ static void __console_unlock(void) * Print one record for the given console. The record printed is whatever * record is the next available record for the given console. * + * @text is a buffer of size CONSOLE_LOG_MAX. + * + * If extended messages should be printed, @ext_text is a buffer of size + * CONSOLE_EXT_LOG_MAX. Otherwise @ext_text must be NULL. + * + * If dropped messages should be printed, @dropped_text is a buffer of size + * DROPPED_TEXT_MAX. Otherwise @dropped_text must be NULL. + * * Requires the console_lock. * * Returns false if the given console has no next record to print, otherwi= se @@ -2569,17 +2584,16 @@ static void __console_unlock(void) * console_lock, in which case the caller is no longer holding the * console_lock. Otherwise it is set to false. */ -static bool console_emit_next_record(struct console *con, bool *handover) +static bool console_emit_next_record(struct console *con, char *text, char= *ext_text, + char *dropped_text, bool *handover) { - static char ext_text[CONSOLE_EXT_LOG_MAX]; - static char text[CONSOLE_LOG_MAX]; struct printk_info info; struct printk_record r; unsigned long flags; char *write_text; size_t len; =20 - prb_rec_init_rd(&r, &info, text, sizeof(text)); + prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); =20 *handover =3D false; =20 @@ -2597,13 +2611,13 @@ static bool console_emit_next_record(struct console= *con, bool *handover) goto skip; } =20 - if (con->flags & CON_EXTENDED) { - write_text =3D &ext_text[0]; - len =3D info_print_ext_header(ext_text, sizeof(ext_text), r.info); - len +=3D msg_print_ext_body(ext_text + len, sizeof(ext_text) - len, + if (ext_text) { + write_text =3D ext_text; + len =3D info_print_ext_header(ext_text, CONSOLE_EXT_LOG_MAX, r.info); + len +=3D msg_print_ext_body(ext_text + len, CONSOLE_EXT_LOG_MAX - len, &r.text_buf[0], r.info->text_len, &r.info->dev_info); } else { - write_text =3D &text[0]; + write_text =3D text; len =3D record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, pr= intk_time); } =20 @@ -2621,7 +2635,7 @@ static bool console_emit_next_record(struct console *= con, bool *handover) console_lock_spinning_enable(); =20 stop_critical_timings(); /* don't trace print latency */ - call_console_driver(con, write_text, len); + call_console_driver(con, write_text, len, dropped_text); start_critical_timings(); =20 con->seq++; @@ -2650,6 +2664,9 @@ static bool console_emit_next_record(struct console *= con, bool *handover) */ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *h= andover) { + static char dropped_text[DROPPED_TEXT_MAX]; + static char ext_text[CONSOLE_EXT_LOG_MAX]; + static char text[CONSOLE_LOG_MAX]; bool any_usable =3D false; struct console *con; bool any_progress; @@ -2667,7 +2684,16 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove continue; any_usable =3D true; =20 - progress =3D console_emit_next_record(con, handover); + if (con->flags & CON_EXTENDED) { + /* Extended consoles do not print "dropped messages". */ + progress =3D console_emit_next_record(con, &text[0], + &ext_text[0], NULL, + handover); + } else { + progress =3D console_emit_next_record(con, &text[0], + NULL, &dropped_text[0], + handover); + } if (*handover) return true; =20 --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 C46D9C4321E for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240637AbiBGTpI (ORCPT ); Mon, 7 Feb 2022 14:45:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59260 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240458AbiBGTnb (ORCPT ); Mon, 7 Feb 2022 14:43:31 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id DD7D5C0401E0 for ; Mon, 7 Feb 2022 11:43:30 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263008; 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=Xh+B+R8a+dMM5UjgQTfTZzdQRiBRIrfEWMgEQ7PqZr4=; b=oxU66eRg65n32p2KUIctY1A+KzyV5RB9AU0O+WYXGbTgVw7BAhR7iDmj/AksDLex4cUs4a QaVaO7ffQkhRCekj/ijMWwWb7PG53opfFkiiBLv5IcNOvod1hmGLn2reGRmjqcN/lu2kDd KNiWvVt2izzohNp2QN9I9Fspg8dbtyel9kR62iSmUgfTWUbMxfh+YRUMFsT1aCMTSiKqX3 6hMy81izOCGnowK6w2daIF8fkBNRzltpqw+Y49gE07NVFJe+AeSUJlsq5l4reiDHaruSVn iKZUWEnf1tk49RsKzBDhf+WYsWd10oqeJ5nyg+KEF3AuuDKcgp0vvv3+4tyebg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263008; 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=Xh+B+R8a+dMM5UjgQTfTZzdQRiBRIrfEWMgEQ7PqZr4=; b=dhHckI5TD0w/WJOQt8dcmU5LVT3Gx6FAcGu6DFRs2yTgc5/WrBWsgDnx8jF+HxjIUQtFwz WaClvWVYAqcf1ICw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v1 08/13] printk: add pr_flush() Date: Mon, 7 Feb 2022 20:49:18 +0106 Message-Id: <20220207194323.273637-9-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Provide a might-sleep function to allow waiting for threaded console printers to catch up to the latest logged message. Use pr_flush() whenever it is desirable to get buffered messages printed before continuing: suspend_console(), resume_console(), console_stop(), console_start(), console_unblank(). Signed-off-by: John Ogness --- include/linux/printk.h | 7 ++++ kernel/printk/printk.c | 73 +++++++++++++++++++++++++++++++++++++++++- 2 files changed, 79 insertions(+), 1 deletion(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index cd8192e611a1..6596f02d1f05 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -170,6 +170,8 @@ extern void __printk_safe_exit(void); #define printk_deferred_enter __printk_safe_enter #define printk_deferred_exit __printk_safe_exit =20 +extern bool pr_flush(int timeout_ms, bool reset_on_progress); + /* * Please don't use printk_ratelimit(), because it shares ratelimiting sta= te * with all other unrelated printk_ratelimit() callsites. Instead use @@ -224,6 +226,11 @@ static inline void printk_deferred_exit(void) { } =20 +static inline bool pr_flush(int timeout_ms, bool reset_on_progress) +{ + return true; +} + static inline int printk_ratelimit(void) { return 0; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 02bde45c1149..1e80fd052bd5 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2449,6 +2449,7 @@ void suspend_console(void) if (!console_suspend_enabled) return; pr_info("Suspending console(s) (use no_console_suspend to debug)\n"); + pr_flush(1000, true); console_lock(); console_suspended =3D 1; up_console_sem(); @@ -2461,6 +2462,7 @@ void resume_console(void) down_console_sem(); console_suspended =3D 0; console_unlock(); + pr_flush(1000, true); } =20 /** @@ -2802,8 +2804,10 @@ void console_unblank(void) if (oops_in_progress) { if (down_trylock_console_sem() !=3D 0) return; - } else + } else { + pr_flush(1000, true); console_lock(); + } =20 console_locked =3D 1; console_may_schedule =3D 0; @@ -2869,6 +2873,7 @@ struct tty_driver *console_device(int *index) */ void console_stop(struct console *console) { + pr_flush(1000, true); console_lock(); console->flags &=3D ~CON_ENABLED; console_unlock(); @@ -2880,6 +2885,7 @@ void console_start(struct console *console) console_lock(); console->flags |=3D CON_ENABLED; console_unlock(); + pr_flush(1000, true); } EXPORT_SYMBOL(console_start); =20 @@ -3249,6 +3255,71 @@ static int __init printk_late_init(void) late_initcall(printk_late_init); =20 #if defined CONFIG_PRINTK +/** + * pr_flush() - Wait for printing threads to catch up. + * + * @timeout_ms: The maximum time (in ms) to wait. + * @reset_on_progress: Reset the timeout if forward progress is seen. + * + * A value of 0 for @timeout_ms means no waiting will occur. A value of -1 + * represents infinite waiting. + * + * If @reset_on_progress is true, the timeout will be reset whenever any + * printer has been seen to make some forward progress. + * + * Context: Process context. May sleep while acquiring console lock. + * Return: true if all enabled printers are caught up. + */ +bool pr_flush(int timeout_ms, bool reset_on_progress) +{ + int remaining =3D timeout_ms; + struct console *con; + u64 last_diff =3D 0; + u64 printk_seq; + u64 diff; + u64 seq; + + might_sleep(); + + seq =3D prb_next_seq(prb); + + for (;;) { + diff =3D 0; + + console_lock(); + for_each_console(con) { + if (!console_is_usable(con)) + continue; + printk_seq =3D con->seq; + if (printk_seq < seq) + diff +=3D seq - printk_seq; + } + console_unlock(); + + if (diff !=3D last_diff && reset_on_progress) + remaining =3D timeout_ms; + + if (diff =3D=3D 0 || remaining =3D=3D 0) + break; + + if (remaining < 0) { + /* no timeout limit */ + msleep(100); + } else if (remaining < 100) { + msleep(remaining); + remaining =3D 0; + } else { + msleep(100); + remaining -=3D 100; + } + + last_diff =3D diff; + } + + return (diff =3D=3D 0); +} +EXPORT_SYMBOL(pr_flush); + /* * Delayed printk version, for scheduler-internal messages: */ --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 DC9B3C433EF for ; Mon, 7 Feb 2022 19:44:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239977AbiBGToa (ORCPT ); Mon, 7 Feb 2022 14:44:30 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59282 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240721AbiBGTnd (ORCPT ); Mon, 7 Feb 2022 14:43:33 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 34162C0401E4; Mon, 7 Feb 2022 11:43:31 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263009; 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=eOR5WbQJrKK+I/SA7qP7nQGJ8obD2pFQ2CzlcnvaYpM=; b=oLAXtihwjw5NTCGy6UBmwHk0N4OD7qItM20ufBP6OKE7TM8ipSAWNpGRF7N4ljR8p+5p0p DTtaFUJzjgrEITlidqxpEvEbxDmY1uHZXshwJr/DKl1efthMM6hrbEsIDB5f0YteiwuWCT yILCLoXaVIm7jnNUBB2O67+8G1Wd5DL3peSac3bz+/7a1uW7eLInWBMp9SEqi+YDh5L9vA BlSXWRUUT05Pr4c/z/PCn8lf0J8KGcQEr2xr7mgwGVDNj5OtSK8kWwyq9aUpMTYcKKedqO W04ZK8c4CV7eIuKLpdobpP1Yj7Qk5SfUxPLuz8teKbW48WYrrjo7hhxCwXo+Ig== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263009; 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=eOR5WbQJrKK+I/SA7qP7nQGJ8obD2pFQ2CzlcnvaYpM=; b=NFEyFRFN1R95oj98NXvw4e9vVBpYcdZOhVckpRFgjcguUzktJwy67jB4w3UOsG8aQrES9m rffhgvIhlH9GJ2BQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , "Paul E. McKenney" , Josh Triplett , Mathieu Desnoyers , Lai Jiangshan , Joel Fernandes , Corey Minyard , Kees Cook , Christian Brauner , Daniel Bristot de Oliveira , Andy Shevchenko , Peter Zijlstra , Daniel Lezcano , "Rafael J. Wysocki" , Mark Brown , Josef Bacik , "Eric W. Biederman" , Shawn Guo , Matti Vaittinen , Andrew Morton , Wang Qing , Tejun Heo , Randy Dunlap , Alexander Potapenko , Stephen Boyd , Nicholas Piggin , rcu@vger.kernel.org Subject: [PATCH printk v1 09/13] printk: add functions to allow direct printing Date: Mon, 7 Feb 2022 20:49:19 +0106 Message-Id: <20220207194323.273637-10-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Once kthread printing is introduced, console printing will no longer occur in the context of the printk caller. However, there are some special contexts where it is desirable for the printk caller to directly print out kernel messages. Using pr_flush() to wait for threaded printers is only possible if the caller is in a sleepable context and the kthreads are active. That is not always the case. Introduce printk_direct_enter() and printk_direct_exit() functions to explicitly (and globally) activate/deactivate direct console printing. Activate direct printing for: - sysrq - emergency reboot/shutdown - cpu and rcu stalls - hard and soft lockups - hung tasks - stack dumps Signed-off-by: John Ogness --- drivers/tty/sysrq.c | 2 ++ include/linux/printk.h | 11 +++++++++++ kernel/hung_task.c | 11 ++++++++++- kernel/printk/printk.c | 39 ++++++++++++++++++++++++++++++++++++++- kernel/rcu/tree_stall.h | 2 ++ kernel/reboot.c | 14 +++++++++++++- kernel/watchdog.c | 4 ++++ kernel/watchdog_hld.c | 4 ++++ lib/dump_stack.c | 2 ++ lib/nmi_backtrace.c | 2 ++ 10 files changed, 88 insertions(+), 3 deletions(-) diff --git a/drivers/tty/sysrq.c b/drivers/tty/sysrq.c index bbfd004449b5..a809f56c392f 100644 --- a/drivers/tty/sysrq.c +++ b/drivers/tty/sysrq.c @@ -594,9 +594,11 @@ void __handle_sysrq(int key, bool check_mask) * should not) and is the invoked operation enabled? */ if (!check_mask || sysrq_on_mask(op_p->enable_mask)) { + printk_direct_enter(); pr_info("%s\n", op_p->action_msg); console_loglevel =3D orig_log_level; op_p->handler(key); + printk_direct_exit(); } else { pr_info("This sysrq operation is disabled.\n"); console_loglevel =3D orig_log_level; diff --git a/include/linux/printk.h b/include/linux/printk.h index 6596f02d1f05..eedf7546ff89 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -170,6 +170,9 @@ extern void __printk_safe_exit(void); #define printk_deferred_enter __printk_safe_enter #define printk_deferred_exit __printk_safe_exit =20 +extern void printk_direct_enter(void); +extern void printk_direct_exit(void); + extern bool pr_flush(int timeout_ms, bool reset_on_progress); =20 /* @@ -226,6 +229,14 @@ static inline void printk_deferred_exit(void) { } =20 +static inline void printk_direct_enter(void) +{ +} + +static inline void printk_direct_exit(void) +{ +} + static inline bool pr_flush(int timeout_ms, bool reset_on_progress) { return true; diff --git a/kernel/hung_task.c b/kernel/hung_task.c index 9888e2bc8c76..6de7cec90c3b 100644 --- a/kernel/hung_task.c +++ b/kernel/hung_task.c @@ -125,6 +125,8 @@ static void check_hung_task(struct task_struct *t, unsi= gned long timeout) * complain: */ if (sysctl_hung_task_warnings) { + printk_direct_enter(); + if (sysctl_hung_task_warnings > 0) sysctl_hung_task_warnings--; pr_err("INFO: task %s:%d blocked for more than %ld seconds.\n", @@ -140,6 +142,8 @@ static void check_hung_task(struct task_struct *t, unsi= gned long timeout) =20 if (sysctl_hung_task_all_cpu_backtrace) hung_task_show_all_bt =3D true; + + printk_direct_exit(); } =20 touch_nmi_watchdog(); @@ -202,12 +206,17 @@ static void check_hung_uninterruptible_tasks(unsigned= long timeout) } unlock: rcu_read_unlock(); - if (hung_task_show_lock) + if (hung_task_show_lock) { + printk_direct_enter(); debug_show_all_locks(); + printk_direct_exit(); + } =20 if (hung_task_show_all_bt) { hung_task_show_all_bt =3D false; + printk_direct_enter(); trigger_all_cpu_backtrace(); + printk_direct_exit(); } =20 if (hung_task_call_panic) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1e80fd052bd5..719b05e6ce3b 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -348,6 +348,31 @@ static int console_msg_format =3D MSG_FORMAT_DEFAULT; static DEFINE_MUTEX(syslog_lock); =20 #ifdef CONFIG_PRINTK +static atomic_t printk_direct =3D ATOMIC_INIT(0); + +/** + * printk_direct_enter - cause console printing to occur in the context of + * printk() callers + * + * This globally effects all printk() callers. + * + * Context: Any context. + */ +void printk_direct_enter(void) +{ + atomic_inc(&printk_direct); +} + +/** + * printk_direct_exit - restore console printing behavior from direct + * + * Context: Any context. + */ +void printk_direct_exit(void) +{ + atomic_dec(&printk_direct); +} + DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */ @@ -3325,6 +3350,7 @@ EXPORT_SYMBOL(pr_flush); */ #define PRINTK_PENDING_WAKEUP 0x01 #define PRINTK_PENDING_OUTPUT 0x02 +#define PRINTK_DIRECT_OUTPUT 0x04 =20 static DEFINE_PER_CPU(int, printk_pending); =20 @@ -3333,9 +3359,15 @@ static void wake_up_klogd_work_func(struct irq_work = *irq_work) int pending =3D __this_cpu_xchg(printk_pending, 0); =20 if (pending & PRINTK_PENDING_OUTPUT) { + if (pending & PRINTK_DIRECT_OUTPUT) + printk_direct_enter(); + /* If trylock fails, someone else is doing the printing */ if (console_trylock()) console_unlock(); + + if (pending & PRINTK_DIRECT_OUTPUT) + printk_direct_exit(); } =20 if (pending & PRINTK_PENDING_WAKEUP) @@ -3360,11 +3392,16 @@ void wake_up_klogd(void) =20 void defer_console_output(void) { + int val =3D PRINTK_PENDING_OUTPUT; + if (!printk_percpu_data_ready()) return; =20 + if (atomic_read(&printk_direct)) + val |=3D PRINTK_DIRECT_OUTPUT; + preempt_disable(); - __this_cpu_or(printk_pending, PRINTK_PENDING_OUTPUT); + __this_cpu_or(printk_pending, val); irq_work_queue(this_cpu_ptr(&wake_up_klogd_work)); preempt_enable(); } diff --git a/kernel/rcu/tree_stall.h b/kernel/rcu/tree_stall.h index 5e2fa6fd97f1..26953f9b2ab2 100644 --- a/kernel/rcu/tree_stall.h +++ b/kernel/rcu/tree_stall.h @@ -610,6 +610,7 @@ static void print_cpu_stall(unsigned long gps) * See Documentation/RCU/stallwarn.rst for info on how to debug * RCU CPU stall warnings. */ + printk_direct_enter(); trace_rcu_stall_warning(rcu_state.name, TPS("SelfDetected")); pr_err("INFO: %s self-detected stall on CPU\n", rcu_state.name); raw_spin_lock_irqsave_rcu_node(rdp->mynode, flags); @@ -644,6 +645,7 @@ static void print_cpu_stall(unsigned long gps) */ set_tsk_need_resched(current); set_preempt_need_resched(); + printk_direct_exit(); } =20 static void check_cpu_stall(struct rcu_data *rdp) diff --git a/kernel/reboot.c b/kernel/reboot.c index 6bcc5d6a6572..0f219ce610fc 100644 --- a/kernel/reboot.c +++ b/kernel/reboot.c @@ -447,9 +447,11 @@ static int __orderly_reboot(void) ret =3D run_cmd(reboot_cmd); =20 if (ret) { + printk_direct_enter(); pr_warn("Failed to start orderly reboot: forcing the issue\n"); emergency_sync(); kernel_restart(NULL); + printk_direct_exit(); } =20 return ret; @@ -462,6 +464,7 @@ static int __orderly_poweroff(bool force) ret =3D run_cmd(poweroff_cmd); =20 if (ret && force) { + printk_direct_enter(); pr_warn("Failed to start orderly shutdown: forcing the issue\n"); =20 /* @@ -471,6 +474,7 @@ static int __orderly_poweroff(bool force) */ emergency_sync(); kernel_power_off(); + printk_direct_exit(); } =20 return ret; @@ -528,6 +532,8 @@ EXPORT_SYMBOL_GPL(orderly_reboot); */ static void hw_failure_emergency_poweroff_func(struct work_struct *work) { + printk_direct_enter(); + /* * We have reached here after the emergency shutdown waiting period has * expired. This means orderly_poweroff has not been able to shut off @@ -544,6 +550,8 @@ static void hw_failure_emergency_poweroff_func(struct w= ork_struct *work) */ pr_emerg("Hardware protection shutdown failed. Trying emergency restart\n= "); emergency_restart(); + + printk_direct_exit(); } =20 static DECLARE_DELAYED_WORK(hw_failure_emergency_poweroff_work, @@ -582,11 +590,13 @@ void hw_protection_shutdown(const char *reason, int m= s_until_forced) { static atomic_t allow_proceed =3D ATOMIC_INIT(1); =20 + printk_direct_enter(); + pr_emerg("HARDWARE PROTECTION shutdown (%s)\n", reason); =20 /* Shutdown should be initiated only once. */ if (!atomic_dec_and_test(&allow_proceed)) - return; + goto out; =20 /* * Queue a backup emergency shutdown in the event of @@ -594,6 +604,8 @@ void hw_protection_shutdown(const char *reason, int ms_= until_forced) */ hw_failure_emergency_poweroff(ms_until_forced); orderly_poweroff(true); +out: + printk_direct_exit(); } EXPORT_SYMBOL_GPL(hw_protection_shutdown); =20 diff --git a/kernel/watchdog.c b/kernel/watchdog.c index ad912511a0c0..930563f155ee 100644 --- a/kernel/watchdog.c +++ b/kernel/watchdog.c @@ -424,6 +424,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hr= timer *hrtimer) /* Start period for the next softlockup warning. */ update_report_ts(); =20 + printk_direct_enter(); + pr_emerg("BUG: soft lockup - CPU#%d stuck for %us! [%s:%d]\n", smp_processor_id(), duration, current->comm, task_pid_nr(current)); @@ -442,6 +444,8 @@ static enum hrtimer_restart watchdog_timer_fn(struct hr= timer *hrtimer) add_taint(TAINT_SOFTLOCKUP, LOCKDEP_STILL_OK); if (softlockup_panic) panic("softlockup: hung tasks"); + + printk_direct_exit(); } =20 return HRTIMER_RESTART; diff --git a/kernel/watchdog_hld.c b/kernel/watchdog_hld.c index 247bf0b1582c..56cfbae94c42 100644 --- a/kernel/watchdog_hld.c +++ b/kernel/watchdog_hld.c @@ -135,6 +135,8 @@ static void watchdog_overflow_callback(struct perf_even= t *event, if (__this_cpu_read(hard_watchdog_warn) =3D=3D true) return; =20 + printk_direct_enter(); + pr_emerg("Watchdog detected hard LOCKUP on cpu %d\n", this_cpu); print_modules(); @@ -155,6 +157,8 @@ static void watchdog_overflow_callback(struct perf_even= t *event, if (hardlockup_panic) nmi_panic(regs, "Hard LOCKUP"); =20 + printk_direct_exit(); + __this_cpu_write(hard_watchdog_warn, true); return; } diff --git a/lib/dump_stack.c b/lib/dump_stack.c index 83471e81501a..e3b4eeb1dcb4 100644 --- a/lib/dump_stack.c +++ b/lib/dump_stack.c @@ -102,9 +102,11 @@ asmlinkage __visible void dump_stack_lvl(const char *l= og_lvl) * Permit this cpu to perform nested stack dumps while serialising * against other CPUs */ + printk_direct_enter(); printk_cpu_sync_get_irqsave(flags); __dump_stack(log_lvl); printk_cpu_sync_put_irqrestore(flags); + printk_direct_exit(); } EXPORT_SYMBOL(dump_stack_lvl); =20 diff --git a/lib/nmi_backtrace.c b/lib/nmi_backtrace.c index d01aec6ae15c..dabeb35bc8dc 100644 --- a/lib/nmi_backtrace.c +++ b/lib/nmi_backtrace.c @@ -99,6 +99,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs) * Allow nested NMI backtraces while serializing * against other CPUs. */ + printk_direct_enter(); printk_cpu_sync_get_irqsave(flags); if (!READ_ONCE(backtrace_idle) && regs && cpu_in_idle(instruction_pointe= r(regs))) { pr_warn("NMI backtrace for cpu %d skipped: idling at %pS\n", @@ -111,6 +112,7 @@ bool nmi_cpu_backtrace(struct pt_regs *regs) dump_stack(); } printk_cpu_sync_put_irqrestore(flags); + printk_direct_exit(); cpumask_clear_cpu(cpu, to_cpumask(backtrace_mask)); return true; } --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 0BABDC433EF for ; Mon, 7 Feb 2022 19:45:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S239870AbiBGToz (ORCPT ); Mon, 7 Feb 2022 14:44:55 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59300 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240734AbiBGTne (ORCPT ); Mon, 7 Feb 2022 14:43:34 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48C69C0401DA for ; Mon, 7 Feb 2022 11:43:33 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263010; 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=I/+tO1foi0Y2Jl4gBJOBBXHVMgC1lG8mZRnsEtx1+Ms=; b=r6aim1QmJwP1Et76JlWvlODj8d9ZgKUCOagNmdgI965rmll3fNNo3Jy69m707YkjmPQbfR EvjL3Gx8sO3iHfalDPDUXFq+4kZu7myu3U3yAclfnqa6iKLhFX9ZP8wODzl779WyT7xvxw CZw8Qc5ench9WU9DZfS2wVpBTZweaQw99JAU5V3y9iauQPp5AdSUdfbG8Ayoxohu5cG6je vtIYfDJU5x4O8y8I37Qv8CQGkRZtwyi/s6KEBXbCx0K43tpVifyiAp1qGpjyCAqYBniEcQ m24bfBTotzPulMWFqJBolMSg1WXlZbmlHeFZpXypvBCMnGJsR8JKhXI1jpbWng== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263010; 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=I/+tO1foi0Y2Jl4gBJOBBXHVMgC1lG8mZRnsEtx1+Ms=; b=+X33JuijHrJU3oQTPQe1lBIS0HXGWwsg10GUeVDlZ19LG333SD6t4gHKBBQtW6st4qwUBD zStNmidibGj1zWAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v1 10/13] printk: add kthread console printers Date: Mon, 7 Feb 2022 20:49:20 +0106 Message-Id: <20220207194323.273637-11-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Create a kthread for each console to perform console printing. During normal operation (@system_state =3D=3D SYSTEM_RUNNING), the kthread printers are responsible for all printing on their respective consoles. During non-normal operation, console printing is done as it has been: within the context of the printk caller or within irq work triggered by the printk caller. Console printers synchronize against each other and against console lockers by taking the console lock for each message that is printed. Signed-off-by: John Ogness --- include/linux/console.h | 2 + kernel/printk/printk.c | 159 +++++++++++++++++++++++++++++++++++++++- 2 files changed, 159 insertions(+), 2 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 56a6669471a6..0f94b1771df8 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -153,6 +153,8 @@ struct console { uint ospeed; u64 seq; unsigned long dropped; + struct task_struct *thread; + void *data; struct console *next; }; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 719b05e6ce3b..e182f31fec58 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -347,6 +347,13 @@ static int console_msg_format =3D MSG_FORMAT_DEFAULT; /* syslog_lock protects syslog_* variables and write access to clear_seq. = */ static DEFINE_MUTEX(syslog_lock); =20 +/* + * A flag to signify if printk_late_init() has already started the kthread + * printers. If true, any later registered consoles must start their own + * kthread directly. The flag is write protected by the console_lock. + */ +static bool kthreads_started; + #ifdef CONFIG_PRINTK static atomic_t printk_direct =3D ATOMIC_INIT(0); =20 @@ -373,6 +380,14 @@ void printk_direct_exit(void) atomic_dec(&printk_direct); } =20 +static inline bool allow_direct_printing(void) +{ + return (!kthreads_started || + system_state !=3D SYSTEM_RUNNING || + oops_in_progress || + atomic_read(&printk_direct)); +} + DECLARE_WAIT_QUEUE_HEAD(log_wait); /* All 3 protected by @syslog_lock. */ /* the next printk record to read by syslog(READ) or /proc/kmsg */ @@ -2225,7 +2240,7 @@ asmlinkage int vprintk_emit(int facility, int level, printed_len =3D vprintk_store(facility, level, dev_info, fmt, args); =20 /* If called from the scheduler, we can not call up(). */ - if (!in_sched) { + if (!in_sched && allow_direct_printing()) { /* * Disable preemption to avoid being preempted while holding * console_sem which would prevent anyone from printing to @@ -2266,6 +2281,8 @@ asmlinkage __visible int _printk(const char *fmt, ...) } EXPORT_SYMBOL(_printk); =20 +static void start_printk_kthread(struct console *con); + #else /* CONFIG_PRINTK */ =20 #define CONSOLE_LOG_MAX 0 @@ -2299,6 +2316,8 @@ static void call_console_driver(struct console *con, = const char *text, size_t le } static bool suppress_message_printing(int level) { return false; } static void printk_delay(int level) {} +static void start_printk_kthread(struct console *con) {} +static bool allow_direct_printing(void) { return true; } =20 #endif /* CONFIG_PRINTK */ =20 @@ -2487,6 +2506,10 @@ void resume_console(void) down_console_sem(); console_suspended =3D 0; console_unlock(); + + /* Wake the kthread printers. */ + wake_up_klogd(); + pr_flush(1000, true); } =20 @@ -2702,6 +2725,10 @@ static bool console_flush_all(bool do_cond_resched, = u64 *next_seq, bool *handove *handover =3D false; =20 do { + /* Let the kthread printers do the work if they can. */ + if (!allow_direct_printing()) + break; + any_progress =3D false; =20 for_each_console(con) { @@ -2910,6 +2937,10 @@ void console_start(struct console *console) console_lock(); console->flags |=3D CON_ENABLED; console_unlock(); + + /* Wake the kthread printers. */ + wake_up_klogd(); + pr_flush(1000, true); } EXPORT_SYMBOL(console_start); @@ -3114,6 +3145,8 @@ void register_console(struct console *newcon) /* Begin with next message. */ newcon->seq =3D prb_next_seq(prb); } + if (kthreads_started) + start_printk_kthread(newcon); console_unlock(); console_sysfs_notify(); =20 @@ -3170,6 +3203,11 @@ int unregister_console(struct console *console) } } =20 + if (console->thread) { + kthread_stop(console->thread); + console->thread =3D NULL; + } + if (res) goto out_disable_unlock; =20 @@ -3275,6 +3313,13 @@ static int __init printk_late_init(void) ret =3D cpuhp_setup_state_nocalls(CPUHP_AP_ONLINE_DYN, "printk:online", console_cpu_notify, NULL); WARN_ON(ret < 0); + + console_lock(); + for_each_console(con) + start_printk_kthread(con); + kthreads_started =3D true; + console_unlock(); + return 0; } late_initcall(printk_late_init); @@ -3345,6 +3390,116 @@ bool pr_flush(int timeout_ms, bool reset_on_progres= s) } EXPORT_SYMBOL(pr_flush); =20 +static bool printer_should_wake(struct console *con, u64 seq) +{ + short flags; + + if (kthread_should_stop()) + return true; + + if (console_suspended) + return false; + + /* + * This is an unsafe read to con->flags, but false positives + * are not an issue as long as they are rare. + */ + flags =3D data_race(READ_ONCE(con->flags)); + if (!(flags & CON_ENABLED)) + return false; + + return prb_read_valid(prb, seq, NULL); +} + +static int printk_kthread_func(void *data) +{ + struct console *con =3D data; + char *dropped_text =3D NULL; + char *ext_text =3D NULL; + bool progress; + bool handover; + u64 seq =3D 0; + char *text; + int error; + + pr_info("%sconsole [%s%d]: printing thread started\n", + (con->flags & CON_BOOT) ? "boot" : "", + con->name, con->index); + + text =3D kmalloc(CONSOLE_LOG_MAX, GFP_KERNEL); + if (!text) + goto out; + + if (con->flags & CON_EXTENDED) { + ext_text =3D kmalloc(CONSOLE_EXT_LOG_MAX, GFP_KERNEL); + if (!ext_text) + goto out; + } else { + dropped_text =3D kmalloc(DROPPED_TEXT_MAX, GFP_KERNEL); + if (!dropped_text) + goto out; + } + + for (;;) { + error =3D wait_event_interruptible(log_wait, printer_should_wake(con, se= q)); + + if (kthread_should_stop()) + break; + + if (error) + continue; + + do { + console_lock(); + if (console_suspended) { + console_unlock(); + break; + } + + /* + * Even though the printk kthread is always preemptible, it is + * still not allowed to call cond_resched() from within + * console drivers. The task may become non-preemptible in the + * console driver call chain. For example, vt_console_print() + * takes a spinlock and then can call into fbcon_redraw(), + * which can conditionally invoke cond_resched(). + */ + console_may_schedule =3D 0; + progress =3D console_emit_next_record(con, text, ext_text, + dropped_text, &handover); + if (handover) + break; + + seq =3D con->seq; + + /* Unlock console without invoking direct printing. */ + __console_unlock(); + } while (progress); + } +out: + kfree(dropped_text); + kfree(ext_text); + kfree(text); + pr_info("%sconsole [%s%d]: printing thread stopped\n", + (con->flags & CON_BOOT) ? "boot" : "", + con->name, con->index); + return 0; +} + +/* Must be called within console_lock(). */ +static void start_printk_kthread(struct console *con) +{ + con->thread =3D kthread_run(printk_kthread_func, con, + "pr/%s%d", con->name, con->index); + if (IS_ERR(con->thread)) { + con->thread =3D NULL; + pr_err("%sconsole [%s%d]: unable to start printing thread\n", + (con->flags & CON_BOOT) ? "boot" : "", + con->name, con->index); + return; + } +} + /* * Delayed printk version, for scheduler-internal messages: */ @@ -3371,7 +3526,7 @@ static void wake_up_klogd_work_func(struct irq_work *= irq_work) } =20 if (pending & PRINTK_PENDING_WAKEUP) - wake_up_interruptible(&log_wait); + wake_up_interruptible_all(&log_wait); } =20 static DEFINE_PER_CPU(struct irq_work, wake_up_klogd_work) =3D --=20 2.30.2 From nobody Mon Jun 29 15:59:55 2026 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 1C8C9C4167E for ; Mon, 7 Feb 2022 19:46:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240844AbiBGTpQ (ORCPT ); Mon, 7 Feb 2022 14:45:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59314 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240744AbiBGTnf (ORCPT ); Mon, 7 Feb 2022 14:43:35 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5048C0401E1 for ; Mon, 7 Feb 2022 11:43:33 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263010; 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=Dj3bzcV/JKnAQq1HK9ibyS2ElcfEPKvgkUc6/7SCgNI=; b=nMmbmbR8ZEaKUEC8ojvRV2LQeX8JhrAilGvxEy3k6NRASW7nSHePovmBW6ILZr4wGq8/r7 jllZM49kj0wWi6JrjR2wSuqaSXnCRORPvOsMnwc7h5685P03Lb5UaUfXmJD0rc/U0LvPXg 0LVKOD1I6DioILulQasVPx1axwYS3axECmpx8No9wB1MhUJ2JjhFRptrnWbyEtL8Shh/UV mhKnovR4sNCtlsRrd0iJWQr2rWfTaYrCAmsF9Y0gcuAek0omQSUXA42veC/y3R2WDthfKw kKwBuEOwGllueocx//7KgNPjC8ICt8ewIAMCyyozEmc8sZsIgApoZB51+bPALw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263010; 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=Dj3bzcV/JKnAQq1HK9ibyS2ElcfEPKvgkUc6/7SCgNI=; b=vYRhR+IFj6maZm1JY7MMv1kHL1tg6bDVhYsb4biiLoTUlwc3QVia7oW7S5ntVFcFB+coLD 6wX+J/BhicZu9uAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v1 11/13] printk: reimplement console_lock for proper kthread support Date: Mon, 7 Feb 2022 20:49:21 +0106 Message-Id: <20220207194323.273637-12-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" With non-threaded console printers preemption is disabled while holding the console lock in order to avoid the situation where the console printer is scheduled away and no other task can lock the console (for printing or otherwise). Disabling preemption is necessary because the console lock is implemented purely as a semaphore, which has no owner. Like non-threaded console printers, kthread printers use the console lock to synchronize during printing. However, since they use console_lock() instead of a best-effort console_trylock(), it is not possible to disable preemption upon locking. Therefore an alternative for synchronizing and avoiding the above mentioned situation is needed. The kthread printers do not need to synchronize against each other, but they do need to synchronize against console_lock() callers. To provide this synchronization, introduce a per-console mutex. The mutex is taken by the kthread printer during printing and is also taken by console_lock() callers. Since mutexes have owners, when calling console_lock(), the scheduler is able to schedule any kthread printers that may have been preempted while printing. Rather than console_lock() callers holding the per-console mutex for the duration of the console lock, the per-console mutex is only taken in order to set a new CON_PAUSED flag, which is checked by the kthread printers. This avoids any issues due to nested locking between the various per-console mutexes. The kthread printers must also synchronize against console_trylock() callers. Since console_trylock() is non-blocking, a global atomic counter will be used to identify if any kthread printers are active. The kthread printers will also check the atomic counter to identify if the console has been locked by another task via console_trylock(). A locking overview for console_lock(), console_trylock(), and the kthread printers is as follows (pseudo code): console_lock() { down(&console_sem); for_each_console(con) { mutex_lock(&con->lock); con->flags |=3D CON_PAUSED; mutex_unlock(&con->lock); } } console_trylock() { assert(down_trylock(&console_sem)); assert(atomic_cmpxchg(&console_lock_count, 0, -1)); } kthread_printer() { mutex_lock(&con->lock); assert(con->flags & CON_PAUSED); assert(atomic_inc_unless_negative(&console_lock_count)); con->write(); atomic_dec(&console_lock_count); mutex_unlock(&con->lock); } Also note that the console owner and waiter logic now only applies between contexts that have both taken the console lock via console_trylock(). This is for 2 reasons: 1. Contexts that have taken the console lock via console_lock() require a sleepable context when unlocking to unpause the kthread printers. But a waiter context has used console_trylock() and may not be sleepable. 2. The kthread printers no longer acquire the console lock, so it is not possible to handover the console lock. This also has implications for console_unlock(), which attempts a console_trylock() before returning. Introduce console_trylock_sched() to allow console_unlock() to specify if it is in a sleepable context. Signed-off-by: John Ogness --- include/linux/console.h | 15 ++++ kernel/printk/printk.c | 190 +++++++++++++++++++++++++++++++--------- 2 files changed, 166 insertions(+), 39 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 0f94b1771df8..c51c7f5507a5 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -16,6 +16,7 @@ =20 #include #include +#include =20 struct vc_data; struct console_font_op; @@ -136,6 +137,7 @@ static inline int con_debug_leave(void) #define CON_ANYTIME (16) /* Safe to call before per-cpu resources ready */ #define CON_BRL (32) /* Used for a braille device */ #define CON_EXTENDED (64) /* Use the extended output format a la /dev/kmsg= */ +#define CON_PAUSED (128) /* Sleep while console is locked */ =20 struct console { char name[16]; @@ -155,6 +157,19 @@ struct console { unsigned long dropped; struct task_struct *thread; =20 + /* + * The per-console lock is used by printing kthreads to synchronize + * this console with callers of console_lock(). This is necessary in + * order to allow printing kthreads to run in parallel to each other, + * while each safely accessing their own @flags and synchronizing + * against direct printing via console_lock/console_unlock. + * + * Note: For synchronizing against direct printing via + * console_trylock/console_unlock, see the static global + * variable @console_lock_count. + */ + struct mutex lock; + void *data; struct console *next; }; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index e182f31fec58..135fbe647092 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -214,6 +214,26 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table,= int write, /* Number of registered extended console drivers. */ static int nr_ext_console_drivers; =20 +/* + * Used to synchronize printing kthreads against direct printing via + * console_trylock/console_unlock. + * + * Values: + * -1 =3D console locked (via trylock), kthreads will not print + * 0 =3D no kthread printing, console not locked (via trylock) + * >0 =3D kthread(s) actively printing + * + * Note: For synchronizing against direct printing via + * console_lock/console_unlock, see the @lock variable in + * struct console. + */ +static atomic_t console_lock_count =3D ATOMIC_INIT(0); + +#define console_excl_trylock() (atomic_cmpxchg(&console_lock_count, 0, -1)= =3D=3D 0) +#define console_excl_unlock() atomic_cmpxchg(&console_lock_count, -1, 0) +#define console_printer_tryenter() atomic_inc_unless_negative(&console_loc= k_count) +#define console_printer_exit() atomic_dec(&console_lock_count) + /* * Helper macros to handle lockdep when locking/unlocking console_sem. We = use * macros instead of functions so that _RET_IP_ contains useful informatio= n. @@ -256,6 +276,37 @@ static void __up_console_sem(unsigned long ip) } #define up_console_sem() __up_console_sem(_RET_IP_) =20 +/* + * Tracks whether kthread printers are all paused. A value of true implies + * that the console is locked via console_lock() or the console is suspend= ed. + * Reading and writing to this variable requires holding @console_sem. + */ +static bool consoles_paused; + +/* + * Pause or unpause all kthread printers. + * + * Requires the console_lock. + */ +static void __pause_all_consoles(bool do_pause) +{ + struct console *con; + + for_each_console(con) { + mutex_lock(&con->lock); + if (do_pause) + con->flags |=3D CON_PAUSED; + else + con->flags &=3D ~CON_PAUSED; + mutex_unlock(&con->lock); + } + + consoles_paused =3D do_pause; +} + +#define pause_all_consoles() __pause_all_consoles(true) +#define unpause_all_consoles() __pause_all_consoles(false) + /* * This is used for debugging the mess that is the VT code by * keeping track if we have the console semaphore held. It's @@ -2506,10 +2557,6 @@ void resume_console(void) down_console_sem(); console_suspended =3D 0; console_unlock(); - - /* Wake the kthread printers. */ - wake_up_klogd(); - pr_flush(1000, true); } =20 @@ -2547,6 +2594,7 @@ void console_lock(void) down_console_sem(); if (console_suspended) return; + pause_all_consoles(); console_locked =3D 1; console_may_schedule =3D 1; } @@ -2568,15 +2616,45 @@ int console_trylock(void) up_console_sem(); return 0; } + if (!console_excl_trylock()) { + up_console_sem(); + return 0; + } console_locked =3D 1; console_may_schedule =3D 0; return 1; } EXPORT_SYMBOL(console_trylock); =20 +/* + * A variant of console_trylock() that allows specifying if the context may + * sleep. If yes, a trylock on @console_sem is attempted and if successful, + * the threaded printers are paused. This is important to ensure that + * sleepable contexts do not become involved in console_lock handovers and + * will call cond_resched() during the printing loop. + */ +static int console_trylock_sched(bool may_schedule) +{ + if (!may_schedule) + return console_trylock(); + + might_sleep(); + + if (down_trylock_console_sem()) + return 0; + if (console_suspended) { + up_console_sem(); + return 0; + } + pause_all_consoles(); + console_locked =3D 1; + console_may_schedule =3D 1; + return 1; +} + int is_console_locked(void) { - return console_locked; + return (console_locked || atomic_read(&console_lock_count)); } EXPORT_SYMBOL(is_console_locked); =20 @@ -2610,6 +2688,19 @@ static inline bool console_is_usable(struct console = *con) static void __console_unlock(void) { console_locked =3D 0; + + /* + * Depending on whether console_lock() or console_trylock() was used, + * appropriately allow the kthread printers to continue. + */ + if (consoles_paused) + unpause_all_consoles(); + else + console_excl_unlock(); + + /* Wake the kthread printers. */ + wake_up_klogd(); + up_console_sem(); } =20 @@ -2632,7 +2723,8 @@ static void __console_unlock(void) * * @handover will be set to true if a printk waiter has taken over the * console_lock, in which case the caller is no longer holding the - * console_lock. Otherwise it is set to false. + * console_lock. Otherwise it is set to false. A NULL pointer may be provi= ded + * to disable allowing the console_lock to be taken over by a printk waite= r. */ static bool console_emit_next_record(struct console *con, char *text, char= *ext_text, char *dropped_text, bool *handover) @@ -2640,12 +2732,14 @@ static bool console_emit_next_record(struct console= *con, char *text, char *ext_ struct printk_info info; struct printk_record r; unsigned long flags; + bool allow_handover; char *write_text; size_t len; =20 prb_rec_init_rd(&r, &info, text, CONSOLE_LOG_MAX); =20 - *handover =3D false; + if (handover) + *handover =3D false; =20 if (!prb_read_valid(prb, con->seq, &r)) return false; @@ -2671,18 +2765,23 @@ static bool console_emit_next_record(struct console= *con, char *text, char *ext_ len =3D record_print_text(&r, console_msg_format & MSG_FORMAT_SYSLOG, pr= intk_time); } =20 - /* - * While actively printing out messages, if another printk() - * were to occur on another CPU, it may wait for this one to - * finish. This task can not be preempted if there is a - * waiter waiting to take over. - * - * Interrupts are disabled because the hand over to a waiter - * must not be interrupted until the hand over is completed - * (@console_waiter is cleared). - */ - printk_safe_enter_irqsave(flags); - console_lock_spinning_enable(); + /* Handovers may only happen between trylock contexts. */ + allow_handover =3D (handover && atomic_read(&console_lock_count) =3D=3D -= 1); + + if (allow_handover) { + /* + * While actively printing out messages, if another printk() + * were to occur on another CPU, it may wait for this one to + * finish. This task can not be preempted if there is a + * waiter waiting to take over. + * + * Interrupts are disabled because the hand over to a waiter + * must not be interrupted until the hand over is completed + * (@console_waiter is cleared). + */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + } =20 stop_critical_timings(); /* don't trace print latency */ call_console_driver(con, write_text, len, dropped_text); @@ -2690,8 +2789,10 @@ static bool console_emit_next_record(struct console = *con, char *text, char *ext_ =20 con->seq++; =20 - *handover =3D console_lock_spinning_disable_and_check(); - printk_safe_exit_irqrestore(flags); + if (allow_handover) { + *handover =3D console_lock_spinning_disable_and_check(); + printk_safe_exit_irqrestore(flags); + } =20 printk_delay(r.info->level); skip: @@ -2825,7 +2926,7 @@ void console_unlock(void) * Re-check if there is a new record to flush. If the trylock * fails, another context is already handling the printing. */ - } while (prb_read_valid(prb, next_seq, NULL) && console_trylock()); + } while (prb_read_valid(prb, next_seq, NULL) && console_trylock_sched(do_= cond_resched)); } EXPORT_SYMBOL(console_unlock); =20 @@ -2856,6 +2957,10 @@ void console_unblank(void) if (oops_in_progress) { if (down_trylock_console_sem() !=3D 0) return; + if (!console_excl_trylock()) { + up_console_sem(); + return; + } } else { pr_flush(1000, true); console_lock(); @@ -2937,10 +3042,6 @@ void console_start(struct console *console) console_lock(); console->flags |=3D CON_ENABLED; console_unlock(); - - /* Wake the kthread printers. */ - wake_up_klogd(); - pr_flush(1000, true); } EXPORT_SYMBOL(console_start); @@ -3135,7 +3236,11 @@ void register_console(struct console *newcon) if (newcon->flags & CON_EXTENDED) nr_ext_console_drivers++; =20 + if (consoles_paused) + newcon->flags |=3D CON_PAUSED; + newcon->dropped =3D 0; + mutex_init(&newcon->lock); if (newcon->flags & CON_PRINTBUFFER) { /* Get a consistent copy of @syslog_seq. */ mutex_lock(&syslog_lock); @@ -3397,16 +3502,17 @@ static bool printer_should_wake(struct console *con= , u64 seq) if (kthread_should_stop()) return true; =20 - if (console_suspended) - return false; - /* * This is an unsafe read to con->flags, but false positives * are not an issue as long as they are rare. */ flags =3D data_race(READ_ONCE(con->flags)); - if (!(flags & CON_ENABLED)) + + if (!(flags & CON_ENABLED) || + (flags & CON_PAUSED) || + atomic_read(&console_lock_count) =3D=3D -1) { return false; + } =20 return prb_read_valid(prb, seq, NULL); } @@ -3417,7 +3523,6 @@ static int printk_kthread_func(void *data) char *dropped_text =3D NULL; char *ext_text =3D NULL; bool progress; - bool handover; u64 seq =3D 0; char *text; int error; @@ -3450,9 +3555,17 @@ static int printk_kthread_func(void *data) continue; =20 do { - console_lock(); - if (console_suspended) { - console_unlock(); + error =3D mutex_lock_interruptible(&con->lock); + if (error) + break; + + if (!console_is_usable(con)) { + mutex_unlock(&con->lock); + break; + } + + if ((con->flags & CON_PAUSED) || !console_printer_tryenter()) { + mutex_unlock(&con->lock); break; } =20 @@ -3466,14 +3579,13 @@ static int printk_kthread_func(void *data) */ console_may_schedule =3D 0; progress =3D console_emit_next_record(con, text, ext_text, - dropped_text, &handover); - if (handover) - break; + dropped_text, NULL); =20 seq =3D con->seq; =20 - /* Unlock console without invoking direct printing. */ - __console_unlock(); + console_printer_exit(); + + mutex_unlock(&con->lock); } while (progress); } out: --=20 2.30.2 From nobody Mon Jun 29 15:59:56 2026 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 06FE7C4167B for ; Mon, 7 Feb 2022 19:46:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240813AbiBGTpO (ORCPT ); Mon, 7 Feb 2022 14:45:14 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59284 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240724AbiBGTnd (ORCPT ); Mon, 7 Feb 2022 14:43:33 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 48F48C0401E6 for ; Mon, 7 Feb 2022 11:43:32 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263010; 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=Wz4b8g7RhPiNelQL/klnUcQZ1lWWUK2rdxPkiBZW57s=; b=NkQ9Tzkk+waPE4Lx3hILnB/SEXPjt9O0DPteuf5yohGTXni5i6bt3h6TasqLt56Z0LAEUA a/Nq4Fvw53pRhkeAs7CAE5Ps3bkfYxefH4usfEOtyPKYAbrdXfDU8jXF3Si8EnEpmlV06/ 2l73Xi9e5r/VFxS1ad0ZM2weUGslPPyPI/0mTbNPSPGGR1mqEKHJIzUUt8ZJaqPAWlpsaU GE6lJ264fWXTvDQ4cepXLe03iSVmRpwiwqwJSQ0wUiQ2ZkwrWKaFoqWeDSSjJGck1Mm22a EvKUcI/PD1U5GCKlhMjUZBCxnzvxZc/fWIufOTJ4RA8FUK9N7sACU5xsrc7Hnw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263010; 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=Wz4b8g7RhPiNelQL/klnUcQZ1lWWUK2rdxPkiBZW57s=; b=VYiRfbKySMkonqCi+iIKU7JtpTkuoiQlT94ayGBwIImidq/e5p6/N8xGM1S8Op0ZZwofyo uC6KFmr+iDdetYAQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v1 12/13] printk: remove @console_locked Date: Mon, 7 Feb 2022 20:49:22 +0106 Message-Id: <20220207194323.273637-13-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" The static global variable @console_locked is used to help debug VT code to make sure that certain code paths are running with the console_lock held. However, this information is also available with the static global variable @consoles_paused (for locking via console_lock()), and the static global variable @console_lock_count (for locking via console_trylock()). Remove @console_locked and update is_console_locked() to use the alternative variables. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- kernel/printk/printk.c | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 135fbe647092..086155578f10 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -307,15 +307,7 @@ static void __pause_all_consoles(bool do_pause) #define pause_all_consoles() __pause_all_consoles(true) #define unpause_all_consoles() __pause_all_consoles(false) =20 -/* - * This is used for debugging the mess that is the VT code by - * keeping track if we have the console semaphore held. It's - * definitely not the perfect debug tool (we don't know if _WE_ - * hold it and are racing, but it helps tracking those weird code - * paths in the console code where we end up in places I want - * locked without the console semaphore held). - */ -static int console_locked, console_suspended; +static int console_suspended; =20 /* * Array of consoles built from command line options (console=3D) @@ -2595,7 +2587,6 @@ void console_lock(void) if (console_suspended) return; pause_all_consoles(); - console_locked =3D 1; console_may_schedule =3D 1; } EXPORT_SYMBOL(console_lock); @@ -2620,7 +2611,6 @@ int console_trylock(void) up_console_sem(); return 0; } - console_locked =3D 1; console_may_schedule =3D 0; return 1; } @@ -2647,14 +2637,25 @@ static int console_trylock_sched(bool may_schedule) return 0; } pause_all_consoles(); - console_locked =3D 1; console_may_schedule =3D 1; return 1; } =20 +/* + * This is used to help to make sure that certain paths within the VT code= are + * running with the console lock held. It is definitely not the perfect de= bug + * tool (it is not known if the VT code is the task holding the console lo= ck), + * but it helps tracking those weird code paths in the console code such as + * when the console is suspended: where the console is not locked but no + * console printing may occur. + * + * Note: This returns true when the console is suspended but is not locked. + * This is intentional because the VT code must consider that situat= ion + * the same as if the console was locked. + */ int is_console_locked(void) { - return (console_locked || atomic_read(&console_lock_count)); + return (consoles_paused || atomic_read(&console_lock_count)); } EXPORT_SYMBOL(is_console_locked); =20 @@ -2687,8 +2688,6 @@ static inline bool console_is_usable(struct console *= con) =20 static void __console_unlock(void) { - console_locked =3D 0; - /* * Depending on whether console_lock() or console_trylock() was used, * appropriately allow the kthread printers to continue. @@ -2966,7 +2965,6 @@ void console_unblank(void) console_lock(); } =20 - console_locked =3D 1; console_may_schedule =3D 0; for_each_console(c) if ((c->flags & CON_ENABLED) && c->unblank) --=20 2.30.2 From nobody Mon Jun 29 15:59:56 2026 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 AE541C43217 for ; Mon, 7 Feb 2022 19:46:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240556AbiBGTpG (ORCPT ); Mon, 7 Feb 2022 14:45:06 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59316 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240743AbiBGTnf (ORCPT ); Mon, 7 Feb 2022 14:43:35 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6F9D4C0401DA for ; Mon, 7 Feb 2022 11:43:34 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1644263011; 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=+2Lr9d9qHR05sofK1L6gRmeoW4SDlcmhBs2p6+5unzY=; b=Ye6Rq+yPI0pCCoA1ujHlzs6Hut1pSADQzp1YfshvjDIyKFSP9dINja/z0yiDEzW/x04OzP JM06uk+EqWX8b4j0Dtu3CrJ2ZHvACglmdRx3BZ67bAAhEyXagw10XLJWP3dae3elrDKSyU /cFEGpg11bRTusJgJeCpW3U4+SxGQlfPRPz7bC6QOGGzCNTZF6BqdPriVTnvyMDJ58QkIi cseJ0wilL4O1ClqttjT7VNz+ck1mo9whfQqXTjKyBRCzMBf1/NcKcL4851RDKNLm79//Bk rm2RPkGx7CkQPFMqBcgqUjSgwXNNjhfYcIFbF0ewKDfoQcxAucWVStfI/33UAA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1644263011; 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=+2Lr9d9qHR05sofK1L6gRmeoW4SDlcmhBs2p6+5unzY=; b=tqjp5W7SLXHhL+7/7InwEQae83NyGcoKqnf64AdDtX9zW1j1+SP32XyV6LQI1bRpmTuefc /9+RuiKBj655JVCQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman , Jiri Slaby , Max Filippov , David Sterba , =?UTF-8?q?Samuel=20Iglesias=20Gons=C3=A1lvez?= , Bhaskar Chowdhury , Igor Matheus Andrade Torrente , Tetsuo Handa , nick black Subject: [PATCH printk v1 13/13] console: introduce CON_MIGHT_SLEEP for vt Date: Mon, 7 Feb 2022 20:49:23 +0106 Message-Id: <20220207194323.273637-14-john.ogness@linutronix.de> In-Reply-To: <20220207194323.273637-1-john.ogness@linutronix.de> References: <20220207194323.273637-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Type: text/plain; charset="utf-8" Deadlocks and the framebuffer console have been a recurring issue that is getting worse. Daniel Vetter suggested [0] that fbcon->write() should no longer be called from an atomic context. Introduce a new console flag CON_MIGHT_SLEEP for a console driver to specify that it is only called from sleepable contexts. Set the fbcon to use this new flag. [0] https://lore.kernel.org/all/YYuS1uNhxWOEX1Ci@phenom.ffwll.local Signed-off-by: John Ogness --- drivers/tty/vt/vt.c | 2 +- include/linux/console.h | 1 + kernel/printk/printk.c | 2 ++ 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/drivers/tty/vt/vt.c b/drivers/tty/vt/vt.c index 7359c3e80d63..ab4712cc9327 100644 --- a/drivers/tty/vt/vt.c +++ b/drivers/tty/vt/vt.c @@ -3161,7 +3161,7 @@ static struct console vt_console_driver =3D { .write =3D vt_console_print, .device =3D vt_console_device, .unblank =3D unblank_screen, - .flags =3D CON_PRINTBUFFER, + .flags =3D CON_PRINTBUFFER|CON_MIGHT_SLEEP, .index =3D -1, }; #endif diff --git a/include/linux/console.h b/include/linux/console.h index c51c7f5507a5..ea52c56b3ff8 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -138,6 +138,7 @@ static inline int con_debug_leave(void) #define CON_BRL (32) /* Used for a braille device */ #define CON_EXTENDED (64) /* Use the extended output format a la /dev/kmsg= */ #define CON_PAUSED (128) /* Sleep while console is locked */ +#define CON_MIGHT_SLEEP (256) /* Can only be called from sleepable context= */ =20 struct console { char name[16]; diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 086155578f10..b92ef67a5aa2 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2836,6 +2836,8 @@ static bool console_flush_all(bool do_cond_resched, u= 64 *next_seq, bool *handove =20 if (!console_is_usable(con)) continue; + if ((con->flags & CON_MIGHT_SLEEP) && !do_cond_resched) + continue; any_usable =3D true; =20 if (con->flags & CON_EXTENDED) { --=20 2.30.2