Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

rv: Unify DA event handling functions across monitor types

The DA event handling functions are mostly duplicated because the
per-task monitors need to propagate the task struct while others do not.

Unify the functions, handle the difference by always passing an
identifier which is the task's pid for per-task monitors but is ignored
for the other types. Only keep the actual tracepoint calling separated.

Reviewed-by: Nam Cao <namcao@linutronix.de>
Link: https://lore.kernel.org/r/20260330111010.153663-2-gmonaco@redhat.com
Signed-off-by: Gabriele Monaco <gmonaco@redhat.com>

+137 -176
+137 -176
include/rv/da_monitor.h
··· 28 28 29 29 static struct rv_monitor rv_this; 30 30 31 + /* 32 + * Type for the target id, default to int but can be overridden. 33 + */ 34 + #ifndef da_id_type 35 + #define da_id_type int 36 + #endif 37 + 31 38 static void react(enum states curr_state, enum events event) 32 39 { 33 40 rv_react(&rv_this, ··· 103 96 104 97 return 1; 105 98 } 106 - 107 - #if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU 108 - /* 109 - * Event handler for implicit monitors. Implicit monitor is the one which the 110 - * handler does not need to specify which da_monitor to manipulate. Examples 111 - * of implicit monitor are the per_cpu or the global ones. 112 - * 113 - * Retry in case there is a race between getting and setting the next state, 114 - * warn and reset the monitor if it runs out of retries. The monitor should be 115 - * able to handle various orders. 116 - */ 117 - 118 - static inline bool da_event(struct da_monitor *da_mon, enum events event) 119 - { 120 - enum states curr_state, next_state; 121 - 122 - curr_state = READ_ONCE(da_mon->curr_state); 123 - for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { 124 - next_state = model_get_next_state(curr_state, event); 125 - if (next_state == INVALID_STATE) { 126 - react(curr_state, event); 127 - CONCATENATE(trace_error_, MONITOR_NAME)( 128 - model_get_state_name(curr_state), 129 - model_get_event_name(event)); 130 - return false; 131 - } 132 - if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { 133 - CONCATENATE(trace_event_, MONITOR_NAME)( 134 - model_get_state_name(curr_state), 135 - model_get_event_name(event), 136 - model_get_state_name(next_state), 137 - model_is_final_state(next_state)); 138 - return true; 139 - } 140 - } 141 - 142 - trace_rv_retries_error(__stringify(MONITOR_NAME), model_get_event_name(event)); 143 - pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) 144 - " retries reached for event %s, resetting monitor %s", 145 - model_get_event_name(event), __stringify(MONITOR_NAME)); 146 - return false; 147 - } 148 - 149 - #elif RV_MON_TYPE == RV_MON_PER_TASK 150 - /* 151 - * Event handler for per_task monitors. 152 - * 153 - * Retry in case there is a race between getting and setting the next state, 154 - * warn and reset the monitor if it runs out of retries. The monitor should be 155 - * able to handle various orders. 156 - */ 157 - 158 - static inline bool da_event(struct da_monitor *da_mon, struct task_struct *tsk, 159 - enum events event) 160 - { 161 - enum states curr_state, next_state; 162 - 163 - curr_state = READ_ONCE(da_mon->curr_state); 164 - for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { 165 - next_state = model_get_next_state(curr_state, event); 166 - if (next_state == INVALID_STATE) { 167 - react(curr_state, event); 168 - CONCATENATE(trace_error_, MONITOR_NAME)(tsk->pid, 169 - model_get_state_name(curr_state), 170 - model_get_event_name(event)); 171 - return false; 172 - } 173 - if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { 174 - CONCATENATE(trace_event_, MONITOR_NAME)(tsk->pid, 175 - model_get_state_name(curr_state), 176 - model_get_event_name(event), 177 - model_get_state_name(next_state), 178 - model_is_final_state(next_state)); 179 - return true; 180 - } 181 - } 182 - 183 - trace_rv_retries_error(__stringify(MONITOR_NAME), model_get_event_name(event)); 184 - pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) 185 - " retries reached for event %s, resetting monitor %s", 186 - model_get_event_name(event), __stringify(MONITOR_NAME)); 187 - return false; 188 - } 189 - #endif /* RV_MON_TYPE */ 190 99 191 100 #if RV_MON_TYPE == RV_MON_GLOBAL 192 101 /* ··· 258 335 259 336 #if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU 260 337 /* 338 + * Trace events for implicit monitors. Implicit monitor is the one which the 339 + * handler does not need to specify which da_monitor to manipulate. Examples 340 + * of implicit monitor are the per_cpu or the global ones. 341 + */ 342 + 343 + static inline void da_trace_event(struct da_monitor *da_mon, 344 + char *curr_state, char *event, 345 + char *next_state, bool is_final, 346 + da_id_type id) 347 + { 348 + CONCATENATE(trace_event_, MONITOR_NAME)(curr_state, event, next_state, 349 + is_final); 350 + } 351 + 352 + static inline void da_trace_error(struct da_monitor *da_mon, 353 + char *curr_state, char *event, 354 + da_id_type id) 355 + { 356 + CONCATENATE(trace_error_, MONITOR_NAME)(curr_state, event); 357 + } 358 + 359 + #elif RV_MON_TYPE == RV_MON_PER_TASK 360 + /* 361 + * Trace events for per_task monitors, report the PID of the task. 362 + */ 363 + 364 + static inline void da_trace_event(struct da_monitor *da_mon, 365 + char *curr_state, char *event, 366 + char *next_state, bool is_final, 367 + da_id_type id) 368 + { 369 + CONCATENATE(trace_event_, MONITOR_NAME)(id, curr_state, event, 370 + next_state, is_final); 371 + } 372 + 373 + static inline void da_trace_error(struct da_monitor *da_mon, 374 + char *curr_state, char *event, 375 + da_id_type id) 376 + { 377 + CONCATENATE(trace_error_, MONITOR_NAME)(id, curr_state, event); 378 + } 379 + #endif /* RV_MON_TYPE */ 380 + 381 + /* 382 + * da_event - handle an event for the da_mon 383 + * 384 + * This function is valid for both implicit and id monitors. 385 + * Retry in case there is a race between getting and setting the next state, 386 + * warn and reset the monitor if it runs out of retries. The monitor should be 387 + * able to handle various orders. 388 + */ 389 + static inline bool da_event(struct da_monitor *da_mon, enum events event, da_id_type id) 390 + { 391 + enum states curr_state, next_state; 392 + 393 + curr_state = READ_ONCE(da_mon->curr_state); 394 + for (int i = 0; i < MAX_DA_RETRY_RACING_EVENTS; i++) { 395 + next_state = model_get_next_state(curr_state, event); 396 + if (next_state == INVALID_STATE) { 397 + react(curr_state, event); 398 + da_trace_error(da_mon, model_get_state_name(curr_state), 399 + model_get_event_name(event), id); 400 + return false; 401 + } 402 + if (likely(try_cmpxchg(&da_mon->curr_state, &curr_state, next_state))) { 403 + da_trace_event(da_mon, model_get_state_name(curr_state), 404 + model_get_event_name(event), 405 + model_get_state_name(next_state), 406 + model_is_final_state(next_state), id); 407 + return true; 408 + } 409 + } 410 + 411 + trace_rv_retries_error(__stringify(MONITOR_NAME), model_get_event_name(event)); 412 + pr_warn("rv: " __stringify(MAX_DA_RETRY_RACING_EVENTS) 413 + " retries reached for event %s, resetting monitor %s", 414 + model_get_event_name(event), __stringify(MONITOR_NAME)); 415 + return false; 416 + } 417 + 418 + static inline void __da_handle_event_common(struct da_monitor *da_mon, 419 + enum events event, da_id_type id) 420 + { 421 + if (!da_event(da_mon, event, id)) 422 + da_monitor_reset(da_mon); 423 + } 424 + 425 + static inline void __da_handle_event(struct da_monitor *da_mon, 426 + enum events event, da_id_type id) 427 + { 428 + if (da_monitor_handling_event(da_mon)) 429 + __da_handle_event_common(da_mon, event, id); 430 + } 431 + 432 + static inline bool __da_handle_start_event(struct da_monitor *da_mon, 433 + enum events event, da_id_type id) 434 + { 435 + if (!da_monitor_enabled()) 436 + return 0; 437 + if (unlikely(!da_monitoring(da_mon))) { 438 + da_monitor_start(da_mon); 439 + return 0; 440 + } 441 + 442 + __da_handle_event_common(da_mon, event, id); 443 + 444 + return 1; 445 + } 446 + 447 + static inline bool __da_handle_start_run_event(struct da_monitor *da_mon, 448 + enum events event, da_id_type id) 449 + { 450 + if (!da_monitor_enabled()) 451 + return 0; 452 + if (unlikely(!da_monitoring(da_mon))) 453 + da_monitor_start(da_mon); 454 + 455 + __da_handle_event_common(da_mon, event, id); 456 + 457 + return 1; 458 + } 459 + 460 + #if RV_MON_TYPE == RV_MON_GLOBAL || RV_MON_TYPE == RV_MON_PER_CPU 461 + /* 261 462 * Handle event for implicit monitor: da_get_monitor() will figure out 262 463 * the monitor. 263 464 */ 264 - 265 - static inline void __da_handle_event(struct da_monitor *da_mon, 266 - enum events event) 267 - { 268 - bool retval; 269 - 270 - retval = da_event(da_mon, event); 271 - if (!retval) 272 - da_monitor_reset(da_mon); 273 - } 274 465 275 466 /* 276 467 * da_handle_event - handle an event 277 468 */ 278 469 static inline void da_handle_event(enum events event) 279 470 { 280 - struct da_monitor *da_mon = da_get_monitor(); 281 - bool retval; 282 - 283 - retval = da_monitor_handling_event(da_mon); 284 - if (!retval) 285 - return; 286 - 287 - __da_handle_event(da_mon, event); 471 + __da_handle_event(da_get_monitor(), event, 0); 288 472 } 289 473 290 474 /* ··· 406 376 */ 407 377 static inline bool da_handle_start_event(enum events event) 408 378 { 409 - struct da_monitor *da_mon; 410 - 411 - if (!da_monitor_enabled()) 412 - return 0; 413 - 414 - da_mon = da_get_monitor(); 415 - 416 - if (unlikely(!da_monitoring(da_mon))) { 417 - da_monitor_start(da_mon); 418 - return 0; 419 - } 420 - 421 - __da_handle_event(da_mon, event); 422 - 423 - return 1; 379 + return __da_handle_start_event(da_get_monitor(), event, 0); 424 380 } 425 381 426 382 /* ··· 417 401 */ 418 402 static inline bool da_handle_start_run_event(enum events event) 419 403 { 420 - struct da_monitor *da_mon; 421 - 422 - if (!da_monitor_enabled()) 423 - return 0; 424 - 425 - da_mon = da_get_monitor(); 426 - 427 - if (unlikely(!da_monitoring(da_mon))) 428 - da_monitor_start(da_mon); 429 - 430 - __da_handle_event(da_mon, event); 431 - 432 - return 1; 404 + return __da_handle_start_run_event(da_get_monitor(), event, 0); 433 405 } 434 406 435 407 #elif RV_MON_TYPE == RV_MON_PER_TASK ··· 425 421 * Handle event for per task. 426 422 */ 427 423 428 - static inline void __da_handle_event(struct da_monitor *da_mon, 429 - struct task_struct *tsk, enum events event) 430 - { 431 - bool retval; 432 - 433 - retval = da_event(da_mon, tsk, event); 434 - if (!retval) 435 - da_monitor_reset(da_mon); 436 - } 437 - 438 424 /* 439 425 * da_handle_event - handle an event 440 426 */ 441 427 static inline void da_handle_event(struct task_struct *tsk, enum events event) 442 428 { 443 - struct da_monitor *da_mon = da_get_monitor(tsk); 444 - bool retval; 445 - 446 - retval = da_monitor_handling_event(da_mon); 447 - if (!retval) 448 - return; 449 - 450 - __da_handle_event(da_mon, tsk, event); 429 + __da_handle_event(da_get_monitor(tsk), event, tsk->pid); 451 430 } 452 431 453 432 /* ··· 446 459 static inline bool da_handle_start_event(struct task_struct *tsk, 447 460 enum events event) 448 461 { 449 - struct da_monitor *da_mon; 450 - 451 - if (!da_monitor_enabled()) 452 - return 0; 453 - 454 - da_mon = da_get_monitor(tsk); 455 - 456 - if (unlikely(!da_monitoring(da_mon))) { 457 - da_monitor_start(da_mon); 458 - return 0; 459 - } 460 - 461 - __da_handle_event(da_mon, tsk, event); 462 - 463 - return 1; 462 + return __da_handle_start_event(da_get_monitor(tsk), event, tsk->pid); 464 463 } 465 464 466 465 /* ··· 458 485 static inline bool da_handle_start_run_event(struct task_struct *tsk, 459 486 enum events event) 460 487 { 461 - struct da_monitor *da_mon; 462 - 463 - if (!da_monitor_enabled()) 464 - return 0; 465 - 466 - da_mon = da_get_monitor(tsk); 467 - 468 - if (unlikely(!da_monitoring(da_mon))) 469 - da_monitor_start(da_mon); 470 - 471 - __da_handle_event(da_mon, tsk, event); 472 - 473 - return 1; 488 + return __da_handle_start_run_event(da_get_monitor(tsk), event, tsk->pid); 474 489 } 475 490 #endif /* RV_MON_TYPE */ 476 491