Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp4790593ybl; Mon, 9 Dec 2019 17:00:08 -0800 (PST) X-Google-Smtp-Source: APXvYqz/VD1nWS4dLs4CLSEcjElIU4J9t5Z5Ks0FJOiiub8IZ14VcgfQnuKbCfmuYK5c8hWAL+CX X-Received: by 2002:a9d:73c4:: with SMTP id m4mr22416816otk.87.1575939608077; Mon, 09 Dec 2019 17:00:08 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1575939608; cv=none; d=google.com; s=arc-20160816; b=bBcC2QkPyKkn4dAZ3bXDSn9bJ3azqKy6j61GgRu/cZ3GG0hvdhaRwhS6ZU7h6/lhDD UXZoUxjJ2F045/glsmgyNc5h9Ta21QmtQdmK3UtDsvXiDXvm54Qx0FLu9QC9yL7iYxY9 1xJJemp4ms5mWFdIAsOKikSVjx7uJsiA1FVcCPh2y0IFpRYbMVEBex3c9tKNZ7aKSNX5 m+Fo9x6hZsCIkzS3fB8Hdv3lUFatq+ZB3OhwLFyryL4wn79M15jovJSbpZCtKBIXukSA BVlhmGWWlhuV0mIRaYGYMaNFqsprLXazT7gsV/AcEVnB5mSIimE4++tMUjGTw9GuQPFD 1c5g== 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=ZzqNhh5M/MKGSQMSH7VKU/s35DNOqc8v1p+JuX0/yUc=; b=KGTHwo79u/Y6WjnsbBmJ+0/gHgmusCq5QI77DyM6hWkxQdNqkdNnpFhz14LbiHZliF zZaMOrVTAvgUYDdnMZl9/KaXZm1o8EeAPoZ1tID9n7MZ24+f7nWEQs0tIHxt3Ks7Tq2E jjoDy+hYU7JyySQJ6brUaM2GN1Fy8aRuTa7kdD+57ZZCjtzepftcgzATBVHMI97g/xKn VBLK6zQ/JjQyTJbwfoM3mSUvcw0P2lHbMPr+yqU4VCNHQSjG0z/3soFT9kcabEhGOHDk JZNQueSkBbRMDU3JLrRzc3ojsv1GXFH1JnPtPVjXf66QzB/AzYMEyS67tw/k3iSNrAVm XO+Q== 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 w11si1055226oig.45.2019.12.09.16.59.55; Mon, 09 Dec 2019 17:00:08 -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 S1727501AbfLJA6E (ORCPT + 99 others); Mon, 9 Dec 2019 19:58:04 -0500 Received: from gate.crashing.org ([63.228.1.57]:40870 "EHLO gate.crashing.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726743AbfLJA6E (ORCPT ); Mon, 9 Dec 2019 19:58:04 -0500 Received: from localhost (localhost.localdomain [127.0.0.1]) by gate.crashing.org (8.14.1/8.14.1) with ESMTP id xBA0vQdi010522; Mon, 9 Dec 2019 18:57:27 -0600 Message-ID: Subject: [RFC/PATCH] printk: Fix preferred console selection with multiple matches From: Benjamin Herrenschmidt To: linux-kernel@vger.kernel.org Cc: Petr Mladek , Sergey Senozhatsky , Steven Rostedt , Linus Torvalds , AlekseyMakarov Date: Tue, 10 Dec 2019 11:57:26 +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 In the following circumstances, the rule of selecting the console corresponding to the last "console=" entry on the command line as the preferred console (CON_CONSDEV, ie, /dev/console) fails. This is a specific example, but it could happen with different consoles that have a similar name aliasing mechanism. - The kernel command line has both console=tty0 and console=ttyS0 in that order (the latter with speed etc... arguments). This is common with some cloud setups such as Amazon Linux. - add_preferred_console is called early to register "uart0". In our case that happens from acpi_parse_spcr() on arm64 since the "enable_console" argument is true on that architecture. This causes "uart0" to become entry 0 of the console_cmdline array. Now, because of the above, what happens is: - add_preferred_console is called by the cmdline parsing for tty0 and ttyS0 respectively, thus occupying entries 1 and 2 of the console_cmdline array (since this happens after ACPI SPCR parsing). At that point preferred_console is set to 2 as expected. - When the tty layer kicks in, it will call register_console for tty0. This will match entry 1 in console_cmdline array. It isn't our preferred console but because it's our only console at this point, it will end up "first" in the consoles list. - When 8250 probes the actual serial port later on, it calls register_console for ttyS0. At that point the loop in register_console tries to match it with the entries in the console_cmdline array. Ideally this should match ttyS0 in entry 2, which is preferred, causing it to be inserted first and to replace tty0 as CONSDEV. However, 8250 provides a "match" hook in its struct console, and that hook will match "uart" as an alias to "ttyS". So we match uart0 at entry 0 in the array which is not the preferred console and will not match entry 2 which is since we break out of the loop on the first match. As a result, we don't set CONSDEV and don't insert it first, but second in the console list. As a result, we end up with tty0 remaining first in the array, and thus /dev/console going there instead of the last user specified one which is ttyS0. This tentative fix changes the loop in register_console to continue matching with the array until the match is actually the preferred console. Signed-off-by: Benjamin Herrenschmidt --- kernel/printk/printk.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 5aa96098c64d..d36b9901c0e0 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2646,8 +2646,8 @@ void register_console(struct console *newcon) if (i == preferred_console) { newcon->flags |= CON_CONSDEV; has_preferred = true; + break; } - break; } if (!(newcon->flags & CON_ENABLED)) -- 2.17.1