From nobody Mon Apr 6 04:57:29 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 8D13EC6FA82 for ; Sat, 10 Sep 2022 22:30:22 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229534AbiIJWaU (ORCPT ); Sat, 10 Sep 2022 18:30:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47700 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230421AbiIJW2o (ORCPT ); Sat, 10 Sep 2022 18:28:44 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BC6E3459A7 for ; Sat, 10 Sep 2022 15:28:13 -0700 (PDT) Message-ID: <20220910222301.824778546@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662848891; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1nKppJo7BxmMJFXIfi8EoIqbK71Dxa8Haxjpt9z15Tk=; b=g5J80fbLvcjgxTYMH/kc3+rSHiZf9tIk3s1vdHkcLgJfjdFUugd9s6TV48/9SOtc9JpDkt JH9SnrWIBdUlbFCU8akBmSuSm+UVdI2Ilcj3RR6Quevp2Y0s+e5R64MlVFaliyhsySXYn3 UAOCpzcmYFLYcCZYZTI2nah7UYOqhi8j2t7JtKoa2ZGm59htiRxPx+eXccSdCY0TiViJLm TaFChIZ+cm5SKeb0vCA2yuOG1WQYx4uYVFbFAadgDMQKu3Wyz3vOm74/TSErGk0Xa0klF8 9SR5MwBtyqSJXVYrAgzTBsrD82FZoGKnXqQgAvVpL526/XnSOU45qrfcctQApg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662848891; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: references:references; bh=1nKppJo7BxmMJFXIfi8EoIqbK71Dxa8Haxjpt9z15Tk=; b=MCwdSrA92NhXmAsBZe8KYswZIlVESP+CVhdL4sdzlA3yE2+ZjhM71Dlvjcj5X2Kxip1pqr Kwv8Zv3ZsX3pnhAg== From: Thomas Gleixner To: LKML Cc: John Ogness , Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , Peter Zijlstra , "Paul E. McKenney" , Daniel Vetter , Greg Kroah-Hartman , Helge Deller , Jason Wessel , Daniel Thompson , John Ogness Subject: [patch RFC 25/29] printk: Provide functions to emit a ringbuffer record on non-BKL consoles References: <20220910221947.171557773@linutronix.de> MIME-Version: 1.0 Date: Sun, 11 Sep 2022 00:28:10 +0200 (CEST) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" Utilize the shared fill function and add the required safety points to check for handover/takeover and invoke the atomic write function of the console driver. Add the proper handling for updating the sequence number. Co-Developed-by: John Ogness Signed-off-by: John Ogness Signed-off-by: Thomas Gleixner --- include/linux/console.h | 7 ++ kernel/printk/printk_nobkl.c | 107 ++++++++++++++++++++++++++++++++++++++= ++++- 2 files changed, 112 insertions(+), 2 deletions(-) --- a/include/linux/console.h +++ b/include/linux/console.h @@ -281,10 +281,12 @@ struct console; * @newseq: The sequence number for progress * @prio: Priority of the context * @txtbuf: Pointer to the text buffer for this context + * @dropped: Dropped counter for the current context * @thread: The acquire is printk thread context * @hostile: Hostile takeover requested. Cleared on normal * acquire or friendly handover * @spinwait: Spinwait on acquire if possible + * @backlog: Ringbuffer has pending records */ struct cons_context { struct console *console; @@ -297,9 +299,11 @@ struct cons_context { unsigned int spinwait_max_us; enum cons_prio prio; struct cons_text_buf *txtbuf; + unsigned long dropped; unsigned int thread : 1; unsigned int hostile : 1; unsigned int spinwait : 1; + unsigned int backlog : 1; }; =20 /** @@ -380,6 +384,9 @@ struct console { #ifndef CONFIG_64BIT atomic_t __private atomic_seq; #endif + + bool (*write_atomic)(struct console *con, struct cons_write_context *wctx= t); + struct cons_context_data __percpu *pcpu_data; struct cons_context_data ctxt_data; }; --- a/kernel/printk/printk_nobkl.c +++ b/kernel/printk/printk_nobkl.c @@ -328,7 +328,7 @@ static void cons_context_sequence_init(s * invalid. Caller has to reacquire the console. */ #ifdef CONFIG_64BIT -static bool __maybe_unused cons_sequence_try_update(struct cons_context *c= txt) +static bool cons_sequence_try_update(struct cons_context *ctxt) { struct console *con =3D ctxt->console; struct cons_state old, new; @@ -354,7 +354,7 @@ static bool __maybe_unused cons_sequence return true; } #else -static bool __maybe_unused cons_sequence_try_update(struct cons_context *c= txt) +static bool cons_sequence_try_update(struct cons_context *ctxt) { struct console *con =3D ctxt->console; unsigned long old, new, cur; @@ -1006,6 +1006,109 @@ bool console_exit_unsafe(struct cons_wri return __console_update_unsafe(wctxt, false); } =20 +static bool cons_fill_outbuf(struct cons_outbuf_desc *desc); + +/** + * cons_get_record - Fill the buffer with the next pending ringbuffer reco= rd + * @wctxt: The write context which will be handed to the write function + * + * Returns: True if there are records to print. If the output buffer is + * filled @wctxt->outbuf points to the text, otherwise it is NULL. + * + * False signals that there are no pending records anymore and + * the printing can stop. + */ +static bool cons_get_record(struct cons_write_context *wctxt) +{ + struct cons_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con =3D ctxt->console; + struct cons_outbuf_desc desc =3D { + .txtbuf =3D ctxt->txtbuf, + .extmsg =3D con->flags & CON_EXTENDED, + .seq =3D ctxt->newseq, + .dropped =3D ctxt->dropped, + }; + bool progress =3D cons_fill_outbuf(&desc); + + ctxt->newseq =3D desc.seq; + ctxt->dropped =3D desc.dropped; + + wctxt->pos =3D 0; + wctxt->len =3D desc.len; + wctxt->outbuf =3D desc.outbuf; + return progress; +} + +/** + * cons_emit_record - Emit record in the acquired context + * @wctxt: The write context which will be handed to the write function + * + * Returns: False if the operation was aborted (takeover) + * True otherwise + * + * In case of takeover the caller is not allowed to touch console state. + * The console is owned by someone else. If the caller wants to print + * more it has to reacquire the console first. + * + * If it returns true @wctxt->ctxt.backlog indicates whether there are + * still records pending in the ringbuffer, + */ +static int __maybe_unused cons_emit_record(struct cons_write_context *wctx= t) +{ + struct cons_context *ctxt =3D &ACCESS_PRIVATE(wctxt, ctxt); + struct console *con =3D ctxt->console; + bool done =3D false; + + /* + * @con->dropped is not protected in case of hostile takeovers so + * the update below is racy. Annotate it accordingly. + */ + ctxt->dropped =3D data_race(READ_ONCE(con->dropped)); + + /* Fill the output buffer with the next record */ + ctxt->backlog =3D cons_get_record(wctxt); + if (!ctxt->backlog) + return true; + + /* Safety point. Don't touch state in case of takeover */ + if (!console_can_proceed(wctxt)) + return false; + + /* Counterpart to the read above */ + WRITE_ONCE(con->dropped, ctxt->dropped); + + /* + * In case of skipped records, Update sequence state in @con. + */ + if (!wctxt->outbuf) + goto update; + + /* Tell the driver about potential unsafe state */ + wctxt->unsafe =3D ctxt->state.unsafe; + + if (!ctxt->thread && con->write_atomic) { + done =3D con->write_atomic(con, wctxt); + } else { + cons_release(ctxt); + WARN_ON_ONCE(1); + return false; + } + + /* If not done, the write was aborted due to takeover */ + if (!done) + return false; + + ctxt->newseq++; +update: + /* + * The sequence update attempt is not part of console_release() + * because in panic situations the console is not released by + * the panic CPU until all records are written. On 32bit the + * sequence is seperate from state anyway. + */ + return cons_sequence_try_update(ctxt); +} + /** * cons_nobkl_init - Initialize the NOBKL console state * @con: Console to initialize