From nobody Mon Apr 6 04:56:42 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 8EC25ECAAD3 for ; Sat, 10 Sep 2022 22:28:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230128AbiIJW2w (ORCPT ); Sat, 10 Sep 2022 18:28:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46232 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230135AbiIJW2B (ORCPT ); Sat, 10 Sep 2022 18:28:01 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D911D42AEA for ; Sat, 10 Sep 2022 15:27:53 -0700 (PDT) Message-ID: <20220910222301.084428332@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1662848871; 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=fGXSmzhEoK8b0GTobsLbcAxSd+NNxBh/Huh2f6MgBe8=; b=oEoG4XV/p63GR35K1PB67q56l0RUQRKw6YTPQ6y3RalqcnfpaMMmKI3NSxD0i0qIm2CMRm +ULaMQMVKoCodg2N6gDErwVLpyx9bw4pRcNB6PmFCxWzDxtTQUSGrgzbIBr0mtuvP0/Ozq 3BWp1QpP5x8fRcNkxC0H1XUrUCXlyVb2JsrxYUj/kYtZv7veSbGvCPE5c7mcylreff3Fjc g4Dh96r9+i819DPBNfIysF9G2EPbI3/QhaRs4pq0fn2o8ovOwH8uWFrIhFAAgj2KgsTpJE ZdCK1ora8+Iusf+S/+3rMd3ABaPewKlCSxtpsaLso2HxBxc2x8anqqDhSphYew== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1662848871; 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=fGXSmzhEoK8b0GTobsLbcAxSd+NNxBh/Huh2f6MgBe8=; b=XNjATjL37sd9bZqMc50Uk1ZjkOB2XZLy9+WnHzUcStr8G587JSVK2QVuK/z+MfYlvEN3ta zTxv+K0dAQU52NAQ== 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 Subject: [patch RFC 12/29] printk: Prepare for SCRU console list protection References: <20220910221947.171557773@linutronix.de> MIME-Version: 1.0 Date: Sun, 11 Sep 2022 00:27:50 +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" Provide a SRCU protected variant to walk the console list. Preperatory change for a new console infrastructure which operates independent of console BKL. Signed-off-by: Thomas Gleixner --- include/linux/console.h | 14 +++++++++++++- kernel/printk/printk.c | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) --- a/include/linux/console.h +++ b/include/linux/console.h @@ -15,7 +15,7 @@ #define _LINUX_CONSOLE_H_ 1 =20 #include -#include +#include #include =20 struct vc_data; @@ -161,6 +161,7 @@ struct console { #ifdef CONFIG_LOCKDEP extern void lockdep_assert_console_lock_held(void); extern void lockdep_assert_console_list_lock_held(void); +extern bool console_srcu_read_lock_is_held(void); #else static inline void lockdep_assert_console_lock_held(void) { } static inline void lockdep_assert_console_list_lock_held(void) { } @@ -172,6 +173,17 @@ extern void console_list_lock(void) __ac extern void console_list_unlock(void) __releases(console_mutex); =20 /** + * for_each_console_srcu() - Iterator over registered consoles + * @con: struct console pointer used as loop cursor + * + * Requires console_srcu_read_lock to be held. Can be invoked from + * any context. + */ +#define for_each_console_srcu(con) \ + hlist_for_each_entry_srcu(con, &console_list, node, \ + console_srcu_read_lock_is_held()) + +/** * for_each_registered_console() - Iterator over registered consoles * @con: struct console pointer used as loop cursor * --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -90,6 +90,7 @@ static DEFINE_MUTEX(console_mutex); static DEFINE_SEMAPHORE(console_sem); HLIST_HEAD(console_list); EXPORT_SYMBOL_GPL(console_list); +DEFINE_STATIC_SRCU(console_srcu); =20 /* * System may need to suppress printk message under certain @@ -118,6 +119,10 @@ void lockdep_assert_console_list_lock_he lockdep_assert_held(&console_mutex); } =20 +bool console_srcu_read_lock_is_held(void) +{ + return srcu_read_lock_held(&console_srcu); +} #endif =20 enum devkmsg_log_bits { @@ -3227,9 +3232,9 @@ void register_console(struct console *ne */ console_lock(); if (newcon->flags & CON_CONSDEV || hlist_empty(&console_list)) - hlist_add_head(&newcon->node, &console_list); + hlist_add_head_rcu(&newcon->node, &console_list); else - hlist_add_behind(&newcon->node, console_list.first); + hlist_add_behind_rcu(&newcon->node, console_list.first); =20 /* Ensure this flag is always set for the head of the list */ cons_first()->flags |=3D CON_CONSDEV; @@ -3245,6 +3250,7 @@ void register_console(struct console *ne newcon->seq =3D prb_next_seq(prb); } console_unlock(); + /* No need to synchronize SRCU here! */ console_sysfs_notify(); =20 /* @@ -3290,7 +3296,7 @@ static int console_unregister_locked(str if (hlist_unhashed(&console->node)) goto out_unlock; =20 - hlist_del_init(&console->node); + hlist_del_init_rcu(&console->node); =20 /* * @@ -3305,6 +3311,10 @@ static int console_unregister_locked(str cons_first()->flags |=3D CON_CONSDEV; =20 console_unlock(); + + /* Ensure that all SRCU list walks have completed */ + synchronize_srcu(&console_srcu); + console_sysfs_notify(); =20 if (console->exit)