Received: by 2002:ab2:1149:0:b0:1f3:1f8c:d0c6 with SMTP id z9csp2357556lqz; Tue, 2 Apr 2024 15:15:04 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXExSggB915EOx1EjQKbA6Q30m1gYwIq6DknzSMvedinu89qv0l/DKoagY5D7TdHTTaIvw6Ci62UizaSJeq13IyXyfpot0j7ltpx3KJxw== X-Google-Smtp-Source: AGHT+IHJpnvhQMCsFa6Dv7ynjZqOi+mEPojLmOy1Gm9dxBsgUedISRHrNp4DeOH4fmLX3j8tX7qp X-Received: by 2002:a05:622a:144b:b0:432:cac3:7b3c with SMTP id v11-20020a05622a144b00b00432cac37b3cmr15770694qtx.10.1712096104695; Tue, 02 Apr 2024 15:15:04 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1712096104; cv=pass; d=google.com; s=arc-20160816; b=BiheI1gIqFBNoq5+23wOZ19tzN7AaJZiDMwS64AjRww3Os+5n14z5jtD9rE90u5uIB HnsX1kvXmR1Ok/wowzOReOT4Bsjyjb1VzDmRL22doO1TAYoBdHuO+Izb5erxfhLkwaG1 PVsIC+FkGNbM8mwhd/R/RLk94FHKR8RAMpornbiYfAisE86Bvl0iFha/5iTm25BR05y8 X3s/cwL7ncMD7L6Ugox9SKj2EgLFmPImbTh0pAKzSH44+sadtNtB+rNxzR23mmstv/Q8 0asQihxH9zPO9NAU5D+LquYknwsGG2kiT+DPITDlz96gDWwYUMFSI10JSlIHRqlODfnE oZ/g== 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=3qTtvBCC1FB1iFzJGtCPvpWwllmrX3We+4s6ahKMrS0=; fh=pKnPKUsir0uEGSsor+4Zc2vgbu+g+ayvUgsdzkuXaoA=; b=glik0DGjJIAGJmKJrUqBDhIXeapglT1AuiNaFsQwMa0nRydEjmE/ArLpcV7KduMXhY ITJqeU9HuMjNEfibzyiDWsMk9CjVOLxFD5g6RbB5TSQSWC5hYPvq2UgH9D7SjFDU8FBM I13idD5y4Gd6OVst+PCFVw4HqhAEqx32cdQ7dQHzmQ9dL2hGx5rEeFVixndTgNjvEovK 4bVbw2d2HkzOYlx/zgf6hVtSQvBWJonq8qVlZTQrjqlV0fNaiq0CJp5B50iBzFUCoxV0 NG5DMgbwjvyjnfql+s1UFAaWtqmq+w4A6V1jwJA4QDsL35rLwoGgGMX4swSlRP0ANpBt lQ3Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=4KsA66rD; 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-128777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v2-20020a05622a130200b004313fb77480si13334335qtk.360.2024.04.02.15.15.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 02 Apr 2024 15:15:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-128777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=4KsA66rD; 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-128777-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-128777-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 5900B1C222E1 for ; Tue, 2 Apr 2024 22:15:04 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 4558316190A; Tue, 2 Apr 2024 22:11:48 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="4KsA66rD"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="hUMvRiV+" 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 07CE2160784 for ; Tue, 2 Apr 2024 22:11:45 +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=1712095907; cv=none; b=MMqz8nnEWGoZ+k/TWw9iUMWpNAwUNY0RUuTPJAGd5DPR0F8hAXetQUK51Ng8lrGNeGonHkzJQwUp13dK6yh3vpza2WmpKIfxnZ48smNzKmn2CKw1J6rXf+9uUT4HaYok4UB4u3tADRnyjz7O4JqKpeIQ156YAv2Zr1+6WXMwqfM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712095907; c=relaxed/simple; bh=z0EAC/Y2Ie4y+zt3dvWpk09O5Hod2omIJg/QrXj5w3w=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dgHwfGex0CDQG4SoUd3BsWhqPeFZKPjmdZIz3Ag3uwCohHYu2Ongt7CQNw/cZXAdC/P5tKW/iQMrgu8sALt8gz9RM8MfB/R1Qm/l27Mon3zGLeTDMvgpuIu+OxvjJxshZpqoqA5r4HlS1+C1q7Qz8htmm6UJzW/D0u3AqjynwDI= 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=4KsA66rD; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=hUMvRiV+; 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=1712095903; 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=3qTtvBCC1FB1iFzJGtCPvpWwllmrX3We+4s6ahKMrS0=; b=4KsA66rD8Wvz1i0pvurd3ZFbVX0oZI7AfS+9c/84YSI7cH3C5Qcp1BYnr3MvUBCZVCe/bu h4vD1Zp4+u7KHckUojG9BtolHFTHLTNMXnl2dxbhnyhp9Uee4rPca+jIQLBEtaJU3ifzBw wn7WJ41TOUpSgvYsCLtx2+DDnCY6zmfTxsbXD9lNir/p64AZlF53UMDJ/PX8jDbaucigms b2W9x0h0O0yeUCJBwAuCX2xFlKoqTI64fZB6N9rcaZTBWhcBYexfuogCw0DcevBc+I/MWA SNLkJnHLGFhtn2k1R8Gg4FRvR5LRiHdu5gE4jzrz/pcSKN8lqKv7bOj8UxRMtQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1712095903; 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=3qTtvBCC1FB1iFzJGtCPvpWwllmrX3We+4s6ahKMrS0=; b=hUMvRiV+NewBAKe1O2C8hxq93H/Qjj85yZcvC29QEaVNLQUinI7YGTpbT/tuJ1XS3k6myB 4UW7ymwLq+HkAdAA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org Subject: [PATCH printk v4 17/27] printk: nbcon: Use nbcon consoles in console_flush_all() Date: Wed, 3 Apr 2024 00:17:19 +0206 Message-Id: <20240402221129.2613843-18-john.ogness@linutronix.de> In-Reply-To: <20240402221129.2613843-1-john.ogness@linutronix.de> References: <20240402221129.2613843-1-john.ogness@linutronix.de> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Allow nbcon consoles to print messages in the legacy printk() caller context (printing via unlock) by integrating them into console_flush_all(). The write_atomic() callback is used for printing. Provide nbcon_legacy_emit_next_record(), which acts as the nbcon variant of console_emit_next_record(). Call this variant within console_flush_all() for nbcon consoles. Since nbcon consoles use their own @nbcon_seq variable to track the next record to print, this also must be appropriately handled. Signed-off-by: John Ogness --- kernel/printk/internal.h | 6 ++++ kernel/printk/nbcon.c | 77 ++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 17 ++++++--- 3 files changed, 95 insertions(+), 5 deletions(-) diff --git a/kernel/printk/internal.h b/kernel/printk/internal.h index a8df764fd0c5..acf53c35b7a0 100644 --- a/kernel/printk/internal.h +++ b/kernel/printk/internal.h @@ -78,6 +78,8 @@ void defer_console_output(void); u16 printk_parse_prefix(const char *text, int *level, enum printk_info_flags *flags); +void console_lock_spinning_enable(void); +int console_lock_spinning_disable_and_check(int cookie); u64 nbcon_seq_read(struct console *con); void nbcon_seq_force(struct console *con, u64 seq); @@ -85,6 +87,8 @@ bool nbcon_alloc(struct console *con); void nbcon_init(struct console *con); void nbcon_free(struct console *con); void nbcon_atomic_flush_pending(void); +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie); /* * Check if the given console is currently capable and allowed to print @@ -140,6 +144,8 @@ static inline bool nbcon_alloc(struct console *con) { return false; } static inline void nbcon_init(struct console *con) { } static inline void nbcon_free(struct console *con) { } static inline void nbcon_atomic_flush_pending(void) { } +static inline bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie) { return false; } static inline bool console_is_usable(struct console *con, short flags) { return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index fcdab2eaaedb..599fff3c0ab3 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -541,6 +541,7 @@ static struct printk_buffers panic_nbcon_pbufs; * nbcon_context_try_acquire - Try to acquire nbcon console * @ctxt: The context of the caller * + * Context: Any context which could not be migrated to another CPU. * Return: True if the console was acquired. False otherwise. * * If the caller allowed an unsafe hostile takeover, on success the @@ -935,6 +936,82 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) return nbcon_context_exit_unsafe(ctxt); } +/** + * nbcon_atomic_emit_one - Print one record for an nbcon console using the + * write_atomic() callback + * @wctxt: An initialized write context struct to use for this context + * + * Return: False if it is known there are no more records to print, + * otherwise true. + * + * This is an internal helper to handle the locking of the console before + * calling nbcon_emit_next_record(). + */ +static bool nbcon_atomic_emit_one(struct nbcon_write_context *wctxt) +{ + struct nbcon_context *ctxt = &ACCESS_PRIVATE(wctxt, ctxt); + + if (!nbcon_context_try_acquire(ctxt)) + return true; + + /* + * nbcon_emit_next_record() returns false when the console was + * handed over or taken over. In both cases the context is no + * longer valid. + */ + if (!nbcon_emit_next_record(wctxt)) + return true; + + nbcon_context_release(ctxt); + + return ctxt->backlog; +} + +/** + * nbcon_legacy_emit_next_record - Print one record for an nbcon console + * in legacy contexts + * @con: The console to print on + * @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 + * both the console_lock and the SRCU read lock. Otherwise it + * is set to false. + * @cookie: The cookie from the SRCU read lock. + * + * Context: Any context except NMI. + * Return: False if the given console has no next record to print, + * otherwise true. + * + * This function is meant to be called by console_flush_all() to print records + * on nbcon consoles from legacy context (printing via console unlocking). + * Essentially it is the nbcon version of console_emit_next_record(). + */ +bool nbcon_legacy_emit_next_record(struct console *con, bool *handover, + int cookie) +{ + struct nbcon_write_context wctxt = { }; + struct nbcon_context *ctxt = &ACCESS_PRIVATE(&wctxt, ctxt); + unsigned long flags; + bool progress; + + *handover = false; + + /* Use the same procedure as console_emit_next_record(). */ + printk_safe_enter_irqsave(flags); + console_lock_spinning_enable(); + stop_critical_timings(); + + ctxt->console = con; + ctxt->prio = NBCON_PRIO_NORMAL; + + progress = nbcon_atomic_emit_one(&wctxt); + + start_critical_timings(); + *handover = console_lock_spinning_disable_and_check(cookie); + printk_safe_exit_irqrestore(flags); + + return progress; +} + /** * __nbcon_atomic_flush_pending_con - Flush specified nbcon console using its * write_atomic() callback diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index a1b3309e12c1..df84c6bfbb2d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -1860,7 +1860,7 @@ static bool console_waiter; * there may be a waiter spinning (like a spinlock). Also it must be * ready to hand over the lock at the end of the section. */ -static void console_lock_spinning_enable(void) +void console_lock_spinning_enable(void) { /* * Do not use spinning in panic(). The panic CPU wants to keep the lock. @@ -1899,7 +1899,7 @@ static void console_lock_spinning_enable(void) * * Return: 1 if the lock rights were passed, 0 otherwise. */ -static int console_lock_spinning_disable_and_check(int cookie) +int console_lock_spinning_disable_and_check(int cookie) { int waiter; @@ -2951,13 +2951,20 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove cookie = console_srcu_read_lock(); for_each_console_srcu(con) { short flags = console_srcu_read_flags(con); + u64 printk_seq; bool progress; if (!console_is_usable(con, flags)) continue; any_usable = true; - progress = console_emit_next_record(con, handover, cookie); + if (flags & CON_NBCON) { + progress = nbcon_legacy_emit_next_record(con, handover, cookie); + printk_seq = nbcon_seq_read(con); + } else { + progress = console_emit_next_record(con, handover, cookie); + printk_seq = con->seq; + } /* * If a handover has occurred, the SRCU read lock @@ -2967,8 +2974,8 @@ static bool console_flush_all(bool do_cond_resched, u64 *next_seq, bool *handove return false; /* Track the next of the highest seq flushed. */ - if (con->seq > *next_seq) - *next_seq = con->seq; + if (printk_seq > *next_seq) + *next_seq = printk_seq; if (!progress) continue; -- 2.39.2