[PATCH 2/3] netconsole: Attach cmdline target to dynamic target

From: Breno Leitao
Date: Mon Oct 02 2023 - 11:55:56 EST


Enable the attachment of a dynamic target to the target created during
boot time. The boot-time targets are named as "cmdline\d", where "\d" is
a number starting at 0.

If the user creates a dynamic target named "cmdline0", it will attach to
the first target created at boot time (as defined in the
`netconsole=...` command line argument). `cmdline1` will attach to the
second target and so forth.

If there is no netconsole target created at boot time, then, the target
name could be reused.

Relevant design discussion:
https://lore.kernel.org/all/ZRWRal5bW93px4km@xxxxxxxxx/

Suggested-by: Joel Becker <jlbec@xxxxxxxxxxxx>
Signed-off-by: Breno Leitao <leitao@xxxxxxxxxx>
---
drivers/net/netconsole.c | 24 ++++++++++++++++++++++++
1 file changed, 24 insertions(+)

diff --git a/drivers/net/netconsole.c b/drivers/net/netconsole.c
index b68456054a0c..6235f56dc652 100644
--- a/drivers/net/netconsole.c
+++ b/drivers/net/netconsole.c
@@ -685,6 +685,23 @@ static const struct config_item_type netconsole_target_type = {
.ct_owner = THIS_MODULE,
};

+static struct netconsole_target *find_cmdline_target(const char *name)
+{
+ struct netconsole_target *nt, *ret = NULL;
+ unsigned long flags;
+
+ spin_lock_irqsave(&target_list_lock, flags);
+ list_for_each_entry(nt, &target_list, list) {
+ if (!strcmp(nt->item.ci_name, name)) {
+ ret = nt;
+ break;
+ }
+ }
+ spin_unlock_irqrestore(&target_list_lock, flags);
+
+ return ret;
+}
+
/*
* Group operations and type for netconsole_subsys.
*/
@@ -695,6 +712,13 @@ static struct config_item *make_netconsole_target(struct config_group *group,
struct netconsole_target *nt;
unsigned long flags;

+ /* Checking if there is a target created populated at boot time */
+ if (!strncmp(name, DEFAULT_TARGET_NAME, strlen(DEFAULT_TARGET_NAME))) {
+ nt = find_cmdline_target(name);
+ if (nt)
+ return &nt->item;
+ }
+
nt = alloc_and_init();
if (!nt)
return ERR_PTR(-ENOMEM);
--
2.34.1