From nobody Sat Feb 7 15:59:59 2026 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9ED122DBF47 for ; Fri, 6 Feb 2026 16:50:24 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770396624; cv=none; b=gY3gDmP0jTFo9BpNHNX//zBrjQ+3ntFIZtbBGBO/C00CWuJaA9eGRLz3T7l5XjGqG/LVzk+JipNP2qyRyivFv18E2gb/1rlbWsuSsrPaMtxtxb5HMSLTz3SJ8UmTThnGnnPQdg5uFg4QVw0yf0us8uTk7e16zFdAl//uGpp41Rc= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1770396624; c=relaxed/simple; bh=ut88w96/DVfaWRxzklmSBGL7DufjQBHFK177d5GFX1M=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=ps7OENo4fDbi+p4MTvanFQogahLKurpQQ0SA5SY7F7EyODu/NNB80YUbdh1VTC3gTJWv5KQTqctYQ6aRdD4FHZd+mRdZlujE3re7F56Hf7Ohi9ZaBVkfDfA0lIhDDpFkq7aIQck+v7iqQBoxPbiWrEjUaSNTrHYFnHXqrYUkDx8= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com; spf=pass smtp.mailfrom=suse.com; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=enjTDlFG; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b=iQiSTh+2; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=quarantine dis=none) header.from=suse.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="enjTDlFG"; dkim=pass (1024-bit key) header.d=suse.com header.i=@suse.com header.b="iQiSTh+2" Received: from pathway.suse.cz (unknown [IPv6:2a07:de40:b2bf:1b::12bd]) by smtp-out1.suse.de (Postfix) with ESMTP id B60DA3E6E6; Fri, 6 Feb 2026 16:50:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1770396623; h=from:from:reply-to: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=IqmZnGwgIeBsZqyvsIiYTP8E2ig+f3k1b+FAgn3zQfg=; b=enjTDlFGcyLZbQJN1tn6tq3oKyIvPVmE6mkZkOMc4AfNCPRBN2BbRzzhrRR6aBAgUnxoQt dP297np/JRxAXVtaHQph5g76sWwSJD/Nb++njcZpAk6OAWPMKh9TmxLzOl7cJmGL+pUeyy dgAd6Sn3DVzZQYLgp0A2BIEXNqMfmfY= Authentication-Results: smtp-out1.suse.de; dkim=pass header.d=suse.com header.s=susede1 header.b=iQiSTh+2 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1770396622; h=from:from:reply-to: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=IqmZnGwgIeBsZqyvsIiYTP8E2ig+f3k1b+FAgn3zQfg=; b=iQiSTh+2LmnjRtrr/VrclycO8lJTBY2///ag5L6IndCf2kFijfKdhs9wv8726byk+IBOm1 skqrDpu8RGv5vwUIsy6kAWdp592doDNiVG9ztiHwYDZAN6fWS7nwxEx+hg6NtXg1JBahm9 6ZrVFTmLLhOOsfksFTrqHWcK6OJih4A= From: Petr Mladek To: John Ogness Cc: Sergey Senozhatsky , Steven Rostedt , Marcos Paulo de Souza , Chris Down , linux-kernel@vger.kernel.org, Petr Mladek Subject: [PATCH 3/8] printk: Separate code for adding/updating preferred console metadata Date: Fri, 6 Feb 2026 17:49:57 +0100 Message-ID: <20260206165002.496724-4-pmladek@suse.com> X-Mailer: git-send-email 2.52.0 In-Reply-To: <20260206165002.496724-1-pmladek@suse.com> References: <20260206165002.496724-1-pmladek@suse.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spamd-Result: default: False [16.67 / 50.00]; SPAM_FLAG(5.00)[]; NEURAL_SPAM_LONG(3.50)[1.000]; BAYES_HAM(-3.00)[100.00%]; NEURAL_SPAM_SHORT(2.98)[0.994]; HFILTER_HOSTNAME_UNKNOWN(2.50)[]; RDNS_NONE(2.00)[]; ONCE_RECEIVED(1.20)[]; HFILTER_HELO_IP_A(1.00)[pathway.suse.cz]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; HFILTER_HELO_NORES_A_OR_MX(0.30)[pathway.suse.cz]; R_DKIM_ALLOW(-0.20)[suse.com:s=susede1]; MIME_GOOD(-0.10)[text/plain]; MX_GOOD(-0.01)[]; RBL_SPAMHAUS_BLOCKED_OPENRESOLVER(0.00)[2a07:de40:b2bf:1b::12bd:from]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCPT_COUNT_SEVEN(0.00)[7]; ARC_NA(0.00)[]; TO_DN_SOME(0.00)[]; MIME_TRACE(0.00)[0:+]; DBL_BLOCKED_OPENRESOLVER(0.00)[pathway.suse.cz:helo,suse.com:mid,suse.com:dkim,suse.com:email]; DIRECT_TO_MX(0.00)[git-send-email 2.52.0]; DKIM_SIGNED(0.00)[suse.com:s=susede1]; FROM_EQ_ENVFROM(0.00)[]; SPAMHAUS_XBL(0.00)[2a07:de40:b2bf:1b::12bd:from]; FROM_HAS_DN(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_ZERO(0.00)[0]; DNSWL_BLOCKED(0.00)[2a07:de40:b2bf:1b::12bd:from]; DKIM_TRACE(0.00)[suse.com:+] X-Spam-Flag: YES X-Spamd-Bar: ++++++++++++++++ X-Rspamd-Queue-Id: B60DA3E6E6 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Action: add header X-Spam-Level: **************** X-Spam-Score: 16.67 X-Spam: Yes Content-Type: text/plain; charset="utf-8" The logic for adding or updating a preferred console is currently duplicated within __add_preferred_console(), making the code difficult to follow and prone to consistency issues. Introduce update_preferred_console() to centralize the initialization and updating of struct preferred_console entries. This refactoring explicitly defines and enforces the following rules: 1. Console names and/or indexes are not set when a console is preferred via devname; these are resolved later during device matching. 2. Console names are only added alongside a valid index. 3. Only matching entries are updated. 4. Console and Braille options are never cleared. They are updated only via the command line. 5. The global 'preferred_dev_console' index and 'console_set_on_cmdline' flag are updated consistently. Additionally, rename braille_set_options() to braille_update_options() to better reflect its conditional behavior. While this is primarily a refactoring, it fixes two minor behavioral consistencies: console options can now be overridden via the command line, and Braille options are preserved even if a non-Braille console with the same name was previously defined. Signed-off-by: Petr Mladek --- kernel/printk/braille.h | 7 +-- kernel/printk/printk.c | 110 +++++++++++++++++++++++++++------------- 2 files changed, 80 insertions(+), 37 deletions(-) diff --git a/kernel/printk/braille.h b/kernel/printk/braille.h index 55cd3178a17a..0bdac303f8b1 100644 --- a/kernel/printk/braille.h +++ b/kernel/printk/braille.h @@ -5,9 +5,10 @@ #ifdef CONFIG_A11Y_BRAILLE_CONSOLE =20 static inline void -braille_set_options(struct preferred_console *pc, char *brl_options) +braille_update_options(struct preferred_console *pc, char *brl_options) { - pc->brl_options =3D brl_options; + if (brl_options) + pc->brl_options =3D brl_options; } =20 /* @@ -29,7 +30,7 @@ _braille_unregister_console(struct console *console); #else =20 static inline void -braille_set_options(struct preferred_console *pc, char *brl_options) +braille_update_options(struct preferred_console *pc, char *brl_options) { } =20 diff --git a/kernel/printk/printk.c b/kernel/printk/printk.c index 3f856a438e74..ee57c7ac9d02 100644 --- a/kernel/printk/printk.c +++ b/kernel/printk/printk.c @@ -2491,18 +2491,82 @@ asmlinkage __visible void early_printk(const char *= fmt, ...) } #endif =20 -static void set_user_specified(struct preferred_console *pc, bool user_spe= cified) +static int update_preferred_console(int i, const char *name, const short i= dx, + const char *devname, char *options, + char *brl_options, bool user_specified) { - if (!user_specified) - return; + struct preferred_console *pc; + + if (i >=3D MAX_PREFERRED_CONSOLES) + return -E2BIG; + + pc =3D &preferred_consoles[i]; + + if (!name && !devname) + return -EINVAL; + + if (devname) { + /* + * A valid console name and index will get assigned when + * a matching device gets registered. + */ + if (name) { + pr_err("Adding a preferred console devname with a hard-coded console na= me: %s, %s\n", + devname, name); + return -EINVAL; + } + if (idx !=3D -1) { + pr_err("Adding a preferred console devname with a hard-coded index: %s,= %d\n", + devname, idx); + return -EINVAL; + } + + if (!pc->devname[0]) { + strscpy(pc->devname, devname); + pc->index =3D idx; + } else if (strcmp(pc->devname, devname) !=3D 0) { + pr_err("Updating a preferred console with an invalid devname: %s vs. %s= \n", + pc->devname, devname); + return -EINVAL; + } + } + + if (name) { + /* A console name must be defined with a valid index. */ + if (idx < 0) { + pr_err("Adding a preferred console with an invalid index: %s, %d\n", + name, idx); + return -EINVAL; + } + + if (!pc->name[0]) { + strscpy(pc->name, name); + pc->index =3D idx; + } else if (strcmp(pc->name, name) !=3D 0 || pc->index !=3D idx) { + pr_err("Updating a preferred console with an invalid name or index: %s%= d vs. %s%d\n", + pc->name, pc->index, name, idx); + return -EINVAL; + } + } + + if (!pc->options || (user_specified && options)) + pc->options =3D options; + + braille_update_options(pc, brl_options); + + if (!brl_options) + preferred_dev_console =3D i; =20 /* * @c console was defined by the user on the command line. * Do not clear when added twice also by SPCR or the device tree. */ - pc->user_specified =3D true; - /* At least one console defined by the user on the command line. */ - console_set_on_cmdline =3D 1; + if (user_specified) { + pc->user_specified =3D true; + console_set_on_cmdline =3D 1; + } + + return 0; } =20 static int __add_preferred_console(const char *name, const short idx, @@ -2515,14 +2579,6 @@ static int __add_preferred_console(const char *name,= const short idx, if (!name && !devname) return -EINVAL; =20 - /* - * We use a signed short index for struct console for device drivers to - * indicate a not yet assigned index or port. However, a negative index - * value is not valid when the console name and index are defined on - * the command line. - */ - if (name && idx < 0) - return -EINVAL; =20 /* * See if this tty is not yet registered, and @@ -2531,28 +2587,14 @@ static int __add_preferred_console(const char *name= , const short idx, for (i =3D 0, pc =3D preferred_consoles; i < MAX_PREFERRED_CONSOLES && (pc->name[0] || pc->devname[0]); i++, pc++) { - if ((name && strcmp(pc->name, name) =3D=3D 0 && pc->index =3D=3D idx) || - (devname && strcmp(pc->devname, devname) =3D=3D 0)) { - if (!brl_options) - preferred_dev_console =3D i; - set_user_specified(pc, user_specified); - return 0; - } + if (name && strcmp(pc->name, name) =3D=3D 0 && pc->index =3D=3D idx) + break; + if (devname && strcmp(pc->devname, devname) =3D=3D 0) + break; } - if (i =3D=3D MAX_PREFERRED_CONSOLES) - return -E2BIG; - if (!brl_options) - preferred_dev_console =3D i; - if (name) - strscpy(pc->name, name); - if (devname) - strscpy(pc->devname, devname); - pc->options =3D options; - set_user_specified(pc, user_specified); - braille_set_options(pc, brl_options); =20 - pc->index =3D idx; - return 0; + return update_preferred_console(i, name, idx, devname, options, + brl_options, user_specified); } =20 static int __init console_msg_format_setup(char *str) --=20 2.52.0