1 // SPDX-License-Identifier: GPL-2.0
2 #include <linux/ftrace.h>
3 #include <linux/tracepoint.h>
4 #include <linux/kernel.h>
5 #include <linux/module.h>
6 #include <linux/init.h>
7 #include <linux/rv.h>
8 #include <rv/instrumentation.h>
9 #include <rv/da_monitor.h>
10 
11 #define MODULE_NAME "wwnr"
12 
13 #include <trace/events/rv.h>
14 #include <trace/events/sched.h>
15 
16 #include "wwnr.h"
17 
18 static struct rv_monitor rv_wwnr;
19 DECLARE_DA_MON_PER_TASK(wwnr, unsigned char);
20 
handle_switch(void * data,bool preempt,struct task_struct * p,struct task_struct * n,unsigned int prev_state)21 static void handle_switch(void *data, bool preempt, struct task_struct *p,
22 			  struct task_struct *n, unsigned int prev_state)
23 {
24 	/* start monitoring only after the first suspension */
25 	if (prev_state == TASK_INTERRUPTIBLE)
26 		da_handle_start_event_wwnr(p, switch_out_wwnr);
27 	else
28 		da_handle_event_wwnr(p, switch_out_wwnr);
29 
30 	da_handle_event_wwnr(n, switch_in_wwnr);
31 }
32 
handle_wakeup(void * data,struct task_struct * p)33 static void handle_wakeup(void *data, struct task_struct *p)
34 {
35 	da_handle_event_wwnr(p, wakeup_wwnr);
36 }
37 
enable_wwnr(void)38 static int enable_wwnr(void)
39 {
40 	int retval;
41 
42 	retval = da_monitor_init_wwnr();
43 	if (retval)
44 		return retval;
45 
46 	rv_attach_trace_probe("wwnr", sched_switch, handle_switch);
47 	rv_attach_trace_probe("wwnr", sched_wakeup, handle_wakeup);
48 
49 	return 0;
50 }
51 
disable_wwnr(void)52 static void disable_wwnr(void)
53 {
54 	rv_wwnr.enabled = 0;
55 
56 	rv_detach_trace_probe("wwnr", sched_switch, handle_switch);
57 	rv_detach_trace_probe("wwnr", sched_wakeup, handle_wakeup);
58 
59 	da_monitor_destroy_wwnr();
60 }
61 
62 static struct rv_monitor rv_wwnr = {
63 	.name = "wwnr",
64 	.description = "wakeup while not running per-task testing model.",
65 	.enable = enable_wwnr,
66 	.disable = disable_wwnr,
67 	.reset = da_monitor_reset_all_wwnr,
68 	.enabled = 0,
69 };
70 
register_wwnr(void)71 static int __init register_wwnr(void)
72 {
73 	rv_register_monitor(&rv_wwnr);
74 	return 0;
75 }
76 
unregister_wwnr(void)77 static void __exit unregister_wwnr(void)
78 {
79 	rv_unregister_monitor(&rv_wwnr);
80 }
81 
82 module_init(register_wwnr);
83 module_exit(unregister_wwnr);
84 
85 MODULE_LICENSE("GPL");
86 MODULE_AUTHOR("Daniel Bristot de Oliveira <bristot@kernel.org>");
87 MODULE_DESCRIPTION("wwnr: wakeup while not running monitor");
88