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 guard(mutex)(&rv_interface_lock);
379
380 if (val)
381 retval = rv_enable_monitor(mon);
382 else
383 retval = rv_disable_monitor(mon);
384
385 return retval ? : count;
386 }
387
388 static const struct file_operations interface_enable_fops = {
389 .open = simple_open,
390 .write = monitor_enable_write_data,
391 .read = monitor_enable_read_data,
392 };
393
394 /*
395 * Interface to read monitors description.
396 */
monitor_desc_read_data(struct file * filp,char __user * user_buf,size_t count,loff_t * ppos)397 static ssize_t monitor_desc_read_data(struct file *filp, char __user *user_buf, size_t count,
398 loff_t *ppos)
399 {
400 struct rv_monitor *mon = filp->private_data;
401 char buff[256];
402
403 memset(buff, 0, sizeof(buff));
404
405 snprintf(buff, sizeof(buff), "%s\n", mon->description);
406
407 return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1);
408 }
409
410 static const struct file_operations interface_desc_fops = {
411 .open = simple_open,
412 .read = monitor_desc_read_data,
413 };
414
415 /*
416 * During the registration of a monitor, this function creates
417 * the monitor dir, where the specific options of the monitor
418 * are exposed.
419 */
create_monitor_dir(struct rv_monitor * mon,struct rv_monitor * parent)420 static int create_monitor_dir(struct rv_monitor *mon, struct rv_monitor *parent)
421 {
422 struct dentry *root = parent ? parent->root_d : get_monitors_root();
423 struct dentry *dir __free(rv_remove) = rv_create_dir(mon->name, root);
424 struct dentry *tmp;
425 int retval;
426
427 if (!dir)
428 return -ENOMEM;
429
430 tmp = rv_create_file("enable", RV_MODE_WRITE, dir, mon, &interface_enable_fops);
431 if (!tmp)
432 return -ENOMEM;
433
434 tmp = rv_create_file("desc", RV_MODE_READ, dir, mon, &interface_desc_fops);
435 if (!tmp)
436 return -ENOMEM;
437
438 retval = reactor_populate_monitor(mon, dir);
439 if (retval)
440 return retval;
441
442 mon->root_d = no_free_ptr(dir);
443 return 0;
444 }
445
446 /*
447 * Available/Enable monitor shared seq functions.
448 */
monitors_show(struct seq_file * m,void * p)449 static int monitors_show(struct seq_file *m, void *p)
450 {
451 struct rv_monitor *mon = container_of(p, struct rv_monitor, list);
452
453 if (mon->parent)
454 seq_printf(m, "%s:%s\n", mon->parent->name, mon->name);
455 else
456 seq_printf(m, "%s\n", mon->name);
457 return 0;
458 }
459
460 /*
461 * Used by the seq file operations at the end of a read
462 * operation.
463 */
monitors_stop(struct seq_file * m,void * p)464 static void monitors_stop(struct seq_file *m, void *p)
465 {
466 mutex_unlock(&rv_interface_lock);
467 }
468
469 /*
470 * Available monitor seq functions.
471 */
available_monitors_start(struct seq_file * m,loff_t * pos)472 static void *available_monitors_start(struct seq_file *m, loff_t *pos)
473 {
474 mutex_lock(&rv_interface_lock);
475 return seq_list_start(&rv_monitors_list, *pos);
476 }
477
available_monitors_next(struct seq_file * m,void * p,loff_t * pos)478 static void *available_monitors_next(struct seq_file *m, void *p, loff_t *pos)
479 {
480 return seq_list_next(p, &rv_monitors_list, pos);
481 }
482
483 /*
484 * Enable monitor seq functions.
485 */
enabled_monitors_next(struct seq_file * m,void * p,loff_t * pos)486 static void *enabled_monitors_next(struct seq_file *m, void *p, loff_t *pos)
487 {
488 struct rv_monitor *mon = container_of(p, struct rv_monitor, list);
489
490 (*pos)++;
491
492 list_for_each_entry_continue(mon, &rv_monitors_list, list) {
493 if (mon->enabled)
494 return &mon->list;
495 }
496
497 return NULL;
498 }
499
enabled_monitors_start(struct seq_file * m,loff_t * pos)500 static void *enabled_monitors_start(struct seq_file *m, loff_t *pos)
501 {
502 struct list_head *head;
503 loff_t l;
504
505 mutex_lock(&rv_interface_lock);
506
507 if (list_empty(&rv_monitors_list))
508 return NULL;
509
510 head = &rv_monitors_list;
511
512 for (l = 0; l <= *pos; ) {
513 head = enabled_monitors_next(m, head, &l);
514 if (!head)
515 break;
516 }
517
518 return head;
519 }
520
521 /*
522 * available/enabled monitors seq definition.
523 */
524 static const struct seq_operations available_monitors_seq_ops = {
525 .start = available_monitors_start,
526 .next = available_monitors_next,
527 .stop = monitors_stop,
528 .show = monitors_show
529 };
530
531 static const struct seq_operations enabled_monitors_seq_ops = {
532 .start = enabled_monitors_start,
533 .next = enabled_monitors_next,
534 .stop = monitors_stop,
535 .show = monitors_show
536 };
537
538 /*
539 * available_monitors interface.
540 */
available_monitors_open(struct inode * inode,struct file * file)541 static int available_monitors_open(struct inode *inode, struct file *file)
542 {
543 return seq_open(file, &available_monitors_seq_ops);
544 };
545
546 static const struct file_operations available_monitors_ops = {
547 .open = available_monitors_open,
548 .read = seq_read,
549 .llseek = seq_lseek,
550 .release = seq_release
551 };
552
553 /*
554 * enabled_monitors interface.
555 */
disable_all_monitors(void)556 static void disable_all_monitors(void)
557 {
558 struct rv_monitor *mon;
559 int enabled = 0;
560
561 guard(mutex)(&rv_interface_lock);
562
563 list_for_each_entry(mon, &rv_monitors_list, list)
564 enabled += __rv_disable_monitor(mon, false);
565
566 if (enabled) {
567 /*
568 * Wait for the execution of all events to finish.
569 * Otherwise, the data used by the monitor could
570 * be inconsistent. i.e., if the monitor is re-enabled.
571 */
572 tracepoint_synchronize_unregister();
573 }
574 }
575
enabled_monitors_open(struct inode * inode,struct file * file)576 static int enabled_monitors_open(struct inode *inode, struct file *file)
577 {
578 if ((file->f_mode & FMODE_WRITE) && (file->f_flags & O_TRUNC))
579 disable_all_monitors();
580
581 return seq_open(file, &enabled_monitors_seq_ops);
582 };
583
enabled_monitors_write(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)584 static ssize_t enabled_monitors_write(struct file *filp, const char __user *user_buf,
585 size_t count, loff_t *ppos)
586 {
587 char buff[MAX_RV_MONITOR_NAME_SIZE + 2];
588 struct rv_monitor *mon;
589 int retval = -EINVAL;
590 bool enable = true;
591 char *ptr, *tmp;
592 int len;
593
594 if (count < 1 || count > MAX_RV_MONITOR_NAME_SIZE + 1)
595 return -EINVAL;
596
597 memset(buff, 0, sizeof(buff));
598
599 retval = simple_write_to_buffer(buff, sizeof(buff) - 1, ppos, user_buf, count);
600 if (retval < 0)
601 return -EFAULT;
602
603 ptr = strim(buff);
604
605 if (ptr[0] == '!') {
606 enable = false;
607 ptr++;
608 }
609
610 len = strlen(ptr);
611 if (!len)
612 return count;
613
614 guard(mutex)(&rv_interface_lock);
615
616 retval = -EINVAL;
617
618 /* we support 1 nesting level, trim the parent */
619 tmp = strstr(ptr, ":");
620 if (tmp)
621 ptr = tmp+1;
622
623 list_for_each_entry(mon, &rv_monitors_list, list) {
624 if (strcmp(ptr, mon->name) != 0)
625 continue;
626
627 /*
628 * Monitor found!
629 */
630 if (enable)
631 retval = rv_enable_monitor(mon);
632 else
633 retval = rv_disable_monitor(mon);
634
635 if (retval)
636 return retval;
637 return count;
638 }
639
640 return retval;
641 }
642
643 static const struct file_operations enabled_monitors_ops = {
644 .open = enabled_monitors_open,
645 .read = seq_read,
646 .write = enabled_monitors_write,
647 .llseek = seq_lseek,
648 .release = seq_release,
649 };
650
651 /*
652 * Monitoring on global switcher!
653 */
654 static bool __read_mostly monitoring_on;
655
656 /**
657 * rv_monitoring_on - checks if monitoring is on
658 *
659 * Returns 1 if on, 0 otherwise.
660 */
rv_monitoring_on(void)661 bool rv_monitoring_on(void)
662 {
663 return READ_ONCE(monitoring_on);
664 }
665
666 /*
667 * monitoring_on general switcher.
668 */
monitoring_on_read_data(struct file * filp,char __user * user_buf,size_t count,loff_t * ppos)669 static ssize_t monitoring_on_read_data(struct file *filp, char __user *user_buf,
670 size_t count, loff_t *ppos)
671 {
672 const char *buff;
673
674 buff = rv_monitoring_on() ? "1\n" : "0\n";
675
676 return simple_read_from_buffer(user_buf, count, ppos, buff, strlen(buff) + 1);
677 }
678
turn_monitoring_off(void)679 static void turn_monitoring_off(void)
680 {
681 WRITE_ONCE(monitoring_on, false);
682 }
683
reset_all_monitors(void)684 static void reset_all_monitors(void)
685 {
686 struct rv_monitor *mon;
687
688 list_for_each_entry(mon, &rv_monitors_list, list) {
689 if (mon->enabled && mon->reset)
690 mon->reset();
691 }
692 }
693
turn_monitoring_on(void)694 static void turn_monitoring_on(void)
695 {
696 WRITE_ONCE(monitoring_on, true);
697 }
698
turn_monitoring_on_with_reset(void)699 static void turn_monitoring_on_with_reset(void)
700 {
701 lockdep_assert_held(&rv_interface_lock);
702
703 if (rv_monitoring_on())
704 return;
705
706 /*
707 * Monitors might be out of sync with the system if events were not
708 * processed because of !rv_monitoring_on().
709 *
710 * Reset all monitors, forcing a re-sync.
711 */
712 reset_all_monitors();
713 turn_monitoring_on();
714 }
715
monitoring_on_write_data(struct file * filp,const char __user * user_buf,size_t count,loff_t * ppos)716 static ssize_t monitoring_on_write_data(struct file *filp, const char __user *user_buf,
717 size_t count, loff_t *ppos)
718 {
719 int retval;
720 bool val;
721
722 retval = kstrtobool_from_user(user_buf, count, &val);
723 if (retval)
724 return retval;
725
726 guard(mutex)(&rv_interface_lock);
727
728 if (val)
729 turn_monitoring_on_with_reset();
730 else
731 turn_monitoring_off();
732
733 /*
734 * Wait for the execution of all events to finish
735 * before returning to user-space.
736 */
737 tracepoint_synchronize_unregister();
738
739 return count;
740 }
741
742 static const struct file_operations monitoring_on_fops = {
743 .open = simple_open,
744 .write = monitoring_on_write_data,
745 .read = monitoring_on_read_data,
746 };
747
destroy_monitor_dir(struct rv_monitor * mon)748 static void destroy_monitor_dir(struct rv_monitor *mon)
749 {
750 rv_remove(mon->root_d);
751 }
752
753 /**
754 * rv_register_monitor - register a rv monitor.
755 * @monitor: The rv_monitor to be registered.
756 * @parent: The parent of the monitor to be registered, NULL if not nested.
757 *
758 * Returns 0 if successful, error otherwise.
759 */
rv_register_monitor(struct rv_monitor * monitor,struct rv_monitor * parent)760 int rv_register_monitor(struct rv_monitor *monitor, struct rv_monitor *parent)
761 {
762 struct rv_monitor *r;
763 int retval = 0;
764
765 if (strlen(monitor->name) >= MAX_RV_MONITOR_NAME_SIZE) {
766 pr_info("Monitor %s has a name longer than %d\n", monitor->name,
767 MAX_RV_MONITOR_NAME_SIZE);
768 return -EINVAL;
769 }
770
771 guard(mutex)(&rv_interface_lock);
772
773 list_for_each_entry(r, &rv_monitors_list, list) {
774 if (strcmp(monitor->name, r->name) == 0) {
775 pr_info("Monitor %s is already registered\n", monitor->name);
776 return -EEXIST;
777 }
778 }
779
780 if (parent && rv_is_nested_monitor(parent)) {
781 pr_info("Parent monitor %s is already nested, cannot nest further\n",
782 parent->name);
783 return -EINVAL;
784 }
785
786 monitor->parent = parent;
787
788 retval = create_monitor_dir(monitor, parent);
789 if (retval)
790 return retval;
791
792 /* keep children close to the parent for easier visualisation */
793 if (parent)
794 list_add(&monitor->list, &parent->list);
795 else
796 list_add_tail(&monitor->list, &rv_monitors_list);
797
798 return 0;
799 }
800
801 /**
802 * rv_unregister_monitor - unregister a rv monitor.
803 * @monitor: The rv_monitor to be unregistered.
804 *
805 * Returns 0 if successful, error otherwise.
806 */
rv_unregister_monitor(struct rv_monitor * monitor)807 int rv_unregister_monitor(struct rv_monitor *monitor)
808 {
809 guard(mutex)(&rv_interface_lock);
810
811 rv_disable_monitor(monitor);
812 list_del(&monitor->list);
813 destroy_monitor_dir(monitor);
814
815 return 0;
816 }
817
rv_init_interface(void)818 int __init rv_init_interface(void)
819 {
820 struct dentry *tmp;
821 int retval;
822 struct dentry *root_dir __free(rv_remove) = rv_create_dir("rv", NULL);
823
824 if (!root_dir)
825 return 1;
826
827 rv_root.monitors_dir = rv_create_dir("monitors", root_dir);
828 if (!rv_root.monitors_dir)
829 return 1;
830
831 tmp = rv_create_file("available_monitors", RV_MODE_READ, root_dir, NULL,
832 &available_monitors_ops);
833 if (!tmp)
834 return 1;
835
836 tmp = rv_create_file("enabled_monitors", RV_MODE_WRITE, root_dir, NULL,
837 &enabled_monitors_ops);
838 if (!tmp)
839 return 1;
840
841 tmp = rv_create_file("monitoring_on", RV_MODE_WRITE, root_dir, NULL,
842 &monitoring_on_fops);
843 if (!tmp)
844 return 1;
845 retval = init_rv_reactors(root_dir);
846 if (retval)
847 return 1;
848
849 turn_monitoring_on();
850
851 rv_root.root_dir = no_free_ptr(root_dir);
852
853 return 0;
854 }
855