From patchwork Mon Nov 7 14:16:31 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: John Ogness X-Patchwork-Id: 16450 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:6687:0:0:0:0:0 with SMTP id l7csp2078771wru; Mon, 7 Nov 2022 06:21:08 -0800 (PST) X-Google-Smtp-Source: AMsMyM7a6wIuatLFfc+Fpft+Oo2xYoSdhOGsfs918QKAbr4coq2G+axTXxNMTz/yiNgmT4AFILsr X-Received: by 2002:a63:5a0b:0:b0:434:4748:4e7a with SMTP id o11-20020a635a0b000000b0043447484e7amr43023901pgb.561.1667830868233; Mon, 07 Nov 2022 06:21:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1667830868; cv=none; d=google.com; s=arc-20160816; b=a+x/ayLLbhlkbpFs96MqR2QFXjv5FZvH0FHo+6ggzjtcNvIHkc8r9WlYfegM3Fhzda vE5UFBjTyWNJIfJVwAWIerSsNwTVXaEhVilEpqBAM8+rlMrEXCTsVwKVXzANUog8PPgJ w/m5X16IA/QvVe39Yv7GU/n3oE0Vru7bRmMTFz/GGFDkTyrsoyokrCsgqHf4I2mi6LQk LvmajdykScjgO4CBsYiM9cVIP+mKurVzPAizR2Kpt3YZ4K7bw7wY9oelnsNnWyd6dwLX j7NjHYC43lV48hyxZY+f8cQIqwx7jCXQk0Opt/5fa4wslwd899dhQcBSJbwh5Q76XJIV YCzQ== 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=gS+TlnBfnvuNwOKZQIZcdjPH9bhpfrs5K1fOz4tyq8E=; b=ENXuQBR1baOgXQFC/0QpphktIZlrDHjWkMhztteFinFkiTi4jn0aS/HqiZJDX+K+VN WrCd3ByIfZhTakduBaif/YQ6GDa9sVjLUe1AQfqvm85ue50YXG066FNiMF1VKk8L5bb0 bsFFkbqNHxytcgjPsT9McB4h50nozngc1LphrHMrAaOQqrh1yF3HCq57bI4oN1Axm4TX W5qQUfBVDOHgo6B5SLfd0n26YH8fqcjl4fFmH6mzSqIwD7f/uYWEUmS8zR+MkLd4ridT ZgeMOdVRJ0awAlKU3duG3ACcTA67/DkqJQIhDVxcLZskUluBPRv2X5DJSw6B5wu6oyoA zQXA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=djqqVbIJ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 x14-20020a63170e000000b00464a9874658si10755078pgl.6.2022.11.07.06.20.54; Mon, 07 Nov 2022 06:21:08 -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=djqqVbIJ; dkim=neutral (no key) header.i=@linutronix.de header.s=2020e; 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 S232431AbiKGOTM (ORCPT + 99 others); Mon, 7 Nov 2022 09:19:12 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56776 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232250AbiKGOR1 (ORCPT ); Mon, 7 Nov 2022 09:17:27 -0500 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 2101A1D31F; Mon, 7 Nov 2022 06:16:57 -0800 (PST) From: John Ogness DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1667830615; 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=gS+TlnBfnvuNwOKZQIZcdjPH9bhpfrs5K1fOz4tyq8E=; b=djqqVbIJuMlxRG/KUYbjTE3WlJPb8/4BRjZqiVgvYMTr7/Sg0autRsyaU2JWaZq3VN3Mfw OwXHjvPPuCWuVO7A4iO0R7oz5UwNRLhdk02uk4jpCy9Gt7MNNPJoFi4PLw9QL+wwMeqZgG x8w4JTnSU8McnTmTMP+zEv1TCPiXlXiHUYwTYDDfpGnYPpWIDb6LZ00TZMmkMkukcDDlD8 pGbuhmV/Z89XLkI7qVEP/xlfkQntbD1FliCvEtwapvNn1D8KcDpxh7Pcm+BFSRCbglYTQK WSb0+BfzqLoXNbMVTMuI8qVDma66XMtIjHtCHVcUsZm6wYyw6AxM9KLuzMDR0g== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1667830615; 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=gS+TlnBfnvuNwOKZQIZcdjPH9bhpfrs5K1fOz4tyq8E=; b=M60ZeenQNKgOoEahohQx+InKZNToZ2PTl+S+h/Y9/Dwe3ZAZMQrMX+0/9pXQL0rFd4ySGx /D2ZyiONrI29IoCg== To: Petr Mladek Cc: Sergey Senozhatsky , Steven Rostedt , Thomas Gleixner , linux-kernel@vger.kernel.org, Helge Deller , Greg Kroah-Hartman , Javier Martinez Canillas , Thomas Zimmermann , Juergen Gross , Boris Ostrovsky , Tom Rix , linux-fbdev@vger.kernel.org, dri-devel@lists.freedesktop.org Subject: [PATCH printk v3 33/40] printk, xen: fbfront: create/use safe function for forcing preferred Date: Mon, 7 Nov 2022 15:22:31 +0106 Message-Id: <20221107141638.3790965-34-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?1748847420221812624?= X-GMAIL-MSGID: =?utf-8?q?1748847420221812624?= With commit 9e124fe16ff2("xen: Enable console tty by default in domU if it's not a dummy") a hack was implemented to make sure that the tty console remains the console behind the /dev/console device. The main problem with the hack is that, after getting the console pointer to the tty console, it is assumed the pointer is still valid after releasing the console_sem. This assumption is incorrect and unsafe. Make the hack safe by introducing a new function console_force_preferred_locked() and perform the full operation under the console_list_lock. Signed-off-by: John Ogness --- drivers/video/fbdev/xen-fbfront.c | 12 +++------ include/linux/console.h | 1 + kernel/printk/printk.c | 44 ++++++++++++++++++++++++++++--- 3 files changed, 46 insertions(+), 11 deletions(-) diff --git a/drivers/video/fbdev/xen-fbfront.c b/drivers/video/fbdev/xen-fbfront.c index 4d2694d904aa..8752d389e382 100644 --- a/drivers/video/fbdev/xen-fbfront.c +++ b/drivers/video/fbdev/xen-fbfront.c @@ -504,18 +504,14 @@ static void xenfb_make_preferred_console(void) if (console_set_on_cmdline) return; - console_lock(); + console_list_lock(); for_each_console(c) { if (!strcmp(c->name, "tty") && c->index == 0) break; } - console_unlock(); - if (c) { - unregister_console(c); - c->flags |= CON_CONSDEV; - c->flags &= ~CON_PRINTBUFFER; /* don't print again */ - register_console(c); - } + if (c) + console_force_preferred_locked(c); + console_list_unlock(); } static int xenfb_resume(struct xenbus_device *dev) diff --git a/include/linux/console.h b/include/linux/console.h index cdae70e27377..b6b5d796d15c 100644 --- a/include/linux/console.h +++ b/include/linux/console.h @@ -273,6 +273,7 @@ enum con_flush_mode { }; extern int add_preferred_console(char *name, int idx, char *options); +extern void console_force_preferred_locked(struct console *con); extern void register_console(struct console *); extern int unregister_console(struct console *); extern void console_lock(void); diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index be40a9688403..d74e6e609f7d 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -247,9 +247,10 @@ int devkmsg_sysctl_set_loglvl(struct ctl_table *table, int write, void console_list_lock(void) { /* - * In unregister_console(), synchronize_srcu() is called with the - * console_list_lock held. Therefore it is not allowed that the - * console_list_lock is taken with the srcu_lock held. + * In unregister_console() and console_force_preferred_locked(), + * synchronize_srcu() is called with the console_list_lock held. + * Therefore it is not allowed that the console_list_lock is taken + * with the srcu_lock held. * * Whether or not this context is in the read-side critical section * can only be detected if the appropriate debug options are enabled. @@ -3457,6 +3458,43 @@ int unregister_console(struct console *console) } EXPORT_SYMBOL(unregister_console); +/** + * console_force_preferred_locked - force a registered console preferred + * @con: The registered console to force preferred. + * + * Must be called under console_list_lock(). + */ +void console_force_preferred_locked(struct console *con) +{ + struct console *cur_pref_con; + + if (!console_is_registered_locked(con)) + return; + + cur_pref_con = console_first(); + + /* Already preferred? */ + if (cur_pref_con == con) + return; + + hlist_del_init_rcu(&con->node); + + /* + * Ensure that all SRCU list walks have completed so that the console + * can be added to the beginning of the console list and its forward + * list pointer can be re-initialized. + */ + synchronize_srcu(&console_srcu); + + con->flags |= CON_CONSDEV; + WARN_ON(!con->device); + + /* Only the new head can have CON_CONSDEV set. */ + WRITE_ONCE(cur_pref_con->flags, cur_pref_con->flags & ~CON_CONSDEV); + hlist_add_behind_rcu(&con->node, console_list.first); +} +EXPORT_SYMBOL(console_force_preferred_locked); + /* * Initialize the console device. This is called *early*, so * we can't necessarily depend on lots of kernel help here.