From patchwork Mon Nov 14 16:29:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 19889 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2238142wru; Mon, 14 Nov 2022 08:31:25 -0800 (PST) X-Google-Smtp-Source: AA0mqf6x2YBPS2oMGXjaCaeczm8OZ7dWRqAOHPZ1sCTq0mx9Deobp4R6c0OVtZCvF6bmT3OXVNHk X-Received: by 2002:a17:90b:3d10:b0:212:ed36:fc74 with SMTP id pt16-20020a17090b3d1000b00212ed36fc74mr14807030pjb.172.1668443485038; Mon, 14 Nov 2022 08:31:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668443485; cv=none; d=google.com; s=arc-20160816; b=hfclbKwRU5V3sQDPsSnhWThtFyzkt8ZlMNQrVSTqkodAYTMAF9tVDrKgjvHEYQ3EZx hIdnruSk/Jsap9J1U28YwBTABcHW8HI7ZouVHeUF9HtcfWA/nxyLR6xrouasU3afU1/4 O4H8MvsVpF3YebhL3xHm+SbtOKZzSzBjsUK+25lkbxWM83QPRfTe4aS6EDw8PStr4mEU ZC+t/Bw8BM5eWC+8sM+lR6X23Th7m3aRrDF6cL48lzIhroVrQdqrRtYfH7BJy7EeZD+h ZrLouPtTSaHZZx0tzqb6WUjkiSunCwgqxVGt0lvLHjUtWqdS39qlqqutEdLNUteroS33 h/ug== 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=yNQONpXKLQCgtojOiA7woSE6WWehDrB23B3xD86mEsY=; b=R3xIe05G7K5Z73Oh/uUOy85oVThaNgEDYn2pk+qO9JrIpB8Wpf7UPDByczCZN4WOSa iHlcukKxleqp36Qk2yOlR2VhVuVVXEFkyy7Ban4npKfMrMXMUI02RdTOT5jdl/0TCxTo /flkCmV+XtB9v8Eu1JYfF35+T7tuHM4IWlM4q+qJeXnlR/Om+eVp2YpjyEd568i75tyd np9P39xOxqDwh+ifIaYbJBdj+Vt5SywOLRs8Y0/7TuDN5l7RJpVoHPIHgRDTW2bCW+NR WpqTGgRVa9mmzn5f+YPnJIi8wLwIpq100Vaa9ZPiXc5uI1fn5X9XiZSDaedGRfA+Sxa9 YMAQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b="SGGd/1Vc"; dkim=neutral (no key) header.i=@linutronix.de header.b=xwbmKegz; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id c8-20020a630d08000000b004629cd1dbb1si9804531pgl.329.2022.11.14.08.30.52; Mon, 14 Nov 2022 08:31:25 -0800 (PST) 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="SGGd/1Vc"; dkim=neutral (no key) header.i=@linutronix.de header.b=xwbmKegz; 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 S237606AbiKNQa1 (ORCPT + 99 others); Mon, 14 Nov 2022 11:30:27 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35750 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237555AbiKNQ3q (ORCPT ); Mon, 14 Nov 2022 11:29:46 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0E452EB1 for ; Mon, 14 Nov 2022 08:29:40 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668443379; 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=yNQONpXKLQCgtojOiA7woSE6WWehDrB23B3xD86mEsY=; b=SGGd/1Vc58i2rYuzhVWBqWHjszRgMZhvnT3oKayx2GYx8/INV2Ype+7smcVWafRhlvCjsV dlFlOD2YIZru6VoP5bfq3KHTWxneCjpgDPUU9FZJdE7VdnZaIXcn8lOQuWonP1wUE55SJl aZ4ElXS0zoytLriQLgKBm7W73gXDCXsEC59rX+unP1M5AFR4iZDC6LBRFi6t8y/5QCHMut TuWpzZ8aHePjJGNKLFOA0+fzo4jQKvBrX6zgklJzVNXkru+1+Cnlu3wWqsM45ZBifxCAx3 7bwI4GqLAZJmhqMXHwyGVxz7VDih4xcq+QimpQXvQrJJcae3W2WvtgWVKqO5Hw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668443379; 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=yNQONpXKLQCgtojOiA7woSE6WWehDrB23B3xD86mEsY=; b=xwbmKegzg6Lrvh1pBjkU3Brmk9CyefQmno6CuRD8GILGq6wDoOyIK4OWzsLrOE4M32qFvf kMk6pMrHBfUsxTBw== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 11/39] console: introduce wrappers to read/write console flags Date: Mon, 14 Nov 2022 17:35:04 +0106 Message-Id: <20221114162932.141883-12-john.ogness@linutronix.de> In-Reply-To: <20221114162932.141883-1-john.ogness@linutronix.de> References: <20221114162932.141883-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-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1749489795619884941?= X-GMAIL-MSGID: =?utf-8?q?1749489795619884941?= After switching to SRCU for console list iteration, some readers will begin readings console->flags as a data race. Locklessly reading console->flags provides a consistent value because there is at most one CPU modifying console->flags and that CPU is using only read-modify-write operations. Introduce a wrapper for SRCU iterators to read console flags. Introduce a matching wrapper to write to flags of registered consoles. Writing to flags of registered consoles is synchronized by the console_list_lock. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 45 +++++++++++++++++++++++++++++++++++++++++ kernel/printk/printk.c | 10 ++++----- 2 files changed, 50 insertions(+), 5 deletions(-) diff --git a/include/linux/console.h b/include/linux/console.h index 24d83e24840b..c1ca461d088a 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -183,6 +183,51 @@ extern void console_list_unlock(void) __releases(console_mutex); extern struct hlist_head console_list; +/** + * console_srcu_read_flags - Locklessly read the console flags + * @con: struct console pointer of console to read flags from + * + * This function provides the necessary READ_ONCE() and data_race() + * notation for locklessly reading the console flags. The READ_ONCE() + * in this function matches the WRITE_ONCE() when @flags are modified + * for registered consoles with console_srcu_write_flags(). + * + * Only use this function to read console flags when locklessly + * iterating the console list via srcu. + * + * Context: Any context. + */ +static inline short console_srcu_read_flags(const struct console *con) +{ + WARN_ON_ONCE(!console_srcu_read_lock_is_held()); + + /* + * Locklessly reading console->flags provides a consistent + * read value because there is at most one CPU modifying + * console->flags and that CPU is using only read-modify-write + * operations to do so. + */ + return data_race(READ_ONCE(con->flags)); +} + +/** + * console_srcu_write_flags - Write flags for a registered console + * @con: struct console pointer of console to write flags to + * @flags: new flags value to write + * + * Only use this function to write flags for registered consoles. It + * requires holding the console_list_lock. + * + * Context: Any context. + */ +static inline void console_srcu_write_flags(struct console *con, short flags) +{ + lockdep_assert_console_list_lock_held(); + + /* This matches the READ_ONCE() in console_srcu_read_flags(). */ + WRITE_ONCE(con->flags, flags); +} + /** * for_each_console_srcu() - 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 e3f81dda5b09..41c90b768c1c 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3068,7 +3068,7 @@ void console_stop(struct console *console) __pr_flush(console, 1000, true); console_list_lock(); console_lock(); - console->flags &= ~CON_ENABLED; + console_srcu_write_flags(console, console->flags & ~CON_ENABLED); console_unlock(); console_list_unlock(); @@ -3086,7 +3086,7 @@ void console_start(struct console *console) { console_list_lock(); console_lock(); - console->flags |= CON_ENABLED; + console_srcu_write_flags(console, console->flags | CON_ENABLED); console_unlock(); console_list_unlock(); __pr_flush(console, 1000, true); @@ -3314,7 +3314,7 @@ void register_console(struct console *newcon) } else if (newcon->flags & CON_CONSDEV) { /* Only the new head can have CON_CONSDEV set. */ - console_first()->flags &= ~CON_CONSDEV; + console_srcu_write_flags(console_first(), console_first()->flags & ~CON_CONSDEV); hlist_add_head_rcu(&newcon->node, &console_list); } else { @@ -3371,7 +3371,7 @@ static int unregister_console_locked(struct console *console) console_lock(); /* Disable it unconditionally */ - console->flags &= ~CON_ENABLED; + console_srcu_write_flags(console, console->flags & ~CON_ENABLED); if (hlist_unhashed(&console->node)) { console_unlock(); @@ -3390,7 +3390,7 @@ static int unregister_console_locked(struct console *console) * console has any device attached. Oh well.... */ if (!hlist_empty(&console_list) && console->flags & CON_CONSDEV) - console_first()->flags |= CON_CONSDEV; + console_srcu_write_flags(console_first(), console_first()->flags | CON_CONSDEV); console_unlock();