Received: by 2002:a05:6359:c8b:b0:c7:702f:21d4 with SMTP id go11csp2392825rwb; Thu, 29 Sep 2022 09:36:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5KKUmkHdKvJzcCq+2Ohq4R73f/4VumMOiuV7/JYRmg/JN2vsarxsCG7gxi1tYPljr4m2yi X-Received: by 2002:a17:907:3f20:b0:782:2626:c665 with SMTP id hq32-20020a1709073f2000b007822626c665mr3467634ejc.38.1664469393385; Thu, 29 Sep 2022 09:36:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1664469393; cv=none; d=google.com; s=arc-20160816; b=v+0r5LbZpKG1M081saf2NNTj75wPBgFPjcRkJPceDNELvHnbKjbny6nNQZ2zLgBAn4 rr6sHlPxx1DI2Plm4RGf953Z/O2yio7dizDBdVxl9tudeSRIXApDiUQIITZe8wk39xeV tNe/RmSkWoFvS9pfAeVU01U4fMCuPYJuU48C6C7CjpXsPpm8Ep4PxlxO20LQiP4WsdnL ycTlA+8v7edQExdD62MsYHghg5Yz8QOSsUNmycUxLiYxGFNKGVDjJUisHt54uRoc/Bd8 /ToY6khzxJ6kAKFt7qStxx/M5JhlFXPOTxla50O7KopyfS9aFoI2VbpX3UwiY5DXAiEO xgFQ== 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=zAcEDAeXByKAJJXb2Hk5mUl7jEDvdCepq+Gn9p2xl/A=; b=bOsz5GbL2/Ton7+TKBJa9Z7caMEwTC6SgS5Ne1PhaUI27BwtaxgkPobmdbGrzu6pcV +kMDtjE7ySaNFrJ1veqiaJWK9MNStzgBNFncpXRo0prey/jTat+GNGkjYFRCRUPuBxSo AafuSa57iPOeF8S2huw9I5jmd6BbkNG3qcAoOzsns09RxGDzp91pJWvbTyqsLbyCAvyt yMiQvfq0uOxUzIPf/AWf0NNGb+myFW9OerXnrzaoAh29XbejkCON/CPLxyuALoqqjP0u cYLsciyBar11DrxNKGDXoICEOLXgsYwtz2v3dKnAFhIfNnoxao74Yri9rjbiXeIVL+dg BstQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@suse.com header.s=susede1 header.b=ivB8DCvT; 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 g11-20020a056402320b00b00457dbe73825si1592388eda.386.2022.09.29.09.36.06; Thu, 29 Sep 2022 09:36:33 -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=@suse.com header.s=susede1 header.b=ivB8DCvT; 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 S235556AbiI2Pol (ORCPT + 99 others); Thu, 29 Sep 2022 11:44:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52740 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235731AbiI2PoN (ORCPT ); Thu, 29 Sep 2022 11:44:13 -0400 Received: from smtp-out1.suse.de (smtp-out1.suse.de [IPv6:2001:67c:2178:6::1c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 570471BB23C for ; Thu, 29 Sep 2022 08:43:18 -0700 (PDT) Received: from relay2.suse.de (relay2.suse.de [149.44.160.134]) by smtp-out1.suse.de (Postfix) with ESMTP id 8670721905; Thu, 29 Sep 2022 15:43:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1664466196; 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=zAcEDAeXByKAJJXb2Hk5mUl7jEDvdCepq+Gn9p2xl/A=; b=ivB8DCvTah53CyexdRyV+LMZXNXZxkigoTAJzwuISaWXWL5llmLSefpYy2QdF4QmikzsUJ r/BjX9Rwk8P1aQhGc5bBOdAySNAipqeQDT+rssG5P9yvYdt3GyrNMHn81lZ+D6JJ0HXNHf 3M3lDb82adqCVtrhQXwepAJnw8JkDdw= Received: from suse.cz (unknown [10.100.208.146]) (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 341DE2C14E; Thu, 29 Sep 2022 15:43:16 +0000 (UTC) Date: Thu, 29 Sep 2022 17:43:15 +0200 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 06/18] printk: Protect [un]register_console() with a mutex Message-ID: References: <20220924000454.3319186-1-john.ogness@linutronix.de> <20220924000454.3319186-7-john.ogness@linutronix.de> <87mtajkqvu.fsf@jogness.linutronix.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <87mtajkqvu.fsf@jogness.linutronix.de> X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, 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-09-29 01:48:29, John Ogness wrote: > On 2022-09-27, Petr Mladek wrote: > > Hmm, the new mutex is really nasty. It has very strange semantic. > > It makes the locking even more complicated. > > We are working to replace the BKL-console_lock with new separate clearly > defined mechanisms. > > The new mutex provides full synchronization for list changes as well as > changes to items of that list. (Really console->flags is the only change > to items of the list.) OK. > For some places in the code it is very clear that the console_lock can > be completely replaced (either with srcu or the new mutex). For other > places, it is not yet clear why the console_lock is being used and so > both console_lock and mutex are used. One important and tricky location is console_trylock() in vprintk_emit(). And the related for_each_console() called from console_unlock()->console_flush_all(). It is the legacy mode that tries to print to the consoles immediately. I am not sure if we could _ever_ remove this mode. And it is most likely the main reason why semaphore is used instead of a mutex: + printk() can be called in atomic context + also there is the console_trylock_spinning() trick that allows to transfer the semaphore to another owner without locking. Do you see any RT-friendly solution for the legacy mode, please? Maybe, an atomic variable (cmpxchg) can be used together with the SRCU list. But I am not sure if srcu_read_lock can be transferred to another context. Also this would not solve priority inversion. Not to say that it might kill SRCU performance on the entire system. > > The ideal solution would be take console_lock() here. > > We should be looking where we can remove console_lock, not identifying > new locations to add it. Yes, we do not want this big kernel lock. Honestly, I am not completely sure what is the exact purpose. My guess is that console_lock() is used to prevent calling con->write() when some internal console driver state is manipulated. If the above is true then it might be solvable by some driver-specific lock. The question is where the lock should be. It is possible that it might require adding the lock into struct console. Anyway, some lock will still be needed to synchronize the list. But could it be mutex? What about the legacy mode of printk_emit()? > > A good enough solution might be call this under the later added > > srcu_read_lock(&console_srcu) and use for_each_console_srcu(). > > @console_srcu does not allow safe reading of console->flags. It only > provides safe list iteration and reading of immutable fields. The new > mutex must be used for reading console->flags. > > Note that for the NOBKL consoles (not part of this series), a new atomic > state variable is used so that console->flags is not needed. That means > for NOBKL consoles the new mutex is further reduced in scope to provide > only list synchronization. Good to know. > > Or is this part of some strategy to remove console_sem later, please? > > Yes! One of the main points of this final phase of the rework is to > remove console_sem usage (for NOBKL consoles). If a system is running > with only NOBKL consoles registered, ideally that system should never > call console_lock()/console_trylock(). Once all drivers have converted > over to the NOBKL interface, console_sem will serve no purpose for the > printk and console frameworks, so it can be removed. Is this realistic? And even if we convert all console drivers then people still might want the legacy mode. My understanding is that some atomic consoles would be real hacks. They might be good enough for panic(). But what about running system. It seems that people might want the legacy more even on running system. Will it be doable with mutex? I am sorry. I was about to answer this mail with "fair enough". But then I thought more about it... I would really like to avoid state where we have two locks (semaphore and mutex) serializing the same thing (console list). Best Regards, Petr