Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp3612013rwn; Sat, 10 Sep 2022 16:11:06 -0700 (PDT) X-Google-Smtp-Source: AA6agR5M00SrPhnBTGOG3L4/3eog/AIGggCgNZqmVqXN3vG1D0mgzNBv41R5WZnRxIPWKSkPFyYw X-Received: by 2002:a17:907:2bf8:b0:770:837a:e3b8 with SMTP id gv56-20020a1709072bf800b00770837ae3b8mr13263113ejc.562.1662851466452; Sat, 10 Sep 2022 16:11:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662851466; cv=none; d=google.com; s=arc-20160816; b=GVdLux+tU66c0OBuCGjYta41TFpb8h4jIA83NR61jv/G/LenYK5xIwjFAotOP3jo3d rfLzhAikF+92KTFp4HFI4/2gdd3UwDFcs1twSNTj36YURKlNquNytduv059+eIhn+907 i8zEkIHzcps9OuSRCQsp3wwXu/HKBFzGi7RRKtAwGFB9LAmYwvTYwDIwGYgRO6lQaDlv 3EjnbEg3S0PLBvEhCeX1hAMNoJGSO0LT+MBYlZzk6HdLFUl9F0E79/IGV9X93ZBmyS4r aZMu5BqEKFjaQnY6Kvl+UvXbHkFGVm21ZMciOxHpizOF4bYlyItNvY0QTv0820UzJoBf +f/A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:date:mime-version:references:subject:cc:to:from :dkim-signature:dkim-signature:message-id; bh=fGXSmzhEoK8b0GTobsLbcAxSd+NNxBh/Huh2f6MgBe8=; b=zqNJJCmU5eILLUbI2l2tdFa0+f4yrz6GORulHNGU+8OasXFlJ4Mm2qWNZ7E+1/uiOF JQCalOdvPwN0FDm+OopTIjEjomGijUQjOFXsd5cw8do/6xRINket+14/sNE9FFpEyQbH C3xNQCatwr76AHxitFDvJfoEn8ThLW1ru3nW3vEg0co6rla0ChoPFJBG4Oezn8UlEuIv t0hWYSH4/O5FRJncjXhX2flcxoKZcd/OkwgoC0Hg5zejgirsaKkQtSWcLWGw0pqxMP+/ qpcgSKL7IL4+yQkGQ+DrUlwwrkWTfToTl95zYNLwwU9GbIr8ABLfcA5LP13lRZ1Smui+ ZDDg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="oEoG4XV/"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=XNjATjL3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l26-20020a50d6da000000b00445d00730b3si3984513edj.416.2022.09.10.16.10.36; Sat, 10 Sep 2022 16:11:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="oEoG4XV/"; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=XNjATjL3; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230434AbiIJW2v (ORCPT + 99 others); Sat, 10 Sep 2022 18:28:51 -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 Content-Type: text/plain; charset=UTF-8 Date: Sun, 11 Sep 2022 00:27:50 +0200 (CEST) X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_PASS,T_SCC_BODY_TEXT_LINE 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 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 #include -#include +#include #include 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); /** + * 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); /* * 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); } +bool console_srcu_read_lock_is_held(void) +{ + return srcu_read_lock_held(&console_srcu); +} #endif 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); /* Ensure this flag is always set for the head of the list */ cons_first()->flags |= CON_CONSDEV; @@ -3245,6 +3250,7 @@ void register_console(struct console *ne newcon->seq = prb_next_seq(prb); } console_unlock(); + /* No need to synchronize SRCU here! */ console_sysfs_notify(); /* @@ -3290,7 +3296,7 @@ static int console_unregister_locked(str if (hlist_unhashed(&console->node)) goto out_unlock; - hlist_del_init(&console->node); + hlist_del_init_rcu(&console->node); /* * @@ -3305,6 +3311,10 @@ static int console_unregister_locked(str cons_first()->flags |= CON_CONSDEV; console_unlock(); + + /* Ensure that all SRCU list walks have completed */ + synchronize_srcu(&console_srcu); + console_sysfs_notify(); if (console->exit)