From 30984ccf31b7f0678fad05a22e06bb9f6014c1ce Mon Sep 17 00:00:00 2001 From: Gabriele Monaco Date: Wed, 26 Nov 2025 11:42:32 +0100 Subject: rv: Refactor da_monitor to minimise macros The da_monitor helper functions are generated from macros of the type: DECLARE_DA_FUNCTION(name, type) \ static void da_func_x_##name(type arg) {} \ static void da_func_y_##name(type arg) {} \ This is good to minimise code duplication but the long macros made of skipped end of lines is rather hard to parse. Since functions are static, the advantage of naming them differently for each monitor is minimal. Refactor the da_monitor.h file to minimise macros, instead of declaring functions from macros, we simply declare them with the same name for all monitors (e.g. da_func_x) and for any remaining reference to the monitor name (e.g. tracepoints, enums, global variables) we use the CONCATENATE macro. In this way the file is much easier to maintain while keeping the same generality. Functions depending on the monitor types are now conditionally compiled according to the value of RV_MON_TYPE, which must be defined in the monitor source. The monitor type can be specified as in the original implementation, although it's best to keep the default implementation (unsigned char) as not all parts of code support larger data types, and likely there's no need. We keep the empty macro definitions to ease review of this change with diff tools, but cleanup is required. Also adapt existing monitors to keep the build working. Reviewed-by: Nam Cao Link: https://lore.kernel.org/r/20251126104241.291258-2-gmonaco@redhat.com Signed-off-by: Gabriele Monaco --- include/linux/rv.h | 4 + include/rv/automata.h | 132 +++---- include/rv/da_monitor.h | 898 ++++++++++++++++++++++++------------------------ 3 files changed, 520 insertions(+), 514 deletions(-) (limited to 'include') diff --git a/include/linux/rv.h b/include/linux/rv.h index 92fd467547e7..58774eb3aecf 100644 --- a/include/linux/rv.h +++ b/include/linux/rv.h @@ -10,6 +10,10 @@ #define MAX_DA_NAME_LEN 32 #define MAX_DA_RETRY_RACING_EVENTS 3 +#define RV_MON_GLOBAL 0 +#define RV_MON_PER_CPU 1 +#define RV_MON_PER_TASK 2 + #ifdef CONFIG_RV #include #include diff --git a/include/rv/automata.h b/include/rv/automata.h index eb9e636809a0..5b5d2e94c034 100644 --- a/include/rv/automata.h +++ b/include/rv/automata.h @@ -6,6 +6,20 @@ * models in C generated by the dot2k tool. */ +#ifndef MONITOR_NAME +#error "MONITOR_NAME macro is not defined. Did you include $(MODEL_NAME).h generated by rvgen?" +#endif + +#ifndef type +#define type unsigned char +#endif + +#define RV_AUTOMATON_NAME CONCATENATE(automaton_, MONITOR_NAME) +#define EVENT_MAX CONCATENATE(event_max_, MONITOR_NAME) +#define STATE_MAX CONCATENATE(state_max_, MONITOR_NAME) +#define events CONCATENATE(events_, MONITOR_NAME) +#define states CONCATENATE(states_, MONITOR_NAME) + /* * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata * @@ -13,63 +27,63 @@ * as suffix for the functions and data. These functions will handle automaton * with data type 'type'. */ -#define DECLARE_AUTOMATA_HELPERS(name, type) \ - \ -/* \ - * model_get_state_name_##name - return the (string) name of the given state \ - */ \ -static char *model_get_state_name_##name(enum states_##name state) \ -{ \ - if ((state < 0) || (state >= state_max_##name)) \ - return "INVALID"; \ - \ - return automaton_##name.state_names[state]; \ -} \ - \ -/* \ - * model_get_event_name_##name - return the (string) name of the given event \ - */ \ -static char *model_get_event_name_##name(enum events_##name event) \ -{ \ - if ((event < 0) || (event >= event_max_##name)) \ - return "INVALID"; \ - \ - return automaton_##name.event_names[event]; \ -} \ - \ -/* \ - * model_get_initial_state_##name - return the automaton's initial state \ - */ \ -static inline type model_get_initial_state_##name(void) \ -{ \ - return automaton_##name.initial_state; \ -} \ - \ -/* \ - * model_get_next_state_##name - process an automaton event occurrence \ - * \ - * Given the current state (curr_state) and the event (event), returns \ - * the next state, or INVALID_STATE in case of error. \ - */ \ -static inline type model_get_next_state_##name(enum states_##name curr_state, \ - enum events_##name event) \ -{ \ - if ((curr_state < 0) || (curr_state >= state_max_##name)) \ - return INVALID_STATE; \ - \ - if ((event < 0) || (event >= event_max_##name)) \ - return INVALID_STATE; \ - \ - return automaton_##name.function[curr_state][event]; \ -} \ - \ -/* \ - * model_is_final_state_##name - check if the given state is a final state \ - */ \ -static inline bool model_is_final_state_##name(enum states_##name state) \ -{ \ - if ((state < 0) || (state >= state_max_##name)) \ - return 0; \ - \ - return automaton_##name.final_states[state]; \ +#define DECLARE_AUTOMATA_HELPERS(name, type) + +/* + * model_get_state_name - return the (string) name of the given state + */ +static char *model_get_state_name(enum states state) +{ + if ((state < 0) || (state >= STATE_MAX)) + return "INVALID"; + + return RV_AUTOMATON_NAME.state_names[state]; +} + +/* + * model_get_event_name - return the (string) name of the given event + */ +static char *model_get_event_name(enum events event) +{ + if ((event < 0) || (event >= EVENT_MAX)) + return "INVALID"; + + return RV_AUTOMATON_NAME.event_names[event]; +} + +/* + * model_get_initial_state - return the automaton's initial state + */ +static inline type model_get_initial_state(void) +{ + return RV_AUTOMATON_NAME.initial_state; +} + +/* + * model_get_next_state - process an automaton event occurrence + * + * Given the current state (curr_state) and the event (event), returns + * the next state, or INVALID_STATE in case of error. + */ +static inline type model_get_next_state(enum states curr_state, + enum events event) +{ + if ((curr_state < 0) || (curr_state >= STATE_MAX)) + return INVALID_STATE; + + if ((event < 0) || (event >= EVENT_MAX)) + return INVALID_STATE; + + return RV_AUTOMATON_NAME.function[curr_state][event]; +} + +/* + * model_is_final_state - check if the given state is a final state + */ +static inline bool model_is_final_state(enum states state) +{ + if ((state < 0) || (state >= STATE_MAX)) + return 0; + + return RV_AUTOMATON_NAME.final_states[state]; } diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h index 0cef64366538..a4555dafa8d0 100644 --- a/include/rv/da_monitor.h +++ b/include/rv/da_monitor.h @@ -13,82 +13,85 @@ #include #include +#include #include #include +static struct rv_monitor rv_this; + /* * Generic helpers for all types of deterministic automata monitors. */ -#define DECLARE_DA_MON_GENERIC_HELPERS(name, type) \ - \ -static void react_##name(type curr_state, type event) \ -{ \ - rv_react(&rv_##name, \ - "rv: monitor %s does not allow event %s on state %s\n", \ - #name, \ - model_get_event_name_##name(event), \ - model_get_state_name_##name(curr_state)); \ -} \ - \ -/* \ - * da_monitor_reset_##name - reset a monitor and setting it to init state \ - */ \ -static inline void da_monitor_reset_##name(struct da_monitor *da_mon) \ -{ \ - da_mon->monitoring = 0; \ - da_mon->curr_state = model_get_initial_state_##name(); \ -} \ - \ -/* \ - * da_monitor_start_##name - start monitoring \ - * \ - * The monitor will ignore all events until monitoring is set to true. This \ - * function needs to be called to tell the monitor to start monitoring. \ - */ \ -static inline void da_monitor_start_##name(struct da_monitor *da_mon) \ -{ \ - da_mon->curr_state = model_get_initial_state_##name(); \ - da_mon->monitoring = 1; \ -} \ - \ -/* \ - * da_monitoring_##name - returns true if the monitor is processing events \ - */ \ -static inline bool da_monitoring_##name(struct da_monitor *da_mon) \ -{ \ - return da_mon->monitoring; \ -} \ - \ -/* \ - * da_monitor_enabled_##name - checks if the monitor is enabled \ - */ \ -static inline bool da_monitor_enabled_##name(void) \ -{ \ - /* global switch */ \ - if (unlikely(!rv_monitoring_on())) \ - return 0; \ - \ - /* monitor enabled */ \ - if (unlikely(!rv_##name.enabled)) \ - return 0; \ - \ - return 1; \ -} \ - \ -/* \ - * da_monitor_handling_event_##name - checks if the monitor is ready to handle events \ - */ \ -static inline bool da_monitor_handling_event_##name(struct da_monitor *da_mon) \ -{ \ - \ - if (!da_monitor_enabled_##name()) \ - return 0; \ - \ - /* monitor is actually monitoring */ \ - if (unlikely(!da_monitoring_##name(da_mon))) \ - return 0; \ - \ - return 1; \ +#define DECLARE_DA_MON_GENERIC_HELPERS(name, type) + +static void react(type curr_state, type event) +{ + rv_react(&rv_this, + "rv: monitor %s does not allow event %s on state %s\n", + __stringify(MONITOR_NAME), + model_get_event_name(event), + model_get_state_name(curr_state)); +} + +/* + * da_monitor_reset - reset a monitor and setting it to init state + */ +static inline void da_monitor_reset(struct da_monitor *da_mon) +{ + da_mon->monitoring = 0; + da_mon->curr_state = model_get_initial_state(); +} + +/* + * da_monitor_start - start monitoring + * + * The monitor will ignore all events until monitoring is set to true. This + * function needs to be called to tell the monitor to start monitoring. + */ +static inline void da_monitor_start(struct da_monitor *da_mon) +{ + da_mon->curr_state = model_get_initial_state(); + da_mon->monitoring = 1; +} + +/* + * da_monitoring - returns true if the monitor is processing events + */ +static inline bool da_monitoring(struct da_monitor *da_mon) +{ + return da_mon->monitoring; +} + +/* + * da_monitor_enabled - checks if the monitor is enabled + */ +static inline bool da_monitor_enabled(void) +{ + /* global switch */ + if (unlikely(!rv_monitoring_on())) + return 0; + + /* monitor enabled */ + if (unlikely(!rv_this.enabled)) + return 0; + + return 1; +} + +/* + * da_monitor_handling_event - checks if the monitor is ready to handle events + */ +static inline bool da_monitor_handling_event(struct da_monitor *da_mon) +{ + + if (!da_monitor_enabled()) + return 0; + + /* monitor is actually monitoring */ + if (unlikely(!da_monitoring(da_mon))) + return 0; + + return 1; } /* @@ -100,37 +103,37 @@ static inline bool da_monitor_handling_event_##name(struct da_monitor *da_mon) * warn and reset the monitor if it runs out of retries. The monitor should be * able to handle various orders. */ -#define DECLARE_DA_MON_MODEL_HANDLER_IMPLICIT(name, type) \ - \ -static inline bool \ -da_event_##name(struct da_monitor *da_mon, enum events_##name event) \ -{ \ - enum states_##name curr_state, next_state; \ - \ - curr_state = READ_ONCE(da_mon->curr_state); \ - for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { \ - next_state = model_get_next_state_##name(curr_state, event); \ - if (next_state == INVALID_STATE) { \ - react_##name(curr_state, event); \ - trace_error_##name(model_get_state_name_##name(curr_state), \ - model_get_event_name_##name(event)); \ - return false; \ - } \ - if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { \ - trace_event_##name(model_get_state_name_##name(curr_state), \ - model_get_event_name_##name(event), \ - model_get_state_name_##name(next_state), \ - model_is_final_state_##name(next_state)); \ - return true; \ - } \ - } \ - \ - trace_rv_retries_error(#name, model_get_event_name_##name(event)); \ - pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) \ - " retries reached for event %s, resetting monitor %s", \ - model_get_event_name_##name(event), #name); \ - return false; \ -} \ +#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU + +static inline bool +da_event(struct da_monitor *da_mon, enum events event) +{ + enum states curr_state, next_state; + + curr_state = READ_ONCE(da_mon->curr_state); + for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { + next_state = model_get_next_state(curr_state, event); + if (next_state == INVALID_STATE) { + react(curr_state, event); + CONCATENATE(trace_error_, MONITOR_NAME)(model_get_state_name(curr_state), + model_get_event_name(event)); + return false; + } + if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { + CONCATENATE(trace_event_, MONITOR_NAME)(model_get_state_name(curr_state), + model_get_event_name(event), + model_get_state_name(next_state), + model_is_final_state(next_state)); + return true; + } + } + + trace_rv_retries_error(__stringify(MONITOR_NAME), model_get_event_name(event)); + pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) + " retries reached for event %s, resetting monitor %s", + model_get_event_name(event), __stringify(MONITOR_NAME)); + return false; +} /* * Event handler for per_task monitors. @@ -139,395 +142,380 @@ da_event_##name(struct da_monitor *da_mon, enum events_##name event) \ * warn and reset the monitor if it runs out of retries. The monitor should be * able to handle various orders. */ -#define DECLARE_DA_MON_MODEL_HANDLER_PER_TASK(name, type) \ - \ -static inline bool da_event_##name(struct da_monitor *da_mon, struct task_struct *tsk, \ - enum events_##name event) \ -{ \ - enum states_##name curr_state, next_state; \ - \ - curr_state = READ_ONCE(da_mon->curr_state); \ - for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { \ - next_state = model_get_next_state_##name(curr_state, event); \ - if (next_state == INVALID_STATE) { \ - react_##name(curr_state, event); \ - trace_error_##name(tsk->pid, \ - model_get_state_name_##name(curr_state), \ - model_get_event_name_##name(event)); \ - return false; \ - } \ - if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { \ - trace_event_##name(tsk->pid, \ - model_get_state_name_##name(curr_state), \ - model_get_event_name_##name(event), \ - model_get_state_name_##name(next_state), \ - model_is_final_state_##name(next_state)); \ - return true; \ - } \ - } \ - \ - trace_rv_retries_error(#name, model_get_event_name_##name(event)); \ - pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) \ - " retries reached for event %s, resetting monitor %s", \ - model_get_event_name_##name(event), #name); \ - return false; \ +#elif RV_MON_TYPE == RV_MON_PER_TASK + +static inline bool da_event(struct da_monitor *da_mon, struct task_struct *tsk, + enum events event) +{ + enum states curr_state, next_state; + + curr_state = READ_ONCE(da_mon->curr_state); + for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { + next_state = model_get_next_state(curr_state, event); + if (next_state == INVALID_STATE) { + react(curr_state, event); + CONCATENATE(trace_error_, MONITOR_NAME)(tsk->pid, + model_get_state_name(curr_state), + model_get_event_name(event)); + return false; + } + if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { + CONCATENATE(trace_event_, MONITOR_NAME)(tsk->pid, + model_get_state_name(curr_state), + model_get_event_name(event), + model_get_state_name(next_state), + model_is_final_state(next_state)); + return true; + } + } + + trace_rv_retries_error(__stringify(MONITOR_NAME), model_get_event_name(event)); + pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) + " retries reached for event %s, resetting monitor %s", + model_get_event_name(event), __stringify(MONITOR_NAME)); + return false; } +#endif /* * Functions to define, init and get a global monitor. */ -#define DECLARE_DA_MON_INIT_GLOBAL(name, type) \ - \ -/* \ - * global monitor (a single variable) \ - */ \ -static struct da_monitor da_mon_##name; \ - \ -/* \ - * da_get_monitor_##name - return the global monitor address \ - */ \ -static struct da_monitor *da_get_monitor_##name(void) \ -{ \ - return &da_mon_##name; \ -} \ - \ -/* \ - * da_monitor_reset_all_##name - reset the single monitor \ - */ \ -static void da_monitor_reset_all_##name(void) \ -{ \ - da_monitor_reset_##name(da_get_monitor_##name()); \ -} \ - \ -/* \ - * da_monitor_init_##name - initialize a monitor \ - */ \ -static inline int da_monitor_init_##name(void) \ -{ \ - da_monitor_reset_all_##name(); \ - return 0; \ -} \ - \ -/* \ - * da_monitor_destroy_##name - destroy the monitor \ - */ \ -static inline void da_monitor_destroy_##name(void) \ -{ \ - return; \ +#if RV_MON_TYPE == RV_MON_GLOBAL + +/* + * global monitor (a single variable) + */ +static struct da_monitor da_mon_this; + +/* + * da_get_monitor - return the global monitor address + */ +static struct da_monitor *da_get_monitor(void) +{ + return &da_mon_this; +} + +/* + * da_monitor_reset_all - reset the single monitor + */ +static void da_monitor_reset_all(void) +{ + da_monitor_reset(da_get_monitor()); +} + +/* + * da_monitor_init - initialize a monitor + */ +static inline int da_monitor_init(void) +{ + da_monitor_reset_all(); + return 0; +} + +/* + * da_monitor_destroy - destroy the monitor + */ +static inline void da_monitor_destroy(void) +{ + return; } /* * Functions to define, init and get a per-cpu monitor. */ -#define DECLARE_DA_MON_INIT_PER_CPU(name, type) \ - \ -/* \ - * per-cpu monitor variables \ - */ \ -static DEFINE_PER_CPU(struct da_monitor, da_mon_##name); \ - \ -/* \ - * da_get_monitor_##name - return current CPU monitor address \ - */ \ -static struct da_monitor *da_get_monitor_##name(void) \ -{ \ - return this_cpu_ptr(&da_mon_##name); \ -} \ - \ -/* \ - * da_monitor_reset_all_##name - reset all CPUs' monitor \ - */ \ -static void da_monitor_reset_all_##name(void) \ -{ \ - struct da_monitor *da_mon; \ - int cpu; \ - for_each_cpu(cpu, cpu_online_mask) { \ - da_mon = per_cpu_ptr(&da_mon_##name, cpu); \ - da_monitor_reset_##name(da_mon); \ - } \ -} \ - \ -/* \ - * da_monitor_init_##name - initialize all CPUs' monitor \ - */ \ -static inline int da_monitor_init_##name(void) \ -{ \ - da_monitor_reset_all_##name(); \ - return 0; \ -} \ - \ -/* \ - * da_monitor_destroy_##name - destroy the monitor \ - */ \ -static inline void da_monitor_destroy_##name(void) \ -{ \ - return; \ +#elif RV_MON_TYPE == RV_MON_PER_CPU + +/* + * per-cpu monitor variables + */ +static DEFINE_PER_CPU(struct da_monitor, da_mon_this); + +/* + * da_get_monitor - return current CPU monitor address + */ +static struct da_monitor *da_get_monitor(void) +{ + return this_cpu_ptr(&da_mon_this); +} + +/* + * da_monitor_reset_all - reset all CPUs' monitor + */ +static void da_monitor_reset_all(void) +{ + struct da_monitor *da_mon; + int cpu; + for_each_cpu(cpu, cpu_online_mask) { + da_mon = per_cpu_ptr(&da_mon_this, cpu); + da_monitor_reset(da_mon); + } +} + +/* + * da_monitor_init - initialize all CPUs' monitor + */ +static inline int da_monitor_init(void) +{ + da_monitor_reset_all(); + return 0; +} + +/* + * da_monitor_destroy - destroy the monitor + */ +static inline void da_monitor_destroy(void) +{ + return; } /* * Functions to define, init and get a per-task monitor. */ -#define DECLARE_DA_MON_INIT_PER_TASK(name, type) \ - \ -/* \ - * The per-task monitor is stored a vector in the task struct. This variable \ - * stores the position on the vector reserved for this monitor. \ - */ \ -static int task_mon_slot_##name = RV_PER_TASK_MONITOR_INIT; \ - \ -/* \ - * da_get_monitor_##name - return the monitor in the allocated slot for tsk \ - */ \ -static inline struct da_monitor *da_get_monitor_##name(struct task_struct *tsk) \ -{ \ - return &tsk->rv[task_mon_slot_##name].da_mon; \ -} \ - \ -static void da_monitor_reset_all_##name(void) \ -{ \ - struct task_struct *g, *p; \ - int cpu; \ - \ - read_lock(&tasklist_lock); \ - for_each_process_thread(g, p) \ - da_monitor_reset_##name(da_get_monitor_##name(p)); \ - for_each_present_cpu(cpu) \ - da_monitor_reset_##name(da_get_monitor_##name(idle_task(cpu))); \ - read_unlock(&tasklist_lock); \ -} \ - \ -/* \ - * da_monitor_init_##name - initialize the per-task monitor \ - * \ - * Try to allocate a slot in the task's vector of monitors. If there \ - * is an available slot, use it and reset all task's monitor. \ - */ \ -static int da_monitor_init_##name(void) \ -{ \ - int slot; \ - \ - slot = rv_get_task_monitor_slot(); \ - if (slot < 0 || slot >= RV_PER_TASK_MONITOR_INIT) \ - return slot; \ - \ - task_mon_slot_##name = slot; \ - \ - da_monitor_reset_all_##name(); \ - return 0; \ -} \ - \ -/* \ - * da_monitor_destroy_##name - return the allocated slot \ - */ \ -static inline void da_monitor_destroy_##name(void) \ -{ \ - if (task_mon_slot_##name == RV_PER_TASK_MONITOR_INIT) { \ - WARN_ONCE(1, "Disabling a disabled monitor: " #name); \ - return; \ - } \ - rv_put_task_monitor_slot(task_mon_slot_##name); \ - task_mon_slot_##name = RV_PER_TASK_MONITOR_INIT; \ - return; \ +#elif RV_MON_TYPE == RV_MON_PER_TASK + +/* + * The per-task monitor is stored a vector in the task struct. This variable + * stores the position on the vector reserved for this monitor. + */ +static int task_mon_slot = RV_PER_TASK_MONITOR_INIT; + +/* + * da_get_monitor - return the monitor in the allocated slot for tsk + */ +static inline struct da_monitor *da_get_monitor(struct task_struct *tsk) +{ + return &tsk->rv[task_mon_slot].da_mon; +} + +static void da_monitor_reset_all(void) +{ + struct task_struct *g, *p; + int cpu; + + read_lock(&tasklist_lock); + for_each_process_thread(g, p) + da_monitor_reset(da_get_monitor(p)); + for_each_present_cpu(cpu) + da_monitor_reset(da_get_monitor(idle_task(cpu))); + read_unlock(&tasklist_lock); } /* - * Handle event for implicit monitor: da_get_monitor_##name() will figure out + * da_monitor_init - initialize the per-task monitor + * + * Try to allocate a slot in the task's vector of monitors. If there + * is an available slot, use it and reset all task's monitor. + */ +static int da_monitor_init(void) +{ + int slot; + + slot = rv_get_task_monitor_slot(); + if (slot < 0 || slot >= RV_PER_TASK_MONITOR_INIT) + return slot; + + task_mon_slot = slot; + + da_monitor_reset_all(); + return 0; +} + +/* + * da_monitor_destroy - return the allocated slot + */ +static inline void da_monitor_destroy(void) +{ + if (task_mon_slot == RV_PER_TASK_MONITOR_INIT) { + WARN_ONCE(1, "Disabling a disabled monitor: " __stringify(MONITOR_NAME)); + return; + } + rv_put_task_monitor_slot(task_mon_slot); + task_mon_slot = RV_PER_TASK_MONITOR_INIT; + return; +} +#endif + +/* + * Handle event for implicit monitor: da_get_monitor() will figure out * the monitor. */ -#define DECLARE_DA_MON_MONITOR_HANDLER_IMPLICIT(name, type) \ - \ -static inline void __da_handle_event_##name(struct da_monitor *da_mon, \ - enum events_##name event) \ -{ \ - bool retval; \ - \ - retval = da_event_##name(da_mon, event); \ - if (!retval) \ - da_monitor_reset_##name(da_mon); \ -} \ - \ -/* \ - * da_handle_event_##name - handle an event \ - */ \ -static inline void da_handle_event_##name(enum events_##name event) \ -{ \ - struct da_monitor *da_mon = da_get_monitor_##name(); \ - bool retval; \ - \ - retval = da_monitor_handling_event_##name(da_mon); \ - if (!retval) \ - return; \ - \ - __da_handle_event_##name(da_mon, event); \ -} \ - \ -/* \ - * da_handle_start_event_##name - start monitoring or handle event \ - * \ - * This function is used to notify the monitor that the system is returning \ - * to the initial state, so the monitor can start monitoring in the next event. \ - * Thus: \ - * \ - * If the monitor already started, handle the event. \ - * If the monitor did not start yet, start the monitor but skip the event. \ - */ \ -static inline bool da_handle_start_event_##name(enum events_##name event) \ -{ \ - struct da_monitor *da_mon; \ - \ - if (!da_monitor_enabled_##name()) \ - return 0; \ - \ - da_mon = da_get_monitor_##name(); \ - \ - if (unlikely(!da_monitoring_##name(da_mon))) { \ - da_monitor_start_##name(da_mon); \ - return 0; \ - } \ - \ - __da_handle_event_##name(da_mon, event); \ - \ - return 1; \ -} \ - \ -/* \ - * da_handle_start_run_event_##name - start monitoring and handle event \ - * \ - * This function is used to notify the monitor that the system is in the \ - * initial state, so the monitor can start monitoring and handling event. \ - */ \ -static inline bool da_handle_start_run_event_##name(enum events_##name event) \ -{ \ - struct da_monitor *da_mon; \ - \ - if (!da_monitor_enabled_##name()) \ - return 0; \ - \ - da_mon = da_get_monitor_##name(); \ - \ - if (unlikely(!da_monitoring_##name(da_mon))) \ - da_monitor_start_##name(da_mon); \ - \ - __da_handle_event_##name(da_mon, event); \ - \ - return 1; \ +#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU + +static inline void __da_handle_event(struct da_monitor *da_mon, + enum events event) +{ + bool retval; + + retval = da_event(da_mon, event); + if (!retval) + da_monitor_reset(da_mon); +} + +/* + * da_handle_event - handle an event + */ +static inline void da_handle_event(enum events event) +{ + struct da_monitor *da_mon = da_get_monitor(); + bool retval; + + retval = da_monitor_handling_event(da_mon); + if (!retval) + return; + + __da_handle_event(da_mon, event); +} + +/* + * da_handle_start_event - start monitoring or handle event + * + * This function is used to notify the monitor that the system is returning + * to the initial state, so the monitor can start monitoring in the next event. + * Thus: + * + * If the monitor already started, handle the event. + * If the monitor did not start yet, start the monitor but skip the event. + */ +static inline bool da_handle_start_event(enum events event) +{ + struct da_monitor *da_mon; + + if (!da_monitor_enabled()) + return 0; + + da_mon = da_get_monitor(); + + if (unlikely(!da_monitoring(da_mon))) { + da_monitor_start(da_mon); + return 0; + } + + __da_handle_event(da_mon, event); + + return 1; +} + +/* + * da_handle_start_run_event - start monitoring and handle event + * + * This function is used to notify the monitor that the system is in the + * initial state, so the monitor can start monitoring and handling event. + */ +static inline bool da_handle_start_run_event(enum events event) +{ + struct da_monitor *da_mon; + + if (!da_monitor_enabled()) + return 0; + + da_mon = da_get_monitor(); + + if (unlikely(!da_monitoring(da_mon))) + da_monitor_start(da_mon); + + __da_handle_event(da_mon, event); + + return 1; } /* * Handle event for per task. */ -#define DECLARE_DA_MON_MONITOR_HANDLER_PER_TASK(name, type) \ - \ -static inline void \ -__da_handle_event_##name(struct da_monitor *da_mon, struct task_struct *tsk, \ - enum events_##name event) \ -{ \ - bool retval; \ - \ - retval = da_event_##name(da_mon, tsk, event); \ - if (!retval) \ - da_monitor_reset_##name(da_mon); \ -} \ - \ -/* \ - * da_handle_event_##name - handle an event \ - */ \ -static inline void \ -da_handle_event_##name(struct task_struct *tsk, enum events_##name event) \ -{ \ - struct da_monitor *da_mon = da_get_monitor_##name(tsk); \ - bool retval; \ - \ - retval = da_monitor_handling_event_##name(da_mon); \ - if (!retval) \ - return; \ - \ - __da_handle_event_##name(da_mon, tsk, event); \ -} \ - \ -/* \ - * da_handle_start_event_##name - start monitoring or handle event \ - * \ - * This function is used to notify the monitor that the system is returning \ - * to the initial state, so the monitor can start monitoring in the next event. \ - * Thus: \ - * \ - * If the monitor already started, handle the event. \ - * If the monitor did not start yet, start the monitor but skip the event. \ - */ \ -static inline bool \ -da_handle_start_event_##name(struct task_struct *tsk, enum events_##name event) \ -{ \ - struct da_monitor *da_mon; \ - \ - if (!da_monitor_enabled_##name()) \ - return 0; \ - \ - da_mon = da_get_monitor_##name(tsk); \ - \ - if (unlikely(!da_monitoring_##name(da_mon))) { \ - da_monitor_start_##name(da_mon); \ - return 0; \ - } \ - \ - __da_handle_event_##name(da_mon, tsk, event); \ - \ - return 1; \ -} \ - \ -/* \ - * da_handle_start_run_event_##name - start monitoring and handle event \ - * \ - * This function is used to notify the monitor that the system is in the \ - * initial state, so the monitor can start monitoring and handling event. \ - */ \ -static inline bool \ -da_handle_start_run_event_##name(struct task_struct *tsk, enum events_##name event) \ -{ \ - struct da_monitor *da_mon; \ - \ - if (!da_monitor_enabled_##name()) \ - return 0; \ - \ - da_mon = da_get_monitor_##name(tsk); \ - \ - if (unlikely(!da_monitoring_##name(da_mon))) \ - da_monitor_start_##name(da_mon); \ - \ - __da_handle_event_##name(da_mon, tsk, event); \ - \ - return 1; \ +#elif RV_MON_TYPE == RV_MON_PER_TASK + +static inline void +__da_handle_event(struct da_monitor *da_mon, struct task_struct *tsk, + enum events event) +{ + bool retval; + + retval = da_event(da_mon, tsk, event); + if (!retval) + da_monitor_reset(da_mon); +} + +/* + * da_handle_event - handle an event + */ +static inline void +da_handle_event(struct task_struct *tsk, enum events event) +{ + struct da_monitor *da_mon = da_get_monitor(tsk); + bool retval; + + retval = da_monitor_handling_event(da_mon); + if (!retval) + return; + + __da_handle_event(da_mon, tsk, event); +} + +/* + * da_handle_start_event - start monitoring or handle event + * + * This function is used to notify the monitor that the system is returning + * to the initial state, so the monitor can start monitoring in the next event. + * Thus: + * + * If the monitor already started, handle the event. + * If the monitor did not start yet, start the monitor but skip the event. + */ +static inline bool +da_handle_start_event(struct task_struct *tsk, enum events event) +{ + struct da_monitor *da_mon; + + if (!da_monitor_enabled()) + return 0; + + da_mon = da_get_monitor(tsk); + + if (unlikely(!da_monitoring(da_mon))) { + da_monitor_start(da_mon); + return 0; + } + + __da_handle_event(da_mon, tsk, event); + + return 1; +} + +/* + * da_handle_start_run_event - start monitoring and handle event + * + * This function is used to notify the monitor that the system is in the + * initial state, so the monitor can start monitoring and handling event. + */ +static inline bool +da_handle_start_run_event(struct task_struct *tsk, enum events event) +{ + struct da_monitor *da_mon; + + if (!da_monitor_enabled()) + return 0; + + da_mon = da_get_monitor(tsk); + + if (unlikely(!da_monitoring(da_mon))) + da_monitor_start(da_mon); + + __da_handle_event(da_mon, tsk, event); + + return 1; } +#endif /* * Entry point for the global monitor. */ -#define DECLARE_DA_MON_GLOBAL(name, type) \ - \ -DECLARE_AUTOMATA_HELPERS(name, type) \ -DECLARE_DA_MON_GENERIC_HELPERS(name, type) \ -DECLARE_DA_MON_MODEL_HANDLER_IMPLICIT(name, type) \ -DECLARE_DA_MON_INIT_GLOBAL(name, type) \ -DECLARE_DA_MON_MONITOR_HANDLER_IMPLICIT(name, type) +#define DECLARE_DA_MON_GLOBAL(name, type) /* * Entry point for the per-cpu monitor. */ -#define DECLARE_DA_MON_PER_CPU(name, type) \ - \ -DECLARE_AUTOMATA_HELPERS(name, type) \ -DECLARE_DA_MON_GENERIC_HELPERS(name, type) \ -DECLARE_DA_MON_MODEL_HANDLER_IMPLICIT(name, type) \ -DECLARE_DA_MON_INIT_PER_CPU(name, type) \ -DECLARE_DA_MON_MONITOR_HANDLER_IMPLICIT(name, type) +#define DECLARE_DA_MON_PER_CPU(name, type) /* * Entry point for the per-task monitor. */ -#define DECLARE_DA_MON_PER_TASK(name, type) \ - \ -DECLARE_AUTOMATA_HELPERS(name, type) \ -DECLARE_DA_MON_GENERIC_HELPERS(name, type) \ -DECLARE_DA_MON_MODEL_HANDLER_PER_TASK(name, type) \ -DECLARE_DA_MON_INIT_PER_TASK(name, type) \ -DECLARE_DA_MON_MONITOR_HANDLER_PER_TASK(name, type) +#define DECLARE_DA_MON_PER_TASK(name, type) -- cgit v1.2.3 From fa9b26dfa172d145447fc1fca0337dfcf8588d19 Mon Sep 17 00:00:00 2001 From: Gabriele Monaco Date: Wed, 26 Nov 2025 11:42:33 +0100 Subject: rv: Cleanup da_monitor after refactor Previous changes refactored the da_monitor header file to avoid using macros, however empty macros (e.g. DECLARE_DA_FUNCTION) were left to ease review with diff tools. Most macros also get the argument type which doesn't really have a purpose since states have their own enum and the storage in struct da_monitor is fixed to unsigned int. Remove empty and no longer required macros and substitute the type parameter with the appropriate enum. Additionally break long line and adjust the format overall. Reviewed-by: Nam Cao Link: https://lore.kernel.org/r/20251126104241.291258-3-gmonaco@redhat.com Signed-off-by: Gabriele Monaco --- include/rv/automata.h | 24 ++++------- include/rv/da_monitor.h | 107 +++++++++++++++++++----------------------------- 2 files changed, 50 insertions(+), 81 deletions(-) (limited to 'include') diff --git a/include/rv/automata.h b/include/rv/automata.h index 5b5d2e94c034..4a4eb40cf09a 100644 --- a/include/rv/automata.h +++ b/include/rv/automata.h @@ -6,29 +6,19 @@ * models in C generated by the dot2k tool. */ +#ifndef _RV_AUTOMATA_H +#define _RV_AUTOMATA_H + #ifndef MONITOR_NAME #error "MONITOR_NAME macro is not defined. Did you include $(MODEL_NAME).h generated by rvgen?" #endif -#ifndef type -#define type unsigned char -#endif - #define RV_AUTOMATON_NAME CONCATENATE(automaton_, MONITOR_NAME) #define EVENT_MAX CONCATENATE(event_max_, MONITOR_NAME) #define STATE_MAX CONCATENATE(state_max_, MONITOR_NAME) #define events CONCATENATE(events_, MONITOR_NAME) #define states CONCATENATE(states_, MONITOR_NAME) -/* - * DECLARE_AUTOMATA_HELPERS - define a set of helper functions for automata - * - * Define a set of helper functions for automata. The 'name' argument is used - * as suffix for the functions and data. These functions will handle automaton - * with data type 'type'. - */ -#define DECLARE_AUTOMATA_HELPERS(name, type) - /* * model_get_state_name - return the (string) name of the given state */ @@ -54,7 +44,7 @@ static char *model_get_event_name(enum events event) /* * model_get_initial_state - return the automaton's initial state */ -static inline type model_get_initial_state(void) +static inline enum states model_get_initial_state(void) { return RV_AUTOMATON_NAME.initial_state; } @@ -65,8 +55,8 @@ static inline type model_get_initial_state(void) * Given the current state (curr_state) and the event (event), returns * the next state, or INVALID_STATE in case of error. */ -static inline type model_get_next_state(enum states curr_state, - enum events event) +static inline enum states model_get_next_state(enum states curr_state, + enum events event) { if ((curr_state < 0) || (curr_state >= STATE_MAX)) return INVALID_STATE; @@ -87,3 +77,5 @@ static inline bool model_is_final_state(enum states state) return RV_AUTOMATON_NAME.final_states[state]; } + +#endif diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h index a4555dafa8d0..7b28ef9f73bd 100644 --- a/include/rv/da_monitor.h +++ b/include/rv/da_monitor.h @@ -11,6 +11,9 @@ * Documentation/trace/rv/da_monitor_synthesis.rst */ +#ifndef _RV_DA_MONITOR_H +#define _RV_DA_MONITOR_H + #include #include #include @@ -19,12 +22,7 @@ static struct rv_monitor rv_this; -/* - * Generic helpers for all types of deterministic automata monitors. - */ -#define DECLARE_DA_MON_GENERIC_HELPERS(name, type) - -static void react(type curr_state, type event) +static void react(enum states curr_state, enum events event) { rv_react(&rv_this, "rv: monitor %s does not allow event %s on state %s\n", @@ -83,7 +81,6 @@ static inline bool da_monitor_enabled(void) */ static inline bool da_monitor_handling_event(struct da_monitor *da_mon) { - if (!da_monitor_enabled()) return 0; @@ -94,6 +91,7 @@ static inline bool da_monitor_handling_event(struct da_monitor *da_mon) return 1; } +#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU /* * Event handler for implicit monitors. Implicit monitor is the one which the * handler does not need to specify which da_monitor to manipulate. Examples @@ -103,10 +101,8 @@ static inline bool da_monitor_handling_event(struct da_monitor *da_mon) * warn and reset the monitor if it runs out of retries. The monitor should be * able to handle various orders. */ -#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU -static inline bool -da_event(struct da_monitor *da_mon, enum events event) +static inline bool da_event(struct da_monitor *da_mon, enum events event) { enum states curr_state, next_state; @@ -115,15 +111,17 @@ da_event(struct da_monitor *da_mon, enum events event) next_state = model_get_next_state(curr_state, event); if (next_state == INVALID_STATE) { react(curr_state, event); - CONCATENATE(trace_error_, MONITOR_NAME)(model_get_state_name(curr_state), - model_get_event_name(event)); + CONCATENATE(trace_error_, MONITOR_NAME)( + model_get_state_name(curr_state), + model_get_event_name(event)); return false; } if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { - CONCATENATE(trace_event_, MONITOR_NAME)(model_get_state_name(curr_state), - model_get_event_name(event), - model_get_state_name(next_state), - model_is_final_state(next_state)); + CONCATENATE(trace_event_, MONITOR_NAME)( + model_get_state_name(curr_state), + model_get_event_name(event), + model_get_state_name(next_state), + model_is_final_state(next_state)); return true; } } @@ -135,6 +133,7 @@ da_event(struct da_monitor *da_mon, enum events event) return false; } +#elif RV_MON_TYPE == RV_MON_PER_TASK /* * Event handler for per_task monitors. * @@ -142,10 +141,9 @@ da_event(struct da_monitor *da_mon, enum events event) * warn and reset the monitor if it runs out of retries. The monitor should be * able to handle various orders. */ -#elif RV_MON_TYPE == RV_MON_PER_TASK static inline bool da_event(struct da_monitor *da_mon, struct task_struct *tsk, - enum events event) + enum events event) { enum states curr_state, next_state; @@ -155,16 +153,16 @@ static inline bool da_event(struct da_monitor *da_mon, struct task_struct *tsk, if (next_state == INVALID_STATE) { react(curr_state, event); CONCATENATE(trace_error_, MONITOR_NAME)(tsk->pid, - model_get_state_name(curr_state), - model_get_event_name(event)); + model_get_state_name(curr_state), + model_get_event_name(event)); return false; } if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { CONCATENATE(trace_event_, MONITOR_NAME)(tsk->pid, - model_get_state_name(curr_state), - model_get_event_name(event), - model_get_state_name(next_state), - model_is_final_state(next_state)); + model_get_state_name(curr_state), + model_get_event_name(event), + model_get_state_name(next_state), + model_is_final_state(next_state)); return true; } } @@ -175,12 +173,12 @@ static inline bool da_event(struct da_monitor *da_mon, struct task_struct *tsk, model_get_event_name(event), __stringify(MONITOR_NAME)); return false; } -#endif +#endif /* RV_MON_TYPE */ +#if RV_MON_TYPE == RV_MON_GLOBAL /* * Functions to define, init and get a global monitor. */ -#if RV_MON_TYPE == RV_MON_GLOBAL /* * global monitor (a single variable) @@ -215,15 +213,12 @@ static inline int da_monitor_init(void) /* * da_monitor_destroy - destroy the monitor */ -static inline void da_monitor_destroy(void) -{ - return; -} +static inline void da_monitor_destroy(void) { } +#elif RV_MON_TYPE == RV_MON_PER_CPU /* * Functions to define, init and get a per-cpu monitor. */ -#elif RV_MON_TYPE == RV_MON_PER_CPU /* * per-cpu monitor variables @@ -245,6 +240,7 @@ static void da_monitor_reset_all(void) { struct da_monitor *da_mon; int cpu; + for_each_cpu(cpu, cpu_online_mask) { da_mon = per_cpu_ptr(&da_mon_this, cpu); da_monitor_reset(da_mon); @@ -263,15 +259,12 @@ static inline int da_monitor_init(void) /* * da_monitor_destroy - destroy the monitor */ -static inline void da_monitor_destroy(void) -{ - return; -} +static inline void da_monitor_destroy(void) { } +#elif RV_MON_TYPE == RV_MON_PER_TASK /* * Functions to define, init and get a per-task monitor. */ -#elif RV_MON_TYPE == RV_MON_PER_TASK /* * The per-task monitor is stored a vector in the task struct. This variable @@ -331,18 +324,17 @@ static inline void da_monitor_destroy(void) } rv_put_task_monitor_slot(task_mon_slot); task_mon_slot = RV_PER_TASK_MONITOR_INIT; - return; } -#endif +#endif /* RV_MON_TYPE */ +#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU /* * Handle event for implicit monitor: da_get_monitor() will figure out * the monitor. */ -#if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU static inline void __da_handle_event(struct da_monitor *da_mon, - enum events event) + enum events event) { bool retval; @@ -418,14 +410,13 @@ static inline bool da_handle_start_run_event(enum events event) return 1; } +#elif RV_MON_TYPE == RV_MON_PER_TASK /* * Handle event for per task. */ -#elif RV_MON_TYPE == RV_MON_PER_TASK -static inline void -__da_handle_event(struct da_monitor *da_mon, struct task_struct *tsk, - enum events event) +static inline void __da_handle_event(struct da_monitor *da_mon, + struct task_struct *tsk, enum events event) { bool retval; @@ -437,8 +428,7 @@ __da_handle_event(struct da_monitor *da_mon, struct task_struct *tsk, /* * da_handle_event - handle an event */ -static inline void -da_handle_event(struct task_struct *tsk, enum events event) +static inline void da_handle_event(struct task_struct *tsk, enum events event) { struct da_monitor *da_mon = da_get_monitor(tsk); bool retval; @@ -460,8 +450,8 @@ da_handle_event(struct task_struct *tsk, enum events event) * If the monitor already started, handle the event. * If the monitor did not start yet, start the monitor but skip the event. */ -static inline bool -da_handle_start_event(struct task_struct *tsk, enum events event) +static inline bool da_handle_start_event(struct task_struct *tsk, + enum events event) { struct da_monitor *da_mon; @@ -486,8 +476,8 @@ da_handle_start_event(struct task_struct *tsk, enum events event) * This function is used to notify the monitor that the system is in the * initial state, so the monitor can start monitoring and handling event. */ -static inline bool -da_handle_start_run_event(struct task_struct *tsk, enum events event) +static inline bool da_handle_start_run_event(struct task_struct *tsk, + enum events event) { struct da_monitor *da_mon; @@ -503,19 +493,6 @@ da_handle_start_run_event(struct task_struct *tsk, enum events event) return 1; } -#endif +#endif /* RV_MON_TYPE */ -/* - * Entry point for the global monitor. - */ -#define DECLARE_DA_MON_GLOBAL(name, type) - -/* - * Entry point for the per-cpu monitor. - */ -#define DECLARE_DA_MON_PER_CPU(name, type) - -/* - * Entry point for the per-task monitor. - */ -#define DECLARE_DA_MON_PER_TASK(name, type) +#endif -- cgit v1.2.3 From 403faa575738a7f92267b2ca2ee56cd1b9373078 Mon Sep 17 00:00:00 2001 From: Shubham Sharma Date: Tue, 30 Dec 2025 13:23:37 +0530 Subject: rv: Fix documentation reference in da_monitor.h Update documentation reference to reflect the file rename. Monitor synthesis documentation was renamed in commit f40a7c060207 ("Documentation/rv: Prepare monitor synthesis document for LTL inclusion") from da_monitor_synthesis.rst to monitor_synthesis.rst. Signed-off-by: Shubham Sharma Reviewed-by: Gabriele Monaco Link: https://lore.kernel.org/r/20251230075337.11993-1-slopixelz@gmail.com Signed-off-by: Gabriele Monaco --- include/rv/da_monitor.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'include') diff --git a/include/rv/da_monitor.h b/include/rv/da_monitor.h index 7b28ef9f73bd..db11d41bb438 100644 --- a/include/rv/da_monitor.h +++ b/include/rv/da_monitor.h @@ -8,7 +8,7 @@ * The dot2k tool is available at tools/verification/dot2k/ * * For further information, see: - * Documentation/trace/rv/da_monitor_synthesis.rst + * Documentation/trace/rv/monitor_synthesis.rst */ #ifndef _RV_DA_MONITOR_H -- cgit v1.2.3