From patchwork Tue Sep 19 23:08:49 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 142188 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp3834735vqi; Tue, 19 Sep 2023 19:56:34 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEyBgiWXrd5r+Rd74rCf1imGPlvOtKhW+KkSR1aZr06s0AhuwaOLdVHjMOw76Ecj5pL/5a1 X-Received: by 2002:a05:6a20:1585:b0:154:9222:684a with SMTP id h5-20020a056a20158500b001549222684amr1557382pzj.24.1695178593899; Tue, 19 Sep 2023 19:56:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1695178593; cv=none; d=google.com; s=arc-20160816; b=vCq8qY6FOV1wi6Kj7TTfLyamrWfRgTsI/AX5WPTueEsnpZW/enyViPImNlnb6rEaB7 04+n94j2GfyFSI4aVpSxBTrxza/k8pvqsc8WTqEGI5qnTmreNsy69oLSnZgA8crRPZMr M7X3yPIQwjARtL77EnX9IO4qWQHh9GBzgrAToMVYOjEJMihaM5h0qdcPIGxFCfBHjAPw fioz6kBn463B5Tv6t0Dh/xeEc3pDY/T/5Hv+bbZleVW+Gc/KVaVTSujABpLOG6+iPTw0 jNu3dH2f8KkNDz1QAHBfOiBuioZQMTmy06gQFMiD9vGkFei9oBypRkaW04nKVkXU/Cts RbRQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:dkim-signature :dkim-signature:from; bh=RXYljq1O5mwfZU+NDYT3eBF6WGuYpS+t9+Qc7DTVOZs=; fh=/NZX815RJy/ecOe1WbZ023sSZUJKZnpaeIZwQG5Ym7U=; b=AgkEy5WEfK2k/L5SnXvWMYEpvyBKFJu0CsE2H9cSyoGPk4b63RYshNz6RN0pGjqo0c CfYaghb1T/CnXK7y/d05mRAeJb0xaP/eH8SV6npCt0d/J3EQVOq4FFDLk8nYVrNFRtiN 7w1h+JQJF7OsaD5ic+Xgxzoo/xIgwiSOC6kANBM0Nv2mtVp578heAZWvgzWxwcKvefEx nTgcTkNtV4rem6YE8nHhyLNegkF+x2ZEvvv1jQfYQTQsVK1DsXeBvOv+Kw9Igk3tSXxC Te3jnBrI7HWCv8c5qsYGQCYGcPSB2ndobAHjFP4jC3q/UISmW1xB0F1rogb00FvaY6Nt NlWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0n9rSWKK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from snail.vger.email (snail.vger.email. [23.128.96.37]) by mx.google.com with ESMTPS id br2-20020a17090b0f0200b002684bc84493si577722pjb.131.2023.09.19.19.56.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Sep 2023 19:56:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) client-ip=23.128.96.37; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=0n9rSWKK; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.37 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by snail.vger.email (Postfix) with ESMTP id D4FAB81D7A7F; Tue, 19 Sep 2023 16:09:55 -0700 (PDT) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at snail.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233675AbjISXJe (ORCPT + 26 others); Tue, 19 Sep 2023 19:09:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:42292 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233399AbjISXJU (ORCPT ); Tue, 19 Sep 2023 19:09:20 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id ACC62C0 for ; Tue, 19 Sep 2023 16:09:12 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1695164950; 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=RXYljq1O5mwfZU+NDYT3eBF6WGuYpS+t9+Qc7DTVOZs=; b=0n9rSWKKgFMMM8oe7OxgVm25UuFUBlbW3YAo2v2icNrgjeHRizUJRTeSvdYT/eGtVcIKJp vjFkpAkv6JhdILbdaNar/Oq0AUKclYJykk+XE3DwtMKMr8FEc7H+1SaJH25CtVSuHK98ez WwHcrluKf3t95h8bAz12tGmzMFDSf6+/jogu926ViKIIYtUAF+G1Tz8vx++q9tfw/Pj2o4 xounm7rjfOk6MvfeuxnCZ0PLv6l9SSFbqTHZQ5rbFR8jD+dXYcV+AhvyPeM6DKLRG4tBMs yIllyCVpEpRJEjNbm+qMif91wyzGcF33SOvNDJvM0qn2yVJvqmo/tDDJh/bNZg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1695164950; 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=RXYljq1O5mwfZU+NDYT3eBF6WGuYpS+t9+Qc7DTVOZs=; b=SF2ALktfgmDTzBDMjoVe+9Uw9oVXGPK8VIxJwdu/Otl/ztdreIl9fLaUgdg5d9EvAO66IX 8+xRq32MGQwOnXBQ== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v2 04/11] printk: nbcon: Provide functions to mark atomic write sections Date: Wed, 20 Sep 2023 01:14:49 +0206 Message-Id: <20230919230856.661435-5-john.ogness@linutronix.de> In-Reply-To: <20230919230856.661435-1-john.ogness@linutronix.de> References: <20230919230856.661435-1-john.ogness@linutronix.de> MIME-Version: 1.0 X-Spam-Status: No, score=-3.9 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INVALID_DATE_TZ_ABSURD, RCVD_IN_DNSWL_MED,SPF_HELO_NONE,SPF_PASS autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (snail.vger.email [0.0.0.0]); Tue, 19 Sep 2023 16:09:56 -0700 (PDT) X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1777523589653532637 X-GMAIL-MSGID: 1777523589653532637 From: Thomas Gleixner WARN/OOPS/PANIC require printing out immediately since the regular printing method (and in the future, the printing threads) might not be able to run. Add per-CPU state to denote the priority/urgency of the output and provide functions to mark the beginning and end of sections where the urgent messages are generated. Note that when a CPU is in a priority elevated state, flushing only occurs when dropping back to a lower priority. This allows the full set of printk records (WARN/OOPS/PANIC output) to be stored in the ringbuffer before beginning to flush the backlog. Co-developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner (Intel) --- include/linux/console.h | 4 ++ kernel/printk/nbcon.c | 89 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+) diff --git a/include/linux/console.h b/include/linux/console.h index e4fc6f7c1496..25a3ddd39083 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -452,10 +452,14 @@ static inline bool console_is_registered(const struct console *con) hlist_for_each_entry(con, &console_list, node) #ifdef CONFIG_PRINTK +extern enum nbcon_prio nbcon_atomic_enter(enum nbcon_prio prio); +extern void nbcon_atomic_exit(enum nbcon_prio prio, enum nbcon_prio prev_prio); extern bool nbcon_can_proceed(struct nbcon_write_context *wctxt); extern bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt); extern bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt); #else +static inline enum nbcon_prio nbcon_atomic_enter(enum nbcon_prio prio) { return NBCON_PRIO_NONE; } +static inline void nbcon_atomic_exit(enum nbcon_prio prio, enum nbcon_prio prev_prio) { } static inline bool nbcon_can_proceed(struct nbcon_write_context *wctxt) { return false; } static inline bool nbcon_enter_unsafe(struct nbcon_write_context *wctxt) { return false; } static inline bool nbcon_exit_unsafe(struct nbcon_write_context *wctxt) { return false; } diff --git a/kernel/printk/nbcon.c b/kernel/printk/nbcon.c index b96077152f49..9359906b575b 100644 --- a/kernel/printk/nbcon.c +++ b/kernel/printk/nbcon.c @@ -961,6 +961,95 @@ static bool nbcon_emit_next_record(struct nbcon_write_context *wctxt) return nbcon_context_exit_unsafe(ctxt); } +/** + * struct nbcon_cpu_state - Per CPU printk context state + * @prio: The current context priority level + * @nesting: Per priority nest counter + */ +struct nbcon_cpu_state { + enum nbcon_prio prio; + int nesting[NBCON_PRIO_MAX]; +}; + +static DEFINE_PER_CPU(struct nbcon_cpu_state, nbcon_pcpu_state); +static struct nbcon_cpu_state early_nbcon_pcpu_state __initdata; + +/** + * nbcon_get_cpu_state - Get the per CPU console state pointer + * + * Returns either a pointer to the per CPU state of the current CPU or to + * the init data state during early boot. + */ +static __ref struct nbcon_cpu_state *nbcon_get_cpu_state(void) +{ + if (!printk_percpu_data_ready()) + return &early_nbcon_pcpu_state; + + return this_cpu_ptr(&nbcon_pcpu_state); +} + +/** + * nbcon_atomic_enter - Enter a context that enforces atomic printing + * @prio: Priority of the context + * + * Return: The previous priority that needs to be fed into + * the corresponding nbcon_atomic_exit() + * Context: Any context. Disables migration. + */ +enum nbcon_prio nbcon_atomic_enter(enum nbcon_prio prio) +{ + struct nbcon_cpu_state *cpu_state; + enum nbcon_prio prev_prio; + + migrate_disable(); + + cpu_state = nbcon_get_cpu_state(); + + prev_prio = cpu_state->prio; + if (prio > prev_prio) + cpu_state->prio = prio; + + /* + * Increment the nesting on @cpu_state->prio (instead of + * @prio) so that a WARN() nested within a panic printout + * does not attempt to scribble state. + */ + cpu_state->nesting[cpu_state->prio]++; + + return prev_prio; +} + +/** + * nbcon_atomic_exit - Exit a context that enforces atomic printing + * @prio: Priority of the context to leave + * @prev_prio: Priority of the previous context for restore + * + * Context: Any context. Enables migration. + * + * @prev_prio is the priority returned by the corresponding + * nbcon_atomic_enter(). + */ +void nbcon_atomic_exit(enum nbcon_prio prio, enum nbcon_prio prev_prio) +{ + struct nbcon_cpu_state *cpu_state; + + cpu_state = nbcon_get_cpu_state(); + + /* + * Undo the nesting of nbcon_atomic_enter() at the CPU state + * priority. + */ + cpu_state->nesting[cpu_state->prio]--; + + /* + * Restore the previous priority, which was returned by + * nbcon_atomic_enter(). + */ + cpu_state->prio = prev_prio; + + migrate_enable(); +} + /** * nbcon_alloc - Allocate buffers needed by the nbcon console * @con: Console to allocate buffers for