From patchwork Mon Nov 14 16:29:13 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 19897 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2238476wru; Mon, 14 Nov 2022 08:31:58 -0800 (PST) X-Google-Smtp-Source: AA0mqf6J7sU2ZDsgyzwgIOvMlXvdxdMyAcQntZFJYwR99+qT0XNYWbR1d8Fzk9XV1UU6WMegu2Px X-Received: by 2002:a17:90b:3949:b0:213:d630:f4af with SMTP id oe9-20020a17090b394900b00213d630f4afmr14490651pjb.77.1668443517807; Mon, 14 Nov 2022 08:31:57 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1668443517; cv=none; d=google.com; s=arc-20160816; b=Z+An57ILqTxx+tWD6xfO3ZLQhvtmajbCTJrUzDowYJG9g4bOwPhAwFj7gB6FasutiJ rU8sJyK7LiVUNvuD2ClDnUkYNoIC4cqHkSUbkBgmauo82c0JsscQECF5JO2aiUHATDhJ HNQ3m2HW/i6W8ioBwh34r1wVWfNlNTEmLoZUBjBS/kkPE6H1CAsQNi4wrtrp5/eXVsEW z+tKLbbgEchJxc3/a+Ju8CQhOvvfUC4tajkMffgU0Ev2JyGbBjeg0eTINDbg3SbA8SwJ x2czTarEOEqp7dgQy1e1ifgLZ+oLflu6/9ruTONP8oJDkWyTs0FUEDjLPXBzOKmDar74 Ns6g== 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=wX56Rx4ehD7yCLYC+HBYrPWlmwQCTi9+gNx/cMRdAa0=; b=GEQoOMAiXandWGYttHC+/ACwWuTbbLzHOqoKppIqjTXh+/DDht4cVDxu2/1fcPCXvM DReX6vG3jHQ53n+XEoUEEfiUTr8j9JSUt5CwQxX1jlrY5RKsSwMGBvpFvOVQyqHPS/T1 5K4flXrY85yDpg+J5EsF5dsPZ9oUVs3slV77FVQfnoU1q8SLYR6WE9Io3iNhVPxMqOzC j34NA2aeB+p70FqUVAZVXbQZZ+Fqgdvn4Wi63G8qkfUkIhFmiN4gh8/J+V1GQ+jxEzuF g064KkSU+xabCSRGvI2ZUZ9twcfuZbUClxhhGnmPhmRAqiC0704iLtDUyWuAOtD6I/2P RYTg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=sZwXHYkH; dkim=neutral (no key) header.i=@linutronix.de; 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 d27-20020a631d5b000000b00434d8692854si10254972pgm.541.2022.11.14.08.31.14; Mon, 14 Nov 2022 08:31:57 -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=sZwXHYkH; dkim=neutral (no key) header.i=@linutronix.de; 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 S237704AbiKNQav (ORCPT + 99 others); Mon, 14 Nov 2022 11:30:51 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35748 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237607AbiKNQaP (ORCPT ); Mon, 14 Nov 2022 11:30:15 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2092922BC4 for ; Mon, 14 Nov 2022 08:29:44 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1668443382; 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=wX56Rx4ehD7yCLYC+HBYrPWlmwQCTi9+gNx/cMRdAa0=; b=sZwXHYkHvPbktNSwjxUK5aj2mlUfsNem40DSF6jkIhmjnLqme07Bj37zy8BIOHUrp4R+kO z20l7qtc9/VbGba0euziTCjOck6y0KeieHFYZ16r4/5p3EUfZkyVMuYj/ZUI1Y9PQXqyKQ wwabfaBCwER2N2R8NdsQJIhHmtea3R6RtskJewsP9Sc3cDiAnrz/ibxX+tysUT3L3qoZ9a RTuLJLASQj//Ycg2JFB4X8jCBVC8rTaTv/+ou5+jjJl98bUO4W74oj0ghdFP/CzB+UUTZK sV2lPIpF5ion3n4mLc5PiZdySngafhsq9WtLsLgbX/eTUODx2a9RiD+3L4b+Pw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1668443382; 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=wX56Rx4ehD7yCLYC+HBYrPWlmwQCTi9+gNx/cMRdAa0=; b=YSUY3x9QKkaBKpLI3HuYgJ3L+uxIygG746+BhVG6JRy2hyD9v0E6waIFwmYOUybnOdJ3Al G7H0jZldXWqYGJBA== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Greg Kroah-Hartman Subject: [PATCH printk v4 20/39] console: introduce console_is_registered() Date: Mon, 14 Nov 2022 17:35:13 +0106 Message-Id: <20221114162932.141883-21-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?1749489830264541121?= X-GMAIL-MSGID: =?utf-8?q?1749489830264541121?= 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 c1ca461d088a..f716e1dd9eaf 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -228,6 +228,34 @@ static inline void console_srcu_write_flags(struct console *con, short flags) WRITE_ONCE(con->flags, flags); } +/* 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 fb609a19f797..e770b1ede6c9 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -3414,7 +3414,7 @@ static int unregister_console_locked(struct console *console) /* Disable it unconditionally */ console_srcu_write_flags(console, console->flags & ~CON_ENABLED); - if (hlist_unhashed(&console->node)) { + if (!console_is_registered_locked(console)) { console_unlock(); return -ENODEV; }