From patchwork Fri Mar 17 11:43:44 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksey Makarov X-Patchwork-Id: 95417 Delivered-To: patch@linaro.org Received: by 10.140.89.134 with SMTP id v6csp265900qgd; Fri, 17 Mar 2017 04:48:32 -0700 (PDT) X-Received: by 10.99.7.3 with SMTP id 3mr5078200pgh.95.1489751312445; Fri, 17 Mar 2017 04:48:32 -0700 (PDT) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id q5si8418455pgj.168.2017.03.17.04.48.32; Fri, 17 Mar 2017 04:48:32 -0700 (PDT) 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; dkim=pass header.i=@linaro.org; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linaro.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751182AbdCQLsT (ORCPT + 12 others); Fri, 17 Mar 2017 07:48:19 -0400 Received: from mail-lf0-f48.google.com ([209.85.215.48]:35871 "EHLO mail-lf0-f48.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750999AbdCQLsR (ORCPT ); Fri, 17 Mar 2017 07:48:17 -0400 Received: by mail-lf0-f48.google.com with SMTP id y193so32007546lfd.3 for ; Fri, 17 Mar 2017 04:48:15 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=nf1iheIQE5Y8GYZAPE4hE8CKtGXWR/SlYXyR4Q0OxN0=; b=WDmlCVD0xcPcZXEbSdN+v7TWtyv578hvgeL+3al8/z5wLg5zpXeCuBR/2aR09vllzY 551DO1ZTGJ/SoUnLQb5gi7hb1AroTV8k5mQ+BtAx8Z65J7aRPMzCWA772x7cP0FETLtq EqOYm9VsrXEmG4f9zla05iYkXGOahMSP4VmFA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=nf1iheIQE5Y8GYZAPE4hE8CKtGXWR/SlYXyR4Q0OxN0=; b=oFKPbLxXBfezaQzwG7CV+iLAYzV3290d6NxT8BE2IWLEnA96+X9X2JwKQNKnvpVMSu nFn/iwZM/mW4eXi+VhkXKfPJThbfB06+e9VUePZ1aRl/ADGic+X3EVfpmkSEU4WLBquJ 7TYjmLL3BfTepSm4ouCsTEg+VU1mHyNuallLu8sQ1tNlEHLFQpOQsmekcgmYSnxihlcr C/wNiGWFCzst3CJiCJR9aHP7fCqSQ/Y4MCtXJ1TGSARfjAAiUpXWamJ7vizTl0zNsBP9 QJ7xzFA/DGN9oBbUSwVjn8FPPTLVJ0q0DIjjxDwzy5aUvQQiLnn/hmOCnReBMLYAEf+4 MoSg== X-Gm-Message-State: AFeK/H0QWPKjz6/dL/oFJfLR48inXwPgw2ZidQXgXifPXduEGH63W/eSuObcWqUCjfys+JiM X-Received: by 10.25.22.21 with SMTP id m21mr4379746lfi.176.1489751289771; Fri, 17 Mar 2017 04:48:09 -0700 (PDT) Received: from localhost.localdomain (nivc-213.auriga.ru. [80.240.102.213]) by smtp.gmail.com with ESMTPSA id r204sm1408660lff.57.2017.03.17.04.48.08 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 17 Mar 2017 04:48:09 -0700 (PDT) From: Aleksey Makarov To: linux-serial@vger.kernel.org Cc: linux-kernel@vger.kernel.org, Aleksey Makarov , Sudeep Holla , Greg Kroah-Hartman , Peter Hurley , Jiri Slaby , Robin Murphy , Steven Rostedt , "Nair, Jayachandran" , Sergey Senozhatsky , Petr Mladek Subject: [PATCH v6 3/3] printk: fix double printing with earlycon Date: Fri, 17 Mar 2017 14:43:44 +0300 Message-Id: <20170317114348.30215-1-aleksey.makarov@linaro.org> X-Mailer: git-send-email 2.12.0 In-Reply-To: <20170315102854.1763-1-aleksey.makarov@linaro.org> References: <20170315102854.1763-1-aleksey.makarov@linaro.org> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org If a console was specified by ACPI SPCR table _and_ command line parameters like "console=ttyAMA0" _and_ "earlycon" were specified, then log messages appear twice. The root cause is that the code traverses the list of specified consoles (the `console_cmdline` array) and stops at the first match. But it may happen that the same console is referred by the elements of this array twice: pl011,mmio,0x87e024000000,115200 -- from SPCR ttyAMA0 -- from command line but in this case `preferred_console` points to the second entry and the flag CON_CONSDEV is not set, so bootconsole is not deregistered. To fix that, split the loop where we search for matching entry of console_cmdline into three parts that do not intersect: 1) search for braille console 2) check for preferred_console 3) match other entries so that these three parts do not To to that introduce predicate _braille_is_braille_console() that checks if its argument is an entry describing a braille console. Also introduce a macro for_each_console_cmdline() to traverse the console_cmdline array. Reported-by: Sudeep Holla Signed-off-by: Aleksey Makarov --- v5 -> v6: - drop v5 and continue to work on v4: - introduce _braille_is_braille_console(). It helps to split original loop into three parts: 1) search for braille console, 2) check for preferred_console, 3) match other entries so that these three parts do not intersect. - introduce for_each_console_cmdline() macros to traverse console_cmdline (Petr Mladek) kernel/printk/braille.h | 12 ++++++ kernel/printk/printk.c | 104 ++++++++++++++++++++++++++++++++++-------------- 2 files changed, 86 insertions(+), 30 deletions(-) -- 2.12.0 diff --git a/kernel/printk/braille.h b/kernel/printk/braille.h index 769d771145c8..183aebf6e1dc 100644 --- a/kernel/printk/braille.h +++ b/kernel/printk/braille.h @@ -18,6 +18,12 @@ _braille_register_console(struct console *console, struct console_cmdline *c); int _braille_unregister_console(struct console *console); +static inline int +_braille_is_braille_console(struct console_cmdline *c) +{ + return !!c->brl_options; +} + #else static inline void @@ -43,6 +49,12 @@ _braille_unregister_console(struct console *console) return 0; } +static inline int +_braille_is_braille_console(struct console_cmdline *c) +{ + return 0; +} + #endif #endif diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index fd752f0c8ef1..ab2433681ca5 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -270,6 +270,11 @@ static struct console *exclusive_console; static struct console_cmdline console_cmdline[MAX_CMDLINECONSOLES]; +#define for_each_console_cmdline(i, c) \ + for (i = 0, c = console_cmdline; \ + i < MAX_CMDLINECONSOLES && c->name[0]; \ + i++, c++) + static int preferred_console = -1; int console_set_on_cmdline; EXPORT_SYMBOL(console_set_on_cmdline); @@ -1905,9 +1910,7 @@ static int __add_preferred_console(char *name, int idx, char *options, * See if this tty is not yet registered, and * if we have a slot free. */ - for (i = 0, c = console_cmdline; - i < MAX_CMDLINECONSOLES && c->name[0]; - i++, c++) { + for_each_console_cmdline(i, c) { if (strcmp(c->name, name) == 0 && c->index == idx) { if (!brl_options) preferred_console = i; @@ -2383,6 +2386,37 @@ static int __init keep_bootcon_setup(char *str) early_param("keep_bootcon", keep_bootcon_setup); +static bool match_console_name(struct console *newcon, + struct console_cmdline *c) +{ + BUILD_BUG_ON(sizeof(c->name) != sizeof(newcon->name)); + if (strcmp(c->name, newcon->name) != 0) + return false; + if (newcon->index >= 0 && newcon->index != c->index) + return false; + if (newcon->index < 0) + newcon->index = c->index; + return true; +} + +static bool match_console(struct console *newcon, struct console_cmdline *c) +{ + if (newcon->match && + newcon->match(newcon, c->name, c->index, c->options) == 0) { + newcon->flags |= CON_ENABLED; + return true; + } + + if (match_console_name(newcon, c)) { + if (!newcon->setup || newcon->setup(newcon, c->options) == 0) + newcon->flags |= CON_ENABLED; + + return true; + } + + return false; +} + /* * The console driver calls this routine during kernel initialization * to register the console printing procedure with printk() and to @@ -2457,40 +2491,50 @@ void register_console(struct console *newcon) } /* - * See if this console matches one we selected on - * the command line. + * See if this console matches one we selected on the command line. + * Do it in three steps: + * + * 1) check if it is a braille console.. */ - 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; - } + for_each_console_cmdline(i, c) + if (_braille_is_braille_console(c) && + match_console_name(newcon, c) && + _braille_register_console(newcon, c)) + return; - newcon->flags |= CON_ENABLED; - if (i == preferred_console) { + /* + * 2) check if this console was set as preferred by command line + * parameters or by call to add_preferred_console(). There may be + * several entries in the console_cmdline array matching with the same + * console, one with newcon->match(), another by name/index: + * + * pl011,mmio,0x87e024000000,115200 -- added from SPCR + * ttyAMA0 -- added from command line + * + * so we can not use the first match. Instead check the + * entry pointed by preferred_console and then all other entries. + */ + if (preferred_console >= 0 && + match_console(newcon, console_cmdline + preferred_console)) { + if (newcon->flags & CON_ENABLED) { newcon->flags |= CON_CONSDEV; has_preferred = true; } - break; + goto match; + } + + /* + * 3) check other entries + */ + for_each_console_cmdline(i, c) { + if (preferred_console == i || _braille_is_braille_console(c)) + continue; + + if (match_console(newcon, c)) + goto match; } +match: if (!(newcon->flags & CON_ENABLED)) return;