From patchwork Mon Nov 7 14:16:20 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 16469 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2081139wru; Mon, 7 Nov 2022 06:25:31 -0800 (PST) X-Google-Smtp-Source: AMsMyM4qNyhep1QF/srOwebaKEy0MeD8TtJK+sffvJ4z5W/5PL5oRa1TQu4llTfxjBNfKxVBeKEq X-Received: by 2002:a05:6402:5207:b0:462:2426:48fc with SMTP id s7-20020a056402520700b00462242648fcmr51214408edd.93.1667831131581; Mon, 07 Nov 2022 06:25:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667831131; cv=none; d=google.com; s=arc-20160816; b=ED+ZkpgSnP3XbKrksg2v6GGaBNNtmIzt2nQv/IlhL3OOqG5igtklZbpykMNK9cV4ID BD8lWRfIrs5fRS6tVi8XDOQsF1J+Qbau+cVc5OarS+Gfr53lv1nPh6UEjW9VFKo4k/Qf mHbnbXBMm9potkL4zfyN4Y6I4daHr49Q/JTczpdTAvj1TdojEfdxZV+XO5NNRZjKeXon iO4fvbGPyQ9VYkc8sEQ7zZxL1sYc2KXGa+j7SO+5x98EMa0CFzezf+47o2DLlI1BvCA7 wrnbzydPfeg4xLXuqHgBLYg8xAHbZNI2aeXHGQQOrfP7XPeb4q+0743GDYw0skupcF1V qwKA== 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=nF9ItXxFwRvPMruy1z9DBE5scW8r/i0a6aD5QIaBD04=; b=IVITvDHX5daI2mQOIGR0pLDBhGuDZQ1CfWe4kLLmnwkpIJ5/SMuV6RDuESBV0xi6hx pPfCPvi0vecWZ3ZFkOTIFs8UV6zpw9CG66/Wyfnaks28LonrhYj0PrT6dmYxHlt6431q Ge1pVl+eEgI/poAOnAE5QglzrXFOYmDd4GX2fu6bH2IXecQudOBAdU9EOraK1b97C5Yj C//JIHdYcSqxp+9sbgzojo30EQ/VA1BZE8l/ANda+SVNOcsGIUGP5XLiTe/J94Fn029u +8rct92LmjkJZHavg7xnwgI/7YXPw92+RITqw5BxgBkBN1oU3L8D9TKE7WHLOYxkE/cy 4eHA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=4UvomhlS; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="WH+/leZj"; 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 p25-20020a50cd99000000b00459a58216d8si8327630edi.504.2022.11.07.06.25.07; Mon, 07 Nov 2022 06:25:31 -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=4UvomhlS; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e header.b="WH+/leZj"; 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 S232490AbiKGOTx (ORCPT + 99 others); Mon, 7 Nov 2022 09:19:53 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55706 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232182AbiKGOQw (ORCPT ); Mon, 7 Nov 2022 09:16:52 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [IPv6:2a0a:51c0:0:12e:550::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A53161CFFE for ; Mon, 7 Nov 2022 06:16:51 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1667830610; 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=nF9ItXxFwRvPMruy1z9DBE5scW8r/i0a6aD5QIaBD04=; b=4UvomhlSZAvFX9ARbtGXTPtRcZnlbiu/Os6h1wdRy1FvTcQ6bQarj88zARfxWv83EAETG9 nf9/td7bOpBTfIDuxNdbLnqkaICA9JTZ6vRVCn5NdRYve1U4ImRGBrCvoLINW3lQZhkq0R XWlKuGl0YbQgyUvW2pIoJt1LN2c2ALoD2o1wQ4d0OpZYcP6tfw7/LInHCIy8n8+CHbA/3i 4gmwoCBlnoDkWAEBNYFPMkMGB9O+7CcB0/qfuIvx8+vocHNyPQpkvoBCx9ZvoQ3ETUCut3 9V48M+wRPHF9FZSvOg28q88hMORPAsWxQToGssYL6natRATqyCSp2F4QTw1IxA== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1667830610; 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=nF9ItXxFwRvPMruy1z9DBE5scW8r/i0a6aD5QIaBD04=; b=WH+/leZjumZ+rt5PP5ML58idCQJBWKJkxwo/rAmnAt7OoTZ9b62aec54aBDjDx/BwP7Ig2 EzYFB3SXZ0SQQhAg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v3 22/40] console: introduce console_is_registered() Date: Mon, 7 Nov 2022 15:22:20 +0106 Message-Id: <20221107141638.3790965-23-john.ogness@linutronix.de> In-Reply-To: <20221107141638.3790965-1-john.ogness@linutronix.de> References: <20221107141638.3790965-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?1748847696359957396?= X-GMAIL-MSGID: =?utf-8?q?1748847696359957396?= Currently it is not possible for drivers to detect if they have already successfully registered their console. Several drivers have multiple paths that lead to console registration. To avoid attempting a 2nd registration (which leads to a WARN), drivers are implementing their own solution. Introduce console_is_registered() so drivers can easily identify if their console is currently registered. A _locked() variant is also provided if the caller is already holding the console_list_lock. Signed-off-by: John Ogness Reviewed-by: Petr Mladek --- include/linux/console.h | 28 ++++++++++++++++++++++++++++ kernel/printk/printk.c | 2 +- 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/include/linux/console.h b/include/linux/console.h index 47be23be8a88..cdae70e27377 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -210,6 +210,34 @@ static inline bool console_is_enabled(const struct console *con) return (data_race(READ_ONCE(con->flags)) & CON_ENABLED); } +/* Variant of console_is_registered() when the console_list_lock is held. */ +static inline bool console_is_registered_locked(const struct console *con) +{ + lockdep_assert_console_list_lock_held(); + return !hlist_unhashed(&con->node); +} + +/* + * console_is_registered - Check if the console is registered + * @con: struct console pointer of console to check + * + * Context: Process context. May sleep while acquiring console list lock. + * Return: true if the console is in the console list, otherwise false. + * + * If false is returned for a console that was previously registered, it + * can be assumed that the console's unregistration is fully completed, + * including the exit() callback after console list removal. + */ +static inline bool console_is_registered(const struct console *con) +{ + bool ret; + + console_list_lock(); + ret = console_is_registered_locked(con); + console_list_unlock(); + return ret; +} + /** * 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 31387ba3fa1a..be40a9688403 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3410,7 +3410,7 @@ static int unregister_console_locked(struct console *console) /* Disable it unconditionally */ WRITE_ONCE(console->flags, console->flags & ~CON_ENABLED); - if (hlist_unhashed(&console->node)) { + if (!console_is_registered_locked(console)) { console_unlock(); return -ENODEV; }