Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp1051479ybv; Wed, 5 Feb 2020 20:03:59 -0800 (PST) X-Google-Smtp-Source: APXvYqzCERkkJEyKU1vDi1noNh/A5urbPrdqP5t/QFJ1jbg2A8FsF7oO1AqlDALN5t9T1JD7oA+R X-Received: by 2002:aca:1708:: with SMTP id j8mr5661443oii.166.1580961839777; Wed, 05 Feb 2020 20:03:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1580961839; cv=none; d=google.com; s=arc-20160816; b=h0LFnuWZmIFIaaSZl3VEFc6qdpowyqP8kaSpwegzr3PNTd0iZrCKbQLWgEJPOHWKKy g6XwrxMCKnNJKxMLzHKVvirQ3pVj6t3hM8P3XT4QnJDIjKepaPnOMH45rOcH1wadoY1e 0GnZKEGx3Eqx5ZppANxylp3mw14E6aVYCsxoXcFput4OpD0fNwKc6YCc7l5muHpqmMZL q//wlV35HHAc3wIxjX8pZf4rP/KFSeWOfXoEFsRqyTek2PY9AvWiINiKTPXYR8+6gVlR AQpqU86uagn5p1gxlp60CeEIbnedKRx0bfza9Q+bFNngA422OQGON5qx20BaDcOvqSEX M1FQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :date:cc:to:from:subject:message-id; bh=bZVS1IIoDSk0PJqznw8Oy3LUP+kwyzsz5l+//wCH/oM=; b=nvVIuZ4E+mEBq8oq2dIqs1Yf1xwFYBaShDa5t50OeyUIWuMIyAbW50ziv+MP6b9V0F YiMyeFGAcxlDsYmXgjgxsGgVC1DXlE+xyL3X6dfRJX5qiL6G46zaStsSy/WFUtGXDOV/ m5JUgj9t3faH4pdAHo9GBf7t5pcfybcw8FTVGK8ekMgnqPZEYc5kVhlr/+l22NfqLtIQ 0ek/y9YjLBX0riL8f1mz95nkrcraCsfA2+/Ch8trbfWI01bT+LKlHzV/JvRv7kc/MnVv T2FEmpj7+RwgPPWEaRXX0EeJ1yoe3hKYV6EFLHh4gi46xurR5/aKYxER661KrCqs3fn2 VUuw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id k4si1248904otp.186.2020.02.05.20.03.46; Wed, 05 Feb 2020 20:03:59 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727702AbgBFECb (ORCPT + 99 others); Wed, 5 Feb 2020 23:02:31 -0500 Received: from kernel.crashing.org ([76.164.61.194]:50560 "EHLO kernel.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727415AbgBFECb (ORCPT ); Wed, 5 Feb 2020 23:02:31 -0500 Received: from localhost (gate.crashing.org [63.228.1.57]) (authenticated bits=0) by kernel.crashing.org (8.14.7/8.14.7) with ESMTP id 01642I1O017458 (version=TLSv1/SSLv3 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Wed, 5 Feb 2020 22:02:22 -0600 Message-ID: Subject: [PATCH v3 1/3] printk: Move console matching logic into a separate function From: Benjamin Herrenschmidt To: Petr Mladek Cc: Sergey Senozhatsky , "linux-kernel@vger.kernel.org" Date: Thu, 06 Feb 2020 15:02:18 +1100 Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.28.5-0ubuntu0.18.04.1 Mime-Version: 1.0 Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org This moves the loop that tries to match a newly registered console with the command line or add_preferred_console list into a separate helper, in order to be able to call it multiple times in subsequent patches. Signed-off-by: Benjamin Herrenschmidt --- Sorry for the delay, I got ... distracted :-) I ended up going for the int return codes instead of the enum, there aren't enough interesting cases anyways. I *hope* the logic doesn't change from the eixsting code. There might be subtle differences if a console is marked as brailled and fails to initialize but I doubt it matters. kernel/printk/printk.c | 107 +++++++++++++++++++++++++---------------- 1 file changed, 66 insertions(+), 41 deletions(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 1ef6f75d92f1..17602d7b7ffc 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -280,6 +280,7 @@ static struct console *exclusive_console; static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; static int preferred_console = -1; +static bool has_preferred_console; int console_set_on_cmdline; EXPORT_SYMBOL(console_set_on_cmdline); @@ -2626,6 +2627,60 @@ static int __init keep_bootcon_setup(char *str) early_param("keep_bootcon", keep_bootcon_setup); +/* + * This is called by register_console() to try to match + * the newly registered console with any of the ones selected + * by either the command line or add_preferred_console() and + * setup/enable it. + * + * Care need to be taken with consoles that are statically + * enabled such as netconsole + */ +static int try_enable_new_console(struct console *newcon) +{ + struct console_cmdline *c; + int i; + + for (i = 0, c = console_cmdline; + i < MAX_CMDLINECONSOLES && c->name[0]; + i++, c++) { + if (!newcon->match || + newcon->match(newcon, c->name, c->index, c->options) != 0) { + /* default matching */ + BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); + if (strcmp(c->name, newcon->name) != 0) + continue; + if (newcon->index >= 0 && + newcon->index != c->index) + continue; + if (newcon->index < 0) + newcon->index = c->index; + + if (_braille_register_console(newcon, c)) + return 0; + + if (newcon->setup && + newcon->setup(newcon, c->options) != 0) + return -EIO; + } + newcon->flags |= CON_ENABLED; + if (i == preferred_console) { + newcon->flags |= CON_CONSDEV; + has_preferred_console = true; + } + return 0; + } + + /* + * Some consoles, such as pstore and netconsole, can be enabled even + * without matching. + */ + if (newcon->flags & CON_ENABLED) + return 0; + + return -ENOENT; +} + /* * The console driver calls this routine during kernel initialization * to register the console printing procedure with printk() and to @@ -2647,11 +2702,9 @@ early_param("keep_bootcon", keep_bootcon_setup); */ void register_console(struct console *newcon) { - int i; unsigned long flags; struct console *bcon = NULL; - struct console_cmdline *c; - static bool has_preferred; + int err; if (console_drivers) for_each_console(bcon) @@ -2678,15 +2731,15 @@ void register_console(struct console *newcon) if (console_drivers && console_drivers->flags & CON_BOOT) bcon = console_drivers; - if (!has_preferred || bcon || !console_drivers) - has_preferred = preferred_console >= 0; + if (!has_preferred_console || bcon || !console_drivers) + has_preferred_console = preferred_console >= 0; /* * See if we want to use this console driver. If we * didn't select a console we take the first one * that registers here. */ - if (!has_preferred) { + if (!has_preferred_console) { if (newcon->index < 0) newcon->index = 0; if (newcon->setup == NULL || @@ -2694,48 +2747,20 @@ void register_console(struct console *newcon) newcon->flags |= CON_ENABLED; if (newcon->device) { newcon->flags |= CON_CONSDEV; - has_preferred = true; + has_preferred_console = true; } } } /* - * See if this console matches one we selected on - * the command line. + * See if this console matches one we selected on + * the command line or if it was statically enabled */ - for (i = 0, c = console_cmdline; - i < MAX_CMDLINECONSOLES && c->name[0]; - i++, c++) { - if (!newcon->match || - newcon->match(newcon, c->name, c->index, c->options) != 0) { - /* default matching */ - BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); - if (strcmp(c->name, newcon->name) != 0) - continue; - if (newcon->index >= 0 && - newcon->index != c->index) - continue; - if (newcon->index < 0) - newcon->index = c->index; - - if (_braille_register_console(newcon, c)) - return; - - if (newcon->setup && - newcon->setup(newcon, c->options) != 0) - break; - } + err = try_enable_new_console(newcon); - newcon->flags |= CON_ENABLED; - if (i == preferred_console) { - newcon->flags |= CON_CONSDEV; - has_preferred = true; - } - break; - } - - if (!(newcon->flags & CON_ENABLED)) - return; + /* printk() messages are not printed to the Braille console. */ + if (err || newcon->flags & CON_BRL) + return; /* * If we have a bootconsole, and are switching to a real console,