Received: by 2002:a05:6358:d09b:b0:dc:cd0c:909e with SMTP id jc27csp2133800rwb; Fri, 11 Nov 2022 05:36:41 -0800 (PST) X-Google-Smtp-Source: AA0mqf6+EtO48CN7R0CAA52rVaaNK6oJX+gCuPVGcBeAQQwL582+d38xiBLWmXEp3fspr0a6/5bl X-Received: by 2002:a17:90b:2302:b0:208:4bfa:51e1 with SMTP id mt2-20020a17090b230200b002084bfa51e1mr1867049pjb.228.1668173801651; Fri, 11 Nov 2022 05:36:41 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668173801; cv=none; d=google.com; s=arc-20160816; b=PcL+6s+u894sWUYEvoZtow30zMfHsKasbO4amVnGsBFY6vAiQ0w6/rSL+UvZ9NWg5M InNDIGKfMYIxXzH+vkAZn+8h4GZ0ON11zPMCP1bVeWB9h+bm/oFYGr4H5WZ2KPZ1awCS IkTUg8HI3Jtew07syvgYWpfQ71o5JNBKD6LeB/1aZUbT7KOymxMgCjfhu5H6c+9sL+fT zMMdF1egOqhD5PfrzBIuYCuVe1FE5PUCS0+PNeDODvy8JFpYlC9hoJalL2eEcqMWF8nN ++oInZv+kW6EpIs4UWCyqiLmuakQ0W9+TeLJ7jCJHQFc36RKUDmatmPJoM/gdxGPlkPf pPpQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=WtMVZmVBNliDJnkN/+t2+YykE0XW+qnU5sFYrxVeDEM=; b=JbL0pvDTmWv5hsigu1forVkE0CTbGt8Us9HxjATkQ0Ey3sVrCSaVw2m93AReyX2vo2 +Mw80qwp/X5w/XnGvK/YWYojkFYZ91SOmvZVihOEoXrOlUdgu44qbppUnHs/80roKit3 9XAuJ2VcmZss5mxa3jaIr9ze+/O1NeOLv4gwTZTPPbwBCPZvgzQN7sYyStYue5tvroSi SEOVMZQQPPcwqp9mIPxgy21RVZ1kf30E/Bm/xWHn+AP4Q0IjqxW2r0TrZ0vmuZ5i3w9A ICvbliCYPphzGeGjaP5YjAqI7B91iQyNnCKa+0TeZu7CBURS4xR1JabUjeUHPPdbppr3 hJnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=GsCge+l3; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id a21-20020aa795b5000000b005639db953adsi2298454pfk.68.2022.11.11.05.36.28; Fri, 11 Nov 2022 05:36:41 -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=@suse.com header.s=susede1 header.b=GsCge+l3; 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=QUARANTINE sp=QUARANTINE dis=NONE) header.from=suse.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233356AbiKKNcK (ORCPT + 91 others); Fri, 11 Nov 2022 08:32:10 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47926 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233593AbiKKNcI (ORCPT ); Fri, 11 Nov 2022 08:32:08 -0500 Received: from smtp-out2.suse.de (smtp-out2.suse.de [IPv6:2001:67c:2178:6::1d]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1352A63B82 for ; Fri, 11 Nov 2022 05:32:07 -0800 (PST) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out2.suse.de (Postfix) with ESMTP id C2A86201CE; Fri, 11 Nov 2022 13:32:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1668173525; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=WtMVZmVBNliDJnkN/+t2+YykE0XW+qnU5sFYrxVeDEM=; b=GsCge+l3Ra1Q+q3wlbzTiQ0iCqf/1jiNAl4CgS5bfql7h70j4+rrIZSnoxMuUPJ1xobVeS DK7MMbsHIV3jqd13c1Md+yvaOA2lOGyAYpZVQqtI41noiqBRoSHRzFpuTIYHMDak8fJz2W mJCx5TcARy5UGMozo/KpkkBRf35rCxo= Received: from suse.cz (unknown [10.100.201.202]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by relay2.suse.de (Postfix) with ESMTPS id 666502C146; Fri, 11 Nov 2022 13:32:05 +0000 (UTC) Date: Fri, 11 Nov 2022 14:32:05 +0100 From: Petr Mladek To: John Ogness Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: Re: [PATCH printk v3 07/40] console: introduce console_is_enabled() wrapper Message-ID: References: <20221107141638.3790965-1-john.ogness@linutronix.de> <20221107141638.3790965-8-john.ogness@linutronix.de> <87a64y6e9k.fsf@jogness.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87a64y6e9k.fsf@jogness.linutronix.de> 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 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 On Thu 2022-11-10 16:11:43, John Ogness wrote: > On 2022-11-08, Petr Mladek wrote: > >> --- a/kernel/printk/printk.c > >> +++ b/kernel/printk/printk.c > >> @@ -3021,7 +3021,7 @@ void console_stop(struct console *console) > >> { > >> __pr_flush(console, 1000, true); > >> console_lock(); > >> - console->flags &= ~CON_ENABLED; > >> + WRITE_ONCE(console->flags, console->flags & ~CON_ENABLED); > > > > My first reaction is that using the atomic operation only for the > > store side is suspicious. It is correct because the read is serialized > > by console_lock(). But it is far from obvious why we need and can do > > it this way. > > The READ_ONCE()/WRITE_ONCE() usage is really about documenting data-race > reads and the writes that they are racing with. > > For WRITE_ONCE() the rule is: > > - If console->flags is modified for a registered console, it is done > under the console_list_lock and using WRITE_ONCE(). > > If we use a wrapper for this rule, then we can also add the lockdep > assertion that console_list_lock is held. > > For READ_ONCE() the rule is: > > - If console->flags is read for a registered console, then either > console_list_lock must be held _or_ it must be read via READ_ONCE(). > > If we use wrappers here, then we can use lockdep assertion on > console_list_lock for the non-READ_ONCE wrapper, and scru_read_lock > assertion for the READ_ONCE wrapper. Exactly, all the assertions are one big advantage for hiding this into an API. > > It would deserve a comment. But there are several other writes. > > Also it is not obvious why many other con->flags modifications > > do not need this. > > > > I think about hiding this into an API. We could also add some > > checks that it is used the right way. Also it might make sense > > to avoid using the READ_ONCE()/WRITE_ONCE by using > > set_bit()/test_bit(). > > I do not see any advantage of set_bit()/test_bit(). They have the > disadvantage that they only work with 1 bit at a time. And there are > multiple sites where more than 1 bit is set/tested. It is important that > the multi-bit tests are simultaneous. Fair enough. > > I would prefer to use the proposed API because it should make all > > accesses more clear and safe. And most importantly, it would help use > > to catch bugs. > > > > But I do not resist on it. The patch looks correct and we could do > > this later. I could live with it if we add some comments above the > > WRITE_ONCE() calls. > > I do not want to do a full API replacement for all console->flags access > in this series or at this time. I am concerned that it is taking us too > far away from our current goal. Also, with the upcoming atomic/threaded > model, all consoles need to be modified that want to use it anyway. So > that would be a more appropriate time to require the use of new API's. Fair enough. > For console_is_enabled() I will add the srcu_read_lock check. I suppose > I should also name the function console_srcu_is_enabled(). > > For the WRITE_ONCE() calls, I will add a static inline wrapper in > printk.c that includes the lockdep console_list_lock assertion. Perhaps > called console_srcu_write_flags(struct console *con, short flags). > > In console_srcu_write_flags() and console_srcu_is_enabled() I can > document their relationship and when they are to be used. Both these > functions are used rarely and should be considered the exception, not > the rule. > > For code that is reading registered console->flags under the > console_list_lock, I will leave the "normal access" as is. Just as I am > leaving the "normal access" for non-registered console-flags as is. We > can convert those to a new generic API later if we think it is really > necessary. Sounds reasonable. The main motivation for introducing the wrappers is that there are currently 40+ locations where console->flags are touched. It is easy to miss something especially when we are reworking the locking around this code. Some of the callers are outside kernel/printk/* which even increases the risk of a misuse. The lockdep checks would help us to catch potential mistakes. Anyway, I am fine with adding the wrappers for the synchronized reads later. This patchset is already big enough. Best Regards, Petr