Skip to content

Commit 00b7e61

Browse files
leitaoNipaLocal
authored and
NipaLocal
committed
netconsole: Only register console drivers when targets are configured
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]> Signed-off-by: NipaLocal <nipa@local>
1 parent d3faab9 commit 00b7e61

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.
@@ -1691,8 +1702,8 @@ static int __init init_netconsole(void)
16911702
{
16921703
int err;
16931704
struct netconsole_target *nt, *tmp;
1705+
u32 console_type_needed = 0;
16941706
unsigned int count = 0;
1695-
bool extended = false;
16961707
unsigned long flags;
16971708
char *target_config;
16981709
char *input = config;
@@ -1708,9 +1719,10 @@ static int __init init_netconsole(void)
17081719
}
17091720
/* Dump existing printks when we register */
17101721
if (nt->extended) {
1711-
extended = true;
1722+
console_type_needed |= CONS_EXTENDED;
17121723
netconsole_ext.flags |= CON_PRINTBUFFER;
17131724
} else {
1725+
console_type_needed |= CONS_BASIC;
17141726
netconsole.flags |= CON_PRINTBUFFER;
17151727
}
17161728

@@ -1729,9 +1741,10 @@ static int __init init_netconsole(void)
17291741
if (err)
17301742
goto undonotifier;
17311743

1732-
if (extended)
1744+
if (console_type_needed & CONS_EXTENDED)
17331745
register_console(&netconsole_ext);
1734-
register_console(&netconsole);
1746+
if (console_type_needed & CONS_BASIC)
1747+
register_console(&netconsole);
17351748
pr_info("network logging started\n");
17361749

17371750
return err;
@@ -1761,7 +1774,8 @@ static void __exit cleanup_netconsole(void)
17611774

17621775
if (console_is_registered(&netconsole_ext))
17631776
unregister_console(&netconsole_ext);
1764-
unregister_console(&netconsole);
1777+
if (console_is_registered(&netconsole))
1778+
unregister_console(&netconsole);
17651779
dynamic_netconsole_exit();
17661780
unregister_netdevice_notifier(&netconsole_netdev_notifier);
17671781

0 commit comments

Comments
 (0)