* [PATCH 1/2] netconsole: give the console of each target a unique name
2026-12-04 20:01 [PATCH 0/2] netconsole: give the console of each target a unique name Chris Down
@ 2026-12-04 20:01 ` Chris Down
2026-12-04 20:44 ` Andy Shevchenko
2026-12-04 20:01 ` [PATCH 2/2] selftests: netconsole: check that targets get distinct console names Chris Down
1 sibling, 1 reply; 4+ messages in thread
From: Chris Down @ 2026-12-04 20:01 UTC (permalink / raw)
To: Breno Leitao, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni
Cc: Shuah Khan, Shuah Khan, Gustavo Luiz Duarte, linux-kselftest,
netdev, linux-kernel, pmladek, john.ogness, printk, kernel-team
Since commit 0d97bae9abbe ("netconsole: register one console per
target") the console of a target is called netcons_<target name>. Only
15 characters of that fit in the name of a console and a target made
through configfs always has index 0, so targets whose names start with
the same seven characters get consoles with the same name and index.
For example, with targets called target_alpha and target_beta:
# grep netcons /proc/consoles
netcons_target_0 -W- (E N )
netcons_target_0 -W- (E N )
The targets which netcons_torture.sh creates all register as
netcons_netcons0 in the same way. A target called cmdline5 which was
not set up by netconsole= is cut to netcons_cmdline0 as well, and that
is already the console of the first target from the command line.
That commit said that names could collide, and on its own all this
means is that /proc/consoles can't tell these consoles apart. It
becomes a real problem once something is keyed by the name. The
per-console loglevel series [0] adds /sys/class/console/<name><index>
for each console, which is also where the replay trigger for
netconsole is expected to go [1]. With that series applied on top,
enabling target_beta above prints a stack trace with
sysfs: cannot create duplicate filename '/devices/virtual/console/netcons_target_0'
and the target is left without a directory in /sys/class/console, so
there is no way to set its loglevel.
To fix this, let's give a new target the lowest index which no other
target with the same console name has. The targets above become
netcons_target_0 and netcons_target_1. A console name shorter than 15
characters, like netcons_foo, can only belong to one target, so target
foo keeps index 0 and is still netcons_foo0.
The index is chosen when the target is created, and not when its
console is registered, so that the console keeps its name while the
target is disabled and enabled again. Choosing it means looking at
every other target. A target is on target_list or
target_cleanup_list, except while process_resume_target() has taken it
off both, which it does with dynamic_netconsole_mutex held.
make_netconsole_target() thus takes that mutex and
target_cleanup_list_lock as well as target_list_lock.
Numbering all the consoles from one counter would be simpler, but then
target foo is no longer always netcons_foo0, and target a with index
10 would show up under the same name as target a1 with index 0.
I ran into this while testing the per-console loglevel series on top
of net-next, with the two targets above. With this patch they get
different names in /proc/consoles and, with that series applied, a
directory each in /sys/class/console. The netconsole selftests still
pass.
[0]: https://lore.kernel.org/r/84b8d91693e56f6026271bc40553bcb6657ab24d.1764272407.git.chris@chrisdown.name
[1]: https://lore.kernel.org/r/at84qygscue8crQi@gmail.com
Fixes: 0d97bae9abbe ("netconsole: register one console per target")
Signed-off-by: Chris Down <chris@chrisdown.name>
---
drivers/net/netconsole.c | 43 +++++++++++++++++++++++++++++++++++++++-
1 file changed, 42 insertions(+), 1 deletion(-)
diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index 4b546194f717..713f7735b062 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -1850,6 +1850,40 @@ static struct netconsole_target *find_cmdline_target(const char *name)
return ret;
}
+static bool console_index_in_use(struct list_head *targets, const char *name,
+ short index)
+{
+ struct netconsole_target *nt;
+
+ list_for_each_entry(nt, targets, list) {
+ if (nt->console.index == index &&
+ !strcmp(nt->console.name, name))
+ return true;
+ }
+
+ return false;
+}
+
+/* Return the lowest index that no target uses together with the console name
+ * @name. A target is on target_list or on target_cleanup_list, unless
+ * process_resume_target() is resuming it under dynamic_netconsole_mutex, so
+ * the caller holds that mutex and the locks of both lists.
+ */
+static short netconsole_free_console_index(const char *name)
+{
+ short index = 0;
+
+ lockdep_assert_held(&dynamic_netconsole_mutex);
+ lockdep_assert_held(&target_cleanup_list_lock);
+ lockdep_assert_held(&target_list_lock);
+
+ while (console_index_in_use(&target_list, name, index) ||
+ console_index_in_use(&target_cleanup_list, name, index))
+ index++;
+
+ return index;
+}
+
/*
* Group operations and type for netconsole_subsys.
*/
@@ -1879,14 +1913,21 @@ static struct config_group *make_netconsole_target(struct config_group *group,
/* Initialize the config_group member */
init_target_config_group(nt, name);
- /* The index stays 0, printk shows the console as netcons_<name>0 */
snprintf(nt->console.name, sizeof(nt->console.name), "netcons_%s",
name);
/* Adding, but it is disabled */
+ dynamic_netconsole_mutex_lock();
+ mutex_lock(&target_cleanup_list_lock);
spin_lock_irqsave(&target_list_lock, flags);
+ /* netcons_<name> is cut to fit, so targets can share it. printk shows
+ * the index after the name, which is 0 unless the name is taken.
+ */
+ nt->console.index = netconsole_free_console_index(nt->console.name);
list_add(&nt->list, &target_list);
spin_unlock_irqrestore(&target_list_lock, flags);
+ mutex_unlock(&target_cleanup_list_lock);
+ dynamic_netconsole_mutex_unlock();
return &nt->group;
}
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread* [PATCH 2/2] selftests: netconsole: check that targets get distinct console names
2026-12-04 20:01 [PATCH 0/2] netconsole: give the console of each target a unique name Chris Down
2026-12-04 20:01 ` [PATCH 1/2] " Chris Down
@ 2026-12-04 20:01 ` Chris Down
1 sibling, 0 replies; 4+ messages in thread
From: Chris Down @ 2026-12-04 20:01 UTC (permalink / raw)
To: Breno Leitao, Andrew Lunn, David S. Miller, Eric Dumazet,
Jakub Kicinski, Paolo Abeni
Cc: Shuah Khan, Shuah Khan, Gustavo Luiz Duarte, linux-kselftest,
netdev, linux-kernel, pmladek, john.ogness, printk, kernel-team
Add a test which creates two targets whose names start with the same
seven characters, so that netcons_<target name> is cut to the same
console name for both, and checks that /proc/consoles lists a
different console for each of them.
It fails without the previous patch:
# ./netcons_console_name.sh
FAIL: targets 9gqXFYF_alpha 9gqXFYF_beta share a console name in /proc/consoles:
netcons_9gqXFYF0
netcons_9gqXFYF0
Signed-off-by: Chris Down <chris@chrisdown.name>
---
.../selftests/drivers/net/netconsole/Makefile | 1 +
.../net/netconsole/netcons_console_name.sh | 70 +++++++++++++++++++
2 files changed, 71 insertions(+)
create mode 100755 tools/testing/selftests/drivers/net/netconsole/netcons_console_name.sh
diff --git a/tools/testing/selftests/drivers/net/netconsole/Makefile b/tools/testing/selftests/drivers/net/netconsole/Makefile
index c7972a40babf..0a689d75aed4 100644
--- a/tools/testing/selftests/drivers/net/netconsole/Makefile
+++ b/tools/testing/selftests/drivers/net/netconsole/Makefile
@@ -9,6 +9,7 @@ TEST_INCLUDES := \
TEST_PROGS := \
netcons_basic.sh \
netcons_cmdline.sh \
+ netcons_console_name.sh \
netcons_fragmented_msg.sh \
netcons_overflow.sh \
netcons_resume.sh \
diff --git a/tools/testing/selftests/drivers/net/netconsole/netcons_console_name.sh b/tools/testing/selftests/drivers/net/netconsole/netcons_console_name.sh
new file mode 100755
index 000000000000..28a3f17f560d
--- /dev/null
+++ b/tools/testing/selftests/drivers/net/netconsole/netcons_console_name.sh
@@ -0,0 +1,70 @@
+#!/usr/bin/env bash
+# SPDX-License-Identifier: GPL-2.0
+
+# This test creates two netconsole targets whose names start with the same
+# seven characters. The console of a target is called netcons_<target name>,
+# cut to the 15 characters that fit in the name of a console, so the consoles
+# of these two targets get the same name and only the index that follows it
+# can tell them apart.
+#
+# The test checks that /proc/consoles lists a different console for each of
+# the two targets.
+#
+# Author: Chris Down <chris@chrisdown.name>
+
+set -euo pipefail
+
+SCRIPTDIR=$(dirname "$(readlink -e "${BASH_SOURCE[0]}")")
+
+source "${SCRIPTDIR}"/../lib/sh/lib_netcons.sh
+
+# The seven characters that both target names start with
+PREFIX=$(mktemp -u XXXXXXX)
+CONSOLE="netcons_${PREFIX}"
+TARGETS=("${PREFIX}_alpha" "${PREFIX}_beta")
+
+function cleanup() {
+ local T
+
+ for T in "${TARGETS[@]}"
+ do
+ cleanup_netcons "${NETCONS_CONFIGFS}/${T}"
+ done
+ do_cleanup
+}
+
+modprobe netdevsim 2> /dev/null || true
+modprobe netconsole 2> /dev/null || true
+
+# Check for basic system dependency and exit if not found
+check_for_dependencies
+# Remove the namespace, interfaces and netconsole targets on exit
+trap cleanup EXIT
+# Create one namespace and two interfaces
+set_network
+
+for T in "${TARGETS[@]}"
+do
+ create_dynamic_target "extended" "${NETCONS_CONFIGFS}/${T}"
+done
+
+# /proc/consoles starts each line with the name and the index of a console
+NAMES=$(awk -v con="${CONSOLE}" 'index($1, con) == 1 { print $1 }' \
+ /proc/consoles)
+NR_CONSOLES=$(echo "${NAMES}" | grep -c . || true)
+NR_NAMES=$(echo "${NAMES}" | sort -u | grep -c . || true)
+
+if [ "${NR_CONSOLES}" -ne "${#TARGETS[@]}" ]
+then
+ echo "FAIL: found ${NR_CONSOLES} consoles called ${CONSOLE}<index>, not ${#TARGETS[@]}" >&2
+ exit "${ksft_fail}"
+fi
+
+if [ "${NR_NAMES}" -ne "${NR_CONSOLES}" ]
+then
+ echo "FAIL: targets ${TARGETS[*]} share a console name in /proc/consoles:" >&2
+ echo "${NAMES}" >&2
+ exit "${ksft_fail}"
+fi
+
+exit "${ksft_pass}"
--
2.56.1
^ permalink raw reply related [flat|nested] 4+ messages in thread