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. With 32768 targets sharing a console name, mkdir of one more fails with ENOSPC until one of them is removed. The netconsole selftests still pass. Link: https://lore.kernel.org/r/84b8d91693e56f6026271bc40553bcb6657ab24d.1764272407.git.chris@chrisdown.name [0] Link: https://lore.kernel.org/r/at84qygscue8crQi@gmail.com [1] Fixes: 0d97bae9abbe ("netconsole: register one console per target") Signed-off-by: Chris Down --- drivers/net/netconsole.c | 65 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 63 insertions(+), 2 deletions(-) diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c index 4b546194f717..348ae1b7decf 100644 --- a/drivers/net/netconsole.c +++ b/drivers/net/netconsole.c @@ -25,6 +25,7 @@ #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt #include +#include #include #include #include @@ -1850,6 +1851,45 @@ static struct netconsole_target *find_cmdline_target(const char *name) return ret; } +/* The index of a console is a short, and printk chooses one itself when it is + * given a negative index, so this is how many targets can share a console + * name. + */ +#define MAX_CONSOLE_INDEXES (SHRT_MAX + 1) + +static void console_indexes_in_use(struct list_head *targets, const char *name, + unsigned long *in_use) +{ + struct netconsole_target *nt; + + list_for_each_entry(nt, targets, list) { + if (!strcmp(nt->console.name, name)) + __set_bit(nt->console.index, in_use); + } +} + +/* 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 int netconsole_free_console_index(const char *name, + unsigned long *in_use) +{ + unsigned long index; + + lockdep_assert_held(&dynamic_netconsole_mutex); + lockdep_assert_held(&target_cleanup_list_lock); + lockdep_assert_held(&target_list_lock); + + console_indexes_in_use(&target_list, name, in_use); + console_indexes_in_use(&target_cleanup_list, name, in_use); + index = find_first_zero_bit(in_use, MAX_CONSOLE_INDEXES); + if (index == MAX_CONSOLE_INDEXES) + return -ENOSPC; + + return index; +} + /* * Group operations and type for netconsole_subsys. */ @@ -1858,7 +1898,9 @@ static struct config_group *make_netconsole_target(struct config_group *group, const char *name) { struct netconsole_target *nt; + unsigned long *in_use; unsigned long flags; + int index; /* Checking if a target by this name was created at boot time. If so, * attach a configfs entry to that target. This enables dynamic @@ -1877,16 +1919,35 @@ static struct config_group *make_netconsole_target(struct config_group *group, if (!nt) return ERR_PTR(-ENOMEM); + in_use = bitmap_zalloc(MAX_CONSOLE_INDEXES, GFP_KERNEL); + if (!in_use) { + kfree(nt); + return ERR_PTR(-ENOMEM); + } + /* 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); - list_add(&nt->list, &target_list); + index = netconsole_free_console_index(nt->console.name, in_use); + if (index >= 0) { + nt->console.index = index; + list_add(&nt->list, &target_list); + } spin_unlock_irqrestore(&target_list_lock, flags); + mutex_unlock(&target_cleanup_list_lock); + dynamic_netconsole_mutex_unlock(); + bitmap_free(in_use); + + if (index < 0) { + config_group_put(&nt->group); + return ERR_PTR(index); + } return &nt->group; } -- 2.56.1