Skip to content

Commit b0a9e2c

Browse files
leitaokuba-moo
authored andcommitted
netconsole: Create a allocation helper
De-duplicate the initialization and allocation code for struct netconsole_target. The same allocation and initialization code is duplicated in two different places in the netconsole subsystem, when the netconsole target is initialized by command line parameters (alloc_param_target()), and dynamically by sysfs (make_netconsole_target()). Create a helper function, and call it from the two different functions. Suggested-by: Eric Dumazet <edumazet@google.com> Signed-off-by: Breno Leitao <leitao@debian.org> Reviewed-by: Simon Horman <horms@kernel.org> Link: https://lore.kernel.org/r/20230811093158.1678322-2-leitao@debian.org Signed-off-by: Jakub Kicinski <kuba@kernel.org>
1 parent f3add6d commit b0a9e2c

1 file changed

Lines changed: 23 additions & 20 deletions

File tree

drivers/net/netconsole.c

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -167,26 +167,38 @@ static void netconsole_target_put(struct netconsole_target *nt)
167167

168168
#endif /* CONFIG_NETCONSOLE_DYNAMIC */
169169

170-
/* Allocate new target (from boot/module param) and setup netpoll for it */
171-
static struct netconsole_target *alloc_param_target(char *target_config)
170+
/* Allocate and initialize with defaults.
171+
* Note that these targets get their config_item fields zeroed-out.
172+
*/
173+
static struct netconsole_target *alloc_and_init(void)
172174
{
173-
int err = -ENOMEM;
174175
struct netconsole_target *nt;
175176

176-
/*
177-
* Allocate and initialize with defaults.
178-
* Note that these targets get their config_item fields zeroed-out.
179-
*/
180177
nt = kzalloc(sizeof(*nt), GFP_KERNEL);
181178
if (!nt)
182-
goto fail;
179+
return nt;
183180

184181
nt->np.name = "netconsole";
185182
strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
186183
nt->np.local_port = 6665;
187184
nt->np.remote_port = 6666;
188185
eth_broadcast_addr(nt->np.remote_mac);
189186

187+
return nt;
188+
}
189+
190+
/* Allocate new target (from boot/module param) and setup netpoll for it */
191+
static struct netconsole_target *alloc_param_target(char *target_config)
192+
{
193+
struct netconsole_target *nt;
194+
int err;
195+
196+
nt = alloc_and_init();
197+
if (!nt) {
198+
err = -ENOMEM;
199+
goto fail;
200+
}
201+
190202
if (*target_config == '+') {
191203
nt->extended = true;
192204
target_config++;
@@ -195,6 +207,7 @@ static struct netconsole_target *alloc_param_target(char *target_config)
195207
if (*target_config == 'r') {
196208
if (!nt->extended) {
197209
pr_err("Netconsole configuration error. Release feature requires extended log message");
210+
err = -EINVAL;
198211
goto fail;
199212
}
200213
nt->release = true;
@@ -664,23 +677,13 @@ static const struct config_item_type netconsole_target_type = {
664677
static struct config_item *make_netconsole_target(struct config_group *group,
665678
const char *name)
666679
{
667-
unsigned long flags;
668680
struct netconsole_target *nt;
681+
unsigned long flags;
669682

670-
/*
671-
* Allocate and initialize with defaults.
672-
* Target is disabled at creation (!enabled).
673-
*/
674-
nt = kzalloc(sizeof(*nt), GFP_KERNEL);
683+
nt = alloc_and_init();
675684
if (!nt)
676685
return ERR_PTR(-ENOMEM);
677686

678-
nt->np.name = "netconsole";
679-
strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
680-
nt->np.local_port = 6665;
681-
nt->np.remote_port = 6666;
682-
eth_broadcast_addr(nt->np.remote_mac);
683-
684687
/* Initialize the config_item member */
685688
config_item_init_type_name(&nt->item, name, &netconsole_target_type);
686689

0 commit comments

Comments
 (0)