Skip to content

Commit 30ed50a

Browse files
leitaogregkh
authored andcommitted
netconsole: Only register console drivers when targets are configured
[ Upstream commit bc0cb64 ] The netconsole driver currently registers the basic console driver unconditionally during initialization, even when only extended targets are configured. This results in unnecessary console registration and performance overhead, as the write_msg() callback is invoked for every log message only to return early when no matching targets are found. Optimize the driver by conditionally registering console drivers based on the actual target configuration. The basic console driver is now registered only when non-extended targets exist, same as the extended console. The implementation also handles dynamic target creation through the configfs interface. This change eliminates unnecessary console driver registrations, redundant write_msg() callbacks for unused console types, and associated lock contention and target list iterations. The optimization is particularly beneficial for systems using only the most common extended console type. Fixes: e2f15f9 ("netconsole: implement extended console support") Signed-off-by: Breno Leitao <[email protected]> Link: https://patch.msgid.link/[email protected] Signed-off-by: Jakub Kicinski <[email protected]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 2f9e82a commit 30ed50a

File tree

1 file changed

+22
-8
lines changed

1 file changed

+22
-8
lines changed

drivers/net/netconsole.c

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -86,17 +86,22 @@ static DEFINE_SPINLOCK(target_list_lock);
8686
static DEFINE_MUTEX(target_cleanup_list_lock);
8787

8888
/*
89-
* Console driver for extended netconsoles. Registered on the first use to
90-
* avoid unnecessarily enabling ext message formatting.
89+
* Console driver for netconsoles. Register only consoles that have
90+
* an associated target of the same type.
9191
*/
92-
static struct console netconsole_ext;
92+
static struct console netconsole_ext, netconsole;
9393

9494
struct netconsole_target_stats {
9595
u64_stats_t xmit_drop_count;
9696
u64_stats_t enomem_count;
9797
struct u64_stats_sync syncp;
9898
};
9999

100+
enum console_type {
101+
CONS_BASIC = BIT(0),
102+
CONS_EXTENDED = BIT(1),
103+
};
104+
100105
/* Features enabled in sysdata. Contrary to userdata, this data is populated by
101106
* the kernel. The fields are designed as bitwise flags, allowing multiple
102107
* features to be set in sysdata_fields.
@@ -491,6 +496,12 @@ static ssize_t enabled_store(struct config_item *item,
491496
if (nt->extended && !console_is_registered(&netconsole_ext))
492497
register_console(&netconsole_ext);
493498

499+
/* User might be enabling the basic format target for the very
500+
* first time, make sure the console is registered.
501+
*/
502+
if (!nt->extended && !console_is_registered(&netconsole))
503+
register_console(&netconsole);
504+
494505
/*
495506
* Skip netpoll_parse_options() -- all the attributes are
496507
* already configured via configfs. Just print them out.
@@ -1690,8 +1701,8 @@ static int __init init_netconsole(void)
16901701
{
16911702
int err;
16921703
struct netconsole_target *nt, *tmp;
1704+
u32 console_type_needed = 0;
16931705
unsigned int count = 0;
1694-
bool extended = false;
16951706
unsigned long flags;
16961707
char *target_config;
16971708
char *input = config;
@@ -1707,9 +1718,10 @@ static int __init init_netconsole(void)
17071718
}
17081719
/* Dump existing printks when we register */
17091720
if (nt->extended) {
1710-
extended = true;
1721+
console_type_needed |= CONS_EXTENDED;
17111722
netconsole_ext.flags |= CON_PRINTBUFFER;
17121723
} else {
1724+
console_type_needed |= CONS_BASIC;
17131725
netconsole.flags |= CON_PRINTBUFFER;
17141726
}
17151727

@@ -1728,9 +1740,10 @@ static int __init init_netconsole(void)
17281740
if (err)
17291741
goto undonotifier;
17301742

1731-
if (extended)
1743+
if (console_type_needed & CONS_EXTENDED)
17321744
register_console(&netconsole_ext);
1733-
register_console(&netconsole);
1745+
if (console_type_needed & CONS_BASIC)
1746+
register_console(&netconsole);
17341747
pr_info("network logging started\n");
17351748

17361749
return err;
@@ -1760,7 +1773,8 @@ static void __exit cleanup_netconsole(void)
17601773

17611774
if (console_is_registered(&netconsole_ext))
17621775
unregister_console(&netconsole_ext);
1763-
unregister_console(&netconsole);
1776+
if (console_is_registered(&netconsole))
1777+
unregister_console(&netconsole);
17641778
dynamic_netconsole_exit();
17651779
unregister_netdevice_notifier(&netconsole_netdev_notifier);
17661780

0 commit comments

Comments
 (0)