1 /* SPDX-License-Identifier: GPL-2.0 */
3 * Copyright (C) 2019-2022 Red Hat, Inc. Daniel Bristot de Oliveira <bristot@kernel.org>
5 * Deterministic automata (DA) monitor functions, to be used together
6 * with automata models in C generated by the dot2k tool.
8 * The dot2k tool is available at tools/verification/dot2k/
10 * For further information, see:
11 * Documentation/trace/rv/da_monitor_synthesis.rst
14 #include <rv/automata.h>
16 #include <linux/bug.h>
18 #ifdef CONFIG_RV_REACTORS
20 #define DECLARE_RV_REACTING_HELPERS(name, type) \
21 static char REACT_MSG_##name[1024]; \
23 static inline char *format_react_msg_##name(type curr_state, type event) \
25 snprintf(REACT_MSG_##name, 1024, \
26 "rv: monitor %s does not allow event %s on state %s\n", \
28 model_get_event_name_##name(event), \
29 model_get_state_name_##name(curr_state)); \
30 return REACT_MSG_##name; \
33 static void cond_react_##name(char *msg) \
35 if (rv_##name.react) \
36 rv_##name.react(msg); \
39 static bool rv_reacting_on_##name(void) \
41 return rv_reacting_on(); \
44 #else /* CONFIG_RV_REACTOR */
46 #define DECLARE_RV_REACTING_HELPERS(name, type) \
47 static inline char *format_react_msg_##name(type curr_state, type event) \
52 static void cond_react_##name(char *msg) \
57 static bool rv_reacting_on_##name(void) \
64 * Generic helpers for all types of deterministic automata monitors.
66 #define DECLARE_DA_MON_GENERIC_HELPERS(name, type) \
68 DECLARE_RV_REACTING_HELPERS(name, type) \
71 * da_monitor_reset_##name - reset a monitor and setting it to init state \
73 static inline void da_monitor_reset_##name(struct da_monitor *da_mon) \
75 da_mon->monitoring = 0; \
76 da_mon->curr_state = model_get_initial_state_##name(); \
80 * da_monitor_curr_state_##name - return the current state \
82 static inline type da_monitor_curr_state_##name(struct da_monitor *da_mon) \
84 return da_mon->curr_state; \
88 * da_monitor_set_state_##name - set the new current state \
91 da_monitor_set_state_##name(struct da_monitor *da_mon, enum states_##name state) \
93 da_mon->curr_state = state; \
97 * da_monitor_start_##name - start monitoring \
99 * The monitor will ignore all events until monitoring is set to true. This \
100 * function needs to be called to tell the monitor to start monitoring. \
102 static inline void da_monitor_start_##name(struct da_monitor *da_mon) \
104 da_mon->curr_state = model_get_initial_state_##name(); \
105 da_mon->monitoring = 1; \
109 * da_monitoring_##name - returns true if the monitor is processing events \
111 static inline bool da_monitoring_##name(struct da_monitor *da_mon) \
113 return da_mon->monitoring; \
117 * da_monitor_enabled_##name - checks if the monitor is enabled \
119 static inline bool da_monitor_enabled_##name(void) \
121 /* global switch */ \
122 if (unlikely(!rv_monitoring_on())) \
125 /* monitor enabled */ \
126 if (unlikely(!rv_##name.enabled)) \
133 * da_monitor_handling_event_##name - checks if the monitor is ready to handle events \
135 static inline bool da_monitor_handling_event_##name(struct da_monitor *da_mon) \
138 if (!da_monitor_enabled_##name()) \
141 /* monitor is actually monitoring */ \
142 if (unlikely(!da_monitoring_##name(da_mon))) \
149 * Event handler for implicit monitors. Implicit monitor is the one which the
150 * handler does not need to specify which da_monitor to manipulate. Examples
151 * of implicit monitor are the per_cpu or the global ones.
153 #define DECLARE_DA_MON_MODEL_HANDLER_IMPLICIT(name, type) \
156 da_event_##name(struct da_monitor *da_mon, enum events_##name event) \
158 type curr_state = da_monitor_curr_state_##name(da_mon); \
159 type next_state = model_get_next_state_##name(curr_state, event); \
161 if (next_state != INVALID_STATE) { \
162 da_monitor_set_state_##name(da_mon, next_state); \
164 trace_event_##name(model_get_state_name_##name(curr_state), \
165 model_get_event_name_##name(event), \
166 model_get_state_name_##name(next_state), \
167 model_is_final_state_##name(next_state)); \
172 if (rv_reacting_on_##name()) \
173 cond_react_##name(format_react_msg_##name(curr_state, event)); \
175 trace_error_##name(model_get_state_name_##name(curr_state), \
176 model_get_event_name_##name(event)); \
182 * Event handler for per_task monitors.
184 #define DECLARE_DA_MON_MODEL_HANDLER_PER_TASK(name, type) \
186 static inline bool da_event_##name(struct da_monitor *da_mon, struct task_struct *tsk, \
187 enum events_##name event) \
189 type curr_state = da_monitor_curr_state_##name(da_mon); \
190 type next_state = model_get_next_state_##name(curr_state, event); \
192 if (next_state != INVALID_STATE) { \
193 da_monitor_set_state_##name(da_mon, next_state); \
195 trace_event_##name(tsk->pid, \
196 model_get_state_name_##name(curr_state), \
197 model_get_event_name_##name(event), \
198 model_get_state_name_##name(next_state), \
199 model_is_final_state_##name(next_state)); \
204 if (rv_reacting_on_##name()) \
205 cond_react_##name(format_react_msg_##name(curr_state, event)); \
207 trace_error_##name(tsk->pid, \
208 model_get_state_name_##name(curr_state), \
209 model_get_event_name_##name(event)); \
215 * Functions to define, init and get a global monitor.
217 #define DECLARE_DA_MON_INIT_GLOBAL(name, type) \
220 * global monitor (a single variable) \
222 static struct da_monitor da_mon_##name; \
225 * da_get_monitor_##name - return the global monitor address \
227 static struct da_monitor *da_get_monitor_##name(void) \
229 return &da_mon_##name; \
233 * da_monitor_reset_all_##name - reset the single monitor \
235 static void da_monitor_reset_all_##name(void) \
237 da_monitor_reset_##name(da_get_monitor_##name()); \
241 * da_monitor_init_##name - initialize a monitor \
243 static inline int da_monitor_init_##name(void) \
245 da_monitor_reset_all_##name(); \
250 * da_monitor_destroy_##name - destroy the monitor \
252 static inline void da_monitor_destroy_##name(void) \
258 * Functions to define, init and get a per-cpu monitor.
260 #define DECLARE_DA_MON_INIT_PER_CPU(name, type) \
263 * per-cpu monitor variables \
265 static DEFINE_PER_CPU(struct da_monitor, da_mon_##name); \
268 * da_get_monitor_##name - return current CPU monitor address \
270 static struct da_monitor *da_get_monitor_##name(void) \
272 return this_cpu_ptr(&da_mon_##name); \
276 * da_monitor_reset_all_##name - reset all CPUs' monitor \
278 static void da_monitor_reset_all_##name(void) \
280 struct da_monitor *da_mon; \
282 for_each_cpu(cpu, cpu_online_mask) { \
283 da_mon = per_cpu_ptr(&da_mon_##name, cpu); \
284 da_monitor_reset_##name(da_mon); \
289 * da_monitor_init_##name - initialize all CPUs' monitor \
291 static inline int da_monitor_init_##name(void) \
293 da_monitor_reset_all_##name(); \
298 * da_monitor_destroy_##name - destroy the monitor \
300 static inline void da_monitor_destroy_##name(void) \
306 * Functions to define, init and get a per-task monitor.
308 #define DECLARE_DA_MON_INIT_PER_TASK(name, type) \
311 * The per-task monitor is stored a vector in the task struct. This variable \
312 * stores the position on the vector reserved for this monitor. \
314 static int task_mon_slot_##name = RV_PER_TASK_MONITOR_INIT; \
317 * da_get_monitor_##name - return the monitor in the allocated slot for tsk \
319 static inline struct da_monitor *da_get_monitor_##name(struct task_struct *tsk) \
321 return &tsk->rv[task_mon_slot_##name].da_mon; \
324 static void da_monitor_reset_all_##name(void) \
326 struct task_struct *g, *p; \
328 read_lock(&tasklist_lock); \
329 for_each_process_thread(g, p) \
330 da_monitor_reset_##name(da_get_monitor_##name(p)); \
331 read_unlock(&tasklist_lock); \
335 * da_monitor_init_##name - initialize the per-task monitor \
337 * Try to allocate a slot in the task's vector of monitors. If there \
338 * is an available slot, use it and reset all task's monitor. \
340 static int da_monitor_init_##name(void) \
344 slot = rv_get_task_monitor_slot(); \
345 if (slot < 0 || slot >= RV_PER_TASK_MONITOR_INIT) \
348 task_mon_slot_##name = slot; \
350 da_monitor_reset_all_##name(); \
355 * da_monitor_destroy_##name - return the allocated slot \
357 static inline void da_monitor_destroy_##name(void) \
359 if (task_mon_slot_##name == RV_PER_TASK_MONITOR_INIT) { \
360 WARN_ONCE(1, "Disabling a disabled monitor: " #name); \
363 rv_put_task_monitor_slot(task_mon_slot_##name); \
364 task_mon_slot_##name = RV_PER_TASK_MONITOR_INIT; \
369 * Handle event for implicit monitor: da_get_monitor_##name() will figure out
372 #define DECLARE_DA_MON_MONITOR_HANDLER_IMPLICIT(name, type) \
374 static inline void __da_handle_event_##name(struct da_monitor *da_mon, \
375 enum events_##name event) \
379 retval = da_event_##name(da_mon, event); \
381 da_monitor_reset_##name(da_mon); \
385 * da_handle_event_##name - handle an event \
387 static inline void da_handle_event_##name(enum events_##name event) \
389 struct da_monitor *da_mon = da_get_monitor_##name(); \
392 retval = da_monitor_handling_event_##name(da_mon); \
396 __da_handle_event_##name(da_mon, event); \
400 * da_handle_start_event_##name - start monitoring or handle event \
402 * This function is used to notify the monitor that the system is returning \
403 * to the initial state, so the monitor can start monitoring in the next event. \
406 * If the monitor already started, handle the event. \
407 * If the monitor did not start yet, start the monitor but skip the event. \
409 static inline bool da_handle_start_event_##name(enum events_##name event) \
411 struct da_monitor *da_mon; \
413 if (!da_monitor_enabled_##name()) \
416 da_mon = da_get_monitor_##name(); \
418 if (unlikely(!da_monitoring_##name(da_mon))) { \
419 da_monitor_start_##name(da_mon); \
423 __da_handle_event_##name(da_mon, event); \
429 * da_handle_start_run_event_##name - start monitoring and handle event \
431 * This function is used to notify the monitor that the system is in the \
432 * initial state, so the monitor can start monitoring and handling event. \
434 static inline bool da_handle_start_run_event_##name(enum events_##name event) \
436 struct da_monitor *da_mon; \
438 if (!da_monitor_enabled_##name()) \
441 da_mon = da_get_monitor_##name(); \
443 if (unlikely(!da_monitoring_##name(da_mon))) \
444 da_monitor_start_##name(da_mon); \
446 __da_handle_event_##name(da_mon, event); \
452 * Handle event for per task.
454 #define DECLARE_DA_MON_MONITOR_HANDLER_PER_TASK(name, type) \
457 __da_handle_event_##name(struct da_monitor *da_mon, struct task_struct *tsk, \
458 enum events_##name event) \
462 retval = da_event_##name(da_mon, tsk, event); \
464 da_monitor_reset_##name(da_mon); \
468 * da_handle_event_##name - handle an event \
471 da_handle_event_##name(struct task_struct *tsk, enum events_##name event) \
473 struct da_monitor *da_mon = da_get_monitor_##name(tsk); \
476 retval = da_monitor_handling_event_##name(da_mon); \
480 __da_handle_event_##name(da_mon, tsk, event); \
484 * da_handle_start_event_##name - start monitoring or handle event \
486 * This function is used to notify the monitor that the system is returning \
487 * to the initial state, so the monitor can start monitoring in the next event. \
490 * If the monitor already started, handle the event. \
491 * If the monitor did not start yet, start the monitor but skip the event. \
494 da_handle_start_event_##name(struct task_struct *tsk, enum events_##name event) \
496 struct da_monitor *da_mon; \
498 if (!da_monitor_enabled_##name()) \
501 da_mon = da_get_monitor_##name(tsk); \
503 if (unlikely(!da_monitoring_##name(da_mon))) { \
504 da_monitor_start_##name(da_mon); \
508 __da_handle_event_##name(da_mon, tsk, event); \
514 * Entry point for the global monitor.
516 #define DECLARE_DA_MON_GLOBAL(name, type) \
518 DECLARE_AUTOMATA_HELPERS(name, type) \
519 DECLARE_DA_MON_GENERIC_HELPERS(name, type) \
520 DECLARE_DA_MON_MODEL_HANDLER_IMPLICIT(name, type) \
521 DECLARE_DA_MON_INIT_GLOBAL(name, type) \
522 DECLARE_DA_MON_MONITOR_HANDLER_IMPLICIT(name, type)
525 * Entry point for the per-cpu monitor.
527 #define DECLARE_DA_MON_PER_CPU(name, type) \
529 DECLARE_AUTOMATA_HELPERS(name, type) \
530 DECLARE_DA_MON_GENERIC_HELPERS(name, type) \
531 DECLARE_DA_MON_MODEL_HANDLER_IMPLICIT(name, type) \
532 DECLARE_DA_MON_INIT_PER_CPU(name, type) \
533 DECLARE_DA_MON_MONITOR_HANDLER_IMPLICIT(name, type)
536 * Entry point for the per-task monitor.
538 #define DECLARE_DA_MON_PER_TASK(name, type) \
540 DECLARE_AUTOMATA_HELPERS(name, type) \
541 DECLARE_DA_MON_GENERIC_HELPERS(name, type) \
542 DECLARE_DA_MON_MODEL_HANDLER_PER_TASK(name, type) \
543 DECLARE_DA_MON_INIT_PER_TASK(name, type) \
544 DECLARE_DA_MON_MONITOR_HANDLER_PER_TASK(name, type)