xref: /linux/kernel/trace/rv/rv.c (revision 260f6f4fda93c8485c8037865c941b42b9cba5d2)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
4  *
5  * This is the online Runtime Verification (RV) interface.
6  *
7  * RV is a lightweight (yet rigorous) method that complements classical
8  * exhaustive verification techniques (such as model checking and
9  * theorem proving) with a more practical approach to complex systems.
10  *
11  * RV works by analyzing the trace of the system's actual execution,
12  * comparing it against a formal specification of the system behavior.
13  * RV can give precise information on the runtime behavior of the
14  * monitored system while enabling the reaction for unexpected
15  * events, avoiding, for example, the propagation of a failure on
16  * safety-critical systems.
17  *
18  * The development of this interface roots in the development of the
19  * paper:
20  *
21  * De Oliveira, Daniel Bristot; Cucinotta, Tommaso; De Oliveira, Romulo
22  * Silva. Efficient formal verification for the Linux kernel. In:
23  * International Conference on Software Engineering and Formal Methods.
24  * Springer, Cham, 2019. p. 315-332.
25  *
26  * And:
27  *
28  * De Oliveira, Daniel Bristot, et al. Automata-based formal analysis
29  * and verification of the real-time Linux kernel. PhD Thesis, 2020.
30  *
31  * == Runtime monitor interface ==
32  *
33  * A monitor is the central part of the runtime verification of a system.
34  *
35  * The monitor stands in between the formal specification of the desired
36  * (or undesired) behavior, and the trace of the actual system.
37  *
38  * In Linux terms, the runtime verification monitors are encapsulated
39  * inside the "RV monitor" abstraction. A RV monitor includes a reference
40  * model of the system, a set of instances of the monitor (per-cpu monitor,
41  * per-task monitor, and so on), and the helper functions that glue the
42  * monitor to the system via trace. Generally, a monitor includes some form
43  * of trace output as a reaction for event parsing and exceptions,
44  * as depicted below:
45  *
46  * Linux  +----- RV Monitor ----------------------------------+ Formal
47  *  Realm |                                                   |  Realm
48  *  +-------------------+     +----------------+     +-----------------+
49  *  |   Linux kernel    |     |     Monitor    |     |     Reference   |
50  *  |     Tracing       |  -> |   Instance(s)  | <-  |       Model     |
51  *  | (instrumentation) |     | (verification) |     | (specification) |
52  *  +-------------------+     +----------------+     +-----------------+
53  *         |                          |                       |
54  *         |                          V                       |
55  *         |                     +----------+                 |
56  *         |                     | Reaction |                 |
57  *         |                     +--+--+--+-+                 |
58  *         |                        |  |  |                   |
59  *         |                        |  |  +-> trace output ?  |
60  *         +------------------------|--|----------------------+
61  *                                  |  +----> panic ?
62  *                                  +-------> <user-specified>
63  *
64  * This file implements the interface for loading RV monitors, and
65  * to control the verification session.
66  *
67  * == Registering monitors ==
68  *
69  * The struct rv_monitor defines a set of callback functions to control
70  * a verification session. For instance, when a given monitor is enabled,
71  * the "enable" callback function is called to hook the instrumentation
72  * functions to the kernel trace events. The "disable" function is called
73  * when disabling the verification session.
74  *
75  * A RV monitor is registered via:
76  *   int rv_register_monitor(struct rv_monitor *monitor);
77  * And unregistered via:
78  *   int rv_unregister_monitor(struct rv_monitor *monitor);
79  *
80  * == User interface ==
81  *
82  * The user interface resembles kernel tracing interface. It presents
83  * these files:
84  *
85  *  "available_monitors"
86  *    - List the available monitors, one per line.
87  *
88  *    For example:
89  *      # cat available_monitors
90  *      wip
91  *      wwnr
92  *
93  *  "enabled_monitors"
94  *    - Lists the enabled monitors, one per line;
95  *    - Writing to it enables a given monitor;
96  *    - Writing a monitor name with a '!' prefix disables it;
97  *    - Truncating the file disables all enabled monitors.
98  *
99  *    For example:
100  *      # cat enabled_monitors
101  *      # echo wip > enabled_monitors
102  *      # echo wwnr >> enabled_monitors
103  *      # cat enabled_monitors
104  *      wip
105  *      wwnr
106  *      # echo '!wip' >> enabled_monitors
107  *      # cat enabled_monitors
108  *      wwnr
109  *      # echo > enabled_monitors
110  *      # cat enabled_monitors
111  *      #
112  *
113  *    Note that more than one monitor can be enabled concurrently.
114  *
115  *  "monitoring_on"
116  *    - It is an on/off general switcher for monitoring. Note
117  *    that it does not disable enabled monitors or detach events,
118  *    but stops the per-entity monitors from monitoring the events
119  *    received from the instrumentation. It resembles the "tracing_on"
120  *    switcher.
121  *
122  *  "monitors/"
123  *    Each monitor will have its own directory inside "monitors/". There
124  *    the monitor specific files will be presented.
125  *    The "monitors/" directory resembles the "events" directory on
126  *    tracefs.
127  *
128  *    For example:
129  *      # cd monitors/wip/
130  *      # ls
131  *      desc  enable
132  *      # cat desc
133  *      auto-generated wakeup in preemptive monitor.
134  *      # cat enable
135  *      0
136  *
137  *  For further information, see:
138  *   Documentation/trace/rv/runtime-verification.rst
139  */
140 
141 #include <linux/kernel.h>
142 #include <linux/module.h>
143 #include <linux/init.h>
144 #include <linux/slab.h>
145 
146 #ifdef CONFIG_RV_MON_EVENTS
147 #define CREATE_TRACE_POINTS
148 #include <rv_trace.h>
149 #endif
150 
151 #include "rv.h"
152 
153 DEFINE_MUTEX(rv_interface_lock);
154 
155 static struct rv_interface rv_root;
156 
157 struct dentry *get_monitors_root(void)
158 {
159 	return rv_root.monitors_dir;
160 }
161 
162 /*
163  * Interface for the monitor register.
164  */
165 LIST_HEAD(rv_monitors_list);
166 
167 static int task_monitor_count;
168 static bool task_monitor_slots[CONFIG_RV_PER_TASK_MONITORS];
169 
170 int rv_get_task_monitor_slot(void)
171 {
172 	int i;
173 
174 	lockdep_assert_held(&rv_interface_lock);
175 
176 	if (task_monitor_count == CONFIG_RV_PER_TASK_MONITORS)
177 		return -EBUSY;
178 
179 	task_monitor_count++;
180 
181 	for (i = 0; i < CONFIG_RV_PER_TASK_MONITORS; i++) {
182 		if (task_monitor_slots[i] == false) {
183 			task_monitor_slots[i] = true;
184 			return i;
185 		}
186 	}
187 
188 	WARN_ONCE(1, "RV task_monitor_count and slots are out of sync\n");
189 
190 	return -EINVAL;
191 }
192 
193 void rv_put_task_monitor_slot(int slot)
194 {
195 	lockdep_assert_held(&rv_interface_lock);
196 
197 	if (slot < 0 || slot >= CONFIG_RV_PER_TASK_MONITORS) {
198 		WARN_ONCE(1, "RV releasing an invalid slot!: %d\n", slot);
199 		return;
200 	}
201 
202 	WARN_ONCE(!task_monitor_slots[slot], "RV releasing unused task_monitor_slots: %d\n",
203 		  slot);
204 
205 	task_monitor_count--;
206 	task_monitor_slots[slot] = false;
207 }
208 
209 /*
210  * Monitors with a parent are nested,
211  * Monitors without a parent could be standalone or containers.
212  */
213 bool rv_is_nested_monitor(struct rv_monitor *mon)
214 {
215 	return mon->parent != NULL;
216 }
217 
218 /*
219  * We set our list to have nested monitors listed after their parent
220  * if a monitor has a child element its a container.
221  * Containers can be also identified based on their function pointers:
222  * as they are not real monitors they do not need function definitions
223  * for enable()/disable(). Use this condition to find empty containers.
224  * Keep both conditions in case we have some non-compliant containers.
225  */
226 bool rv_is_container_monitor(struct rv_monitor *mon)
227 {
228 	struct rv_monitor *next;
229 
230 	if (list_is_last(&mon->list, &rv_monitors_list))
231 		return false;
232 
233 	next = list_next_entry(mon, list);
234 
235 	return next->parent == mon || !mon->enable;
236 }
237 
238 /*
239  * This section collects the monitor/ files and folders.
240  */
241 static ssize_t monitor_enable_read_data(struct file *filp, char __user *user_buf, size_t count,
242 					loff_t *ppos)
243 {
244 	struct rv_monitor *mon = filp->private_data;
245 	const char *buff;
246 
247 	buff = mon->enabled ? "1\n" : "0\n";
248 
249 	return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff)+1);
250 }
251 
252 /*
253  * __rv_disable_monitor - disabled an enabled monitor
254  */
255 static int __rv_disable_monitor(struct rv_monitor *mon, bool sync)
256 {
257 	lockdep_assert_held(&rv_interface_lock);
258 
259 	if (mon->enabled) {
260 		mon->enabled = 0;
261 		if (mon->disable)
262 			mon->disable();
263 
264 		/*
265 		 * Wait for the execution of all events to finish.
266 		 * Otherwise, the data used by the monitor could
267 		 * be inconsistent. i.e., if the monitor is re-enabled.
268 		 */
269 		if (sync)
270 			tracepoint_synchronize_unregister();
271 		return 1;
272 	}
273 	return 0;
274 }
275 
276 static void rv_disable_single(struct rv_monitor *mon)
277 {
278 	__rv_disable_monitor(mon, true);
279 }
280 
281 static int rv_enable_single(struct rv_monitor *mon)
282 {
283 	int retval;
284 
285 	lockdep_assert_held(&rv_interface_lock);
286 
287 	if (mon->enabled)
288 		return 0;
289 
290 	retval = mon->enable();
291 
292 	if (!retval)
293 		mon->enabled = 1;
294 
295 	return retval;
296 }
297 
298 static void rv_disable_container(struct rv_monitor *mon)
299 {
300 	struct rv_monitor *p = mon;
301 	int enabled = 0;
302 
303 	list_for_each_entry_continue(p, &rv_monitors_list, list) {
304 		if (p->parent != mon)
305 			break;
306 		enabled += __rv_disable_monitor(p, false);
307 	}
308 	if (enabled)
309 		tracepoint_synchronize_unregister();
310 	mon->enabled = 0;
311 }
312 
313 static int rv_enable_container(struct rv_monitor *mon)
314 {
315 	struct rv_monitor *p = mon;
316 	int retval = 0;
317 
318 	list_for_each_entry_continue(p, &rv_monitors_list, list) {
319 		if (retval || p->parent != mon)
320 			break;
321 		retval = rv_enable_single(p);
322 	}
323 	if (retval)
324 		rv_disable_container(mon);
325 	else
326 		mon->enabled = 1;
327 	return retval;
328 }
329 
330 /**
331  * rv_disable_monitor - disable a given runtime monitor
332  * @mon: Pointer to the monitor definition structure.
333  *
334  * Returns 0 on success.
335  */
336 int rv_disable_monitor(struct rv_monitor *mon)
337 {
338 	if (rv_is_container_monitor(mon))
339 		rv_disable_container(mon);
340 	else
341 		rv_disable_single(mon);
342 
343 	return 0;
344 }
345 
346 /**
347  * rv_enable_monitor - enable a given runtime monitor
348  * @mon: Pointer to the monitor definition structure.
349  *
350  * Returns 0 on success, error otherwise.
351  */
352 int rv_enable_monitor(struct rv_monitor *mon)
353 {
354 	int retval;
355 
356 	if (rv_is_container_monitor(mon))
357 		retval = rv_enable_container(mon);
358 	else
359 		retval = rv_enable_single(mon);
360 
361 	return retval;
362 }
363 
364 /*
365  * interface for enabling/disabling a monitor.
366  */
367 static ssize_t monitor_enable_write_data(struct file *filp, const char __user *user_buf,
368 					 size_t count, loff_t *ppos)
369 {
370 	struct rv_monitor *mon = filp->private_data;
371 	int retval;
372 	bool val;
373 
374 	retval = kstrtobool_from_user(user_buf, count, &val);
375 	if (retval)
376 		return retval;
377 
378 	mutex_lock(&rv_interface_lock);
379 
380 	if (val)
381 		retval = rv_enable_monitor(mon);
382 	else
383 		retval = rv_disable_monitor(mon);
384 
385 	mutex_unlock(&rv_interface_lock);
386 
387 	return retval ? : count;
388 }
389 
390 static const struct file_operations interface_enable_fops = {
391 	.open   = simple_open,
392 	.write  = monitor_enable_write_data,
393 	.read   = monitor_enable_read_data,
394 };
395 
396 /*
397  * Interface to read monitors description.
398  */
399 static ssize_t monitor_desc_read_data(struct file *filp, char __user *user_buf, size_t count,
400 				      loff_t *ppos)
401 {
402 	struct rv_monitor *mon = filp->private_data;
403 	char buff[256];
404 
405 	memset(buff, 0, sizeof(buff));
406 
407 	snprintf(buff, sizeof(buff), "%s\n", mon->description);
408 
409 	return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1);
410 }
411 
412 static const struct file_operations interface_desc_fops = {
413 	.open   = simple_open,
414 	.read	= monitor_desc_read_data,
415 };
416 
417 /*
418  * During the registration of a monitor, this function creates
419  * the monitor dir, where the specific options of the monitor
420  * are exposed.
421  */
422 static int create_monitor_dir(struct rv_monitor *mon, struct rv_monitor *parent)
423 {
424 	struct dentry *root = parent ? parent->root_d : get_monitors_root();
425 	const char *name = mon->name;
426 	struct dentry *tmp;
427 	int retval;
428 
429 	mon->root_d = rv_create_dir(name, root);
430 	if (!mon->root_d)
431 		return -ENOMEM;
432 
433 	tmp = rv_create_file("enable", RV_MODE_WRITE, mon->root_d, mon, &interface_enable_fops);
434 	if (!tmp) {
435 		retval = -ENOMEM;
436 		goto out_remove_root;
437 	}
438 
439 	tmp = rv_create_file("desc", RV_MODE_READ, mon->root_d, mon, &interface_desc_fops);
440 	if (!tmp) {
441 		retval = -ENOMEM;
442 		goto out_remove_root;
443 	}
444 
445 	retval = reactor_populate_monitor(mon);
446 	if (retval)
447 		goto out_remove_root;
448 
449 	return 0;
450 
451 out_remove_root:
452 	rv_remove(mon->root_d);
453 	return retval;
454 }
455 
456 /*
457  * Available/Enable monitor shared seq functions.
458  */
459 static int monitors_show(struct seq_file *m, void *p)
460 {
461 	struct rv_monitor *mon = container_of(p, struct rv_monitor, list);
462 
463 	if (mon->parent)
464 		seq_printf(m, "%s:%s\n", mon->parent->name, mon->name);
465 	else
466 		seq_printf(m, "%s\n", mon->name);
467 	return 0;
468 }
469 
470 /*
471  * Used by the seq file operations at the end of a read
472  * operation.
473  */
474 static void monitors_stop(struct seq_file *m, void *p)
475 {
476 	mutex_unlock(&rv_interface_lock);
477 }
478 
479 /*
480  * Available monitor seq functions.
481  */
482 static void *available_monitors_start(struct seq_file *m, loff_t *pos)
483 {
484 	mutex_lock(&rv_interface_lock);
485 	return seq_list_start(&rv_monitors_list, *pos);
486 }
487 
488 static void *available_monitors_next(struct seq_file *m, void *p, loff_t *pos)
489 {
490 	return seq_list_next(p, &rv_monitors_list, pos);
491 }
492 
493 /*
494  * Enable monitor seq functions.
495  */
496 static void *enabled_monitors_next(struct seq_file *m, void *p, loff_t *pos)
497 {
498 	struct rv_monitor *mon = p;
499 
500 	(*pos)++;
501 
502 	list_for_each_entry_continue(mon, &rv_monitors_list, list) {
503 		if (mon->enabled)
504 			return mon;
505 	}
506 
507 	return NULL;
508 }
509 
510 static void *enabled_monitors_start(struct seq_file *m, loff_t *pos)
511 {
512 	struct rv_monitor *mon;
513 	loff_t l;
514 
515 	mutex_lock(&rv_interface_lock);
516 
517 	if (list_empty(&rv_monitors_list))
518 		return NULL;
519 
520 	mon = list_entry(&rv_monitors_list, struct rv_monitor, list);
521 
522 	for (l = 0; l <= *pos; ) {
523 		mon = enabled_monitors_next(m, mon, &l);
524 		if (!mon)
525 			break;
526 	}
527 
528 	return mon;
529 }
530 
531 /*
532  * available/enabled monitors seq definition.
533  */
534 static const struct seq_operations available_monitors_seq_ops = {
535 	.start	= available_monitors_start,
536 	.next	= available_monitors_next,
537 	.stop	= monitors_stop,
538 	.show	= monitors_show
539 };
540 
541 static const struct seq_operations enabled_monitors_seq_ops = {
542 	.start  = enabled_monitors_start,
543 	.next   = enabled_monitors_next,
544 	.stop   = monitors_stop,
545 	.show   = monitors_show
546 };
547 
548 /*
549  * available_monitors interface.
550  */
551 static int available_monitors_open(struct inode *inode, struct file *file)
552 {
553 	return seq_open(file, &available_monitors_seq_ops);
554 };
555 
556 static const struct file_operations available_monitors_ops = {
557 	.open    = available_monitors_open,
558 	.read    = seq_read,
559 	.llseek  = seq_lseek,
560 	.release = seq_release
561 };
562 
563 /*
564  * enabled_monitors interface.
565  */
566 static void disable_all_monitors(void)
567 {
568 	struct rv_monitor *mon;
569 	int enabled = 0;
570 
571 	mutex_lock(&rv_interface_lock);
572 
573 	list_for_each_entry(mon, &rv_monitors_list, list)
574 		enabled += __rv_disable_monitor(mon, false);
575 
576 	if (enabled) {
577 		/*
578 		 * Wait for the execution of all events to finish.
579 		 * Otherwise, the data used by the monitor could
580 		 * be inconsistent. i.e., if the monitor is re-enabled.
581 		 */
582 		tracepoint_synchronize_unregister();
583 	}
584 
585 	mutex_unlock(&rv_interface_lock);
586 }
587 
588 static int enabled_monitors_open(struct inode *inode, struct file *file)
589 {
590 	if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
591 		disable_all_monitors();
592 
593 	return seq_open(file, &enabled_monitors_seq_ops);
594 };
595 
596 static ssize_t enabled_monitors_write(struct file *filp, const char __user *user_buf,
597 				      size_t count, loff_t *ppos)
598 {
599 	char buff[MAX_RV_MONITOR_NAME_SIZE + 2];
600 	struct rv_monitor *mon;
601 	int retval = -EINVAL;
602 	bool enable = true;
603 	char *ptr, *tmp;
604 	int len;
605 
606 	if (count < 1 || count > MAX_RV_MONITOR_NAME_SIZE + 1)
607 		return -EINVAL;
608 
609 	memset(buff, 0, sizeof(buff));
610 
611 	retval = simple_write_to_buffer(buff, sizeof(buff) - 1, ppos, user_buf, count);
612 	if (retval < 0)
613 		return -EFAULT;
614 
615 	ptr = strim(buff);
616 
617 	if (ptr[0] == '!') {
618 		enable = false;
619 		ptr++;
620 	}
621 
622 	len = strlen(ptr);
623 	if (!len)
624 		return count;
625 
626 	mutex_lock(&rv_interface_lock);
627 
628 	retval = -EINVAL;
629 
630 	/* we support 1 nesting level, trim the parent */
631 	tmp = strstr(ptr, ":");
632 	if (tmp)
633 		ptr = tmp+1;
634 
635 	list_for_each_entry(mon, &rv_monitors_list, list) {
636 		if (strcmp(ptr, mon->name) != 0)
637 			continue;
638 
639 		/*
640 		 * Monitor found!
641 		 */
642 		if (enable)
643 			retval = rv_enable_monitor(mon);
644 		else
645 			retval = rv_disable_monitor(mon);
646 
647 		if (!retval)
648 			retval = count;
649 
650 		break;
651 	}
652 
653 	mutex_unlock(&rv_interface_lock);
654 	return retval;
655 }
656 
657 static const struct file_operations enabled_monitors_ops = {
658 	.open		= enabled_monitors_open,
659 	.read		= seq_read,
660 	.write		= enabled_monitors_write,
661 	.llseek		= seq_lseek,
662 	.release	= seq_release,
663 };
664 
665 /*
666  * Monitoring on global switcher!
667  */
668 static bool __read_mostly monitoring_on;
669 
670 /**
671  * rv_monitoring_on - checks if monitoring is on
672  *
673  * Returns 1 if on, 0 otherwise.
674  */
675 bool rv_monitoring_on(void)
676 {
677 	/* Ensures that concurrent monitors read consistent monitoring_on */
678 	smp_rmb();
679 	return READ_ONCE(monitoring_on);
680 }
681 
682 /*
683  * monitoring_on general switcher.
684  */
685 static ssize_t monitoring_on_read_data(struct file *filp, char __user *user_buf,
686 				       size_t count, loff_t *ppos)
687 {
688 	const char *buff;
689 
690 	buff = rv_monitoring_on() ? "1\n" : "0\n";
691 
692 	return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1);
693 }
694 
695 static void turn_monitoring_off(void)
696 {
697 	WRITE_ONCE(monitoring_on, false);
698 	/* Ensures that concurrent monitors read consistent monitoring_on */
699 	smp_wmb();
700 }
701 
702 static void reset_all_monitors(void)
703 {
704 	struct rv_monitor *mon;
705 
706 	list_for_each_entry(mon, &rv_monitors_list, list) {
707 		if (mon->enabled && mon->reset)
708 			mon->reset();
709 	}
710 }
711 
712 static void turn_monitoring_on(void)
713 {
714 	WRITE_ONCE(monitoring_on, true);
715 	/* Ensures that concurrent monitors read consistent monitoring_on */
716 	smp_wmb();
717 }
718 
719 static void turn_monitoring_on_with_reset(void)
720 {
721 	lockdep_assert_held(&rv_interface_lock);
722 
723 	if (rv_monitoring_on())
724 		return;
725 
726 	/*
727 	 * Monitors might be out of sync with the system if events were not
728 	 * processed because of !rv_monitoring_on().
729 	 *
730 	 * Reset all monitors, forcing a re-sync.
731 	 */
732 	reset_all_monitors();
733 	turn_monitoring_on();
734 }
735 
736 static ssize_t monitoring_on_write_data(struct file *filp, const char __user *user_buf,
737 					size_t count, loff_t *ppos)
738 {
739 	int retval;
740 	bool val;
741 
742 	retval = kstrtobool_from_user(user_buf, count, &val);
743 	if (retval)
744 		return retval;
745 
746 	mutex_lock(&rv_interface_lock);
747 
748 	if (val)
749 		turn_monitoring_on_with_reset();
750 	else
751 		turn_monitoring_off();
752 
753 	/*
754 	 * Wait for the execution of all events to finish
755 	 * before returning to user-space.
756 	 */
757 	tracepoint_synchronize_unregister();
758 
759 	mutex_unlock(&rv_interface_lock);
760 
761 	return count;
762 }
763 
764 static const struct file_operations monitoring_on_fops = {
765 	.open   = simple_open,
766 	.write  = monitoring_on_write_data,
767 	.read   = monitoring_on_read_data,
768 };
769 
770 static void destroy_monitor_dir(struct rv_monitor *mon)
771 {
772 	rv_remove(mon->root_d);
773 }
774 
775 /**
776  * rv_register_monitor - register a rv monitor.
777  * @monitor:    The rv_monitor to be registered.
778  * @parent:     The parent of the monitor to be registered, NULL if not nested.
779  *
780  * Returns 0 if successful, error otherwise.
781  */
782 int rv_register_monitor(struct rv_monitor *monitor, struct rv_monitor *parent)
783 {
784 	struct rv_monitor *r;
785 	int retval = 0;
786 
787 	if (strlen(monitor->name) >= MAX_RV_MONITOR_NAME_SIZE) {
788 		pr_info("Monitor %s has a name longer than %d\n", monitor->name,
789 			MAX_RV_MONITOR_NAME_SIZE);
790 		return -EINVAL;
791 	}
792 
793 	mutex_lock(&rv_interface_lock);
794 
795 	list_for_each_entry(r, &rv_monitors_list, list) {
796 		if (strcmp(monitor->name, r->name) == 0) {
797 			pr_info("Monitor %s is already registered\n", monitor->name);
798 			retval = -EEXIST;
799 			goto out_unlock;
800 		}
801 	}
802 
803 	if (parent && rv_is_nested_monitor(parent)) {
804 		pr_info("Parent monitor %s is already nested, cannot nest further\n",
805 			parent->name);
806 		retval = -EINVAL;
807 		goto out_unlock;
808 	}
809 
810 	monitor->parent = parent;
811 
812 	retval = create_monitor_dir(monitor, parent);
813 	if (retval)
814 		return retval;
815 
816 	/* keep children close to the parent for easier visualisation */
817 	if (parent)
818 		list_add(&monitor->list, &parent->list);
819 	else
820 		list_add_tail(&monitor->list, &rv_monitors_list);
821 
822 out_unlock:
823 	mutex_unlock(&rv_interface_lock);
824 	return retval;
825 }
826 
827 /**
828  * rv_unregister_monitor - unregister a rv monitor.
829  * @monitor:    The rv_monitor to be unregistered.
830  *
831  * Returns 0 if successful, error otherwise.
832  */
833 int rv_unregister_monitor(struct rv_monitor *monitor)
834 {
835 	mutex_lock(&rv_interface_lock);
836 
837 	rv_disable_monitor(monitor);
838 	list_del(&monitor->list);
839 	destroy_monitor_dir(monitor);
840 
841 	mutex_unlock(&rv_interface_lock);
842 	return 0;
843 }
844 
845 int __init rv_init_interface(void)
846 {
847 	struct dentry *tmp;
848 	int retval;
849 
850 	rv_root.root_dir = rv_create_dir("rv", NULL);
851 	if (!rv_root.root_dir)
852 		goto out_err;
853 
854 	rv_root.monitors_dir = rv_create_dir("monitors", rv_root.root_dir);
855 	if (!rv_root.monitors_dir)
856 		goto out_err;
857 
858 	tmp = rv_create_file("available_monitors", RV_MODE_READ, rv_root.root_dir, NULL,
859 			     &available_monitors_ops);
860 	if (!tmp)
861 		goto out_err;
862 
863 	tmp = rv_create_file("enabled_monitors", RV_MODE_WRITE, rv_root.root_dir, NULL,
864 			     &enabled_monitors_ops);
865 	if (!tmp)
866 		goto out_err;
867 
868 	tmp = rv_create_file("monitoring_on", RV_MODE_WRITE, rv_root.root_dir, NULL,
869 			     &monitoring_on_fops);
870 	if (!tmp)
871 		goto out_err;
872 	retval = init_rv_reactors(rv_root.root_dir);
873 	if (retval)
874 		goto out_err;
875 
876 	turn_monitoring_on();
877 
878 	return 0;
879 
880 out_err:
881 	rv_remove(rv_root.root_dir);
882 	printk(KERN_ERR "RV: Error while creating the RV interface\n");
883 	return 1;
884 }
885