From patchwork Sun Feb 18 18:57:21 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 202854 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:693c:2685:b0:108:e6aa:91d0 with SMTP id mn5csp913279dyc; Sun, 18 Feb 2024 11:02:47 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCUPzN/NW9m1FuVeZpdTzFcUWzUe75qEef39P/mLrj53rk/SVDiR/U3VWVKZjPoITtV1Zph3An8JcA6gvcxOF2HB4eBnJQ== X-Google-Smtp-Source: AGHT+IHDQAVmdA3jlj5DIXdNNoEG9vHdyj4bjcjJrMNObd04g1PudUcFN12ZEcr40wI4zQPljo/v X-Received: by 2002:a05:6402:2051:b0:564:41a5:b976 with SMTP id bc17-20020a056402205100b0056441a5b976mr1777756edb.10.1708282966884; Sun, 18 Feb 2024 11:02:46 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1708282966; cv=pass; d=google.com; s=arc-20160816; b=W/j+KhU4Ndhv0zy/j6ccSxUtmEALlL0gyVF87I85/BID5un1oApldu6g4JBauFSJ4K AK1dWx5omf6+793De1xsiJbKa9JQDIXos5/me2hGzZpzb6hebayT7zyBDxCQQZCA6h+5 uKkUbmufAxgphCAQZuUF3bJ1EGGTI59fgBA0e889rZnmSEfoe8XRwMkYTTOMFbV5nEFd 50FtYTDatHoNCLKno/NcoZDICbP79KTTFPTLRPh7j3tf6n4GSrWOPulStg2tG1+ciw6E HPcmqSd1d3JbF1i8DIFzz6zmP5vOynQqAKOt+R7lLwP8UZiMePNjFPdY9+AFE/PxmN3S 3DiQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:dkim-signature:dkim-signature:from; bh=gPQBcPF1TwjA3+5rOjGCeC1igurDrs1roxu0N8F4XyM=; fh=+yDMW7z2YTzd5MLGkAWGqbGBg6o8yp6ZPnyDOkf7LF8=; b=jTDLkLWy/KGFxFR52vfQ5Mlhpd83PVGk2mkuGXZdPjS/h0TNjI97XqjjtF8lTzxziD Xmt0mcWuaW9fg3/HIgnvJZemp0ZIuodiRE+bBhQmzfLcvglfHcMjJgb/huhpilM1OSpl d44n2euhj/Vz3v7fU7REIq4o7E6TrqulqjoMDp1c1kiex01nwBG9JF4WqbHbI5VWLZRk gYpzmzyA/5mM1gVDm4EXyizOoBwZuTIlteMUkPJnc1FWIqUwDI/uS61wNwsYtcbSnRaA ru7d9XLwH1DL3JEdZAAmuS7cgf4d1kPl6fuFr7dbtJeyaCSflIZWaiDRGkUjpZwzzaTI KmPA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Lh2fhBGX; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-70518-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70518-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id l25-20020aa7d959000000b005643b74ad91si1091893eds.483.2024.02.18.11.02.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 18 Feb 2024 11:02:46 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-70518-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Lh2fhBGX; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; arc=pass (i=1 spf=pass spfdomain=linutronix.de dkim=pass dkdomain=linutronix.de dmarc=pass fromdomain=linutronix.de); spf=pass (google.com: domain of linux-kernel+bounces-70518-ouuuleilei=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-70518-ouuuleilei=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 5B8981F21175 for ; Sun, 18 Feb 2024 19:02:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 1B2C376C96; Sun, 18 Feb 2024 18:58:05 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="Lh2fhBGX"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="JfHPw2o3" Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38E0D73168 for ; Sun, 18 Feb 2024 18:57:56 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282680; cv=none; b=PcMyacDuDXGNX3b/yePeBt4YduGU8jL9B5PEKlrlTdL9/Jld22cEESEg+25QeZOmE7zHR3EKtUSzB7wwple5E0J3Bp3lGAnqaTDeHNC9Q48lMwDULzCsMSd0K85gPafeGSVj0CS/jrugISUzWUHxvW5j0ghOYceOFms+gN68fa8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1708282680; c=relaxed/simple; bh=pfK9qq9Tq8FSQB42rxPrvRkgcIJuLdvvsicEAYeKWHY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=owuA/VnO8X1PE/Qtt9I38T0bSGEzxI1lhFJU8ejO7uRmDRqbJhJYYLvtjlT7WM9TC/RcaPAyoQV/OMv+xEw/A457hnLggxwoQavOCXLwWYBDiHqeLjRwZmkEhHzWcagu3Pzdeh85cX4dakcmGJ8eto3rM/Ka1OqHY9zWNz860Dk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=Lh2fhBGX; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=JfHPw2o3; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1708282675; 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=gPQBcPF1TwjA3+5rOjGCeC1igurDrs1roxu0N8F4XyM=; b=Lh2fhBGXPEgFw0mxMWnVOm8TVECrZ8oSZeW1DqPuvyLg1K6EOxJ1YltQR3GFeVffPmLeEz UJsPBSJjeoYDTAOwY3CAQ7VT1dppthLr3OfLwstrC22Wk7QYtL5fulgWq93PIyyJSdvocr vNG6MNRG8TjgMqNiVuVLYAc4uHTirPbRvwCF/Kep93cVgxH5x62PiwBzGqV+KsZJFr/F1t tTOn2DCCr0uVyohozvfp7oV9IiHgSKuPM/t4FEkO0HORUloe3kWwEo5814T2Z5xIN4QFeK k85YZxNRhU5XYFWCWBavSCSwyz9TBgoy77P/3Htcopis1b4HlDOiG9TEhpLe+Q== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1708282675; 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=gPQBcPF1TwjA3+5rOjGCeC1igurDrs1roxu0N8F4XyM=; b=JfHPw2o3dPQ75TjOWAnNmUeoHWZOYo4xQfB2ra+PsaPjxjR4/cXbQzuej3vXFNHdr0KSDB ciyVw65Fm4ESxhBg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Andrew Morton , Josh Poimboeuf , "Peter Zijlstra (Intel)" , Uros Bizjak , "Guilherme G. Piccoli" , Kefeng Wang , Arnd Bergmann Subject: [PATCH printk v2 21/26] printk: Coordinate direct printing in panic Date: Sun, 18 Feb 2024 20:03:21 +0106 Message-Id: <20240218185726.1994771-22-john.ogness@linutronix.de> In-Reply-To: <20240218185726.1994771-1-john.ogness@linutronix.de> References: <20240218185726.1994771-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1791264520319508568 X-GMAIL-MSGID: 1791264520319508568 Perform printing by nbcon consoles on the panic CPU from the printk() caller context in order to get panic messages printed as soon as possible. If legacy and nbcon consoles are registered, the legacy consoles will no longer perform direct printing on the panic CPU until after the backtrace has been stored. This will give the safe nbcon consoles a chance to print the panic messages before allowing the unsafe legacy consoles to print. If no nbcon consoles are registered, there is no change in behavior (i.e. legacy consoles will always attempt to print from the printk() caller context). Signed-off-by: John Ogness --- include/linux/printk.h | 5 ++++ kernel/panic.c | 2 ++ kernel/printk/printk.c | 53 ++++++++++++++++++++++++++++++++++++------ 3 files changed, 53 insertions(+), 7 deletions(-) diff --git a/include/linux/printk.h b/include/linux/printk.h index fe37395af9e2..a2d40a637226 100644 --- a/include/linux/printk.h +++ b/include/linux/printk.h @@ -197,6 +197,7 @@ void show_regs_print_info(const char *log_lvl); extern asmlinkage void dump_stack_lvl(const char *log_lvl) __cold; extern asmlinkage void dump_stack(void) __cold; void printk_trigger_flush(void); +void printk_legacy_allow_panic_sync(void); extern void uart_nbcon_acquire(struct uart_port *up); extern void uart_nbcon_release(struct uart_port *up); void nbcon_atomic_flush_unsafe(void); @@ -280,6 +281,10 @@ static inline void printk_trigger_flush(void) { } +static inline void printk_legacy_allow_panic_sync(void) +{ +} + static inline void uart_nbcon_acquire(struct uart_port *up) { } diff --git a/kernel/panic.c b/kernel/panic.c index c039f8e1ddae..86813305510f 100644 --- a/kernel/panic.c +++ b/kernel/panic.c @@ -370,6 +370,8 @@ void panic(const char *fmt, ...) */ atomic_notifier_call_chain(&panic_notifier_list, 0, buf); + printk_legacy_allow_panic_sync(); + panic_print_sys_info(false); kmsg_dump(KMSG_DUMP_PANIC); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 2c4ab717c110..109cfdd988aa 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2329,12 +2329,23 @@ int vprintk_store(int facility, int level, return ret; } +static bool legacy_allow_panic_sync; + +/* + * This acts as a one-way switch to allow legacy consoles to print from + * the printk() caller context on a panic CPU. + */ +void printk_legacy_allow_panic_sync(void) +{ + legacy_allow_panic_sync = true; +} + asmlinkage int vprintk_emit(int facility, int level, const struct dev_printk_info *dev_info, const char *fmt, va_list args) { + bool do_trylock_unlock = printing_via_unlock; int printed_len; - bool in_sched = false; /* Suppress unimportant messages after panic happens */ if (unlikely(suppress_printk)) @@ -2350,15 +2361,43 @@ asmlinkage int vprintk_emit(int facility, int level, if (level == LOGLEVEL_SCHED) { level = LOGLEVEL_DEFAULT; - in_sched = true; + /* If called from the scheduler, we can not call up(). */ + do_trylock_unlock = false; } printk_delay(level); printed_len = vprintk_store(facility, level, dev_info, fmt, args); - /* If called from the scheduler, we can not call up(). */ - if (!in_sched && printing_via_unlock) { + if (!have_boot_console && have_nbcon_console) { + bool is_panic_context = this_cpu_in_panic(); + + /* + * In panic, the legacy consoles are not allowed to print from + * the printk calling context unless explicitly allowed. This + * gives the safe nbcon consoles a chance to print out all the + * panic messages first. This restriction only applies if + * there are nbcon consoles registered. + */ + if (is_panic_context) + do_trylock_unlock &= legacy_allow_panic_sync; + + /* + * There are situations where nbcon atomic printing should + * happen in the printk() caller context: + * + * - When this CPU is in panic. + * + * Note that if boot consoles are registered, the + * console_lock/console_unlock dance must be relied upon + * instead because nbcon consoles cannot print simultaneously + * with boot consoles. + */ + if (is_panic_context) + nbcon_atomic_flush_all(); + } + + if (do_trylock_unlock) { /* * The caller may be holding system-critical or * timing-sensitive locks. Disable preemption during @@ -2378,10 +2417,10 @@ asmlinkage int vprintk_emit(int facility, int level, preempt_enable(); } - if (in_sched) - defer_console_output(); - else + if (do_trylock_unlock) wake_up_klogd(); + else + defer_console_output(); return printed_len; }