xref: /linux/drivers/net/netconsole.c (revision 24c776355f4097316a763005434ffff716aa21a8)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  linux/drivers/net/netconsole.c
4  *
5  *  Copyright (C) 2001  Ingo Molnar <mingo@redhat.com>
6  *
7  *  This file contains the implementation of an IRQ-safe, crash-safe
8  *  kernel console implementation that outputs kernel messages to the
9  *  network.
10  *
11  * Modification history:
12  *
13  * 2001-09-17    started by Ingo Molnar.
14  * 2003-08-11    2.6 port by Matt Mackall
15  *               simplified options
16  *               generic card hooks
17  *               works non-modular
18  * 2003-09-07    rewritten with netpoll api
19  */
20 
21 /****************************************************************
22  *
23  ****************************************************************/
24 
25 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
26 
27 #include <linux/mm.h>
28 #include <linux/init.h>
29 #include <linux/module.h>
30 #include <linux/slab.h>
31 #include <linux/console.h>
32 #include <linux/moduleparam.h>
33 #include <linux/kernel.h>
34 #include <linux/string.h>
35 #include <linux/netpoll.h>
36 #include <linux/inet.h>
37 #include <linux/configfs.h>
38 #include <linux/etherdevice.h>
39 #include <linux/hex.h>
40 #include <linux/u64_stats_sync.h>
41 #include <linux/utsname.h>
42 #include <linux/rtnetlink.h>
43 
44 MODULE_AUTHOR("Matt Mackall <mpm@selenic.com>");
45 MODULE_DESCRIPTION("Console driver for network interfaces");
46 MODULE_LICENSE("GPL");
47 
48 #define MAX_PARAM_LENGTH		256
49 #define MAX_EXTRADATA_ENTRY_LEN		256
50 #define MAX_EXTRADATA_VALUE_LEN	200
51 /* The number 3 comes from userdata entry format characters (' ', '=', '\n') */
52 #define MAX_EXTRADATA_NAME_LEN		(MAX_EXTRADATA_ENTRY_LEN - \
53 					MAX_EXTRADATA_VALUE_LEN - 3)
54 #define MAX_USERDATA_ITEMS		256
55 #define MAX_PRINT_CHUNK			1000
56 
57 static char config[MAX_PARAM_LENGTH];
58 module_param_string(netconsole, config, MAX_PARAM_LENGTH, 0);
59 MODULE_PARM_DESC(netconsole, " netconsole=[src-port]@[src-ip]/[dev],[tgt-port]@<tgt-ip>/[tgt-macaddr]");
60 
61 static bool oops_only;
62 module_param(oops_only, bool, 0600);
63 MODULE_PARM_DESC(oops_only, "Only log oops messages");
64 
65 #define NETCONSOLE_PARAM_TARGET_PREFIX "cmdline"
66 
67 #ifndef	MODULE
68 static int __init option_setup(char *opt)
69 {
70 	strscpy(config, opt, MAX_PARAM_LENGTH);
71 	return 1;
72 }
73 __setup("netconsole=", option_setup);
74 #endif	/* MODULE */
75 
76 /* Linked list of all configured targets */
77 static LIST_HEAD(target_list);
78 /* target_cleanup_list is used to track targets that need to be cleaned outside
79  * of target_list_lock. It should be cleaned in the same function it is
80  * populated.
81  */
82 static LIST_HEAD(target_cleanup_list);
83 
84 /* This needs to be a spinlock because write_msg() cannot sleep */
85 static DEFINE_SPINLOCK(target_list_lock);
86 /* This needs to be a mutex because netpoll_cleanup might sleep */
87 static DEFINE_MUTEX(target_cleanup_list_lock);
88 
89 /*
90  * Console driver for netconsoles.  Register only consoles that have
91  * an associated target of the same type.
92  */
93 static struct console netconsole_ext, netconsole;
94 
95 struct netconsole_target_stats  {
96 	u64_stats_t xmit_drop_count;
97 	u64_stats_t enomem_count;
98 	struct u64_stats_sync syncp;
99 };
100 
101 enum console_type {
102 	CONS_BASIC = BIT(0),
103 	CONS_EXTENDED = BIT(1),
104 };
105 
106 /* Features enabled in sysdata. Contrary to userdata, this data is populated by
107  * the kernel. The fields are designed as bitwise flags, allowing multiple
108  * features to be set in sysdata_fields.
109  */
110 enum sysdata_feature {
111 	/* Populate the CPU that sends the message */
112 	SYSDATA_CPU_NR = BIT(0),
113 	/* Populate the task name (as in current->comm) in sysdata */
114 	SYSDATA_TASKNAME = BIT(1),
115 	/* Kernel release/version as part of sysdata */
116 	SYSDATA_RELEASE = BIT(2),
117 	/* Include a per-target message ID as part of sysdata */
118 	SYSDATA_MSGID = BIT(3),
119 	/* Sentinel: highest bit position */
120 	MAX_SYSDATA_ITEMS = 4,
121 };
122 
123 /**
124  * struct netconsole_target - Represents a configured netconsole target.
125  * @list:	Links this target into the target_list.
126  * @group:	Links us into the configfs subsystem hierarchy.
127  * @userdata_group:	Links to the userdata configfs hierarchy
128  * @userdata:		Cached, formatted string of append
129  * @userdata_length:	String length of userdata.
130  * @sysdata:		Cached, formatted string of append
131  * @sysdata_fields:	Sysdata features enabled.
132  * @msgcounter:	Message sent counter.
133  * @stats:	Packet send stats for the target. Used for debugging.
134  * @enabled:	On / off knob to enable / disable target.
135  *		Visible from userspace (read-write).
136  *		We maintain a strict 1:1 correspondence between this and
137  *		whether the corresponding netpoll is active or inactive.
138  *		Also, other parameters of a target may be modified at
139  *		runtime only when it is disabled (enabled == 0).
140  * @extended:	Denotes whether console is extended or not.
141  * @release:	Denotes whether kernel release version should be prepended
142  *		to the message. Depends on extended console.
143  * @np:		The netpoll structure for this target.
144  *		Contains the other userspace visible parameters:
145  *		dev_name	(read-write)
146  *		local_port	(read-write)
147  *		remote_port	(read-write)
148  *		local_ip	(read-write)
149  *		remote_ip	(read-write)
150  *		local_mac	(read-only)
151  *		remote_mac	(read-write)
152  * @buf:	The buffer used to send the full msg to the network stack
153  */
154 struct netconsole_target {
155 	struct list_head	list;
156 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
157 	struct config_group	group;
158 	struct config_group	userdata_group;
159 	char			*userdata;
160 	size_t			userdata_length;
161 	char			sysdata[MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS];
162 
163 	/* bit-wise with sysdata_feature bits */
164 	u32			sysdata_fields;
165 	/* protected by target_list_lock */
166 	u32			msgcounter;
167 #endif
168 	struct netconsole_target_stats stats;
169 	bool			enabled;
170 	bool			extended;
171 	bool			release;
172 	struct netpoll		np;
173 	/* protected by target_list_lock */
174 	char			buf[MAX_PRINT_CHUNK];
175 };
176 
177 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
178 
179 static struct configfs_subsystem netconsole_subsys;
180 static DEFINE_MUTEX(dynamic_netconsole_mutex);
181 
182 static int __init dynamic_netconsole_init(void)
183 {
184 	config_group_init(&netconsole_subsys.su_group);
185 	mutex_init(&netconsole_subsys.su_mutex);
186 	return configfs_register_subsystem(&netconsole_subsys);
187 }
188 
189 static void __exit dynamic_netconsole_exit(void)
190 {
191 	configfs_unregister_subsystem(&netconsole_subsys);
192 }
193 
194 /*
195  * Targets that were created by parsing the boot/module option string
196  * do not exist in the configfs hierarchy (and have NULL names) and will
197  * never go away, so make these a no-op for them.
198  */
199 static void netconsole_target_get(struct netconsole_target *nt)
200 {
201 	if (config_item_name(&nt->group.cg_item))
202 		config_group_get(&nt->group);
203 }
204 
205 static void netconsole_target_put(struct netconsole_target *nt)
206 {
207 	if (config_item_name(&nt->group.cg_item))
208 		config_group_put(&nt->group);
209 }
210 
211 #else	/* !CONFIG_NETCONSOLE_DYNAMIC */
212 
213 static int __init dynamic_netconsole_init(void)
214 {
215 	return 0;
216 }
217 
218 static void __exit dynamic_netconsole_exit(void)
219 {
220 }
221 
222 /*
223  * No danger of targets going away from under us when dynamic
224  * reconfigurability is off.
225  */
226 static void netconsole_target_get(struct netconsole_target *nt)
227 {
228 }
229 
230 static void netconsole_target_put(struct netconsole_target *nt)
231 {
232 }
233 
234 static void populate_configfs_item(struct netconsole_target *nt,
235 				   int cmdline_count)
236 {
237 }
238 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
239 
240 /* Allocate and initialize with defaults.
241  * Note that these targets get their config_item fields zeroed-out.
242  */
243 static struct netconsole_target *alloc_and_init(void)
244 {
245 	struct netconsole_target *nt;
246 
247 	nt = kzalloc(sizeof(*nt), GFP_KERNEL);
248 	if (!nt)
249 		return nt;
250 
251 	if (IS_ENABLED(CONFIG_NETCONSOLE_EXTENDED_LOG))
252 		nt->extended = true;
253 	if (IS_ENABLED(CONFIG_NETCONSOLE_PREPEND_RELEASE))
254 		nt->release = true;
255 
256 	nt->np.name = "netconsole";
257 	strscpy(nt->np.dev_name, "eth0", IFNAMSIZ);
258 	nt->np.local_port = 6665;
259 	nt->np.remote_port = 6666;
260 	eth_broadcast_addr(nt->np.remote_mac);
261 
262 	return nt;
263 }
264 
265 /* Clean up every target in the cleanup_list and move the clean targets back to
266  * the main target_list.
267  */
268 static void netconsole_process_cleanups_core(void)
269 {
270 	struct netconsole_target *nt, *tmp;
271 	unsigned long flags;
272 
273 	/* The cleanup needs RTNL locked */
274 	ASSERT_RTNL();
275 
276 	mutex_lock(&target_cleanup_list_lock);
277 	list_for_each_entry_safe(nt, tmp, &target_cleanup_list, list) {
278 		/* all entries in the cleanup_list needs to be disabled */
279 		WARN_ON_ONCE(nt->enabled);
280 		do_netpoll_cleanup(&nt->np);
281 		/* moved the cleaned target to target_list. Need to hold both
282 		 * locks
283 		 */
284 		spin_lock_irqsave(&target_list_lock, flags);
285 		list_move(&nt->list, &target_list);
286 		spin_unlock_irqrestore(&target_list_lock, flags);
287 	}
288 	WARN_ON_ONCE(!list_empty(&target_cleanup_list));
289 	mutex_unlock(&target_cleanup_list_lock);
290 }
291 
292 static void netconsole_print_banner(struct netpoll *np)
293 {
294 	np_info(np, "local port %d\n", np->local_port);
295 	if (np->ipv6)
296 		np_info(np, "local IPv6 address %pI6c\n", &np->local_ip.in6);
297 	else
298 		np_info(np, "local IPv4 address %pI4\n", &np->local_ip.ip);
299 	np_info(np, "interface name '%s'\n", np->dev_name);
300 	np_info(np, "local ethernet address '%pM'\n", np->dev_mac);
301 	np_info(np, "remote port %d\n", np->remote_port);
302 	if (np->ipv6)
303 		np_info(np, "remote IPv6 address %pI6c\n", &np->remote_ip.in6);
304 	else
305 		np_info(np, "remote IPv4 address %pI4\n", &np->remote_ip.ip);
306 	np_info(np, "remote ethernet address %pM\n", np->remote_mac);
307 }
308 
309 /* Parse the string and populate the `inet_addr` union. Return 0 if IPv4 is
310  * populated, 1 if IPv6 is populated, and -1 upon failure.
311  */
312 static int netpoll_parse_ip_addr(const char *str, union inet_addr *addr)
313 {
314 	const char *end = NULL;
315 	int len;
316 
317 	len = strlen(str);
318 	if (!len)
319 		return -1;
320 
321 	if (str[len - 1] == '\n')
322 		len -= 1;
323 
324 	if (in4_pton(str, len, (void *)addr, -1, &end) > 0 &&
325 	    (!end || *end == 0 || *end == '\n'))
326 		return 0;
327 
328 	if (IS_ENABLED(CONFIG_IPV6) &&
329 	    in6_pton(str, len, (void *)addr, -1, &end) > 0 &&
330 	    (!end || *end == 0 || *end == '\n'))
331 		return 1;
332 
333 	return -1;
334 }
335 
336 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
337 
338 /*
339  * Our subsystem hierarchy is:
340  *
341  * /sys/kernel/config/netconsole/
342  *				|
343  *				<target>/
344  *				|	enabled
345  *				|	release
346  *				|	dev_name
347  *				|	local_port
348  *				|	remote_port
349  *				|	local_ip
350  *				|	remote_ip
351  *				|	local_mac
352  *				|	remote_mac
353  *				|	transmit_errors
354  *				|	userdata/
355  *				|		<key>/
356  *				|			value
357  *				|		...
358  *				|
359  *				<target>/...
360  */
361 
362 static struct netconsole_target *to_target(struct config_item *item)
363 {
364 	struct config_group *cfg_group;
365 
366 	cfg_group = to_config_group(item);
367 	if (!cfg_group)
368 		return NULL;
369 	return container_of(to_config_group(item),
370 			    struct netconsole_target, group);
371 }
372 
373 /* Do the list cleanup with the rtnl lock hold.  rtnl lock is necessary because
374  * netdev might be cleaned-up by calling __netpoll_cleanup(),
375  */
376 static void netconsole_process_cleanups(void)
377 {
378 	/* rtnl lock is called here, because it has precedence over
379 	 * target_cleanup_list_lock mutex and target_cleanup_list
380 	 */
381 	rtnl_lock();
382 	netconsole_process_cleanups_core();
383 	rtnl_unlock();
384 }
385 
386 /* Get rid of possible trailing newline, returning the new length */
387 static void trim_newline(char *s, size_t maxlen)
388 {
389 	size_t len;
390 
391 	len = strnlen(s, maxlen);
392 	if (s[len - 1] == '\n')
393 		s[len - 1] = '\0';
394 }
395 
396 /*
397  * Attribute operations for netconsole_target.
398  */
399 
400 static ssize_t enabled_show(struct config_item *item, char *buf)
401 {
402 	return sysfs_emit(buf, "%d\n", to_target(item)->enabled);
403 }
404 
405 static ssize_t extended_show(struct config_item *item, char *buf)
406 {
407 	return sysfs_emit(buf, "%d\n", to_target(item)->extended);
408 }
409 
410 static ssize_t release_show(struct config_item *item, char *buf)
411 {
412 	return sysfs_emit(buf, "%d\n", to_target(item)->release);
413 }
414 
415 static ssize_t dev_name_show(struct config_item *item, char *buf)
416 {
417 	return sysfs_emit(buf, "%s\n", to_target(item)->np.dev_name);
418 }
419 
420 static ssize_t local_port_show(struct config_item *item, char *buf)
421 {
422 	return sysfs_emit(buf, "%d\n", to_target(item)->np.local_port);
423 }
424 
425 static ssize_t remote_port_show(struct config_item *item, char *buf)
426 {
427 	return sysfs_emit(buf, "%d\n", to_target(item)->np.remote_port);
428 }
429 
430 static ssize_t local_ip_show(struct config_item *item, char *buf)
431 {
432 	struct netconsole_target *nt = to_target(item);
433 
434 	if (nt->np.ipv6)
435 		return sysfs_emit(buf, "%pI6c\n", &nt->np.local_ip.in6);
436 	else
437 		return sysfs_emit(buf, "%pI4\n", &nt->np.local_ip);
438 }
439 
440 static ssize_t remote_ip_show(struct config_item *item, char *buf)
441 {
442 	struct netconsole_target *nt = to_target(item);
443 
444 	if (nt->np.ipv6)
445 		return sysfs_emit(buf, "%pI6c\n", &nt->np.remote_ip.in6);
446 	else
447 		return sysfs_emit(buf, "%pI4\n", &nt->np.remote_ip);
448 }
449 
450 static ssize_t local_mac_show(struct config_item *item, char *buf)
451 {
452 	struct net_device *dev = to_target(item)->np.dev;
453 	static const u8 bcast[ETH_ALEN] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
454 
455 	return sysfs_emit(buf, "%pM\n", dev ? dev->dev_addr : bcast);
456 }
457 
458 static ssize_t remote_mac_show(struct config_item *item, char *buf)
459 {
460 	return sysfs_emit(buf, "%pM\n", to_target(item)->np.remote_mac);
461 }
462 
463 static ssize_t transmit_errors_show(struct config_item *item, char *buf)
464 {
465 	struct netconsole_target *nt = to_target(item);
466 	u64 xmit_drop_count, enomem_count;
467 	unsigned int start;
468 
469 	do {
470 		start = u64_stats_fetch_begin(&nt->stats.syncp);
471 		xmit_drop_count = u64_stats_read(&nt->stats.xmit_drop_count);
472 		enomem_count = u64_stats_read(&nt->stats.enomem_count);
473 	} while (u64_stats_fetch_retry(&nt->stats.syncp, start));
474 
475 	return sysfs_emit(buf, "%llu\n", xmit_drop_count + enomem_count);
476 }
477 
478 /* configfs helper to display if cpu_nr sysdata feature is enabled */
479 static ssize_t sysdata_cpu_nr_enabled_show(struct config_item *item, char *buf)
480 {
481 	struct netconsole_target *nt = to_target(item->ci_parent);
482 	bool cpu_nr_enabled;
483 
484 	mutex_lock(&dynamic_netconsole_mutex);
485 	cpu_nr_enabled = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
486 	mutex_unlock(&dynamic_netconsole_mutex);
487 
488 	return sysfs_emit(buf, "%d\n", cpu_nr_enabled);
489 }
490 
491 /* configfs helper to display if taskname sysdata feature is enabled */
492 static ssize_t sysdata_taskname_enabled_show(struct config_item *item,
493 					     char *buf)
494 {
495 	struct netconsole_target *nt = to_target(item->ci_parent);
496 	bool taskname_enabled;
497 
498 	mutex_lock(&dynamic_netconsole_mutex);
499 	taskname_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
500 	mutex_unlock(&dynamic_netconsole_mutex);
501 
502 	return sysfs_emit(buf, "%d\n", taskname_enabled);
503 }
504 
505 static ssize_t sysdata_release_enabled_show(struct config_item *item,
506 					    char *buf)
507 {
508 	struct netconsole_target *nt = to_target(item->ci_parent);
509 	bool release_enabled;
510 
511 	mutex_lock(&dynamic_netconsole_mutex);
512 	release_enabled = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
513 	mutex_unlock(&dynamic_netconsole_mutex);
514 
515 	return sysfs_emit(buf, "%d\n", release_enabled);
516 }
517 
518 /* Iterate in the list of target, and make sure we don't have any console
519  * register without targets of the same type
520  */
521 static void unregister_netcons_consoles(void)
522 {
523 	struct netconsole_target *nt;
524 	u32 console_type_needed = 0;
525 	unsigned long flags;
526 
527 	spin_lock_irqsave(&target_list_lock, flags);
528 	list_for_each_entry(nt, &target_list, list) {
529 		if (nt->extended)
530 			console_type_needed |= CONS_EXTENDED;
531 		else
532 			console_type_needed |= CONS_BASIC;
533 	}
534 	spin_unlock_irqrestore(&target_list_lock, flags);
535 
536 	if (!(console_type_needed & CONS_EXTENDED) &&
537 	    console_is_registered(&netconsole_ext))
538 		unregister_console(&netconsole_ext);
539 
540 	if (!(console_type_needed & CONS_BASIC) &&
541 	    console_is_registered(&netconsole))
542 		unregister_console(&netconsole);
543 }
544 
545 static ssize_t sysdata_msgid_enabled_show(struct config_item *item,
546 					  char *buf)
547 {
548 	struct netconsole_target *nt = to_target(item->ci_parent);
549 	bool msgid_enabled;
550 
551 	mutex_lock(&dynamic_netconsole_mutex);
552 	msgid_enabled = !!(nt->sysdata_fields & SYSDATA_MSGID);
553 	mutex_unlock(&dynamic_netconsole_mutex);
554 
555 	return sysfs_emit(buf, "%d\n", msgid_enabled);
556 }
557 
558 /*
559  * This one is special -- targets created through the configfs interface
560  * are not enabled (and the corresponding netpoll activated) by default.
561  * The user is expected to set the desired parameters first (which
562  * would enable him to dynamically add new netpoll targets for new
563  * network interfaces as and when they come up).
564  */
565 static ssize_t enabled_store(struct config_item *item,
566 		const char *buf, size_t count)
567 {
568 	struct netconsole_target *nt = to_target(item);
569 	unsigned long flags;
570 	bool enabled;
571 	ssize_t ret;
572 
573 	mutex_lock(&dynamic_netconsole_mutex);
574 	ret = kstrtobool(buf, &enabled);
575 	if (ret)
576 		goto out_unlock;
577 
578 	ret = -EINVAL;
579 	if (enabled == nt->enabled) {
580 		pr_info("network logging has already %s\n",
581 			nt->enabled ? "started" : "stopped");
582 		goto out_unlock;
583 	}
584 
585 	if (enabled) {	/* true */
586 		if (nt->release && !nt->extended) {
587 			pr_err("Not enabling netconsole. Release feature requires extended log message");
588 			goto out_unlock;
589 		}
590 
591 		if (nt->extended && !console_is_registered(&netconsole_ext)) {
592 			netconsole_ext.flags |= CON_ENABLED;
593 			register_console(&netconsole_ext);
594 		}
595 
596 		/* User might be enabling the basic format target for the very
597 		 * first time, make sure the console is registered.
598 		 */
599 		if (!nt->extended && !console_is_registered(&netconsole)) {
600 			netconsole.flags |= CON_ENABLED;
601 			register_console(&netconsole);
602 		}
603 
604 		/*
605 		 * Skip netconsole_parser_cmdline() -- all the attributes are
606 		 * already configured via configfs. Just print them out.
607 		 */
608 		netconsole_print_banner(&nt->np);
609 
610 		ret = netpoll_setup(&nt->np);
611 		if (ret)
612 			goto out_unlock;
613 
614 		nt->enabled = true;
615 		pr_info("network logging started\n");
616 	} else {	/* false */
617 		/* We need to disable the netconsole before cleaning it up
618 		 * otherwise we might end up in write_msg() with
619 		 * nt->np.dev == NULL and nt->enabled == true
620 		 */
621 		mutex_lock(&target_cleanup_list_lock);
622 		spin_lock_irqsave(&target_list_lock, flags);
623 		nt->enabled = false;
624 		/* Remove the target from the list, while holding
625 		 * target_list_lock
626 		 */
627 		list_move(&nt->list, &target_cleanup_list);
628 		spin_unlock_irqrestore(&target_list_lock, flags);
629 		mutex_unlock(&target_cleanup_list_lock);
630 		/* Unregister consoles, whose the last target of that type got
631 		 * disabled.
632 		 */
633 		unregister_netcons_consoles();
634 	}
635 
636 	ret = strnlen(buf, count);
637 	/* Deferred cleanup */
638 	netconsole_process_cleanups();
639 out_unlock:
640 	mutex_unlock(&dynamic_netconsole_mutex);
641 	return ret;
642 }
643 
644 static ssize_t release_store(struct config_item *item, const char *buf,
645 			     size_t count)
646 {
647 	struct netconsole_target *nt = to_target(item);
648 	bool release;
649 	ssize_t ret;
650 
651 	mutex_lock(&dynamic_netconsole_mutex);
652 	if (nt->enabled) {
653 		pr_err("target (%s) is enabled, disable to update parameters\n",
654 		       config_item_name(&nt->group.cg_item));
655 		ret = -EINVAL;
656 		goto out_unlock;
657 	}
658 
659 	ret = kstrtobool(buf, &release);
660 	if (ret)
661 		goto out_unlock;
662 
663 	nt->release = release;
664 
665 	ret = strnlen(buf, count);
666 out_unlock:
667 	mutex_unlock(&dynamic_netconsole_mutex);
668 	return ret;
669 }
670 
671 static ssize_t extended_store(struct config_item *item, const char *buf,
672 		size_t count)
673 {
674 	struct netconsole_target *nt = to_target(item);
675 	bool extended;
676 	ssize_t ret;
677 
678 	mutex_lock(&dynamic_netconsole_mutex);
679 	if (nt->enabled) {
680 		pr_err("target (%s) is enabled, disable to update parameters\n",
681 		       config_item_name(&nt->group.cg_item));
682 		ret = -EINVAL;
683 		goto out_unlock;
684 	}
685 
686 	ret = kstrtobool(buf, &extended);
687 	if (ret)
688 		goto out_unlock;
689 
690 	nt->extended = extended;
691 	ret = strnlen(buf, count);
692 out_unlock:
693 	mutex_unlock(&dynamic_netconsole_mutex);
694 	return ret;
695 }
696 
697 static ssize_t dev_name_store(struct config_item *item, const char *buf,
698 		size_t count)
699 {
700 	struct netconsole_target *nt = to_target(item);
701 
702 	mutex_lock(&dynamic_netconsole_mutex);
703 	if (nt->enabled) {
704 		pr_err("target (%s) is enabled, disable to update parameters\n",
705 		       config_item_name(&nt->group.cg_item));
706 		mutex_unlock(&dynamic_netconsole_mutex);
707 		return -EINVAL;
708 	}
709 
710 	strscpy(nt->np.dev_name, buf, IFNAMSIZ);
711 	trim_newline(nt->np.dev_name, IFNAMSIZ);
712 
713 	mutex_unlock(&dynamic_netconsole_mutex);
714 	return strnlen(buf, count);
715 }
716 
717 static ssize_t local_port_store(struct config_item *item, const char *buf,
718 		size_t count)
719 {
720 	struct netconsole_target *nt = to_target(item);
721 	ssize_t ret = -EINVAL;
722 
723 	mutex_lock(&dynamic_netconsole_mutex);
724 	if (nt->enabled) {
725 		pr_err("target (%s) is enabled, disable to update parameters\n",
726 		       config_item_name(&nt->group.cg_item));
727 		goto out_unlock;
728 	}
729 
730 	ret = kstrtou16(buf, 10, &nt->np.local_port);
731 	if (ret < 0)
732 		goto out_unlock;
733 	ret = strnlen(buf, count);
734 out_unlock:
735 	mutex_unlock(&dynamic_netconsole_mutex);
736 	return ret;
737 }
738 
739 static ssize_t remote_port_store(struct config_item *item,
740 		const char *buf, size_t count)
741 {
742 	struct netconsole_target *nt = to_target(item);
743 	ssize_t ret = -EINVAL;
744 
745 	mutex_lock(&dynamic_netconsole_mutex);
746 	if (nt->enabled) {
747 		pr_err("target (%s) is enabled, disable to update parameters\n",
748 		       config_item_name(&nt->group.cg_item));
749 		goto out_unlock;
750 	}
751 
752 	ret = kstrtou16(buf, 10, &nt->np.remote_port);
753 	if (ret < 0)
754 		goto out_unlock;
755 	ret = strnlen(buf, count);
756 out_unlock:
757 	mutex_unlock(&dynamic_netconsole_mutex);
758 	return ret;
759 }
760 
761 static ssize_t local_ip_store(struct config_item *item, const char *buf,
762 		size_t count)
763 {
764 	struct netconsole_target *nt = to_target(item);
765 	ssize_t ret = -EINVAL;
766 	int ipv6;
767 
768 	mutex_lock(&dynamic_netconsole_mutex);
769 	if (nt->enabled) {
770 		pr_err("target (%s) is enabled, disable to update parameters\n",
771 		       config_item_name(&nt->group.cg_item));
772 		goto out_unlock;
773 	}
774 
775 	ipv6 = netpoll_parse_ip_addr(buf, &nt->np.local_ip);
776 	if (ipv6 == -1)
777 		goto out_unlock;
778 	nt->np.ipv6 = !!ipv6;
779 
780 	ret = strnlen(buf, count);
781 out_unlock:
782 	mutex_unlock(&dynamic_netconsole_mutex);
783 	return ret;
784 }
785 
786 static ssize_t remote_ip_store(struct config_item *item, const char *buf,
787 	       size_t count)
788 {
789 	struct netconsole_target *nt = to_target(item);
790 	ssize_t ret = -EINVAL;
791 	int ipv6;
792 
793 	mutex_lock(&dynamic_netconsole_mutex);
794 	if (nt->enabled) {
795 		pr_err("target (%s) is enabled, disable to update parameters\n",
796 		       config_item_name(&nt->group.cg_item));
797 		goto out_unlock;
798 	}
799 
800 	ipv6 = netpoll_parse_ip_addr(buf, &nt->np.remote_ip);
801 	if (ipv6 == -1)
802 		goto out_unlock;
803 	nt->np.ipv6 = !!ipv6;
804 
805 	ret = strnlen(buf, count);
806 out_unlock:
807 	mutex_unlock(&dynamic_netconsole_mutex);
808 	return ret;
809 }
810 
811 /* Count number of entries we have in userdata.
812  * This is important because userdata only supports MAX_USERDATA_ITEMS
813  * entries. Before enabling any new userdata feature, number of entries needs
814  * to checked for available space.
815  */
816 static size_t count_userdata_entries(struct netconsole_target *nt)
817 {
818 	return list_count_nodes(&nt->userdata_group.cg_children);
819 }
820 
821 static ssize_t remote_mac_store(struct config_item *item, const char *buf,
822 		size_t count)
823 {
824 	struct netconsole_target *nt = to_target(item);
825 	u8 remote_mac[ETH_ALEN];
826 	ssize_t ret = -EINVAL;
827 
828 	mutex_lock(&dynamic_netconsole_mutex);
829 	if (nt->enabled) {
830 		pr_err("target (%s) is enabled, disable to update parameters\n",
831 		       config_item_name(&nt->group.cg_item));
832 		goto out_unlock;
833 	}
834 
835 	if (!mac_pton(buf, remote_mac))
836 		goto out_unlock;
837 	if (buf[MAC_ADDR_STR_LEN] && buf[MAC_ADDR_STR_LEN] != '\n')
838 		goto out_unlock;
839 	memcpy(nt->np.remote_mac, remote_mac, ETH_ALEN);
840 
841 	ret = strnlen(buf, count);
842 out_unlock:
843 	mutex_unlock(&dynamic_netconsole_mutex);
844 	return ret;
845 }
846 
847 struct userdatum {
848 	struct config_item item;
849 	char value[MAX_EXTRADATA_VALUE_LEN];
850 };
851 
852 static struct userdatum *to_userdatum(struct config_item *item)
853 {
854 	return container_of(item, struct userdatum, item);
855 }
856 
857 struct userdata {
858 	struct config_group group;
859 };
860 
861 static struct userdata *to_userdata(struct config_item *item)
862 {
863 	return container_of(to_config_group(item), struct userdata, group);
864 }
865 
866 static struct netconsole_target *userdata_to_target(struct userdata *ud)
867 {
868 	struct config_group *netconsole_group;
869 
870 	netconsole_group = to_config_group(ud->group.cg_item.ci_parent);
871 	return to_target(&netconsole_group->cg_item);
872 }
873 
874 static ssize_t userdatum_value_show(struct config_item *item, char *buf)
875 {
876 	return sysfs_emit(buf, "%s\n", &(to_userdatum(item)->value[0]));
877 }
878 
879 /* Navigate configfs and calculate the lentgh of the formatted string
880  * representing userdata.
881  * Must be called holding netconsole_subsys.su_mutex
882  */
883 static int calc_userdata_len(struct netconsole_target *nt)
884 {
885 	struct userdatum *udm_item;
886 	struct config_item *item;
887 	struct list_head *entry;
888 	int len = 0;
889 
890 	list_for_each(entry, &nt->userdata_group.cg_children) {
891 		item = container_of(entry, struct config_item, ci_entry);
892 		udm_item = to_userdatum(item);
893 		/* Skip userdata with no value set */
894 		if (udm_item->value[0]) {
895 			len += snprintf(NULL, 0, " %s=%s\n", item->ci_name,
896 					udm_item->value);
897 		}
898 	}
899 	return len;
900 }
901 
902 static int update_userdata(struct netconsole_target *nt)
903 {
904 	struct userdatum *udm_item;
905 	struct config_item *item;
906 	struct list_head *entry;
907 	char *old_buf = NULL;
908 	char *new_buf = NULL;
909 	unsigned long flags;
910 	int offset = 0;
911 	int len;
912 
913 	/* Calculate required buffer size */
914 	len = calc_userdata_len(nt);
915 
916 	if (WARN_ON_ONCE(len > MAX_EXTRADATA_ENTRY_LEN * MAX_USERDATA_ITEMS))
917 		return -ENOSPC;
918 
919 	/* Allocate new buffer */
920 	if (len) {
921 		new_buf = kmalloc(len + 1, GFP_KERNEL);
922 		if (!new_buf)
923 			return -ENOMEM;
924 	}
925 
926 	/* Write userdata to new buffer */
927 	list_for_each(entry, &nt->userdata_group.cg_children) {
928 		item = container_of(entry, struct config_item, ci_entry);
929 		udm_item = to_userdatum(item);
930 		/* Skip userdata with no value set */
931 		if (udm_item->value[0]) {
932 			offset += scnprintf(&new_buf[offset], len + 1 - offset,
933 					    " %s=%s\n", item->ci_name,
934 					    udm_item->value);
935 		}
936 	}
937 
938 	WARN_ON_ONCE(offset != len);
939 
940 	/* Switch to new buffer and free old buffer */
941 	spin_lock_irqsave(&target_list_lock, flags);
942 	old_buf = nt->userdata;
943 	nt->userdata = new_buf;
944 	nt->userdata_length = offset;
945 	spin_unlock_irqrestore(&target_list_lock, flags);
946 
947 	kfree(old_buf);
948 
949 	return 0;
950 }
951 
952 static ssize_t userdatum_value_store(struct config_item *item, const char *buf,
953 				     size_t count)
954 {
955 	struct userdatum *udm = to_userdatum(item);
956 	struct netconsole_target *nt;
957 	struct userdata *ud;
958 	ssize_t ret;
959 
960 	if (count > MAX_EXTRADATA_VALUE_LEN)
961 		return -EMSGSIZE;
962 
963 	mutex_lock(&netconsole_subsys.su_mutex);
964 	mutex_lock(&dynamic_netconsole_mutex);
965 
966 	ret = strscpy(udm->value, buf, sizeof(udm->value));
967 	if (ret < 0)
968 		goto out_unlock;
969 	trim_newline(udm->value, sizeof(udm->value));
970 
971 	ud = to_userdata(item->ci_parent);
972 	nt = userdata_to_target(ud);
973 	ret = update_userdata(nt);
974 	if (ret < 0)
975 		goto out_unlock;
976 	ret = count;
977 out_unlock:
978 	mutex_unlock(&dynamic_netconsole_mutex);
979 	mutex_unlock(&netconsole_subsys.su_mutex);
980 	return ret;
981 }
982 
983 /* disable_sysdata_feature - Disable sysdata feature and clean sysdata
984  * @nt: target that is disabling the feature
985  * @feature: feature being disabled
986  */
987 static void disable_sysdata_feature(struct netconsole_target *nt,
988 				    enum sysdata_feature feature)
989 {
990 	nt->sysdata_fields &= ~feature;
991 	nt->sysdata[0] = 0;
992 }
993 
994 static ssize_t sysdata_msgid_enabled_store(struct config_item *item,
995 					   const char *buf, size_t count)
996 {
997 	struct netconsole_target *nt = to_target(item->ci_parent);
998 	bool msgid_enabled, curr;
999 	ssize_t ret;
1000 
1001 	ret = kstrtobool(buf, &msgid_enabled);
1002 	if (ret)
1003 		return ret;
1004 
1005 	mutex_lock(&netconsole_subsys.su_mutex);
1006 	mutex_lock(&dynamic_netconsole_mutex);
1007 	curr = !!(nt->sysdata_fields & SYSDATA_MSGID);
1008 	if (msgid_enabled == curr)
1009 		goto unlock_ok;
1010 
1011 	if (msgid_enabled)
1012 		nt->sysdata_fields |= SYSDATA_MSGID;
1013 	else
1014 		disable_sysdata_feature(nt, SYSDATA_MSGID);
1015 
1016 unlock_ok:
1017 	ret = strnlen(buf, count);
1018 	mutex_unlock(&dynamic_netconsole_mutex);
1019 	mutex_unlock(&netconsole_subsys.su_mutex);
1020 	return ret;
1021 }
1022 
1023 static ssize_t sysdata_release_enabled_store(struct config_item *item,
1024 					     const char *buf, size_t count)
1025 {
1026 	struct netconsole_target *nt = to_target(item->ci_parent);
1027 	bool release_enabled, curr;
1028 	ssize_t ret;
1029 
1030 	ret = kstrtobool(buf, &release_enabled);
1031 	if (ret)
1032 		return ret;
1033 
1034 	mutex_lock(&netconsole_subsys.su_mutex);
1035 	mutex_lock(&dynamic_netconsole_mutex);
1036 	curr = !!(nt->sysdata_fields & SYSDATA_RELEASE);
1037 	if (release_enabled == curr)
1038 		goto unlock_ok;
1039 
1040 	if (release_enabled)
1041 		nt->sysdata_fields |= SYSDATA_RELEASE;
1042 	else
1043 		disable_sysdata_feature(nt, SYSDATA_RELEASE);
1044 
1045 unlock_ok:
1046 	ret = strnlen(buf, count);
1047 	mutex_unlock(&dynamic_netconsole_mutex);
1048 	mutex_unlock(&netconsole_subsys.su_mutex);
1049 	return ret;
1050 }
1051 
1052 static ssize_t sysdata_taskname_enabled_store(struct config_item *item,
1053 					      const char *buf, size_t count)
1054 {
1055 	struct netconsole_target *nt = to_target(item->ci_parent);
1056 	bool taskname_enabled, curr;
1057 	ssize_t ret;
1058 
1059 	ret = kstrtobool(buf, &taskname_enabled);
1060 	if (ret)
1061 		return ret;
1062 
1063 	mutex_lock(&netconsole_subsys.su_mutex);
1064 	mutex_lock(&dynamic_netconsole_mutex);
1065 	curr = !!(nt->sysdata_fields & SYSDATA_TASKNAME);
1066 	if (taskname_enabled == curr)
1067 		goto unlock_ok;
1068 
1069 	if (taskname_enabled)
1070 		nt->sysdata_fields |= SYSDATA_TASKNAME;
1071 	else
1072 		disable_sysdata_feature(nt, SYSDATA_TASKNAME);
1073 
1074 unlock_ok:
1075 	ret = strnlen(buf, count);
1076 	mutex_unlock(&dynamic_netconsole_mutex);
1077 	mutex_unlock(&netconsole_subsys.su_mutex);
1078 	return ret;
1079 }
1080 
1081 /* configfs helper to sysdata cpu_nr feature */
1082 static ssize_t sysdata_cpu_nr_enabled_store(struct config_item *item,
1083 					    const char *buf, size_t count)
1084 {
1085 	struct netconsole_target *nt = to_target(item->ci_parent);
1086 	bool cpu_nr_enabled, curr;
1087 	ssize_t ret;
1088 
1089 	ret = kstrtobool(buf, &cpu_nr_enabled);
1090 	if (ret)
1091 		return ret;
1092 
1093 	mutex_lock(&netconsole_subsys.su_mutex);
1094 	mutex_lock(&dynamic_netconsole_mutex);
1095 	curr = !!(nt->sysdata_fields & SYSDATA_CPU_NR);
1096 	if (cpu_nr_enabled == curr)
1097 		/* no change requested */
1098 		goto unlock_ok;
1099 
1100 	if (cpu_nr_enabled)
1101 		nt->sysdata_fields |= SYSDATA_CPU_NR;
1102 	else
1103 		/* This is special because sysdata might have remaining data
1104 		 * from previous sysdata, and it needs to be cleaned.
1105 		 */
1106 		disable_sysdata_feature(nt, SYSDATA_CPU_NR);
1107 
1108 unlock_ok:
1109 	ret = strnlen(buf, count);
1110 	mutex_unlock(&dynamic_netconsole_mutex);
1111 	mutex_unlock(&netconsole_subsys.su_mutex);
1112 	return ret;
1113 }
1114 
1115 CONFIGFS_ATTR(userdatum_, value);
1116 CONFIGFS_ATTR(sysdata_, cpu_nr_enabled);
1117 CONFIGFS_ATTR(sysdata_, taskname_enabled);
1118 CONFIGFS_ATTR(sysdata_, release_enabled);
1119 CONFIGFS_ATTR(sysdata_, msgid_enabled);
1120 
1121 static struct configfs_attribute *userdatum_attrs[] = {
1122 	&userdatum_attr_value,
1123 	NULL,
1124 };
1125 
1126 static void userdatum_release(struct config_item *item)
1127 {
1128 	kfree(to_userdatum(item));
1129 }
1130 
1131 static struct configfs_item_operations userdatum_ops = {
1132 	.release = userdatum_release,
1133 };
1134 
1135 static const struct config_item_type userdatum_type = {
1136 	.ct_item_ops	= &userdatum_ops,
1137 	.ct_attrs	= userdatum_attrs,
1138 	.ct_owner	= THIS_MODULE,
1139 };
1140 
1141 static struct config_item *userdatum_make_item(struct config_group *group,
1142 					       const char *name)
1143 {
1144 	struct netconsole_target *nt;
1145 	struct userdatum *udm;
1146 	struct userdata *ud;
1147 
1148 	if (strlen(name) > MAX_EXTRADATA_NAME_LEN)
1149 		return ERR_PTR(-ENAMETOOLONG);
1150 
1151 	ud = to_userdata(&group->cg_item);
1152 	nt = userdata_to_target(ud);
1153 	if (count_userdata_entries(nt) >= MAX_USERDATA_ITEMS)
1154 		return ERR_PTR(-ENOSPC);
1155 
1156 	udm = kzalloc(sizeof(*udm), GFP_KERNEL);
1157 	if (!udm)
1158 		return ERR_PTR(-ENOMEM);
1159 
1160 	config_item_init_type_name(&udm->item, name, &userdatum_type);
1161 	return &udm->item;
1162 }
1163 
1164 static void userdatum_drop(struct config_group *group, struct config_item *item)
1165 {
1166 	struct netconsole_target *nt;
1167 	struct userdata *ud;
1168 
1169 	ud = to_userdata(&group->cg_item);
1170 	nt = userdata_to_target(ud);
1171 
1172 	mutex_lock(&dynamic_netconsole_mutex);
1173 	update_userdata(nt);
1174 	config_item_put(item);
1175 	mutex_unlock(&dynamic_netconsole_mutex);
1176 }
1177 
1178 static struct configfs_attribute *userdata_attrs[] = {
1179 	&sysdata_attr_cpu_nr_enabled,
1180 	&sysdata_attr_taskname_enabled,
1181 	&sysdata_attr_release_enabled,
1182 	&sysdata_attr_msgid_enabled,
1183 	NULL,
1184 };
1185 
1186 static struct configfs_group_operations userdata_ops = {
1187 	.make_item		= userdatum_make_item,
1188 	.drop_item		= userdatum_drop,
1189 };
1190 
1191 static const struct config_item_type userdata_type = {
1192 	.ct_item_ops	= &userdatum_ops,
1193 	.ct_group_ops	= &userdata_ops,
1194 	.ct_attrs	= userdata_attrs,
1195 	.ct_owner	= THIS_MODULE,
1196 };
1197 
1198 CONFIGFS_ATTR(, enabled);
1199 CONFIGFS_ATTR(, extended);
1200 CONFIGFS_ATTR(, dev_name);
1201 CONFIGFS_ATTR(, local_port);
1202 CONFIGFS_ATTR(, remote_port);
1203 CONFIGFS_ATTR(, local_ip);
1204 CONFIGFS_ATTR(, remote_ip);
1205 CONFIGFS_ATTR_RO(, local_mac);
1206 CONFIGFS_ATTR(, remote_mac);
1207 CONFIGFS_ATTR(, release);
1208 CONFIGFS_ATTR_RO(, transmit_errors);
1209 
1210 static struct configfs_attribute *netconsole_target_attrs[] = {
1211 	&attr_enabled,
1212 	&attr_extended,
1213 	&attr_release,
1214 	&attr_dev_name,
1215 	&attr_local_port,
1216 	&attr_remote_port,
1217 	&attr_local_ip,
1218 	&attr_remote_ip,
1219 	&attr_local_mac,
1220 	&attr_remote_mac,
1221 	&attr_transmit_errors,
1222 	NULL,
1223 };
1224 
1225 /*
1226  * Item operations and type for netconsole_target.
1227  */
1228 
1229 static void netconsole_target_release(struct config_item *item)
1230 {
1231 	struct netconsole_target *nt = to_target(item);
1232 
1233 	kfree(nt->userdata);
1234 	kfree(nt);
1235 }
1236 
1237 static struct configfs_item_operations netconsole_target_item_ops = {
1238 	.release		= netconsole_target_release,
1239 };
1240 
1241 static const struct config_item_type netconsole_target_type = {
1242 	.ct_attrs		= netconsole_target_attrs,
1243 	.ct_item_ops		= &netconsole_target_item_ops,
1244 	.ct_owner		= THIS_MODULE,
1245 };
1246 
1247 static void init_target_config_group(struct netconsole_target *nt,
1248 				     const char *name)
1249 {
1250 	config_group_init_type_name(&nt->group, name, &netconsole_target_type);
1251 	config_group_init_type_name(&nt->userdata_group, "userdata",
1252 				    &userdata_type);
1253 	configfs_add_default_group(&nt->userdata_group, &nt->group);
1254 }
1255 
1256 static struct netconsole_target *find_cmdline_target(const char *name)
1257 {
1258 	struct netconsole_target *nt, *ret = NULL;
1259 	unsigned long flags;
1260 
1261 	spin_lock_irqsave(&target_list_lock, flags);
1262 	list_for_each_entry(nt, &target_list, list) {
1263 		if (!strcmp(nt->group.cg_item.ci_name, name)) {
1264 			ret = nt;
1265 			break;
1266 		}
1267 	}
1268 	spin_unlock_irqrestore(&target_list_lock, flags);
1269 
1270 	return ret;
1271 }
1272 
1273 /*
1274  * Group operations and type for netconsole_subsys.
1275  */
1276 
1277 static struct config_group *make_netconsole_target(struct config_group *group,
1278 						   const char *name)
1279 {
1280 	struct netconsole_target *nt;
1281 	unsigned long flags;
1282 
1283 	/* Checking if a target by this name was created at boot time.  If so,
1284 	 * attach a configfs entry to that target.  This enables dynamic
1285 	 * control.
1286 	 */
1287 	if (!strncmp(name, NETCONSOLE_PARAM_TARGET_PREFIX,
1288 		     strlen(NETCONSOLE_PARAM_TARGET_PREFIX))) {
1289 		nt = find_cmdline_target(name);
1290 		if (nt) {
1291 			init_target_config_group(nt, name);
1292 			return &nt->group;
1293 		}
1294 	}
1295 
1296 	nt = alloc_and_init();
1297 	if (!nt)
1298 		return ERR_PTR(-ENOMEM);
1299 
1300 	/* Initialize the config_group member */
1301 	init_target_config_group(nt, name);
1302 
1303 	/* Adding, but it is disabled */
1304 	spin_lock_irqsave(&target_list_lock, flags);
1305 	list_add(&nt->list, &target_list);
1306 	spin_unlock_irqrestore(&target_list_lock, flags);
1307 
1308 	return &nt->group;
1309 }
1310 
1311 static void drop_netconsole_target(struct config_group *group,
1312 				   struct config_item *item)
1313 {
1314 	unsigned long flags;
1315 	struct netconsole_target *nt = to_target(item);
1316 
1317 	spin_lock_irqsave(&target_list_lock, flags);
1318 	list_del(&nt->list);
1319 	spin_unlock_irqrestore(&target_list_lock, flags);
1320 
1321 	/*
1322 	 * The target may have never been enabled, or was manually disabled
1323 	 * before being removed so netpoll may have already been cleaned up.
1324 	 */
1325 	if (nt->enabled)
1326 		netpoll_cleanup(&nt->np);
1327 
1328 	config_item_put(&nt->group.cg_item);
1329 }
1330 
1331 static struct configfs_group_operations netconsole_subsys_group_ops = {
1332 	.make_group	= make_netconsole_target,
1333 	.drop_item	= drop_netconsole_target,
1334 };
1335 
1336 static const struct config_item_type netconsole_subsys_type = {
1337 	.ct_group_ops	= &netconsole_subsys_group_ops,
1338 	.ct_owner	= THIS_MODULE,
1339 };
1340 
1341 /* The netconsole configfs subsystem */
1342 static struct configfs_subsystem netconsole_subsys = {
1343 	.su_group	= {
1344 		.cg_item	= {
1345 			.ci_namebuf	= "netconsole",
1346 			.ci_type	= &netconsole_subsys_type,
1347 		},
1348 	},
1349 };
1350 
1351 static void populate_configfs_item(struct netconsole_target *nt,
1352 				   int cmdline_count)
1353 {
1354 	char target_name[16];
1355 
1356 	snprintf(target_name, sizeof(target_name), "%s%d",
1357 		 NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1358 	init_target_config_group(nt, target_name);
1359 }
1360 
1361 static int sysdata_append_cpu_nr(struct netconsole_target *nt, int offset)
1362 {
1363 	return scnprintf(&nt->sysdata[offset],
1364 			 MAX_EXTRADATA_ENTRY_LEN, " cpu=%u\n",
1365 			 raw_smp_processor_id());
1366 }
1367 
1368 static int sysdata_append_taskname(struct netconsole_target *nt, int offset)
1369 {
1370 	return scnprintf(&nt->sysdata[offset],
1371 			 MAX_EXTRADATA_ENTRY_LEN, " taskname=%s\n",
1372 			 current->comm);
1373 }
1374 
1375 static int sysdata_append_release(struct netconsole_target *nt, int offset)
1376 {
1377 	return scnprintf(&nt->sysdata[offset],
1378 			 MAX_EXTRADATA_ENTRY_LEN, " release=%s\n",
1379 			 init_utsname()->release);
1380 }
1381 
1382 static int sysdata_append_msgid(struct netconsole_target *nt, int offset)
1383 {
1384 	wrapping_assign_add(nt->msgcounter, 1);
1385 	return scnprintf(&nt->sysdata[offset],
1386 			 MAX_EXTRADATA_ENTRY_LEN, " msgid=%u\n",
1387 			 nt->msgcounter);
1388 }
1389 
1390 /*
1391  * prepare_sysdata - append sysdata in runtime
1392  * @nt: target to send message to
1393  */
1394 static int prepare_sysdata(struct netconsole_target *nt)
1395 {
1396 	int sysdata_len = 0;
1397 
1398 	if (!nt->sysdata_fields)
1399 		goto out;
1400 
1401 	if (nt->sysdata_fields & SYSDATA_CPU_NR)
1402 		sysdata_len += sysdata_append_cpu_nr(nt, sysdata_len);
1403 	if (nt->sysdata_fields & SYSDATA_TASKNAME)
1404 		sysdata_len += sysdata_append_taskname(nt, sysdata_len);
1405 	if (nt->sysdata_fields & SYSDATA_RELEASE)
1406 		sysdata_len += sysdata_append_release(nt, sysdata_len);
1407 	if (nt->sysdata_fields & SYSDATA_MSGID)
1408 		sysdata_len += sysdata_append_msgid(nt, sysdata_len);
1409 
1410 	WARN_ON_ONCE(sysdata_len >
1411 		     MAX_EXTRADATA_ENTRY_LEN * MAX_SYSDATA_ITEMS);
1412 
1413 out:
1414 	return sysdata_len;
1415 }
1416 #endif	/* CONFIG_NETCONSOLE_DYNAMIC */
1417 
1418 /* Handle network interface device notifications */
1419 static int netconsole_netdev_event(struct notifier_block *this,
1420 				   unsigned long event, void *ptr)
1421 {
1422 	unsigned long flags;
1423 	struct netconsole_target *nt, *tmp;
1424 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1425 	bool stopped = false;
1426 
1427 	if (!(event == NETDEV_CHANGENAME || event == NETDEV_UNREGISTER ||
1428 	      event == NETDEV_RELEASE || event == NETDEV_JOIN))
1429 		goto done;
1430 
1431 	mutex_lock(&target_cleanup_list_lock);
1432 	spin_lock_irqsave(&target_list_lock, flags);
1433 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1434 		netconsole_target_get(nt);
1435 		if (nt->np.dev == dev) {
1436 			switch (event) {
1437 			case NETDEV_CHANGENAME:
1438 				strscpy(nt->np.dev_name, dev->name, IFNAMSIZ);
1439 				break;
1440 			case NETDEV_RELEASE:
1441 			case NETDEV_JOIN:
1442 			case NETDEV_UNREGISTER:
1443 				nt->enabled = false;
1444 				list_move(&nt->list, &target_cleanup_list);
1445 				stopped = true;
1446 			}
1447 		}
1448 		netconsole_target_put(nt);
1449 	}
1450 	spin_unlock_irqrestore(&target_list_lock, flags);
1451 	mutex_unlock(&target_cleanup_list_lock);
1452 
1453 	if (stopped) {
1454 		const char *msg = "had an event";
1455 
1456 		switch (event) {
1457 		case NETDEV_UNREGISTER:
1458 			msg = "unregistered";
1459 			break;
1460 		case NETDEV_RELEASE:
1461 			msg = "released slaves";
1462 			break;
1463 		case NETDEV_JOIN:
1464 			msg = "is joining a master device";
1465 			break;
1466 		}
1467 		pr_info("network logging stopped on interface %s as it %s\n",
1468 			dev->name, msg);
1469 	}
1470 
1471 	/* Process target_cleanup_list entries. By the end, target_cleanup_list
1472 	 * should be empty
1473 	 */
1474 	netconsole_process_cleanups_core();
1475 
1476 done:
1477 	return NOTIFY_DONE;
1478 }
1479 
1480 static struct notifier_block netconsole_netdev_notifier = {
1481 	.notifier_call  = netconsole_netdev_event,
1482 };
1483 
1484 /**
1485  * send_udp - Wrapper for netpoll_send_udp that counts errors
1486  * @nt: target to send message to
1487  * @msg: message to send
1488  * @len: length of message
1489  *
1490  * Calls netpoll_send_udp and classifies the return value. If an error
1491  * occurred it increments statistics in nt->stats accordingly.
1492  * Only calls netpoll_send_udp if CONFIG_NETCONSOLE_DYNAMIC is disabled.
1493  */
1494 static void send_udp(struct netconsole_target *nt, const char *msg, int len)
1495 {
1496 	int result = netpoll_send_udp(&nt->np, msg, len);
1497 
1498 	if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC)) {
1499 		if (result == NET_XMIT_DROP) {
1500 			u64_stats_update_begin(&nt->stats.syncp);
1501 			u64_stats_inc(&nt->stats.xmit_drop_count);
1502 			u64_stats_update_end(&nt->stats.syncp);
1503 		} else if (result == -ENOMEM) {
1504 			u64_stats_update_begin(&nt->stats.syncp);
1505 			u64_stats_inc(&nt->stats.enomem_count);
1506 			u64_stats_update_end(&nt->stats.syncp);
1507 		}
1508 	}
1509 }
1510 
1511 static void send_msg_no_fragmentation(struct netconsole_target *nt,
1512 				      const char *msg,
1513 				      int msg_len,
1514 				      int release_len)
1515 {
1516 	const char *userdata = NULL;
1517 	const char *sysdata = NULL;
1518 	const char *release;
1519 
1520 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1521 	userdata = nt->userdata;
1522 	sysdata = nt->sysdata;
1523 #endif
1524 
1525 	if (release_len) {
1526 		release = init_utsname()->release;
1527 
1528 		scnprintf(nt->buf, MAX_PRINT_CHUNK, "%s,%s", release, msg);
1529 		msg_len += release_len;
1530 	} else {
1531 		memcpy(nt->buf, msg, msg_len);
1532 	}
1533 
1534 	if (userdata)
1535 		msg_len += scnprintf(&nt->buf[msg_len],
1536 				     MAX_PRINT_CHUNK - msg_len, "%s",
1537 				     userdata);
1538 
1539 	if (sysdata)
1540 		msg_len += scnprintf(&nt->buf[msg_len],
1541 				     MAX_PRINT_CHUNK - msg_len, "%s",
1542 				     sysdata);
1543 
1544 	send_udp(nt, nt->buf, msg_len);
1545 }
1546 
1547 static void append_release(char *buf)
1548 {
1549 	const char *release;
1550 
1551 	release = init_utsname()->release;
1552 	scnprintf(buf, MAX_PRINT_CHUNK, "%s,", release);
1553 }
1554 
1555 static void send_fragmented_body(struct netconsole_target *nt,
1556 				 const char *msgbody_ptr, int header_len,
1557 				 int msgbody_len, int sysdata_len)
1558 {
1559 	const char *userdata_ptr = NULL;
1560 	const char *sysdata_ptr = NULL;
1561 	int data_len, data_sent = 0;
1562 	int userdata_offset = 0;
1563 	int sysdata_offset = 0;
1564 	int msgbody_offset = 0;
1565 	int userdata_len = 0;
1566 
1567 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1568 	userdata_ptr = nt->userdata;
1569 	sysdata_ptr = nt->sysdata;
1570 	userdata_len = nt->userdata_length;
1571 #endif
1572 	if (WARN_ON_ONCE(!userdata_ptr && userdata_len != 0))
1573 		return;
1574 
1575 	if (WARN_ON_ONCE(!sysdata_ptr && sysdata_len != 0))
1576 		return;
1577 
1578 	/* data_len represents the number of bytes that will be sent. This is
1579 	 * bigger than MAX_PRINT_CHUNK, thus, it will be split in multiple
1580 	 * packets
1581 	 */
1582 	data_len = msgbody_len + userdata_len + sysdata_len;
1583 
1584 	/* In each iteration of the while loop below, we send a packet
1585 	 * containing the header and a portion of the data. The data is
1586 	 * composed of three parts: msgbody, userdata, and sysdata.
1587 	 * We keep track of how many bytes have been sent from each part using
1588 	 * the *_offset variables.
1589 	 * We keep track of how many bytes have been sent overall using the
1590 	 * data_sent variable, which ranges from 0 to the total bytes to be
1591 	 * sent.
1592 	 */
1593 	while (data_sent < data_len) {
1594 		int userdata_left = userdata_len - userdata_offset;
1595 		int sysdata_left = sysdata_len - sysdata_offset;
1596 		int msgbody_left = msgbody_len - msgbody_offset;
1597 		int buf_offset = 0;
1598 		int this_chunk = 0;
1599 
1600 		/* header is already populated in nt->buf, just append to it */
1601 		buf_offset = header_len;
1602 
1603 		buf_offset += scnprintf(nt->buf + buf_offset,
1604 					 MAX_PRINT_CHUNK - buf_offset,
1605 					 ",ncfrag=%d/%d;", data_sent,
1606 					 data_len);
1607 
1608 		/* append msgbody first */
1609 		this_chunk = min(msgbody_left, MAX_PRINT_CHUNK - buf_offset);
1610 		memcpy(nt->buf + buf_offset, msgbody_ptr + msgbody_offset,
1611 		       this_chunk);
1612 		msgbody_offset += this_chunk;
1613 		buf_offset += this_chunk;
1614 		data_sent += this_chunk;
1615 
1616 		/* after msgbody, append userdata */
1617 		if (userdata_ptr && userdata_left) {
1618 			this_chunk = min(userdata_left,
1619 					 MAX_PRINT_CHUNK - buf_offset);
1620 			memcpy(nt->buf + buf_offset,
1621 			       userdata_ptr + userdata_offset, this_chunk);
1622 			userdata_offset += this_chunk;
1623 			buf_offset += this_chunk;
1624 			data_sent += this_chunk;
1625 		}
1626 
1627 		/* after userdata, append sysdata */
1628 		if (sysdata_ptr && sysdata_left) {
1629 			this_chunk = min(sysdata_left,
1630 					 MAX_PRINT_CHUNK - buf_offset);
1631 			memcpy(nt->buf + buf_offset,
1632 			       sysdata_ptr + sysdata_offset, this_chunk);
1633 			sysdata_offset += this_chunk;
1634 			buf_offset += this_chunk;
1635 			data_sent += this_chunk;
1636 		}
1637 
1638 		/* if all is good, send the packet out */
1639 		if (WARN_ON_ONCE(data_sent > data_len))
1640 			return;
1641 
1642 		send_udp(nt, nt->buf, buf_offset);
1643 	}
1644 }
1645 
1646 static void send_msg_fragmented(struct netconsole_target *nt,
1647 				const char *msg,
1648 				int msg_len,
1649 				int release_len,
1650 				int sysdata_len)
1651 {
1652 	int header_len, msgbody_len;
1653 	const char *msgbody;
1654 
1655 	/* need to insert extra header fields, detect header and msgbody */
1656 	msgbody = memchr(msg, ';', msg_len);
1657 	if (WARN_ON_ONCE(!msgbody))
1658 		return;
1659 
1660 	header_len = msgbody - msg;
1661 	msgbody_len = msg_len - header_len - 1;
1662 	msgbody++;
1663 
1664 	/*
1665 	 * Transfer multiple chunks with the following extra header.
1666 	 * "ncfrag=<byte-offset>/<total-bytes>"
1667 	 */
1668 	if (release_len)
1669 		append_release(nt->buf);
1670 
1671 	/* Copy the header into the buffer */
1672 	memcpy(nt->buf + release_len, msg, header_len);
1673 	header_len += release_len;
1674 
1675 	/* for now on, the header will be persisted, and the msgbody
1676 	 * will be replaced
1677 	 */
1678 	send_fragmented_body(nt, msgbody, header_len, msgbody_len,
1679 			     sysdata_len);
1680 }
1681 
1682 /**
1683  * send_ext_msg_udp - send extended log message to target
1684  * @nt: target to send message to
1685  * @msg: extended log message to send
1686  * @msg_len: length of message
1687  *
1688  * Transfer extended log @msg to @nt.  If @msg is longer than
1689  * MAX_PRINT_CHUNK, it'll be split and transmitted in multiple chunks with
1690  * ncfrag header field added to identify them.
1691  */
1692 static void send_ext_msg_udp(struct netconsole_target *nt, const char *msg,
1693 			     int msg_len)
1694 {
1695 	int userdata_len = 0;
1696 	int release_len = 0;
1697 	int sysdata_len = 0;
1698 
1699 #ifdef CONFIG_NETCONSOLE_DYNAMIC
1700 	sysdata_len = prepare_sysdata(nt);
1701 	userdata_len = nt->userdata_length;
1702 #endif
1703 	if (nt->release)
1704 		release_len = strlen(init_utsname()->release) + 1;
1705 
1706 	if (msg_len + release_len + sysdata_len + userdata_len <= MAX_PRINT_CHUNK)
1707 		return send_msg_no_fragmentation(nt, msg, msg_len, release_len);
1708 
1709 	return send_msg_fragmented(nt, msg, msg_len, release_len,
1710 				   sysdata_len);
1711 }
1712 
1713 static void write_ext_msg(struct console *con, const char *msg,
1714 			  unsigned int len)
1715 {
1716 	struct netconsole_target *nt;
1717 	unsigned long flags;
1718 
1719 	if ((oops_only && !oops_in_progress) || list_empty(&target_list))
1720 		return;
1721 
1722 	spin_lock_irqsave(&target_list_lock, flags);
1723 	list_for_each_entry(nt, &target_list, list)
1724 		if (nt->extended && nt->enabled && netif_running(nt->np.dev))
1725 			send_ext_msg_udp(nt, msg, len);
1726 	spin_unlock_irqrestore(&target_list_lock, flags);
1727 }
1728 
1729 static void write_msg(struct console *con, const char *msg, unsigned int len)
1730 {
1731 	int frag, left;
1732 	unsigned long flags;
1733 	struct netconsole_target *nt;
1734 	const char *tmp;
1735 
1736 	if (oops_only && !oops_in_progress)
1737 		return;
1738 	/* Avoid taking lock and disabling interrupts unnecessarily */
1739 	if (list_empty(&target_list))
1740 		return;
1741 
1742 	spin_lock_irqsave(&target_list_lock, flags);
1743 	list_for_each_entry(nt, &target_list, list) {
1744 		if (!nt->extended && nt->enabled && netif_running(nt->np.dev)) {
1745 			/*
1746 			 * We nest this inside the for-each-target loop above
1747 			 * so that we're able to get as much logging out to
1748 			 * at least one target if we die inside here, instead
1749 			 * of unnecessarily keeping all targets in lock-step.
1750 			 */
1751 			tmp = msg;
1752 			for (left = len; left;) {
1753 				frag = min(left, MAX_PRINT_CHUNK);
1754 				send_udp(nt, tmp, frag);
1755 				tmp += frag;
1756 				left -= frag;
1757 			}
1758 		}
1759 	}
1760 	spin_unlock_irqrestore(&target_list_lock, flags);
1761 }
1762 
1763 static int netconsole_parser_cmdline(struct netpoll *np, char *opt)
1764 {
1765 	bool ipversion_set = false;
1766 	char *cur = opt;
1767 	char *delim;
1768 	int ipv6;
1769 
1770 	if (*cur != '@') {
1771 		delim = strchr(cur, '@');
1772 		if (!delim)
1773 			goto parse_failed;
1774 		*delim = 0;
1775 		if (kstrtou16(cur, 10, &np->local_port))
1776 			goto parse_failed;
1777 		cur = delim;
1778 	}
1779 	cur++;
1780 
1781 	if (*cur != '/') {
1782 		ipversion_set = true;
1783 		delim = strchr(cur, '/');
1784 		if (!delim)
1785 			goto parse_failed;
1786 		*delim = 0;
1787 		ipv6 = netpoll_parse_ip_addr(cur, &np->local_ip);
1788 		if (ipv6 < 0)
1789 			goto parse_failed;
1790 		else
1791 			np->ipv6 = (bool)ipv6;
1792 		cur = delim;
1793 	}
1794 	cur++;
1795 
1796 	if (*cur != ',') {
1797 		/* parse out dev_name or dev_mac */
1798 		delim = strchr(cur, ',');
1799 		if (!delim)
1800 			goto parse_failed;
1801 		*delim = 0;
1802 
1803 		np->dev_name[0] = '\0';
1804 		eth_broadcast_addr(np->dev_mac);
1805 		if (!strchr(cur, ':'))
1806 			strscpy(np->dev_name, cur, sizeof(np->dev_name));
1807 		else if (!mac_pton(cur, np->dev_mac))
1808 			goto parse_failed;
1809 
1810 		cur = delim;
1811 	}
1812 	cur++;
1813 
1814 	if (*cur != '@') {
1815 		/* dst port */
1816 		delim = strchr(cur, '@');
1817 		if (!delim)
1818 			goto parse_failed;
1819 		*delim = 0;
1820 		if (*cur == ' ' || *cur == '\t')
1821 			np_info(np, "warning: whitespace is not allowed\n");
1822 		if (kstrtou16(cur, 10, &np->remote_port))
1823 			goto parse_failed;
1824 		cur = delim;
1825 	}
1826 	cur++;
1827 
1828 	/* dst ip */
1829 	delim = strchr(cur, '/');
1830 	if (!delim)
1831 		goto parse_failed;
1832 	*delim = 0;
1833 	ipv6 = netpoll_parse_ip_addr(cur, &np->remote_ip);
1834 	if (ipv6 < 0)
1835 		goto parse_failed;
1836 	else if (ipversion_set && np->ipv6 != (bool)ipv6)
1837 		goto parse_failed;
1838 	else
1839 		np->ipv6 = (bool)ipv6;
1840 	cur = delim + 1;
1841 
1842 	if (*cur != 0) {
1843 		/* MAC address */
1844 		if (!mac_pton(cur, np->remote_mac))
1845 			goto parse_failed;
1846 	}
1847 
1848 	netconsole_print_banner(np);
1849 
1850 	return 0;
1851 
1852  parse_failed:
1853 	np_info(np, "couldn't parse config at '%s'!\n", cur);
1854 	return -1;
1855 }
1856 
1857 /* Allocate new target (from boot/module param) and setup netpoll for it */
1858 static struct netconsole_target *alloc_param_target(char *target_config,
1859 						    int cmdline_count)
1860 {
1861 	struct netconsole_target *nt;
1862 	int err;
1863 
1864 	nt = alloc_and_init();
1865 	if (!nt) {
1866 		err = -ENOMEM;
1867 		goto fail;
1868 	}
1869 
1870 	if (*target_config == '+') {
1871 		nt->extended = true;
1872 		target_config++;
1873 	}
1874 
1875 	if (*target_config == 'r') {
1876 		if (!nt->extended) {
1877 			pr_err("Netconsole configuration error. Release feature requires extended log message");
1878 			err = -EINVAL;
1879 			goto fail;
1880 		}
1881 		nt->release = true;
1882 		target_config++;
1883 	}
1884 
1885 	/* Parse parameters and setup netpoll */
1886 	err = netconsole_parser_cmdline(&nt->np, target_config);
1887 	if (err)
1888 		goto fail;
1889 
1890 	err = netpoll_setup(&nt->np);
1891 	if (err) {
1892 		pr_err("Not enabling netconsole for %s%d. Netpoll setup failed\n",
1893 		       NETCONSOLE_PARAM_TARGET_PREFIX, cmdline_count);
1894 		if (!IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1895 			/* only fail if dynamic reconfiguration is set,
1896 			 * otherwise, keep the target in the list, but disabled.
1897 			 */
1898 			goto fail;
1899 	} else {
1900 		nt->enabled = true;
1901 	}
1902 	populate_configfs_item(nt, cmdline_count);
1903 
1904 	return nt;
1905 
1906 fail:
1907 	kfree(nt);
1908 	return ERR_PTR(err);
1909 }
1910 
1911 /* Cleanup netpoll for given target (from boot/module param) and free it */
1912 static void free_param_target(struct netconsole_target *nt)
1913 {
1914 	netpoll_cleanup(&nt->np);
1915 #ifdef	CONFIG_NETCONSOLE_DYNAMIC
1916 	kfree(nt->userdata);
1917 #endif
1918 	kfree(nt);
1919 }
1920 
1921 static struct console netconsole_ext = {
1922 	.name	= "netcon_ext",
1923 	.flags	= CON_ENABLED | CON_EXTENDED,
1924 	.write	= write_ext_msg,
1925 };
1926 
1927 static struct console netconsole = {
1928 	.name	= "netcon",
1929 	.flags	= CON_ENABLED,
1930 	.write	= write_msg,
1931 };
1932 
1933 static int __init init_netconsole(void)
1934 {
1935 	int err;
1936 	struct netconsole_target *nt, *tmp;
1937 	u32 console_type_needed = 0;
1938 	unsigned int count = 0;
1939 	unsigned long flags;
1940 	char *target_config;
1941 	char *input = config;
1942 
1943 	if (strnlen(input, MAX_PARAM_LENGTH)) {
1944 		while ((target_config = strsep(&input, ";"))) {
1945 			nt = alloc_param_target(target_config, count);
1946 			if (IS_ERR(nt)) {
1947 				if (IS_ENABLED(CONFIG_NETCONSOLE_DYNAMIC))
1948 					continue;
1949 				err = PTR_ERR(nt);
1950 				goto fail;
1951 			}
1952 			/* Dump existing printks when we register */
1953 			if (nt->extended) {
1954 				console_type_needed |= CONS_EXTENDED;
1955 				netconsole_ext.flags |= CON_PRINTBUFFER;
1956 			} else {
1957 				console_type_needed |= CONS_BASIC;
1958 				netconsole.flags |= CON_PRINTBUFFER;
1959 			}
1960 
1961 			spin_lock_irqsave(&target_list_lock, flags);
1962 			list_add(&nt->list, &target_list);
1963 			spin_unlock_irqrestore(&target_list_lock, flags);
1964 			count++;
1965 		}
1966 	}
1967 
1968 	err = register_netdevice_notifier(&netconsole_netdev_notifier);
1969 	if (err)
1970 		goto fail;
1971 
1972 	err = dynamic_netconsole_init();
1973 	if (err)
1974 		goto undonotifier;
1975 
1976 	if (console_type_needed & CONS_EXTENDED)
1977 		register_console(&netconsole_ext);
1978 	if (console_type_needed & CONS_BASIC)
1979 		register_console(&netconsole);
1980 	pr_info("network logging started\n");
1981 
1982 	return err;
1983 
1984 undonotifier:
1985 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
1986 
1987 fail:
1988 	pr_err("cleaning up\n");
1989 
1990 	/*
1991 	 * Remove all targets and destroy them (only targets created
1992 	 * from the boot/module option exist here). Skipping the list
1993 	 * lock is safe here, and netpoll_cleanup() will sleep.
1994 	 */
1995 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
1996 		list_del(&nt->list);
1997 		free_param_target(nt);
1998 	}
1999 
2000 	return err;
2001 }
2002 
2003 static void __exit cleanup_netconsole(void)
2004 {
2005 	struct netconsole_target *nt, *tmp;
2006 
2007 	if (console_is_registered(&netconsole_ext))
2008 		unregister_console(&netconsole_ext);
2009 	if (console_is_registered(&netconsole))
2010 		unregister_console(&netconsole);
2011 	dynamic_netconsole_exit();
2012 	unregister_netdevice_notifier(&netconsole_netdev_notifier);
2013 
2014 	/*
2015 	 * Targets created via configfs pin references on our module
2016 	 * and would first be rmdir(2)'ed from userspace. We reach
2017 	 * here only when they are already destroyed, and only those
2018 	 * created from the boot/module option are left, so remove and
2019 	 * destroy them. Skipping the list lock is safe here, and
2020 	 * netpoll_cleanup() will sleep.
2021 	 */
2022 	list_for_each_entry_safe(nt, tmp, &target_list, list) {
2023 		list_del(&nt->list);
2024 		free_param_target(nt);
2025 	}
2026 }
2027 
2028 /*
2029  * Use late_initcall to ensure netconsole is
2030  * initialized after network device driver if built-in.
2031  *
2032  * late_initcall() and module_init() are identical if built as module.
2033  */
2034 late_initcall(init_netconsole);
2035 module_exit(cleanup_netconsole);
2036