xref: /linux/kernel/trace/rv/rv.c (revision d6f38c12396397e48092ad9e8a4d7be4de51b942)
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 
get_monitors_root(void)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 
rv_get_task_monitor_slot(void)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 
rv_put_task_monitor_slot(int slot)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  */
rv_is_nested_monitor(struct rv_monitor * mon)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  */
rv_is_container_monitor(struct rv_monitor * mon)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  */
monitor_enable_read_data(struct file * filp,char __user * user_buf,size_t count,loff_t * ppos)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  */
__rv_disable_monitor(struct rv_monitor * mon,bool sync)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 
rv_disable_single(struct rv_monitor * mon)276 static void rv_disable_single(struct rv_monitor *mon)
277 {
278 	__rv_disable_monitor(mon, true);
279 }
280 
rv_enable_single(struct rv_monitor * mon)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 
rv_disable_container(struct rv_monitor * mon)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 
rv_enable_container(struct rv_monitor * mon)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  */
rv_disable_monitor(struct rv_monitor * mon)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  */
rv_enable_monitor(struct rv_monitor * mon)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  */
monitor_enable_write_data(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)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  */
monitor_desc_read_data(struct file * filp,char __user * user_buf,size_t count,loff_t * ppos)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  */
create_monitor_dir(struct rv_monitor * mon,struct rv_monitor * parent)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  */
monitors_show(struct seq_file * m,void * p)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  */
monitors_stop(struct seq_file * m,void * p)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  */
available_monitors_start(struct seq_file * m,loff_t * pos)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 
available_monitors_next(struct seq_file * m,void * p,loff_t * pos)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  */
enabled_monitors_next(struct seq_file * m,void * p,loff_t * pos)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 
enabled_monitors_start(struct seq_file * m,loff_t * pos)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  */
available_monitors_open(struct inode * inode,struct file * file)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  */
disable_all_monitors(void)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 
enabled_monitors_open(struct inode * inode,struct file * file)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 
enabled_monitors_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)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  */
rv_monitoring_on(void)675 bool rv_monitoring_on(void)
676 {
677 	return READ_ONCE(monitoring_on);
678 }
679 
680 /*
681  * monitoring_on general switcher.
682  */
monitoring_on_read_data(struct file * filp,char __user * user_buf,size_t count,loff_t * ppos)683 static ssize_t monitoring_on_read_data(struct file *filp, char __user *user_buf,
684 				       size_t count, loff_t *ppos)
685 {
686 	const char *buff;
687 
688 	buff = rv_monitoring_on() ? "1\n" : "0\n";
689 
690 	return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1);
691 }
692 
turn_monitoring_off(void)693 static void turn_monitoring_off(void)
694 {
695 	WRITE_ONCE(monitoring_on, false);
696 }
697 
reset_all_monitors(void)698 static void reset_all_monitors(void)
699 {
700 	struct rv_monitor *mon;
701 
702 	list_for_each_entry(mon, &rv_monitors_list, list) {
703 		if (mon->enabled && mon->reset)
704 			mon->reset();
705 	}
706 }
707 
turn_monitoring_on(void)708 static void turn_monitoring_on(void)
709 {
710 	WRITE_ONCE(monitoring_on, true);
711 }
712 
turn_monitoring_on_with_reset(void)713 static void turn_monitoring_on_with_reset(void)
714 {
715 	lockdep_assert_held(&rv_interface_lock);
716 
717 	if (rv_monitoring_on())
718 		return;
719 
720 	/*
721 	 * Monitors might be out of sync with the system if events were not
722 	 * processed because of !rv_monitoring_on().
723 	 *
724 	 * Reset all monitors, forcing a re-sync.
725 	 */
726 	reset_all_monitors();
727 	turn_monitoring_on();
728 }
729 
monitoring_on_write_data(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)730 static ssize_t monitoring_on_write_data(struct file *filp, const char __user *user_buf,
731 					size_t count, loff_t *ppos)
732 {
733 	int retval;
734 	bool val;
735 
736 	retval = kstrtobool_from_user(user_buf, count, &val);
737 	if (retval)
738 		return retval;
739 
740 	mutex_lock(&rv_interface_lock);
741 
742 	if (val)
743 		turn_monitoring_on_with_reset();
744 	else
745 		turn_monitoring_off();
746 
747 	/*
748 	 * Wait for the execution of all events to finish
749 	 * before returning to user-space.
750 	 */
751 	tracepoint_synchronize_unregister();
752 
753 	mutex_unlock(&rv_interface_lock);
754 
755 	return count;
756 }
757 
758 static const struct file_operations monitoring_on_fops = {
759 	.open   = simple_open,
760 	.write  = monitoring_on_write_data,
761 	.read   = monitoring_on_read_data,
762 };
763 
destroy_monitor_dir(struct rv_monitor * mon)764 static void destroy_monitor_dir(struct rv_monitor *mon)
765 {
766 	rv_remove(mon->root_d);
767 }
768 
769 /**
770  * rv_register_monitor - register a rv monitor.
771  * @monitor:    The rv_monitor to be registered.
772  * @parent:     The parent of the monitor to be registered, NULL if not nested.
773  *
774  * Returns 0 if successful, error otherwise.
775  */
rv_register_monitor(struct rv_monitor * monitor,struct rv_monitor * parent)776 int rv_register_monitor(struct rv_monitor *monitor, struct rv_monitor *parent)
777 {
778 	struct rv_monitor *r;
779 	int retval = 0;
780 
781 	if (strlen(monitor->name) >= MAX_RV_MONITOR_NAME_SIZE) {
782 		pr_info("Monitor %s has a name longer than %d\n", monitor->name,
783 			MAX_RV_MONITOR_NAME_SIZE);
784 		return -EINVAL;
785 	}
786 
787 	mutex_lock(&rv_interface_lock);
788 
789 	list_for_each_entry(r, &rv_monitors_list, list) {
790 		if (strcmp(monitor->name, r->name) == 0) {
791 			pr_info("Monitor %s is already registered\n", monitor->name);
792 			retval = -EEXIST;
793 			goto out_unlock;
794 		}
795 	}
796 
797 	if (parent && rv_is_nested_monitor(parent)) {
798 		pr_info("Parent monitor %s is already nested, cannot nest further\n",
799 			parent->name);
800 		retval = -EINVAL;
801 		goto out_unlock;
802 	}
803 
804 	monitor->parent = parent;
805 
806 	retval = create_monitor_dir(monitor, parent);
807 	if (retval)
808 		return retval;
809 
810 	/* keep children close to the parent for easier visualisation */
811 	if (parent)
812 		list_add(&monitor->list, &parent->list);
813 	else
814 		list_add_tail(&monitor->list, &rv_monitors_list);
815 
816 out_unlock:
817 	mutex_unlock(&rv_interface_lock);
818 	return retval;
819 }
820 
821 /**
822  * rv_unregister_monitor - unregister a rv monitor.
823  * @monitor:    The rv_monitor to be unregistered.
824  *
825  * Returns 0 if successful, error otherwise.
826  */
rv_unregister_monitor(struct rv_monitor * monitor)827 int rv_unregister_monitor(struct rv_monitor *monitor)
828 {
829 	mutex_lock(&rv_interface_lock);
830 
831 	rv_disable_monitor(monitor);
832 	list_del(&monitor->list);
833 	destroy_monitor_dir(monitor);
834 
835 	mutex_unlock(&rv_interface_lock);
836 	return 0;
837 }
838 
rv_init_interface(void)839 int __init rv_init_interface(void)
840 {
841 	struct dentry *tmp;
842 	int retval;
843 
844 	rv_root.root_dir = rv_create_dir("rv", NULL);
845 	if (!rv_root.root_dir)
846 		goto out_err;
847 
848 	rv_root.monitors_dir = rv_create_dir("monitors", rv_root.root_dir);
849 	if (!rv_root.monitors_dir)
850 		goto out_err;
851 
852 	tmp = rv_create_file("available_monitors", RV_MODE_READ, rv_root.root_dir, NULL,
853 			     &available_monitors_ops);
854 	if (!tmp)
855 		goto out_err;
856 
857 	tmp = rv_create_file("enabled_monitors", RV_MODE_WRITE, rv_root.root_dir, NULL,
858 			     &enabled_monitors_ops);
859 	if (!tmp)
860 		goto out_err;
861 
862 	tmp = rv_create_file("monitoring_on", RV_MODE_WRITE, rv_root.root_dir, NULL,
863 			     &monitoring_on_fops);
864 	if (!tmp)
865 		goto out_err;
866 	retval = init_rv_reactors(rv_root.root_dir);
867 	if (retval)
868 		goto out_err;
869 
870 	turn_monitoring_on();
871 
872 	return 0;
873 
874 out_err:
875 	rv_remove(rv_root.root_dir);
876 	printk(KERN_ERR "RV: Error while creating the RV interface\n");
877 	return 1;
878 }
879