Since commit 0d97bae9abbe ("netconsole: register one console per target") the console of a target is called netcons_. 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/ 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 --- 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_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_ 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