From patchwork Fri Mar 3 15:49:41 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Aleksey Makarov X-Patchwork-Id: 94856 Delivered-To: patch@linaro.org Received: by 10.182.3.34 with SMTP id 2csp275366obz; Fri, 3 Mar 2017 07:53:41 -0800 (PST) X-Received: by 10.84.232.72 with SMTP id f8mr5224765pln.85.1488556421632; Fri, 03 Mar 2017 07:53:41 -0800 (PST) Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id x185si10923555pgd.414.2017.03.03.07.53.41; Fri, 03 Mar 2017 07:53:41 -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; 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 S1752339AbdCCPx3 (ORCPT + 25 others); Fri, 3 Mar 2017 10:53:29 -0500 Received: from mail-lf0-f50.google.com ([209.85.215.50]:33176 "EHLO mail-lf0-f50.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752002AbdCCPxZ (ORCPT ); Fri, 3 Mar 2017 10:53:25 -0500 Received: by mail-lf0-f50.google.com with SMTP id a6so49230494lfa.0 for ; Fri, 03 Mar 2017 07:53:09 -0800 (PST) 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=lYdURzSJtpcdP0SaLDw98rDV9uUDADousmZl13b8om8=; b=UYU4cI/v3+puXwIn0tnse5PjY9wqGY1A3fbyoy2Q0arSJoAxhjWRiX/Zunf4Bak+6r OTEU6C2gNHgSSNMLqCeigkhKsV4uoxpGkvJDaWBRXY71FQXlpXUL8U8DG6NlfI19WPlE mRRqwczS6Ayc+KeRQmBCb9PtInzgU5Y7vg6E0= 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=lYdURzSJtpcdP0SaLDw98rDV9uUDADousmZl13b8om8=; b=Kp/GcQ4rK0mLN3ANFX9i9ph4U9P1X9ZPJbe59/QCTTLOI2yCmo8NV6gpkeke0uD48n 6WRu/+q35tI5k+oGZUOGXgliOE9ZskzqCad+RGL0FS06Z3LUzduZBivQ9v/U1w6lu2xZ 3FtibKED9nFrpwClm59MCZpu9PHisgUJ0eGPQyYxD+3uEGNAyezxDo2G/OoOJ1TN8TEV 9wWMH8M7fesr4f0cOXCzc3czJQzwkoWpb7bx9d5oVGsn1O2uw3hsd0nDMtUNbBltVULU kmIQdYINuyrwu7VCUIDBRLYagFvJXULl4LSLwn5bvP4G5mUhFi5FypL717RXQIXF9dWU 5nVQ== X-Gm-Message-State: AMke39lvqvGreV7sn9Bn2c8w7U1Q5usFdQN3HY8/shJZVqLeIVjPius9/78L8A1u1Nc+H7FR X-Received: by 10.46.80.11 with SMTP id e11mr1310160ljb.116.1488556388604; Fri, 03 Mar 2017 07:53:08 -0800 (PST) Received: from localhost.localdomain (nivc-213.auriga.ru. [80.240.102.213]) by smtp.gmail.com with ESMTPSA id x1sm2391090lfb.54.2017.03.03.07.53.06 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 03 Mar 2017 07:53:07 -0800 (PST) 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 v3 3/3] printk: fix double printing with earlycon Date: Fri, 3 Mar 2017 18:49:41 +0300 Message-Id: <20170303154946.15399-1-aleksey.makarov@linaro.org> X-Mailer: git-send-email 2.11.1 In-Reply-To: <20170302131153.22733-4-aleksey.makarov@linaro.org> References: <20170302131153.22733-4-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, match the console against the `console_cmdline` entry pointed by `preferred_console` and if that fails, match against other entries. The problem is that newcon->match() is not just a match function, it calls setup() so it has side effects. Reported-by: Sudeep Holla Signed-off-by: Aleksey Makarov --- v2 -> v3: v2 still changes the logic of the code and calls newcon->match() several times. V3 fixes that. It initially matches the console against the preferred_console entry, and then if that fails, against other entries. kernel/printk/printk.c | 82 ++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 59 insertions(+), 23 deletions(-) -- 2.11.1 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index ed2a9b31f214..99bea4ae8f4a 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2380,6 +2380,32 @@ static int __init keep_bootcon_setup(char *str) early_param("keep_bootcon", keep_bootcon_setup); +static enum { CONSOLE_MATCH, CONSOLE_MATCH_RETURN, CONSOLE_MATCH_NEXT } +match_console(struct console *newcon, struct console_cmdline *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) + return CONSOLE_MATCH_NEXT; + if (newcon->index >= 0 && newcon->index != c->index) + return CONSOLE_MATCH_NEXT; + if (newcon->index < 0) + newcon->index = c->index; + + if (_braille_register_console(newcon, c)) + return CONSOLE_MATCH_RETURN; + + if (newcon->setup && + newcon->setup(newcon, c->options) != 0) + return CONSOLE_MATCH; + } + + newcon->flags |= CON_ENABLED; + return CONSOLE_MATCH; +} + /* * The console driver calls this routine during kernel initialization * to register the console printing procedure with printk() and to @@ -2454,40 +2480,50 @@ void register_console(struct console *newcon) } /* + * 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 so we + * can not just use the first match. Instead check the entry pointed + * by preferred_console and then all other entries. + */ + if (preferred_console >= 0) { + switch (match_console(newcon, + console_cmdline + preferred_console)) { + case CONSOLE_MATCH: + if (newcon->flags | CON_ENABLED) { + newcon->flags |= CON_CONSDEV; + has_preferred = true; + } + goto match; + case CONSOLE_MATCH_RETURN: + return; + default: + break; + } + } + + /* * See if this console matches one we selected on * the command line. */ 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; - } + if (preferred_console == i) + continue; - newcon->flags |= CON_ENABLED; - if (i == preferred_console) { - newcon->flags |= CON_CONSDEV; - has_preferred = true; + switch (match_console(newcon, c)) { + case CONSOLE_MATCH: + goto match; + case CONSOLE_MATCH_RETURN: + return; + default: + break; } - break; } +match: if (!(newcon->flags & CON_ENABLED)) return;