Message ID | 20221019145600.1282823-5-john.ogness@linutronix.de |
---|---|
State | New |
Headers |
Return-Path: <linux-kernel-owner@vger.kernel.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4ac7:0:0:0:0:0 with SMTP id y7csp377656wrs; Wed, 19 Oct 2022 08:07:39 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5tNd9pHIRciA4LXiDKF6WYRywo8Mmz9R9IfOReg3BB4psGnzLnaa/ic0BEb5+27qfptiof X-Received: by 2002:a17:906:2681:b0:783:6a92:4c38 with SMTP id t1-20020a170906268100b007836a924c38mr7238997ejc.75.1666192058804; Wed, 19 Oct 2022 08:07:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1666192058; cv=none; d=google.com; s=arc-20160816; b=ouPACtQ+d5wObc8keTmSHFxuEZelTdmYO3rBIMMalAXhFT+1SrYR/VaSbtEhg9Kzfg sr8g/r8NWyNgDTQUjrS6bD9gmeM3E6gfMbEDwjMvMbZ2+S294zga2n/DyZaFeeQJDMWK oMJJNB8v8ZmAc4XMRP7SrL5YiwLHuuC0Sfsk695Py+L5FoF7sNd48SmbeHo31fFjJOyF f/Lq5L4ImLKdzPB9lJREIDb2su7VoKR09HT2xNqJpbqUs1zwgmxUh++TcDOgCNQ256oO jMgD01e95KeQ4D1CdVt5eq2NYBGGm8a0iVxiC2Eh6GJqBa8JmWQEosiAxEZLn/akVkza Zt6g== 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=hllJr9Cenmg6tA9bg55xooViBdr/MaRQODlzmP0LypQ=; b=K1fXp3hF+w2Cfa82JRuFWScSuzP4wkh4R6j6VKkxIcDPSP6fsgQHIj6dcrkq5T0hwg Xeu2HdhGCrW4pZEGJhsOUrfoGGMvl4XyyIG++Mg49OkdgJnPnFCk6nDWzyzgXPSnDVGx FbMekW4zSgH3tL6y/CXWPrNni1P1ggdQBkDSiveqNzGLGE46RNNeXZNmOObTV6MfxEZ7 6zOj2Ya8Auy7RKFdo7Ty37+FWQz+1r1kaqcQxCdzp1DAk+YHSOdBIuYaMY6SySIYVvpZ Rd7uLYUmHcKxZOcGXS09+TMTusNO3p8POZCV0ENmt2A4Nadd67+hYr0aZly+cn89RCqZ HRww== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=mMoqhcUJ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=anaafIfR; 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 gn22-20020a1709070d1600b007429f0c69ccsi17545763ejc.579.2022.10.19.08.07.06; Wed, 19 Oct 2022 08:07:38 -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=mMoqhcUJ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b=anaafIfR; 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 S231629AbiJSPDm (ORCPT <rfc822;samuel.l.nystrom@gmail.com> + 99 others); Wed, 19 Oct 2022 11:03:42 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:39466 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229498AbiJSPCt (ORCPT <rfc822;linux-kernel@vger.kernel.org>); Wed, 19 Oct 2022 11:02:49 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E251D17A964 for <linux-kernel@vger.kernel.org>; Wed, 19 Oct 2022 07:57:33 -0700 (PDT) From: John Ogness <john.ogness@linutronix.de> DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1666191364; 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=hllJr9Cenmg6tA9bg55xooViBdr/MaRQODlzmP0LypQ=; b=mMoqhcUJaer0GASfzyAekKN+pSE8kqodiNK415JtDTtl/yYfijstiMQVyIVGVhbYJ7jiZy cKhgwp014bcWywD++czA8VHdfLmMfNHfT9xWvGVzDl1BSXzuTXhemseXYlrav5AHS4+h3b iXhswrgzZMfsRkUXSMPESUzJ+rSu3M7/YNj7ejdSKY5Yxhtm11uOmEXTnfiTuvJDajS8Dt gDw+WexxNI1DOGL9NoyYoTDrKUOZT02uu7KgiIO9lSmuToUrf/p0DUJZYwHx1FxGqlsn5D beK0nlWDHvmI58Iw3MyOaR7HUyvDEIWWDiLi+pCNT0K6BiV6dSSjn9hd4gaF8A== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1666191364; 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=hllJr9Cenmg6tA9bg55xooViBdr/MaRQODlzmP0LypQ=; b=anaafIfR//UTCpo9Mk4ATxTaSPAW/cLDpNejXqfvkq1eS4iO6NOae6e9TO/xryKwhc8urc BQG9YbEHS5/MnBBQ== To: Petr Mladek <pmladek@suse.com> Cc: Sergey Senozhatsky <senozhatsky@chromium.org>, Steven Rostedt <rostedt@goodmis.org>, Thomas Gleixner <tglx@linutronix.de>, linux-kernel@vger.kernel.org, Greg Kroah-Hartman <gregkh@linuxfoundation.org> Subject: [PATCH printk v2 04/38] printk: introduce console_is_enabled() wrapper Date: Wed, 19 Oct 2022 17:01:26 +0206 Message-Id: <20221019145600.1282823-5-john.ogness@linutronix.de> In-Reply-To: <20221019145600.1282823-1-john.ogness@linutronix.de> References: <20221019145600.1282823-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: <linux-kernel.vger.kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1747129004075272593?= X-GMAIL-MSGID: =?utf-8?q?1747129004075272593?= |
Series |
reduce console_lock scope
|
|
Commit Message
John Ogness
Oct. 19, 2022, 2:55 p.m. UTC
After switching to SRCU for console list iteration, some readers
will begin accessing console->flags as a data race. This is safe
because there is at most one CPU modifying console->flags and
using rmw operations.
The primary reason for readers to access console->flags is to
check if the console is enabled. Introduce console_is_enabled()
to mark such access as a data race.
Signed-off-by: John Ogness <john.ogness@linutronix.de>
---
include/linux/console.h | 20 ++++++++++++++++++++
1 file changed, 20 insertions(+)
Comments
On Wed, Oct 19, 2022 at 05:01:26PM +0206, John Ogness wrote: > After switching to SRCU for console list iteration, some readers > will begin accessing console->flags as a data race. This is safe > because there is at most one CPU modifying console->flags and > using rmw operations. > > The primary reason for readers to access console->flags is to > check if the console is enabled. Introduce console_is_enabled() > to mark such access as a data race. > > Signed-off-by: John Ogness <john.ogness@linutronix.de> Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
On Wed 2022-10-19 17:01:26, John Ogness wrote: > After switching to SRCU for console list iteration, some readers > will begin accessing console->flags as a data race. This is safe > because there is at most one CPU modifying console->flags and > using rmw operations. > > The primary reason for readers to access console->flags is to > check if the console is enabled. Introduce console_is_enabled() > to mark such access as a data race. > > Signed-off-by: John Ogness <john.ogness@linutronix.de> > --- > include/linux/console.h | 20 ++++++++++++++++++++ > 1 file changed, 20 insertions(+) > > diff --git a/include/linux/console.h b/include/linux/console.h > index cff86cc615f8..60195cd086dc 100644 > --- a/include/linux/console.h > +++ b/include/linux/console.h > @@ -172,6 +172,26 @@ extern void console_srcu_read_unlock(int cookie); > > extern struct hlist_head console_list; > > +/** > + * console_is_enabled - Check if the console is enabled > + * @con: struct console pointer of console to check > + * > + * This should be used instead of manually testing for the CON_ENABLED > + * bit in the console->flags. > + * > + * Context: Any context. > + */ > +static inline bool console_is_enabled(const struct console *con) > +{ > + /* > + * If SRCU is used, reading of console->flags can be a data > + * race. However, this is safe because there is at most one > + * CPU modifying console->flags and it is using only > + * read-modify-write operations to do so. Hmm, I somehow do not understand the explanation. How does read-modify-write operation make this safe, please? We are interested into one bit. IMHO, it is not important if the flags variable is modified atomically or byte by byte. The important thing is if the reading is synchronized against modifications. This function does not do any synchronization on its own. So, it depends on the caller. I would personally do two variants. for example: console_is_enabled() console_is_enabled_safe() The first variant would be called in situations where the race does not matter and the other when it matters. > + */ > + return (data_race(READ_ONCE(con->flags)) & CON_ENABLED); > +} > + > /** > * for_each_console_srcu() - Iterator over registered consoles > * @con: struct console pointer used as loop cursor Best Regards, Petr
On Fri 2022-10-21 10:57:58, Petr Mladek wrote: > On Wed 2022-10-19 17:01:26, John Ogness wrote: > > After switching to SRCU for console list iteration, some readers > > will begin accessing console->flags as a data race. This is safe > > because there is at most one CPU modifying console->flags and > > using rmw operations. > > > > The primary reason for readers to access console->flags is to > > check if the console is enabled. Introduce console_is_enabled() > > to mark such access as a data race. > > > > Signed-off-by: John Ogness <john.ogness@linutronix.de> > > --- > > include/linux/console.h | 20 ++++++++++++++++++++ > > 1 file changed, 20 insertions(+) > > > > diff --git a/include/linux/console.h b/include/linux/console.h > > index cff86cc615f8..60195cd086dc 100644 > > --- a/include/linux/console.h > > +++ b/include/linux/console.h > > @@ -172,6 +172,26 @@ extern void console_srcu_read_unlock(int cookie); > > > > extern struct hlist_head console_list; > > > > +/** > > + * console_is_enabled - Check if the console is enabled > > + * @con: struct console pointer of console to check > > + * > > + * This should be used instead of manually testing for the CON_ENABLED > > + * bit in the console->flags. > > + * > > + * Context: Any context. > > + */ > > +static inline bool console_is_enabled(const struct console *con) > > +{ > > + /* > > + * If SRCU is used, reading of console->flags can be a data > > + * race. However, this is safe because there is at most one > > + * CPU modifying console->flags and it is using only > > + * read-modify-write operations to do so. > > Hmm, I somehow do not understand the explanation. How does > read-modify-write operation make this safe, please? > > We are interested into one bit. IMHO, it is not important > if the flags variable is modified atomically or byte by byte. > The important thing is if the reading is synchronized against > modifications. > > This function does not do any synchronization on its own. > So, it depends on the caller. > > > I would personally do two variants. for example: > > console_is_enabled() > console_is_enabled_safe() > > The first variant would be called in situations where the race > does not matter and the other when it matters. Still thinking about it. It is possible that console_is_enabled_safe() variant won't be needed because all the callers will be either naturally serialized or can be racy. By other words, it makes sense to use data_race() because there are used racy checks. And there probably is not any caller that would strictly require explicit synchronization when reading this flag. Anyway, if there is any caller that would require explicit synchronization than we need a variant without data_race(). It would be great to somehow explain this in the commit message. Best Regards, Petr
diff --git a/include/linux/console.h b/include/linux/console.h index cff86cc615f8..60195cd086dc 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -172,6 +172,26 @@ extern void console_srcu_read_unlock(int cookie); extern struct hlist_head console_list; +/** + * console_is_enabled - Check if the console is enabled + * @con: struct console pointer of console to check + * + * This should be used instead of manually testing for the CON_ENABLED + * bit in the console->flags. + * + * Context: Any context. + */ +static inline bool console_is_enabled(const struct console *con) +{ + /* + * If SRCU is used, reading of console->flags can be a data + * race. However, this is safe because there is at most one + * CPU modifying console->flags and it is using only + * read-modify-write operations to do so. + */ + return (data_race(READ_ONCE(con->flags)) & CON_ENABLED); +} + /** * for_each_console_srcu() - Iterator over registered consoles * @con: struct console pointer used as loop cursor