Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp1681030rwb; Fri, 23 Sep 2022 17:08:14 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7moSh5RXd2QuV9jZCU6Bs83Er22DMh84+LNLCsb8ez8aO4gZBatqv3F0G0uJLMjaqsUIhy X-Received: by 2002:a17:907:7284:b0:780:2b33:7f18 with SMTP id dt4-20020a170907728400b007802b337f18mr9419640ejc.620.1663978093993; Fri, 23 Sep 2022 17:08:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663978093; cv=none; d=google.com; s=arc-20160816; b=0m5MzGnAqQPxzf3ds78krCToZHEo/8ntic9oHDEW88rCtv1a0Kngc0SIlLQxEYBOPd xH12fKYFNlV9QtznKLyz8BCso7SGjt0++IL4olniMEhdp32j+5yDPfpRk8QZSYO2vHWw Y9ykjjP+oIcxHz1VUn5apmKwnSpVzrevWXc194nXOUiNbKKsf1cZpL7mLxZ4JVwXjzce nuOGsFrnMoD6ZqhHi93ft+Eef3XVngnORTSeTGy39dGaUzFSBQ6zrAkO420mjX0ijOpW DArgWOUj3FBO1wgv6XXympvDoHpQSXA2RjyhuaMF6xNniWpbeqXcw5x0cQ4Hdg/VQUrd oDlw== 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=DfVMZJNRNmUcXs0AcEgf8PmYZBx9ybi5NZy3duOSBFA=; b=T1HTCW1TltsbVZl2SsSk6VImkwot070esIH2RWuBGPPke78hP5Bep3Gd/iwZ4OzIpu MD1ZNm761MFkIqNAiai+euicJHWDQ0UeQqW4q0vJk06owtcINgo9jmE6ErCG1TsbyH9o iayoB+5qeBBeT6RSuqwGfvrwLl39NDtlAufX1gJxRBM25fF8WDKO+vDchXrL5C82qYfl as88j7kGQJ9zcuMN1MpS+9PqOgE8346UxwvwvouWec7PXGSEqnegCou7B/g2My5AwwpB Uf38OO5F4zaQdWmhbs42/VkazpPRHHsWQCFWDLpIv/zeE9/tlhnnOFv9By8hCJED2U93 Y4yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=Zow98vZH; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=qb7loi7g; 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 ds2-20020a170907724200b00782d5dbe2b5si1294161ejc.746.2022.09.23.17.07.48; Fri, 23 Sep 2022 17:08:13 -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=Zow98vZH; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=qb7loi7g; 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 S233093AbiIXAFt (ORCPT + 99 others); Fri, 23 Sep 2022 20:05:49 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54736 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232981AbiIXAFL (ORCPT ); Fri, 23 Sep 2022 20:05:11 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 51119A2220 for ; Fri, 23 Sep 2022 17:05:06 -0700 (PDT) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1663977903; 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=DfVMZJNRNmUcXs0AcEgf8PmYZBx9ybi5NZy3duOSBFA=; b=Zow98vZHUhmdqMsJES3M8pCx7McItBn90Y/GKqk3vKRNbclqVUaov3IAkPienr20Ndlrxu tYBHN9v1A4lJ0U8xdPow1fcAHflWhjwTXU+GNT3/goFE8SgWLeDIWCKXfmv6nhT6pp4jhK MQeBQsq0B3rpi/gOn/n1cbHN2dHAxr4ZK8YRQJk12mMH3tvrUGNflxfBrYQ7fC212nSzpU vC8OXucQG03GD5u0f9R5nhZhGmvXqjVBRdhae9d9oupqMd5XZ4Vbu/ekXePHk7HhNBNl6U aBXR2vC9C0nWcmS/clVrc1wwPs3dMYnIPTJKA0rD40OABRS11H0FrmrRSRKxSQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1663977903; 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=DfVMZJNRNmUcXs0AcEgf8PmYZBx9ybi5NZy3duOSBFA=; b=qb7loi7gRnwUl6Y/52LX2Rz/INvaxFAKfEz5BoDrnfE/NNdN0SBbAXEool0QD9LHVeRXYZ dXfaJ+cbHx2U9IBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk 12/18] printk: Prepare for SCRU console list protection Date: Sat, 24 Sep 2022 02:10:48 +0206 Message-Id: <20220924000454.3319186-13-john.ogness@linutronix.de> In-Reply-To: <20220924000454.3319186-1-john.ogness@linutronix.de> References: <20220924000454.3319186-1-john.ogness@linutronix.de> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 From: Thomas Gleixner 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 Signed-off-by: John Ogness --- include/linux/console.h | 14 +++++++++++++- kernel/printk/printk.c | 16 +++++++++++++--- 2 files changed, 26 insertions(+), 4 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 1e3d0a50cef1..dc0df9d9e7d9 100644 --- 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) { } @@ -171,6 +172,17 @@ extern struct hlist_head console_list; extern void console_list_lock(void) __acquires(console_mutex); 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 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index f1d31dcbd6ba..7e6d1cd34452 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -93,6 +93,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 @@ -121,6 +122,10 @@ void lockdep_assert_console_list_lock_held(void) 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 { @@ -3232,9 +3237,9 @@ void register_console(struct console *newcon) */ 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; @@ -3250,6 +3255,7 @@ void register_console(struct console *newcon) newcon->seq = prb_next_seq(prb); } console_unlock(); + /* No need to synchronize SRCU here! */ console_sysfs_notify(); /* @@ -3295,7 +3301,7 @@ static int console_unregister_locked(struct console *console) if (hlist_unhashed(&console->node)) goto out_unlock; - hlist_del_init(&console->node); + hlist_del_init_rcu(&console->node); /* * @@ -3310,6 +3316,10 @@ static int console_unregister_locked(struct console *console) 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) -- 2.30.2