Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * trace_events_hist - trace event hist triggers |
| 4 | * |
| 5 | * Copyright (C) 2015 Tom Zanussi <tom.zanussi@linux.intel.com> |
| 6 | */ |
| 7 | |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/kallsyms.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10 | #include <linux/security.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 11 | #include <linux/mutex.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/stacktrace.h> |
| 14 | #include <linux/rculist.h> |
| 15 | #include <linux/tracefs.h> |
| 16 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 17 | /* for gfp flag names */ |
| 18 | #include <linux/trace_events.h> |
| 19 | #include <trace/events/mmflags.h> |
| 20 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 21 | #include "tracing_map.h" |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 22 | #include "trace_synth.h" |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 24 | #define ERRORS \ |
| 25 | C(NONE, "No error"), \ |
| 26 | C(DUPLICATE_VAR, "Variable already defined"), \ |
| 27 | C(VAR_NOT_UNIQUE, "Variable name not unique, need to use fully qualified name (subsys.event.var) for variable"), \ |
| 28 | C(TOO_MANY_VARS, "Too many variables defined"), \ |
| 29 | C(MALFORMED_ASSIGNMENT, "Malformed assignment"), \ |
| 30 | C(NAMED_MISMATCH, "Named hist trigger doesn't match existing named trigger (includes variables)"), \ |
| 31 | C(TRIGGER_EEXIST, "Hist trigger already exists"), \ |
| 32 | C(TRIGGER_ENOENT_CLEAR, "Can't clear or continue a nonexistent hist trigger"), \ |
| 33 | C(SET_CLOCK_FAIL, "Couldn't set trace_clock"), \ |
| 34 | C(BAD_FIELD_MODIFIER, "Invalid field modifier"), \ |
| 35 | C(TOO_MANY_SUBEXPR, "Too many subexpressions (3 max)"), \ |
| 36 | C(TIMESTAMP_MISMATCH, "Timestamp units in expression don't match"), \ |
| 37 | C(TOO_MANY_FIELD_VARS, "Too many field variables defined"), \ |
| 38 | C(EVENT_FILE_NOT_FOUND, "Event file not found"), \ |
| 39 | C(HIST_NOT_FOUND, "Matching event histogram not found"), \ |
| 40 | C(HIST_CREATE_FAIL, "Couldn't create histogram for field"), \ |
| 41 | C(SYNTH_VAR_NOT_FOUND, "Couldn't find synthetic variable"), \ |
| 42 | C(SYNTH_EVENT_NOT_FOUND,"Couldn't find synthetic event"), \ |
| 43 | C(SYNTH_TYPE_MISMATCH, "Param type doesn't match synthetic event field type"), \ |
| 44 | C(SYNTH_COUNT_MISMATCH, "Param count doesn't match synthetic event field count"), \ |
| 45 | C(FIELD_VAR_PARSE_FAIL, "Couldn't parse field variable"), \ |
| 46 | C(VAR_CREATE_FIND_FAIL, "Couldn't create or find variable"), \ |
| 47 | C(ONX_NOT_VAR, "For onmax(x) or onchange(x), x must be a variable"), \ |
| 48 | C(ONX_VAR_NOT_FOUND, "Couldn't find onmax or onchange variable"), \ |
| 49 | C(ONX_VAR_CREATE_FAIL, "Couldn't create onmax or onchange variable"), \ |
| 50 | C(FIELD_VAR_CREATE_FAIL,"Couldn't create field variable"), \ |
| 51 | C(TOO_MANY_PARAMS, "Too many action params"), \ |
| 52 | C(PARAM_NOT_FOUND, "Couldn't find param"), \ |
| 53 | C(INVALID_PARAM, "Invalid action param"), \ |
| 54 | C(ACTION_NOT_FOUND, "No action found"), \ |
| 55 | C(NO_SAVE_PARAMS, "No params found for save()"), \ |
| 56 | C(TOO_MANY_SAVE_ACTIONS,"Can't have more than one save() action per hist"), \ |
| 57 | C(ACTION_MISMATCH, "Handler doesn't support action"), \ |
| 58 | C(NO_CLOSING_PAREN, "No closing paren found"), \ |
| 59 | C(SUBSYS_NOT_FOUND, "Missing subsystem"), \ |
| 60 | C(INVALID_SUBSYS_EVENT, "Invalid subsystem or event name"), \ |
| 61 | C(INVALID_REF_KEY, "Using variable references in keys not supported"), \ |
| 62 | C(VAR_NOT_FOUND, "Couldn't find variable"), \ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 63 | C(FIELD_NOT_FOUND, "Couldn't find field"), \ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 64 | C(EMPTY_ASSIGNMENT, "Empty assignment"), \ |
| 65 | C(INVALID_SORT_MODIFIER,"Invalid sort modifier"), \ |
| 66 | C(EMPTY_SORT_FIELD, "Empty sort field"), \ |
| 67 | C(TOO_MANY_SORT_FIELDS, "Too many sort fields (Max = 2)"), \ |
| 68 | C(INVALID_SORT_FIELD, "Sort field must be a key or a val"), \ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 69 | C(INVALID_STR_OPERAND, "String type can not be an operand in expression"), |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 70 | |
| 71 | #undef C |
| 72 | #define C(a, b) HIST_ERR_##a |
| 73 | |
| 74 | enum { ERRORS }; |
| 75 | |
| 76 | #undef C |
| 77 | #define C(a, b) b |
| 78 | |
| 79 | static const char *err_text[] = { ERRORS }; |
| 80 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 81 | struct hist_field; |
| 82 | |
| 83 | typedef u64 (*hist_field_fn_t) (struct hist_field *field, |
| 84 | struct tracing_map_elt *elt, |
| 85 | struct ring_buffer_event *rbe, |
| 86 | void *event); |
| 87 | |
| 88 | #define HIST_FIELD_OPERANDS_MAX 2 |
| 89 | #define HIST_FIELDS_MAX (TRACING_MAP_FIELDS_MAX + TRACING_MAP_VARS_MAX) |
| 90 | #define HIST_ACTIONS_MAX 8 |
| 91 | |
| 92 | enum field_op_id { |
| 93 | FIELD_OP_NONE, |
| 94 | FIELD_OP_PLUS, |
| 95 | FIELD_OP_MINUS, |
| 96 | FIELD_OP_UNARY_MINUS, |
| 97 | }; |
| 98 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 99 | /* |
| 100 | * A hist_var (histogram variable) contains variable information for |
| 101 | * hist_fields having the HIST_FIELD_FL_VAR or HIST_FIELD_FL_VAR_REF |
| 102 | * flag set. A hist_var has a variable name e.g. ts0, and is |
| 103 | * associated with a given histogram trigger, as specified by |
| 104 | * hist_data. The hist_var idx is the unique index assigned to the |
| 105 | * variable by the hist trigger's tracing_map. The idx is what is |
| 106 | * used to set a variable's value and, by a variable reference, to |
| 107 | * retrieve it. |
| 108 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 109 | struct hist_var { |
| 110 | char *name; |
| 111 | struct hist_trigger_data *hist_data; |
| 112 | unsigned int idx; |
| 113 | }; |
| 114 | |
| 115 | struct hist_field { |
| 116 | struct ftrace_event_field *field; |
| 117 | unsigned long flags; |
| 118 | hist_field_fn_t fn; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 119 | unsigned int ref; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 120 | unsigned int size; |
| 121 | unsigned int offset; |
| 122 | unsigned int is_signed; |
| 123 | const char *type; |
| 124 | struct hist_field *operands[HIST_FIELD_OPERANDS_MAX]; |
| 125 | struct hist_trigger_data *hist_data; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 126 | |
| 127 | /* |
| 128 | * Variable fields contain variable-specific info in var. |
| 129 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 130 | struct hist_var var; |
| 131 | enum field_op_id operator; |
| 132 | char *system; |
| 133 | char *event_name; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * The name field is used for EXPR and VAR_REF fields. VAR |
| 137 | * fields contain the variable name in var.name. |
| 138 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 139 | char *name; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 140 | |
| 141 | /* |
| 142 | * When a histogram trigger is hit, if it has any references |
| 143 | * to variables, the values of those variables are collected |
| 144 | * into a var_ref_vals array by resolve_var_refs(). The |
| 145 | * current value of each variable is read from the tracing_map |
| 146 | * using the hist field's hist_var.idx and entered into the |
| 147 | * var_ref_idx entry i.e. var_ref_vals[var_ref_idx]. |
| 148 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 149 | unsigned int var_ref_idx; |
| 150 | bool read_once; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 151 | |
| 152 | unsigned int var_str_idx; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 153 | }; |
| 154 | |
| 155 | static u64 hist_field_none(struct hist_field *field, |
| 156 | struct tracing_map_elt *elt, |
| 157 | struct ring_buffer_event *rbe, |
| 158 | void *event) |
| 159 | { |
| 160 | return 0; |
| 161 | } |
| 162 | |
| 163 | static u64 hist_field_counter(struct hist_field *field, |
| 164 | struct tracing_map_elt *elt, |
| 165 | struct ring_buffer_event *rbe, |
| 166 | void *event) |
| 167 | { |
| 168 | return 1; |
| 169 | } |
| 170 | |
| 171 | static u64 hist_field_string(struct hist_field *hist_field, |
| 172 | struct tracing_map_elt *elt, |
| 173 | struct ring_buffer_event *rbe, |
| 174 | void *event) |
| 175 | { |
| 176 | char *addr = (char *)(event + hist_field->field->offset); |
| 177 | |
| 178 | return (u64)(unsigned long)addr; |
| 179 | } |
| 180 | |
| 181 | static u64 hist_field_dynstring(struct hist_field *hist_field, |
| 182 | struct tracing_map_elt *elt, |
| 183 | struct ring_buffer_event *rbe, |
| 184 | void *event) |
| 185 | { |
| 186 | u32 str_item = *(u32 *)(event + hist_field->field->offset); |
| 187 | int str_loc = str_item & 0xffff; |
| 188 | char *addr = (char *)(event + str_loc); |
| 189 | |
| 190 | return (u64)(unsigned long)addr; |
| 191 | } |
| 192 | |
| 193 | static u64 hist_field_pstring(struct hist_field *hist_field, |
| 194 | struct tracing_map_elt *elt, |
| 195 | struct ring_buffer_event *rbe, |
| 196 | void *event) |
| 197 | { |
| 198 | char **addr = (char **)(event + hist_field->field->offset); |
| 199 | |
| 200 | return (u64)(unsigned long)*addr; |
| 201 | } |
| 202 | |
| 203 | static u64 hist_field_log2(struct hist_field *hist_field, |
| 204 | struct tracing_map_elt *elt, |
| 205 | struct ring_buffer_event *rbe, |
| 206 | void *event) |
| 207 | { |
| 208 | struct hist_field *operand = hist_field->operands[0]; |
| 209 | |
| 210 | u64 val = operand->fn(operand, elt, rbe, event); |
| 211 | |
| 212 | return (u64) ilog2(roundup_pow_of_two(val)); |
| 213 | } |
| 214 | |
| 215 | static u64 hist_field_plus(struct hist_field *hist_field, |
| 216 | struct tracing_map_elt *elt, |
| 217 | struct ring_buffer_event *rbe, |
| 218 | void *event) |
| 219 | { |
| 220 | struct hist_field *operand1 = hist_field->operands[0]; |
| 221 | struct hist_field *operand2 = hist_field->operands[1]; |
| 222 | |
| 223 | u64 val1 = operand1->fn(operand1, elt, rbe, event); |
| 224 | u64 val2 = operand2->fn(operand2, elt, rbe, event); |
| 225 | |
| 226 | return val1 + val2; |
| 227 | } |
| 228 | |
| 229 | static u64 hist_field_minus(struct hist_field *hist_field, |
| 230 | struct tracing_map_elt *elt, |
| 231 | struct ring_buffer_event *rbe, |
| 232 | void *event) |
| 233 | { |
| 234 | struct hist_field *operand1 = hist_field->operands[0]; |
| 235 | struct hist_field *operand2 = hist_field->operands[1]; |
| 236 | |
| 237 | u64 val1 = operand1->fn(operand1, elt, rbe, event); |
| 238 | u64 val2 = operand2->fn(operand2, elt, rbe, event); |
| 239 | |
| 240 | return val1 - val2; |
| 241 | } |
| 242 | |
| 243 | static u64 hist_field_unary_minus(struct hist_field *hist_field, |
| 244 | struct tracing_map_elt *elt, |
| 245 | struct ring_buffer_event *rbe, |
| 246 | void *event) |
| 247 | { |
| 248 | struct hist_field *operand = hist_field->operands[0]; |
| 249 | |
| 250 | s64 sval = (s64)operand->fn(operand, elt, rbe, event); |
| 251 | u64 val = (u64)-sval; |
| 252 | |
| 253 | return val; |
| 254 | } |
| 255 | |
| 256 | #define DEFINE_HIST_FIELD_FN(type) \ |
| 257 | static u64 hist_field_##type(struct hist_field *hist_field, \ |
| 258 | struct tracing_map_elt *elt, \ |
| 259 | struct ring_buffer_event *rbe, \ |
| 260 | void *event) \ |
| 261 | { \ |
| 262 | type *addr = (type *)(event + hist_field->field->offset); \ |
| 263 | \ |
| 264 | return (u64)(unsigned long)*addr; \ |
| 265 | } |
| 266 | |
| 267 | DEFINE_HIST_FIELD_FN(s64); |
| 268 | DEFINE_HIST_FIELD_FN(u64); |
| 269 | DEFINE_HIST_FIELD_FN(s32); |
| 270 | DEFINE_HIST_FIELD_FN(u32); |
| 271 | DEFINE_HIST_FIELD_FN(s16); |
| 272 | DEFINE_HIST_FIELD_FN(u16); |
| 273 | DEFINE_HIST_FIELD_FN(s8); |
| 274 | DEFINE_HIST_FIELD_FN(u8); |
| 275 | |
| 276 | #define for_each_hist_field(i, hist_data) \ |
| 277 | for ((i) = 0; (i) < (hist_data)->n_fields; (i)++) |
| 278 | |
| 279 | #define for_each_hist_val_field(i, hist_data) \ |
| 280 | for ((i) = 0; (i) < (hist_data)->n_vals; (i)++) |
| 281 | |
| 282 | #define for_each_hist_key_field(i, hist_data) \ |
| 283 | for ((i) = (hist_data)->n_vals; (i) < (hist_data)->n_fields; (i)++) |
| 284 | |
| 285 | #define HIST_STACKTRACE_DEPTH 16 |
| 286 | #define HIST_STACKTRACE_SIZE (HIST_STACKTRACE_DEPTH * sizeof(unsigned long)) |
| 287 | #define HIST_STACKTRACE_SKIP 5 |
| 288 | |
| 289 | #define HITCOUNT_IDX 0 |
| 290 | #define HIST_KEY_SIZE_MAX (MAX_FILTER_STR_VAL + HIST_STACKTRACE_SIZE) |
| 291 | |
| 292 | enum hist_field_flags { |
| 293 | HIST_FIELD_FL_HITCOUNT = 1 << 0, |
| 294 | HIST_FIELD_FL_KEY = 1 << 1, |
| 295 | HIST_FIELD_FL_STRING = 1 << 2, |
| 296 | HIST_FIELD_FL_HEX = 1 << 3, |
| 297 | HIST_FIELD_FL_SYM = 1 << 4, |
| 298 | HIST_FIELD_FL_SYM_OFFSET = 1 << 5, |
| 299 | HIST_FIELD_FL_EXECNAME = 1 << 6, |
| 300 | HIST_FIELD_FL_SYSCALL = 1 << 7, |
| 301 | HIST_FIELD_FL_STACKTRACE = 1 << 8, |
| 302 | HIST_FIELD_FL_LOG2 = 1 << 9, |
| 303 | HIST_FIELD_FL_TIMESTAMP = 1 << 10, |
| 304 | HIST_FIELD_FL_TIMESTAMP_USECS = 1 << 11, |
| 305 | HIST_FIELD_FL_VAR = 1 << 12, |
| 306 | HIST_FIELD_FL_EXPR = 1 << 13, |
| 307 | HIST_FIELD_FL_VAR_REF = 1 << 14, |
| 308 | HIST_FIELD_FL_CPU = 1 << 15, |
| 309 | HIST_FIELD_FL_ALIAS = 1 << 16, |
| 310 | }; |
| 311 | |
| 312 | struct var_defs { |
| 313 | unsigned int n_vars; |
| 314 | char *name[TRACING_MAP_VARS_MAX]; |
| 315 | char *expr[TRACING_MAP_VARS_MAX]; |
| 316 | }; |
| 317 | |
| 318 | struct hist_trigger_attrs { |
| 319 | char *keys_str; |
| 320 | char *vals_str; |
| 321 | char *sort_key_str; |
| 322 | char *name; |
| 323 | char *clock; |
| 324 | bool pause; |
| 325 | bool cont; |
| 326 | bool clear; |
| 327 | bool ts_in_usecs; |
| 328 | unsigned int map_bits; |
| 329 | |
| 330 | char *assignment_str[TRACING_MAP_VARS_MAX]; |
| 331 | unsigned int n_assignments; |
| 332 | |
| 333 | char *action_str[HIST_ACTIONS_MAX]; |
| 334 | unsigned int n_actions; |
| 335 | |
| 336 | struct var_defs var_defs; |
| 337 | }; |
| 338 | |
| 339 | struct field_var { |
| 340 | struct hist_field *var; |
| 341 | struct hist_field *val; |
| 342 | }; |
| 343 | |
| 344 | struct field_var_hist { |
| 345 | struct hist_trigger_data *hist_data; |
| 346 | char *cmd; |
| 347 | }; |
| 348 | |
| 349 | struct hist_trigger_data { |
| 350 | struct hist_field *fields[HIST_FIELDS_MAX]; |
| 351 | unsigned int n_vals; |
| 352 | unsigned int n_keys; |
| 353 | unsigned int n_fields; |
| 354 | unsigned int n_vars; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 355 | unsigned int n_var_str; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 356 | unsigned int key_size; |
| 357 | struct tracing_map_sort_key sort_keys[TRACING_MAP_SORT_KEYS_MAX]; |
| 358 | unsigned int n_sort_keys; |
| 359 | struct trace_event_file *event_file; |
| 360 | struct hist_trigger_attrs *attrs; |
| 361 | struct tracing_map *map; |
| 362 | bool enable_timestamps; |
| 363 | bool remove; |
| 364 | struct hist_field *var_refs[TRACING_MAP_VARS_MAX]; |
| 365 | unsigned int n_var_refs; |
| 366 | |
| 367 | struct action_data *actions[HIST_ACTIONS_MAX]; |
| 368 | unsigned int n_actions; |
| 369 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 370 | struct field_var *field_vars[SYNTH_FIELDS_MAX]; |
| 371 | unsigned int n_field_vars; |
| 372 | unsigned int n_field_var_str; |
| 373 | struct field_var_hist *field_var_hists[SYNTH_FIELDS_MAX]; |
| 374 | unsigned int n_field_var_hists; |
| 375 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 376 | struct field_var *save_vars[SYNTH_FIELDS_MAX]; |
| 377 | unsigned int n_save_vars; |
| 378 | unsigned int n_save_var_str; |
| 379 | }; |
| 380 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 381 | struct action_data; |
| 382 | |
| 383 | typedef void (*action_fn_t) (struct hist_trigger_data *hist_data, |
| 384 | struct tracing_map_elt *elt, void *rec, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 385 | struct ring_buffer_event *rbe, void *key, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 386 | struct action_data *data, u64 *var_ref_vals); |
| 387 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 388 | typedef bool (*check_track_val_fn_t) (u64 track_val, u64 var_val); |
| 389 | |
| 390 | enum handler_id { |
| 391 | HANDLER_ONMATCH = 1, |
| 392 | HANDLER_ONMAX, |
| 393 | HANDLER_ONCHANGE, |
| 394 | }; |
| 395 | |
| 396 | enum action_id { |
| 397 | ACTION_SAVE = 1, |
| 398 | ACTION_TRACE, |
| 399 | ACTION_SNAPSHOT, |
| 400 | }; |
| 401 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 402 | struct action_data { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 403 | enum handler_id handler; |
| 404 | enum action_id action; |
| 405 | char *action_name; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 406 | action_fn_t fn; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 407 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 408 | unsigned int n_params; |
| 409 | char *params[SYNTH_FIELDS_MAX]; |
| 410 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 411 | /* |
| 412 | * When a histogram trigger is hit, the values of any |
| 413 | * references to variables, including variables being passed |
| 414 | * as parameters to synthetic events, are collected into a |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 415 | * var_ref_vals array. This var_ref_idx array is an array of |
| 416 | * indices into the var_ref_vals array, one for each synthetic |
| 417 | * event param, and is passed to the synthetic event |
| 418 | * invocation. |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 419 | */ |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 420 | unsigned int var_ref_idx[TRACING_MAP_VARS_MAX]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 421 | struct synth_event *synth_event; |
| 422 | bool use_trace_keyword; |
| 423 | char *synth_event_name; |
| 424 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 425 | union { |
| 426 | struct { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 427 | char *event; |
| 428 | char *event_system; |
| 429 | } match_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 430 | |
| 431 | struct { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 432 | /* |
| 433 | * var_str contains the $-unstripped variable |
| 434 | * name referenced by var_ref, and used when |
| 435 | * printing the action. Because var_ref |
| 436 | * creation is deferred to create_actions(), |
| 437 | * we need a per-action way to save it until |
| 438 | * then, thus var_str. |
| 439 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 440 | char *var_str; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 441 | |
| 442 | /* |
| 443 | * var_ref refers to the variable being |
| 444 | * tracked e.g onmax($var). |
| 445 | */ |
| 446 | struct hist_field *var_ref; |
| 447 | |
| 448 | /* |
| 449 | * track_var contains the 'invisible' tracking |
| 450 | * variable created to keep the current |
| 451 | * e.g. max value. |
| 452 | */ |
| 453 | struct hist_field *track_var; |
| 454 | |
| 455 | check_track_val_fn_t check_val; |
| 456 | action_fn_t save_data; |
| 457 | } track_data; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 458 | }; |
| 459 | }; |
| 460 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 461 | struct track_data { |
| 462 | u64 track_val; |
| 463 | bool updated; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 464 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 465 | unsigned int key_len; |
| 466 | void *key; |
| 467 | struct tracing_map_elt elt; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 468 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 469 | struct action_data *action_data; |
| 470 | struct hist_trigger_data *hist_data; |
| 471 | }; |
| 472 | |
| 473 | struct hist_elt_data { |
| 474 | char *comm; |
| 475 | u64 *var_ref_vals; |
| 476 | char *field_var_str[SYNTH_FIELDS_MAX]; |
| 477 | }; |
| 478 | |
| 479 | struct snapshot_context { |
| 480 | struct tracing_map_elt *elt; |
| 481 | void *key; |
| 482 | }; |
| 483 | |
| 484 | static void track_data_free(struct track_data *track_data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 485 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 486 | struct hist_elt_data *elt_data; |
| 487 | |
| 488 | if (!track_data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 489 | return; |
| 490 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 491 | kfree(track_data->key); |
| 492 | |
| 493 | elt_data = track_data->elt.private_data; |
| 494 | if (elt_data) { |
| 495 | kfree(elt_data->comm); |
| 496 | kfree(elt_data); |
| 497 | } |
| 498 | |
| 499 | kfree(track_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 500 | } |
| 501 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 502 | static struct track_data *track_data_alloc(unsigned int key_len, |
| 503 | struct action_data *action_data, |
| 504 | struct hist_trigger_data *hist_data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 505 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 506 | struct track_data *data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 507 | struct hist_elt_data *elt_data; |
| 508 | |
| 509 | if (!data) |
| 510 | return ERR_PTR(-ENOMEM); |
| 511 | |
| 512 | data->key = kzalloc(key_len, GFP_KERNEL); |
| 513 | if (!data->key) { |
| 514 | track_data_free(data); |
| 515 | return ERR_PTR(-ENOMEM); |
| 516 | } |
| 517 | |
| 518 | data->key_len = key_len; |
| 519 | data->action_data = action_data; |
| 520 | data->hist_data = hist_data; |
| 521 | |
| 522 | elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL); |
| 523 | if (!elt_data) { |
| 524 | track_data_free(data); |
| 525 | return ERR_PTR(-ENOMEM); |
| 526 | } |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 527 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 528 | data->elt.private_data = elt_data; |
| 529 | |
| 530 | elt_data->comm = kzalloc(TASK_COMM_LEN, GFP_KERNEL); |
| 531 | if (!elt_data->comm) { |
| 532 | track_data_free(data); |
| 533 | return ERR_PTR(-ENOMEM); |
| 534 | } |
| 535 | |
| 536 | return data; |
| 537 | } |
| 538 | |
| 539 | static char last_cmd[MAX_FILTER_STR_VAL]; |
| 540 | static char last_cmd_loc[MAX_FILTER_STR_VAL]; |
| 541 | |
| 542 | static int errpos(char *str) |
| 543 | { |
| 544 | return err_pos(last_cmd, str); |
| 545 | } |
| 546 | |
| 547 | static void last_cmd_set(struct trace_event_file *file, char *str) |
| 548 | { |
| 549 | const char *system = NULL, *name = NULL; |
| 550 | struct trace_event_call *call; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 551 | |
| 552 | if (!str) |
| 553 | return; |
| 554 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 555 | strcpy(last_cmd, "hist:"); |
| 556 | strncat(last_cmd, str, MAX_FILTER_STR_VAL - 1 - sizeof("hist:")); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 557 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 558 | if (file) { |
| 559 | call = file->event_call; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 560 | system = call->class->system; |
| 561 | if (system) { |
| 562 | name = trace_event_name(call); |
| 563 | if (!name) |
| 564 | system = NULL; |
| 565 | } |
| 566 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 567 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 568 | if (system) |
| 569 | snprintf(last_cmd_loc, MAX_FILTER_STR_VAL, "hist:%s:%s", system, name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 570 | } |
| 571 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 572 | static void hist_err(struct trace_array *tr, u8 err_type, u8 err_pos) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 573 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 574 | tracing_log_err(tr, last_cmd_loc, last_cmd, err_text, |
| 575 | err_type, err_pos); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 576 | } |
| 577 | |
| 578 | static void hist_err_clear(void) |
| 579 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 580 | last_cmd[0] = '\0'; |
| 581 | last_cmd_loc[0] = '\0'; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 582 | } |
| 583 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 584 | typedef void (*synth_probe_func_t) (void *__data, u64 *var_ref_vals, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 585 | unsigned int *var_ref_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 586 | |
| 587 | static inline void trace_synth(struct synth_event *event, u64 *var_ref_vals, |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 588 | unsigned int *var_ref_idx) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 589 | { |
| 590 | struct tracepoint *tp = event->tp; |
| 591 | |
| 592 | if (unlikely(atomic_read(&tp->key.enabled) > 0)) { |
| 593 | struct tracepoint_func *probe_func_ptr; |
| 594 | synth_probe_func_t probe_func; |
| 595 | void *__data; |
| 596 | |
| 597 | if (!(cpu_online(raw_smp_processor_id()))) |
| 598 | return; |
| 599 | |
| 600 | probe_func_ptr = rcu_dereference_sched((tp)->funcs); |
| 601 | if (probe_func_ptr) { |
| 602 | do { |
| 603 | probe_func = probe_func_ptr->func; |
| 604 | __data = probe_func_ptr->data; |
| 605 | probe_func(__data, var_ref_vals, var_ref_idx); |
| 606 | } while ((++probe_func_ptr)->func); |
| 607 | } |
| 608 | } |
| 609 | } |
| 610 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 611 | static void action_trace(struct hist_trigger_data *hist_data, |
| 612 | struct tracing_map_elt *elt, void *rec, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 613 | struct ring_buffer_event *rbe, void *key, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 614 | struct action_data *data, u64 *var_ref_vals) |
| 615 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 616 | struct synth_event *event = data->synth_event; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 617 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 618 | trace_synth(event, var_ref_vals, data->var_ref_idx); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 619 | } |
| 620 | |
| 621 | struct hist_var_data { |
| 622 | struct list_head list; |
| 623 | struct hist_trigger_data *hist_data; |
| 624 | }; |
| 625 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 626 | static u64 hist_field_timestamp(struct hist_field *hist_field, |
| 627 | struct tracing_map_elt *elt, |
| 628 | struct ring_buffer_event *rbe, |
| 629 | void *event) |
| 630 | { |
| 631 | struct hist_trigger_data *hist_data = hist_field->hist_data; |
| 632 | struct trace_array *tr = hist_data->event_file->tr; |
| 633 | |
| 634 | u64 ts = ring_buffer_event_time_stamp(rbe); |
| 635 | |
| 636 | if (hist_data->attrs->ts_in_usecs && trace_clock_in_ns(tr)) |
| 637 | ts = ns2usecs(ts); |
| 638 | |
| 639 | return ts; |
| 640 | } |
| 641 | |
| 642 | static u64 hist_field_cpu(struct hist_field *hist_field, |
| 643 | struct tracing_map_elt *elt, |
| 644 | struct ring_buffer_event *rbe, |
| 645 | void *event) |
| 646 | { |
| 647 | int cpu = smp_processor_id(); |
| 648 | |
| 649 | return cpu; |
| 650 | } |
| 651 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 652 | /** |
| 653 | * check_field_for_var_ref - Check if a VAR_REF field references a variable |
| 654 | * @hist_field: The VAR_REF field to check |
| 655 | * @var_data: The hist trigger that owns the variable |
| 656 | * @var_idx: The trigger variable identifier |
| 657 | * |
| 658 | * Check the given VAR_REF field to see whether or not it references |
| 659 | * the given variable associated with the given trigger. |
| 660 | * |
| 661 | * Return: The VAR_REF field if it does reference the variable, NULL if not |
| 662 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 663 | static struct hist_field * |
| 664 | check_field_for_var_ref(struct hist_field *hist_field, |
| 665 | struct hist_trigger_data *var_data, |
| 666 | unsigned int var_idx) |
| 667 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 668 | WARN_ON(!(hist_field && hist_field->flags & HIST_FIELD_FL_VAR_REF)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 669 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 670 | if (hist_field && hist_field->var.idx == var_idx && |
| 671 | hist_field->var.hist_data == var_data) |
| 672 | return hist_field; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 673 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 674 | return NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 675 | } |
| 676 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 677 | /** |
| 678 | * find_var_ref - Check if a trigger has a reference to a trigger variable |
| 679 | * @hist_data: The hist trigger that might have a reference to the variable |
| 680 | * @var_data: The hist trigger that owns the variable |
| 681 | * @var_idx: The trigger variable identifier |
| 682 | * |
| 683 | * Check the list of var_refs[] on the first hist trigger to see |
| 684 | * whether any of them are references to the variable on the second |
| 685 | * trigger. |
| 686 | * |
| 687 | * Return: The VAR_REF field referencing the variable if so, NULL if not |
| 688 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 689 | static struct hist_field *find_var_ref(struct hist_trigger_data *hist_data, |
| 690 | struct hist_trigger_data *var_data, |
| 691 | unsigned int var_idx) |
| 692 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 693 | struct hist_field *hist_field; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 694 | unsigned int i; |
| 695 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 696 | for (i = 0; i < hist_data->n_var_refs; i++) { |
| 697 | hist_field = hist_data->var_refs[i]; |
| 698 | if (check_field_for_var_ref(hist_field, var_data, var_idx)) |
| 699 | return hist_field; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 700 | } |
| 701 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 702 | return NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 703 | } |
| 704 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 705 | /** |
| 706 | * find_any_var_ref - Check if there is a reference to a given trigger variable |
| 707 | * @hist_data: The hist trigger |
| 708 | * @var_idx: The trigger variable identifier |
| 709 | * |
| 710 | * Check to see whether the given variable is currently referenced by |
| 711 | * any other trigger. |
| 712 | * |
| 713 | * The trigger the variable is defined on is explicitly excluded - the |
| 714 | * assumption being that a self-reference doesn't prevent a trigger |
| 715 | * from being removed. |
| 716 | * |
| 717 | * Return: The VAR_REF field referencing the variable if so, NULL if not |
| 718 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 719 | static struct hist_field *find_any_var_ref(struct hist_trigger_data *hist_data, |
| 720 | unsigned int var_idx) |
| 721 | { |
| 722 | struct trace_array *tr = hist_data->event_file->tr; |
| 723 | struct hist_field *found = NULL; |
| 724 | struct hist_var_data *var_data; |
| 725 | |
| 726 | list_for_each_entry(var_data, &tr->hist_vars, list) { |
| 727 | if (var_data->hist_data == hist_data) |
| 728 | continue; |
| 729 | found = find_var_ref(var_data->hist_data, hist_data, var_idx); |
| 730 | if (found) |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | return found; |
| 735 | } |
| 736 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 737 | /** |
| 738 | * check_var_refs - Check if there is a reference to any of trigger's variables |
| 739 | * @hist_data: The hist trigger |
| 740 | * |
| 741 | * A trigger can define one or more variables. If any one of them is |
| 742 | * currently referenced by any other trigger, this function will |
| 743 | * determine that. |
| 744 | |
| 745 | * Typically used to determine whether or not a trigger can be removed |
| 746 | * - if there are any references to a trigger's variables, it cannot. |
| 747 | * |
| 748 | * Return: True if there is a reference to any of trigger's variables |
| 749 | */ |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 750 | static bool check_var_refs(struct hist_trigger_data *hist_data) |
| 751 | { |
| 752 | struct hist_field *field; |
| 753 | bool found = false; |
| 754 | int i; |
| 755 | |
| 756 | for_each_hist_field(i, hist_data) { |
| 757 | field = hist_data->fields[i]; |
| 758 | if (field && field->flags & HIST_FIELD_FL_VAR) { |
| 759 | if (find_any_var_ref(hist_data, field->var.idx)) { |
| 760 | found = true; |
| 761 | break; |
| 762 | } |
| 763 | } |
| 764 | } |
| 765 | |
| 766 | return found; |
| 767 | } |
| 768 | |
| 769 | static struct hist_var_data *find_hist_vars(struct hist_trigger_data *hist_data) |
| 770 | { |
| 771 | struct trace_array *tr = hist_data->event_file->tr; |
| 772 | struct hist_var_data *var_data, *found = NULL; |
| 773 | |
| 774 | list_for_each_entry(var_data, &tr->hist_vars, list) { |
| 775 | if (var_data->hist_data == hist_data) { |
| 776 | found = var_data; |
| 777 | break; |
| 778 | } |
| 779 | } |
| 780 | |
| 781 | return found; |
| 782 | } |
| 783 | |
| 784 | static bool field_has_hist_vars(struct hist_field *hist_field, |
| 785 | unsigned int level) |
| 786 | { |
| 787 | int i; |
| 788 | |
| 789 | if (level > 3) |
| 790 | return false; |
| 791 | |
| 792 | if (!hist_field) |
| 793 | return false; |
| 794 | |
| 795 | if (hist_field->flags & HIST_FIELD_FL_VAR || |
| 796 | hist_field->flags & HIST_FIELD_FL_VAR_REF) |
| 797 | return true; |
| 798 | |
| 799 | for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) { |
| 800 | struct hist_field *operand; |
| 801 | |
| 802 | operand = hist_field->operands[i]; |
| 803 | if (field_has_hist_vars(operand, level + 1)) |
| 804 | return true; |
| 805 | } |
| 806 | |
| 807 | return false; |
| 808 | } |
| 809 | |
| 810 | static bool has_hist_vars(struct hist_trigger_data *hist_data) |
| 811 | { |
| 812 | struct hist_field *hist_field; |
| 813 | int i; |
| 814 | |
| 815 | for_each_hist_field(i, hist_data) { |
| 816 | hist_field = hist_data->fields[i]; |
| 817 | if (field_has_hist_vars(hist_field, 0)) |
| 818 | return true; |
| 819 | } |
| 820 | |
| 821 | return false; |
| 822 | } |
| 823 | |
| 824 | static int save_hist_vars(struct hist_trigger_data *hist_data) |
| 825 | { |
| 826 | struct trace_array *tr = hist_data->event_file->tr; |
| 827 | struct hist_var_data *var_data; |
| 828 | |
| 829 | var_data = find_hist_vars(hist_data); |
| 830 | if (var_data) |
| 831 | return 0; |
| 832 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 833 | if (tracing_check_open_get_tr(tr)) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 834 | return -ENODEV; |
| 835 | |
| 836 | var_data = kzalloc(sizeof(*var_data), GFP_KERNEL); |
| 837 | if (!var_data) { |
| 838 | trace_array_put(tr); |
| 839 | return -ENOMEM; |
| 840 | } |
| 841 | |
| 842 | var_data->hist_data = hist_data; |
| 843 | list_add(&var_data->list, &tr->hist_vars); |
| 844 | |
| 845 | return 0; |
| 846 | } |
| 847 | |
| 848 | static void remove_hist_vars(struct hist_trigger_data *hist_data) |
| 849 | { |
| 850 | struct trace_array *tr = hist_data->event_file->tr; |
| 851 | struct hist_var_data *var_data; |
| 852 | |
| 853 | var_data = find_hist_vars(hist_data); |
| 854 | if (!var_data) |
| 855 | return; |
| 856 | |
| 857 | if (WARN_ON(check_var_refs(hist_data))) |
| 858 | return; |
| 859 | |
| 860 | list_del(&var_data->list); |
| 861 | |
| 862 | kfree(var_data); |
| 863 | |
| 864 | trace_array_put(tr); |
| 865 | } |
| 866 | |
| 867 | static struct hist_field *find_var_field(struct hist_trigger_data *hist_data, |
| 868 | const char *var_name) |
| 869 | { |
| 870 | struct hist_field *hist_field, *found = NULL; |
| 871 | int i; |
| 872 | |
| 873 | for_each_hist_field(i, hist_data) { |
| 874 | hist_field = hist_data->fields[i]; |
| 875 | if (hist_field && hist_field->flags & HIST_FIELD_FL_VAR && |
| 876 | strcmp(hist_field->var.name, var_name) == 0) { |
| 877 | found = hist_field; |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | |
| 882 | return found; |
| 883 | } |
| 884 | |
| 885 | static struct hist_field *find_var(struct hist_trigger_data *hist_data, |
| 886 | struct trace_event_file *file, |
| 887 | const char *var_name) |
| 888 | { |
| 889 | struct hist_trigger_data *test_data; |
| 890 | struct event_trigger_data *test; |
| 891 | struct hist_field *hist_field; |
| 892 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 893 | lockdep_assert_held(&event_mutex); |
| 894 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 895 | hist_field = find_var_field(hist_data, var_name); |
| 896 | if (hist_field) |
| 897 | return hist_field; |
| 898 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 899 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 900 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 901 | test_data = test->private_data; |
| 902 | hist_field = find_var_field(test_data, var_name); |
| 903 | if (hist_field) |
| 904 | return hist_field; |
| 905 | } |
| 906 | } |
| 907 | |
| 908 | return NULL; |
| 909 | } |
| 910 | |
| 911 | static struct trace_event_file *find_var_file(struct trace_array *tr, |
| 912 | char *system, |
| 913 | char *event_name, |
| 914 | char *var_name) |
| 915 | { |
| 916 | struct hist_trigger_data *var_hist_data; |
| 917 | struct hist_var_data *var_data; |
| 918 | struct trace_event_file *file, *found = NULL; |
| 919 | |
| 920 | if (system) |
| 921 | return find_event_file(tr, system, event_name); |
| 922 | |
| 923 | list_for_each_entry(var_data, &tr->hist_vars, list) { |
| 924 | var_hist_data = var_data->hist_data; |
| 925 | file = var_hist_data->event_file; |
| 926 | if (file == found) |
| 927 | continue; |
| 928 | |
| 929 | if (find_var_field(var_hist_data, var_name)) { |
| 930 | if (found) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 931 | hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, errpos(var_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 932 | return NULL; |
| 933 | } |
| 934 | |
| 935 | found = file; |
| 936 | } |
| 937 | } |
| 938 | |
| 939 | return found; |
| 940 | } |
| 941 | |
| 942 | static struct hist_field *find_file_var(struct trace_event_file *file, |
| 943 | const char *var_name) |
| 944 | { |
| 945 | struct hist_trigger_data *test_data; |
| 946 | struct event_trigger_data *test; |
| 947 | struct hist_field *hist_field; |
| 948 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 949 | lockdep_assert_held(&event_mutex); |
| 950 | |
| 951 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 952 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 953 | test_data = test->private_data; |
| 954 | hist_field = find_var_field(test_data, var_name); |
| 955 | if (hist_field) |
| 956 | return hist_field; |
| 957 | } |
| 958 | } |
| 959 | |
| 960 | return NULL; |
| 961 | } |
| 962 | |
| 963 | static struct hist_field * |
| 964 | find_match_var(struct hist_trigger_data *hist_data, char *var_name) |
| 965 | { |
| 966 | struct trace_array *tr = hist_data->event_file->tr; |
| 967 | struct hist_field *hist_field, *found = NULL; |
| 968 | struct trace_event_file *file; |
| 969 | unsigned int i; |
| 970 | |
| 971 | for (i = 0; i < hist_data->n_actions; i++) { |
| 972 | struct action_data *data = hist_data->actions[i]; |
| 973 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 974 | if (data->handler == HANDLER_ONMATCH) { |
| 975 | char *system = data->match_data.event_system; |
| 976 | char *event_name = data->match_data.event; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 977 | |
| 978 | file = find_var_file(tr, system, event_name, var_name); |
| 979 | if (!file) |
| 980 | continue; |
| 981 | hist_field = find_file_var(file, var_name); |
| 982 | if (hist_field) { |
| 983 | if (found) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 984 | hist_err(tr, HIST_ERR_VAR_NOT_UNIQUE, |
| 985 | errpos(var_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 986 | return ERR_PTR(-EINVAL); |
| 987 | } |
| 988 | |
| 989 | found = hist_field; |
| 990 | } |
| 991 | } |
| 992 | } |
| 993 | return found; |
| 994 | } |
| 995 | |
| 996 | static struct hist_field *find_event_var(struct hist_trigger_data *hist_data, |
| 997 | char *system, |
| 998 | char *event_name, |
| 999 | char *var_name) |
| 1000 | { |
| 1001 | struct trace_array *tr = hist_data->event_file->tr; |
| 1002 | struct hist_field *hist_field = NULL; |
| 1003 | struct trace_event_file *file; |
| 1004 | |
| 1005 | if (!system || !event_name) { |
| 1006 | hist_field = find_match_var(hist_data, var_name); |
| 1007 | if (IS_ERR(hist_field)) |
| 1008 | return NULL; |
| 1009 | if (hist_field) |
| 1010 | return hist_field; |
| 1011 | } |
| 1012 | |
| 1013 | file = find_var_file(tr, system, event_name, var_name); |
| 1014 | if (!file) |
| 1015 | return NULL; |
| 1016 | |
| 1017 | hist_field = find_file_var(file, var_name); |
| 1018 | |
| 1019 | return hist_field; |
| 1020 | } |
| 1021 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1022 | static u64 hist_field_var_ref(struct hist_field *hist_field, |
| 1023 | struct tracing_map_elt *elt, |
| 1024 | struct ring_buffer_event *rbe, |
| 1025 | void *event) |
| 1026 | { |
| 1027 | struct hist_elt_data *elt_data; |
| 1028 | u64 var_val = 0; |
| 1029 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1030 | if (WARN_ON_ONCE(!elt)) |
| 1031 | return var_val; |
| 1032 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1033 | elt_data = elt->private_data; |
| 1034 | var_val = elt_data->var_ref_vals[hist_field->var_ref_idx]; |
| 1035 | |
| 1036 | return var_val; |
| 1037 | } |
| 1038 | |
| 1039 | static bool resolve_var_refs(struct hist_trigger_data *hist_data, void *key, |
| 1040 | u64 *var_ref_vals, bool self) |
| 1041 | { |
| 1042 | struct hist_trigger_data *var_data; |
| 1043 | struct tracing_map_elt *var_elt; |
| 1044 | struct hist_field *hist_field; |
| 1045 | unsigned int i, var_idx; |
| 1046 | bool resolved = true; |
| 1047 | u64 var_val = 0; |
| 1048 | |
| 1049 | for (i = 0; i < hist_data->n_var_refs; i++) { |
| 1050 | hist_field = hist_data->var_refs[i]; |
| 1051 | var_idx = hist_field->var.idx; |
| 1052 | var_data = hist_field->var.hist_data; |
| 1053 | |
| 1054 | if (var_data == NULL) { |
| 1055 | resolved = false; |
| 1056 | break; |
| 1057 | } |
| 1058 | |
| 1059 | if ((self && var_data != hist_data) || |
| 1060 | (!self && var_data == hist_data)) |
| 1061 | continue; |
| 1062 | |
| 1063 | var_elt = tracing_map_lookup(var_data->map, key); |
| 1064 | if (!var_elt) { |
| 1065 | resolved = false; |
| 1066 | break; |
| 1067 | } |
| 1068 | |
| 1069 | if (!tracing_map_var_set(var_elt, var_idx)) { |
| 1070 | resolved = false; |
| 1071 | break; |
| 1072 | } |
| 1073 | |
| 1074 | if (self || !hist_field->read_once) |
| 1075 | var_val = tracing_map_read_var(var_elt, var_idx); |
| 1076 | else |
| 1077 | var_val = tracing_map_read_var_once(var_elt, var_idx); |
| 1078 | |
| 1079 | var_ref_vals[i] = var_val; |
| 1080 | } |
| 1081 | |
| 1082 | return resolved; |
| 1083 | } |
| 1084 | |
| 1085 | static const char *hist_field_name(struct hist_field *field, |
| 1086 | unsigned int level) |
| 1087 | { |
| 1088 | const char *field_name = ""; |
| 1089 | |
| 1090 | if (level > 1) |
| 1091 | return field_name; |
| 1092 | |
| 1093 | if (field->field) |
| 1094 | field_name = field->field->name; |
| 1095 | else if (field->flags & HIST_FIELD_FL_LOG2 || |
| 1096 | field->flags & HIST_FIELD_FL_ALIAS) |
| 1097 | field_name = hist_field_name(field->operands[0], ++level); |
| 1098 | else if (field->flags & HIST_FIELD_FL_CPU) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1099 | field_name = "common_cpu"; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1100 | else if (field->flags & HIST_FIELD_FL_EXPR || |
| 1101 | field->flags & HIST_FIELD_FL_VAR_REF) { |
| 1102 | if (field->system) { |
| 1103 | static char full_name[MAX_FILTER_STR_VAL]; |
| 1104 | |
| 1105 | strcat(full_name, field->system); |
| 1106 | strcat(full_name, "."); |
| 1107 | strcat(full_name, field->event_name); |
| 1108 | strcat(full_name, "."); |
| 1109 | strcat(full_name, field->name); |
| 1110 | field_name = full_name; |
| 1111 | } else |
| 1112 | field_name = field->name; |
| 1113 | } else if (field->flags & HIST_FIELD_FL_TIMESTAMP) |
| 1114 | field_name = "common_timestamp"; |
| 1115 | |
| 1116 | if (field_name == NULL) |
| 1117 | field_name = ""; |
| 1118 | |
| 1119 | return field_name; |
| 1120 | } |
| 1121 | |
| 1122 | static hist_field_fn_t select_value_fn(int field_size, int field_is_signed) |
| 1123 | { |
| 1124 | hist_field_fn_t fn = NULL; |
| 1125 | |
| 1126 | switch (field_size) { |
| 1127 | case 8: |
| 1128 | if (field_is_signed) |
| 1129 | fn = hist_field_s64; |
| 1130 | else |
| 1131 | fn = hist_field_u64; |
| 1132 | break; |
| 1133 | case 4: |
| 1134 | if (field_is_signed) |
| 1135 | fn = hist_field_s32; |
| 1136 | else |
| 1137 | fn = hist_field_u32; |
| 1138 | break; |
| 1139 | case 2: |
| 1140 | if (field_is_signed) |
| 1141 | fn = hist_field_s16; |
| 1142 | else |
| 1143 | fn = hist_field_u16; |
| 1144 | break; |
| 1145 | case 1: |
| 1146 | if (field_is_signed) |
| 1147 | fn = hist_field_s8; |
| 1148 | else |
| 1149 | fn = hist_field_u8; |
| 1150 | break; |
| 1151 | } |
| 1152 | |
| 1153 | return fn; |
| 1154 | } |
| 1155 | |
| 1156 | static int parse_map_size(char *str) |
| 1157 | { |
| 1158 | unsigned long size, map_bits; |
| 1159 | int ret; |
| 1160 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1161 | ret = kstrtoul(str, 0, &size); |
| 1162 | if (ret) |
| 1163 | goto out; |
| 1164 | |
| 1165 | map_bits = ilog2(roundup_pow_of_two(size)); |
| 1166 | if (map_bits < TRACING_MAP_BITS_MIN || |
| 1167 | map_bits > TRACING_MAP_BITS_MAX) |
| 1168 | ret = -EINVAL; |
| 1169 | else |
| 1170 | ret = map_bits; |
| 1171 | out: |
| 1172 | return ret; |
| 1173 | } |
| 1174 | |
| 1175 | static void destroy_hist_trigger_attrs(struct hist_trigger_attrs *attrs) |
| 1176 | { |
| 1177 | unsigned int i; |
| 1178 | |
| 1179 | if (!attrs) |
| 1180 | return; |
| 1181 | |
| 1182 | for (i = 0; i < attrs->n_assignments; i++) |
| 1183 | kfree(attrs->assignment_str[i]); |
| 1184 | |
| 1185 | for (i = 0; i < attrs->n_actions; i++) |
| 1186 | kfree(attrs->action_str[i]); |
| 1187 | |
| 1188 | kfree(attrs->name); |
| 1189 | kfree(attrs->sort_key_str); |
| 1190 | kfree(attrs->keys_str); |
| 1191 | kfree(attrs->vals_str); |
| 1192 | kfree(attrs->clock); |
| 1193 | kfree(attrs); |
| 1194 | } |
| 1195 | |
| 1196 | static int parse_action(char *str, struct hist_trigger_attrs *attrs) |
| 1197 | { |
| 1198 | int ret = -EINVAL; |
| 1199 | |
| 1200 | if (attrs->n_actions >= HIST_ACTIONS_MAX) |
| 1201 | return ret; |
| 1202 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1203 | if ((str_has_prefix(str, "onmatch(")) || |
| 1204 | (str_has_prefix(str, "onmax(")) || |
| 1205 | (str_has_prefix(str, "onchange("))) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1206 | attrs->action_str[attrs->n_actions] = kstrdup(str, GFP_KERNEL); |
| 1207 | if (!attrs->action_str[attrs->n_actions]) { |
| 1208 | ret = -ENOMEM; |
| 1209 | return ret; |
| 1210 | } |
| 1211 | attrs->n_actions++; |
| 1212 | ret = 0; |
| 1213 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1214 | return ret; |
| 1215 | } |
| 1216 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1217 | static int parse_assignment(struct trace_array *tr, |
| 1218 | char *str, struct hist_trigger_attrs *attrs) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1219 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1220 | int len, ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1221 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1222 | if ((len = str_has_prefix(str, "key=")) || |
| 1223 | (len = str_has_prefix(str, "keys="))) { |
| 1224 | attrs->keys_str = kstrdup(str + len, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1225 | if (!attrs->keys_str) { |
| 1226 | ret = -ENOMEM; |
| 1227 | goto out; |
| 1228 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1229 | } else if ((len = str_has_prefix(str, "val=")) || |
| 1230 | (len = str_has_prefix(str, "vals=")) || |
| 1231 | (len = str_has_prefix(str, "values="))) { |
| 1232 | attrs->vals_str = kstrdup(str + len, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1233 | if (!attrs->vals_str) { |
| 1234 | ret = -ENOMEM; |
| 1235 | goto out; |
| 1236 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1237 | } else if ((len = str_has_prefix(str, "sort="))) { |
| 1238 | attrs->sort_key_str = kstrdup(str + len, GFP_KERNEL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1239 | if (!attrs->sort_key_str) { |
| 1240 | ret = -ENOMEM; |
| 1241 | goto out; |
| 1242 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1243 | } else if (str_has_prefix(str, "name=")) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1244 | attrs->name = kstrdup(str, GFP_KERNEL); |
| 1245 | if (!attrs->name) { |
| 1246 | ret = -ENOMEM; |
| 1247 | goto out; |
| 1248 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1249 | } else if ((len = str_has_prefix(str, "clock="))) { |
| 1250 | str += len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1251 | |
| 1252 | str = strstrip(str); |
| 1253 | attrs->clock = kstrdup(str, GFP_KERNEL); |
| 1254 | if (!attrs->clock) { |
| 1255 | ret = -ENOMEM; |
| 1256 | goto out; |
| 1257 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1258 | } else if ((len = str_has_prefix(str, "size="))) { |
| 1259 | int map_bits = parse_map_size(str + len); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1260 | |
| 1261 | if (map_bits < 0) { |
| 1262 | ret = map_bits; |
| 1263 | goto out; |
| 1264 | } |
| 1265 | attrs->map_bits = map_bits; |
| 1266 | } else { |
| 1267 | char *assignment; |
| 1268 | |
| 1269 | if (attrs->n_assignments == TRACING_MAP_VARS_MAX) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1270 | hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(str)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1271 | ret = -EINVAL; |
| 1272 | goto out; |
| 1273 | } |
| 1274 | |
| 1275 | assignment = kstrdup(str, GFP_KERNEL); |
| 1276 | if (!assignment) { |
| 1277 | ret = -ENOMEM; |
| 1278 | goto out; |
| 1279 | } |
| 1280 | |
| 1281 | attrs->assignment_str[attrs->n_assignments++] = assignment; |
| 1282 | } |
| 1283 | out: |
| 1284 | return ret; |
| 1285 | } |
| 1286 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1287 | static struct hist_trigger_attrs * |
| 1288 | parse_hist_trigger_attrs(struct trace_array *tr, char *trigger_str) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1289 | { |
| 1290 | struct hist_trigger_attrs *attrs; |
| 1291 | int ret = 0; |
| 1292 | |
| 1293 | attrs = kzalloc(sizeof(*attrs), GFP_KERNEL); |
| 1294 | if (!attrs) |
| 1295 | return ERR_PTR(-ENOMEM); |
| 1296 | |
| 1297 | while (trigger_str) { |
| 1298 | char *str = strsep(&trigger_str, ":"); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1299 | char *rhs; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1300 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1301 | rhs = strchr(str, '='); |
| 1302 | if (rhs) { |
| 1303 | if (!strlen(++rhs)) { |
| 1304 | ret = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1305 | hist_err(tr, HIST_ERR_EMPTY_ASSIGNMENT, errpos(str)); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1306 | goto free; |
| 1307 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1308 | ret = parse_assignment(tr, str, attrs); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1309 | if (ret) |
| 1310 | goto free; |
| 1311 | } else if (strcmp(str, "pause") == 0) |
| 1312 | attrs->pause = true; |
| 1313 | else if ((strcmp(str, "cont") == 0) || |
| 1314 | (strcmp(str, "continue") == 0)) |
| 1315 | attrs->cont = true; |
| 1316 | else if (strcmp(str, "clear") == 0) |
| 1317 | attrs->clear = true; |
| 1318 | else { |
| 1319 | ret = parse_action(str, attrs); |
| 1320 | if (ret) |
| 1321 | goto free; |
| 1322 | } |
| 1323 | } |
| 1324 | |
| 1325 | if (!attrs->keys_str) { |
| 1326 | ret = -EINVAL; |
| 1327 | goto free; |
| 1328 | } |
| 1329 | |
| 1330 | if (!attrs->clock) { |
| 1331 | attrs->clock = kstrdup("global", GFP_KERNEL); |
| 1332 | if (!attrs->clock) { |
| 1333 | ret = -ENOMEM; |
| 1334 | goto free; |
| 1335 | } |
| 1336 | } |
| 1337 | |
| 1338 | return attrs; |
| 1339 | free: |
| 1340 | destroy_hist_trigger_attrs(attrs); |
| 1341 | |
| 1342 | return ERR_PTR(ret); |
| 1343 | } |
| 1344 | |
| 1345 | static inline void save_comm(char *comm, struct task_struct *task) |
| 1346 | { |
| 1347 | if (!task->pid) { |
| 1348 | strcpy(comm, "<idle>"); |
| 1349 | return; |
| 1350 | } |
| 1351 | |
| 1352 | if (WARN_ON_ONCE(task->pid < 0)) { |
| 1353 | strcpy(comm, "<XXX>"); |
| 1354 | return; |
| 1355 | } |
| 1356 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1357 | strncpy(comm, task->comm, TASK_COMM_LEN); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1358 | } |
| 1359 | |
| 1360 | static void hist_elt_data_free(struct hist_elt_data *elt_data) |
| 1361 | { |
| 1362 | unsigned int i; |
| 1363 | |
| 1364 | for (i = 0; i < SYNTH_FIELDS_MAX; i++) |
| 1365 | kfree(elt_data->field_var_str[i]); |
| 1366 | |
| 1367 | kfree(elt_data->comm); |
| 1368 | kfree(elt_data); |
| 1369 | } |
| 1370 | |
| 1371 | static void hist_trigger_elt_data_free(struct tracing_map_elt *elt) |
| 1372 | { |
| 1373 | struct hist_elt_data *elt_data = elt->private_data; |
| 1374 | |
| 1375 | hist_elt_data_free(elt_data); |
| 1376 | } |
| 1377 | |
| 1378 | static int hist_trigger_elt_data_alloc(struct tracing_map_elt *elt) |
| 1379 | { |
| 1380 | struct hist_trigger_data *hist_data = elt->map->private_data; |
| 1381 | unsigned int size = TASK_COMM_LEN; |
| 1382 | struct hist_elt_data *elt_data; |
| 1383 | struct hist_field *key_field; |
| 1384 | unsigned int i, n_str; |
| 1385 | |
| 1386 | elt_data = kzalloc(sizeof(*elt_data), GFP_KERNEL); |
| 1387 | if (!elt_data) |
| 1388 | return -ENOMEM; |
| 1389 | |
| 1390 | for_each_hist_key_field(i, hist_data) { |
| 1391 | key_field = hist_data->fields[i]; |
| 1392 | |
| 1393 | if (key_field->flags & HIST_FIELD_FL_EXECNAME) { |
| 1394 | elt_data->comm = kzalloc(size, GFP_KERNEL); |
| 1395 | if (!elt_data->comm) { |
| 1396 | kfree(elt_data); |
| 1397 | return -ENOMEM; |
| 1398 | } |
| 1399 | break; |
| 1400 | } |
| 1401 | } |
| 1402 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1403 | n_str = hist_data->n_field_var_str + hist_data->n_save_var_str + |
| 1404 | hist_data->n_var_str; |
| 1405 | if (n_str > SYNTH_FIELDS_MAX) { |
| 1406 | hist_elt_data_free(elt_data); |
| 1407 | return -EINVAL; |
| 1408 | } |
| 1409 | |
| 1410 | BUILD_BUG_ON(STR_VAR_LEN_MAX & (sizeof(u64) - 1)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1411 | |
| 1412 | size = STR_VAR_LEN_MAX; |
| 1413 | |
| 1414 | for (i = 0; i < n_str; i++) { |
| 1415 | elt_data->field_var_str[i] = kzalloc(size, GFP_KERNEL); |
| 1416 | if (!elt_data->field_var_str[i]) { |
| 1417 | hist_elt_data_free(elt_data); |
| 1418 | return -ENOMEM; |
| 1419 | } |
| 1420 | } |
| 1421 | |
| 1422 | elt->private_data = elt_data; |
| 1423 | |
| 1424 | return 0; |
| 1425 | } |
| 1426 | |
| 1427 | static void hist_trigger_elt_data_init(struct tracing_map_elt *elt) |
| 1428 | { |
| 1429 | struct hist_elt_data *elt_data = elt->private_data; |
| 1430 | |
| 1431 | if (elt_data->comm) |
| 1432 | save_comm(elt_data->comm, current); |
| 1433 | } |
| 1434 | |
| 1435 | static const struct tracing_map_ops hist_trigger_elt_data_ops = { |
| 1436 | .elt_alloc = hist_trigger_elt_data_alloc, |
| 1437 | .elt_free = hist_trigger_elt_data_free, |
| 1438 | .elt_init = hist_trigger_elt_data_init, |
| 1439 | }; |
| 1440 | |
| 1441 | static const char *get_hist_field_flags(struct hist_field *hist_field) |
| 1442 | { |
| 1443 | const char *flags_str = NULL; |
| 1444 | |
| 1445 | if (hist_field->flags & HIST_FIELD_FL_HEX) |
| 1446 | flags_str = "hex"; |
| 1447 | else if (hist_field->flags & HIST_FIELD_FL_SYM) |
| 1448 | flags_str = "sym"; |
| 1449 | else if (hist_field->flags & HIST_FIELD_FL_SYM_OFFSET) |
| 1450 | flags_str = "sym-offset"; |
| 1451 | else if (hist_field->flags & HIST_FIELD_FL_EXECNAME) |
| 1452 | flags_str = "execname"; |
| 1453 | else if (hist_field->flags & HIST_FIELD_FL_SYSCALL) |
| 1454 | flags_str = "syscall"; |
| 1455 | else if (hist_field->flags & HIST_FIELD_FL_LOG2) |
| 1456 | flags_str = "log2"; |
| 1457 | else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP_USECS) |
| 1458 | flags_str = "usecs"; |
| 1459 | |
| 1460 | return flags_str; |
| 1461 | } |
| 1462 | |
| 1463 | static void expr_field_str(struct hist_field *field, char *expr) |
| 1464 | { |
| 1465 | if (field->flags & HIST_FIELD_FL_VAR_REF) |
| 1466 | strcat(expr, "$"); |
| 1467 | |
| 1468 | strcat(expr, hist_field_name(field, 0)); |
| 1469 | |
| 1470 | if (field->flags && !(field->flags & HIST_FIELD_FL_VAR_REF)) { |
| 1471 | const char *flags_str = get_hist_field_flags(field); |
| 1472 | |
| 1473 | if (flags_str) { |
| 1474 | strcat(expr, "."); |
| 1475 | strcat(expr, flags_str); |
| 1476 | } |
| 1477 | } |
| 1478 | } |
| 1479 | |
| 1480 | static char *expr_str(struct hist_field *field, unsigned int level) |
| 1481 | { |
| 1482 | char *expr; |
| 1483 | |
| 1484 | if (level > 1) |
| 1485 | return NULL; |
| 1486 | |
| 1487 | expr = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); |
| 1488 | if (!expr) |
| 1489 | return NULL; |
| 1490 | |
| 1491 | if (!field->operands[0]) { |
| 1492 | expr_field_str(field, expr); |
| 1493 | return expr; |
| 1494 | } |
| 1495 | |
| 1496 | if (field->operator == FIELD_OP_UNARY_MINUS) { |
| 1497 | char *subexpr; |
| 1498 | |
| 1499 | strcat(expr, "-("); |
| 1500 | subexpr = expr_str(field->operands[0], ++level); |
| 1501 | if (!subexpr) { |
| 1502 | kfree(expr); |
| 1503 | return NULL; |
| 1504 | } |
| 1505 | strcat(expr, subexpr); |
| 1506 | strcat(expr, ")"); |
| 1507 | |
| 1508 | kfree(subexpr); |
| 1509 | |
| 1510 | return expr; |
| 1511 | } |
| 1512 | |
| 1513 | expr_field_str(field->operands[0], expr); |
| 1514 | |
| 1515 | switch (field->operator) { |
| 1516 | case FIELD_OP_MINUS: |
| 1517 | strcat(expr, "-"); |
| 1518 | break; |
| 1519 | case FIELD_OP_PLUS: |
| 1520 | strcat(expr, "+"); |
| 1521 | break; |
| 1522 | default: |
| 1523 | kfree(expr); |
| 1524 | return NULL; |
| 1525 | } |
| 1526 | |
| 1527 | expr_field_str(field->operands[1], expr); |
| 1528 | |
| 1529 | return expr; |
| 1530 | } |
| 1531 | |
| 1532 | static int contains_operator(char *str) |
| 1533 | { |
| 1534 | enum field_op_id field_op = FIELD_OP_NONE; |
| 1535 | char *op; |
| 1536 | |
| 1537 | op = strpbrk(str, "+-"); |
| 1538 | if (!op) |
| 1539 | return FIELD_OP_NONE; |
| 1540 | |
| 1541 | switch (*op) { |
| 1542 | case '-': |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1543 | /* |
| 1544 | * Unfortunately, the modifier ".sym-offset" |
| 1545 | * can confuse things. |
| 1546 | */ |
| 1547 | if (op - str >= 4 && !strncmp(op - 4, ".sym-offset", 11)) |
| 1548 | return FIELD_OP_NONE; |
| 1549 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1550 | if (*str == '-') |
| 1551 | field_op = FIELD_OP_UNARY_MINUS; |
| 1552 | else |
| 1553 | field_op = FIELD_OP_MINUS; |
| 1554 | break; |
| 1555 | case '+': |
| 1556 | field_op = FIELD_OP_PLUS; |
| 1557 | break; |
| 1558 | default: |
| 1559 | break; |
| 1560 | } |
| 1561 | |
| 1562 | return field_op; |
| 1563 | } |
| 1564 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1565 | static void get_hist_field(struct hist_field *hist_field) |
| 1566 | { |
| 1567 | hist_field->ref++; |
| 1568 | } |
| 1569 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1570 | static void __destroy_hist_field(struct hist_field *hist_field) |
| 1571 | { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1572 | if (--hist_field->ref > 1) |
| 1573 | return; |
| 1574 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1575 | kfree(hist_field->var.name); |
| 1576 | kfree(hist_field->name); |
| 1577 | kfree(hist_field->type); |
| 1578 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1579 | kfree(hist_field->system); |
| 1580 | kfree(hist_field->event_name); |
| 1581 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1582 | kfree(hist_field); |
| 1583 | } |
| 1584 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1585 | static void destroy_hist_field(struct hist_field *hist_field, |
| 1586 | unsigned int level) |
| 1587 | { |
| 1588 | unsigned int i; |
| 1589 | |
| 1590 | if (level > 3) |
| 1591 | return; |
| 1592 | |
| 1593 | if (!hist_field) |
| 1594 | return; |
| 1595 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1596 | if (hist_field->flags & HIST_FIELD_FL_VAR_REF) |
| 1597 | return; /* var refs will be destroyed separately */ |
| 1598 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1599 | for (i = 0; i < HIST_FIELD_OPERANDS_MAX; i++) |
| 1600 | destroy_hist_field(hist_field->operands[i], level + 1); |
| 1601 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1602 | __destroy_hist_field(hist_field); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1603 | } |
| 1604 | |
| 1605 | static struct hist_field *create_hist_field(struct hist_trigger_data *hist_data, |
| 1606 | struct ftrace_event_field *field, |
| 1607 | unsigned long flags, |
| 1608 | char *var_name) |
| 1609 | { |
| 1610 | struct hist_field *hist_field; |
| 1611 | |
| 1612 | if (field && is_function_field(field)) |
| 1613 | return NULL; |
| 1614 | |
| 1615 | hist_field = kzalloc(sizeof(struct hist_field), GFP_KERNEL); |
| 1616 | if (!hist_field) |
| 1617 | return NULL; |
| 1618 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1619 | hist_field->ref = 1; |
| 1620 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1621 | hist_field->hist_data = hist_data; |
| 1622 | |
| 1623 | if (flags & HIST_FIELD_FL_EXPR || flags & HIST_FIELD_FL_ALIAS) |
| 1624 | goto out; /* caller will populate */ |
| 1625 | |
| 1626 | if (flags & HIST_FIELD_FL_VAR_REF) { |
| 1627 | hist_field->fn = hist_field_var_ref; |
| 1628 | goto out; |
| 1629 | } |
| 1630 | |
| 1631 | if (flags & HIST_FIELD_FL_HITCOUNT) { |
| 1632 | hist_field->fn = hist_field_counter; |
| 1633 | hist_field->size = sizeof(u64); |
| 1634 | hist_field->type = kstrdup("u64", GFP_KERNEL); |
| 1635 | if (!hist_field->type) |
| 1636 | goto free; |
| 1637 | goto out; |
| 1638 | } |
| 1639 | |
| 1640 | if (flags & HIST_FIELD_FL_STACKTRACE) { |
| 1641 | hist_field->fn = hist_field_none; |
| 1642 | goto out; |
| 1643 | } |
| 1644 | |
| 1645 | if (flags & HIST_FIELD_FL_LOG2) { |
| 1646 | unsigned long fl = flags & ~HIST_FIELD_FL_LOG2; |
| 1647 | hist_field->fn = hist_field_log2; |
| 1648 | hist_field->operands[0] = create_hist_field(hist_data, field, fl, NULL); |
| 1649 | hist_field->size = hist_field->operands[0]->size; |
| 1650 | hist_field->type = kstrdup(hist_field->operands[0]->type, GFP_KERNEL); |
| 1651 | if (!hist_field->type) |
| 1652 | goto free; |
| 1653 | goto out; |
| 1654 | } |
| 1655 | |
| 1656 | if (flags & HIST_FIELD_FL_TIMESTAMP) { |
| 1657 | hist_field->fn = hist_field_timestamp; |
| 1658 | hist_field->size = sizeof(u64); |
| 1659 | hist_field->type = kstrdup("u64", GFP_KERNEL); |
| 1660 | if (!hist_field->type) |
| 1661 | goto free; |
| 1662 | goto out; |
| 1663 | } |
| 1664 | |
| 1665 | if (flags & HIST_FIELD_FL_CPU) { |
| 1666 | hist_field->fn = hist_field_cpu; |
| 1667 | hist_field->size = sizeof(int); |
| 1668 | hist_field->type = kstrdup("unsigned int", GFP_KERNEL); |
| 1669 | if (!hist_field->type) |
| 1670 | goto free; |
| 1671 | goto out; |
| 1672 | } |
| 1673 | |
| 1674 | if (WARN_ON_ONCE(!field)) |
| 1675 | goto out; |
| 1676 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1677 | /* Pointers to strings are just pointers and dangerous to dereference */ |
| 1678 | if (is_string_field(field) && |
| 1679 | (field->filter_type != FILTER_PTR_STRING)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1680 | flags |= HIST_FIELD_FL_STRING; |
| 1681 | |
| 1682 | hist_field->size = MAX_FILTER_STR_VAL; |
| 1683 | hist_field->type = kstrdup(field->type, GFP_KERNEL); |
| 1684 | if (!hist_field->type) |
| 1685 | goto free; |
| 1686 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1687 | if (field->filter_type == FILTER_STATIC_STRING) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1688 | hist_field->fn = hist_field_string; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1689 | hist_field->size = field->size; |
| 1690 | } else if (field->filter_type == FILTER_DYN_STRING) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1691 | hist_field->fn = hist_field_dynstring; |
| 1692 | else |
| 1693 | hist_field->fn = hist_field_pstring; |
| 1694 | } else { |
| 1695 | hist_field->size = field->size; |
| 1696 | hist_field->is_signed = field->is_signed; |
| 1697 | hist_field->type = kstrdup(field->type, GFP_KERNEL); |
| 1698 | if (!hist_field->type) |
| 1699 | goto free; |
| 1700 | |
| 1701 | hist_field->fn = select_value_fn(field->size, |
| 1702 | field->is_signed); |
| 1703 | if (!hist_field->fn) { |
| 1704 | destroy_hist_field(hist_field, 0); |
| 1705 | return NULL; |
| 1706 | } |
| 1707 | } |
| 1708 | out: |
| 1709 | hist_field->field = field; |
| 1710 | hist_field->flags = flags; |
| 1711 | |
| 1712 | if (var_name) { |
| 1713 | hist_field->var.name = kstrdup(var_name, GFP_KERNEL); |
| 1714 | if (!hist_field->var.name) |
| 1715 | goto free; |
| 1716 | } |
| 1717 | |
| 1718 | return hist_field; |
| 1719 | free: |
| 1720 | destroy_hist_field(hist_field, 0); |
| 1721 | return NULL; |
| 1722 | } |
| 1723 | |
| 1724 | static void destroy_hist_fields(struct hist_trigger_data *hist_data) |
| 1725 | { |
| 1726 | unsigned int i; |
| 1727 | |
| 1728 | for (i = 0; i < HIST_FIELDS_MAX; i++) { |
| 1729 | if (hist_data->fields[i]) { |
| 1730 | destroy_hist_field(hist_data->fields[i], 0); |
| 1731 | hist_data->fields[i] = NULL; |
| 1732 | } |
| 1733 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1734 | |
| 1735 | for (i = 0; i < hist_data->n_var_refs; i++) { |
| 1736 | WARN_ON(!(hist_data->var_refs[i]->flags & HIST_FIELD_FL_VAR_REF)); |
| 1737 | __destroy_hist_field(hist_data->var_refs[i]); |
| 1738 | hist_data->var_refs[i] = NULL; |
| 1739 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1740 | } |
| 1741 | |
| 1742 | static int init_var_ref(struct hist_field *ref_field, |
| 1743 | struct hist_field *var_field, |
| 1744 | char *system, char *event_name) |
| 1745 | { |
| 1746 | int err = 0; |
| 1747 | |
| 1748 | ref_field->var.idx = var_field->var.idx; |
| 1749 | ref_field->var.hist_data = var_field->hist_data; |
| 1750 | ref_field->size = var_field->size; |
| 1751 | ref_field->is_signed = var_field->is_signed; |
| 1752 | ref_field->flags |= var_field->flags & |
| 1753 | (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); |
| 1754 | |
| 1755 | if (system) { |
| 1756 | ref_field->system = kstrdup(system, GFP_KERNEL); |
| 1757 | if (!ref_field->system) |
| 1758 | return -ENOMEM; |
| 1759 | } |
| 1760 | |
| 1761 | if (event_name) { |
| 1762 | ref_field->event_name = kstrdup(event_name, GFP_KERNEL); |
| 1763 | if (!ref_field->event_name) { |
| 1764 | err = -ENOMEM; |
| 1765 | goto free; |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | if (var_field->var.name) { |
| 1770 | ref_field->name = kstrdup(var_field->var.name, GFP_KERNEL); |
| 1771 | if (!ref_field->name) { |
| 1772 | err = -ENOMEM; |
| 1773 | goto free; |
| 1774 | } |
| 1775 | } else if (var_field->name) { |
| 1776 | ref_field->name = kstrdup(var_field->name, GFP_KERNEL); |
| 1777 | if (!ref_field->name) { |
| 1778 | err = -ENOMEM; |
| 1779 | goto free; |
| 1780 | } |
| 1781 | } |
| 1782 | |
| 1783 | ref_field->type = kstrdup(var_field->type, GFP_KERNEL); |
| 1784 | if (!ref_field->type) { |
| 1785 | err = -ENOMEM; |
| 1786 | goto free; |
| 1787 | } |
| 1788 | out: |
| 1789 | return err; |
| 1790 | free: |
| 1791 | kfree(ref_field->system); |
| 1792 | kfree(ref_field->event_name); |
| 1793 | kfree(ref_field->name); |
| 1794 | |
| 1795 | goto out; |
| 1796 | } |
| 1797 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1798 | static int find_var_ref_idx(struct hist_trigger_data *hist_data, |
| 1799 | struct hist_field *var_field) |
| 1800 | { |
| 1801 | struct hist_field *ref_field; |
| 1802 | int i; |
| 1803 | |
| 1804 | for (i = 0; i < hist_data->n_var_refs; i++) { |
| 1805 | ref_field = hist_data->var_refs[i]; |
| 1806 | if (ref_field->var.idx == var_field->var.idx && |
| 1807 | ref_field->var.hist_data == var_field->hist_data) |
| 1808 | return i; |
| 1809 | } |
| 1810 | |
| 1811 | return -ENOENT; |
| 1812 | } |
| 1813 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1814 | /** |
| 1815 | * create_var_ref - Create a variable reference and attach it to trigger |
| 1816 | * @hist_data: The trigger that will be referencing the variable |
| 1817 | * @var_field: The VAR field to create a reference to |
| 1818 | * @system: The optional system string |
| 1819 | * @event_name: The optional event_name string |
| 1820 | * |
| 1821 | * Given a variable hist_field, create a VAR_REF hist_field that |
| 1822 | * represents a reference to it. |
| 1823 | * |
| 1824 | * This function also adds the reference to the trigger that |
| 1825 | * now references the variable. |
| 1826 | * |
| 1827 | * Return: The VAR_REF field if successful, NULL if not |
| 1828 | */ |
| 1829 | static struct hist_field *create_var_ref(struct hist_trigger_data *hist_data, |
| 1830 | struct hist_field *var_field, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1831 | char *system, char *event_name) |
| 1832 | { |
| 1833 | unsigned long flags = HIST_FIELD_FL_VAR_REF; |
| 1834 | struct hist_field *ref_field; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1835 | int i; |
| 1836 | |
| 1837 | /* Check if the variable already exists */ |
| 1838 | for (i = 0; i < hist_data->n_var_refs; i++) { |
| 1839 | ref_field = hist_data->var_refs[i]; |
| 1840 | if (ref_field->var.idx == var_field->var.idx && |
| 1841 | ref_field->var.hist_data == var_field->hist_data) { |
| 1842 | get_hist_field(ref_field); |
| 1843 | return ref_field; |
| 1844 | } |
| 1845 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1846 | |
| 1847 | ref_field = create_hist_field(var_field->hist_data, NULL, flags, NULL); |
| 1848 | if (ref_field) { |
| 1849 | if (init_var_ref(ref_field, var_field, system, event_name)) { |
| 1850 | destroy_hist_field(ref_field, 0); |
| 1851 | return NULL; |
| 1852 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1853 | |
| 1854 | hist_data->var_refs[hist_data->n_var_refs] = ref_field; |
| 1855 | ref_field->var_ref_idx = hist_data->n_var_refs++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1856 | } |
| 1857 | |
| 1858 | return ref_field; |
| 1859 | } |
| 1860 | |
| 1861 | static bool is_var_ref(char *var_name) |
| 1862 | { |
| 1863 | if (!var_name || strlen(var_name) < 2 || var_name[0] != '$') |
| 1864 | return false; |
| 1865 | |
| 1866 | return true; |
| 1867 | } |
| 1868 | |
| 1869 | static char *field_name_from_var(struct hist_trigger_data *hist_data, |
| 1870 | char *var_name) |
| 1871 | { |
| 1872 | char *name, *field; |
| 1873 | unsigned int i; |
| 1874 | |
| 1875 | for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { |
| 1876 | name = hist_data->attrs->var_defs.name[i]; |
| 1877 | |
| 1878 | if (strcmp(var_name, name) == 0) { |
| 1879 | field = hist_data->attrs->var_defs.expr[i]; |
| 1880 | if (contains_operator(field) || is_var_ref(field)) |
| 1881 | continue; |
| 1882 | return field; |
| 1883 | } |
| 1884 | } |
| 1885 | |
| 1886 | return NULL; |
| 1887 | } |
| 1888 | |
| 1889 | static char *local_field_var_ref(struct hist_trigger_data *hist_data, |
| 1890 | char *system, char *event_name, |
| 1891 | char *var_name) |
| 1892 | { |
| 1893 | struct trace_event_call *call; |
| 1894 | |
| 1895 | if (system && event_name) { |
| 1896 | call = hist_data->event_file->event_call; |
| 1897 | |
| 1898 | if (strcmp(system, call->class->system) != 0) |
| 1899 | return NULL; |
| 1900 | |
| 1901 | if (strcmp(event_name, trace_event_name(call)) != 0) |
| 1902 | return NULL; |
| 1903 | } |
| 1904 | |
| 1905 | if (!!system != !!event_name) |
| 1906 | return NULL; |
| 1907 | |
| 1908 | if (!is_var_ref(var_name)) |
| 1909 | return NULL; |
| 1910 | |
| 1911 | var_name++; |
| 1912 | |
| 1913 | return field_name_from_var(hist_data, var_name); |
| 1914 | } |
| 1915 | |
| 1916 | static struct hist_field *parse_var_ref(struct hist_trigger_data *hist_data, |
| 1917 | char *system, char *event_name, |
| 1918 | char *var_name) |
| 1919 | { |
| 1920 | struct hist_field *var_field = NULL, *ref_field = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1921 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1922 | |
| 1923 | if (!is_var_ref(var_name)) |
| 1924 | return NULL; |
| 1925 | |
| 1926 | var_name++; |
| 1927 | |
| 1928 | var_field = find_event_var(hist_data, system, event_name, var_name); |
| 1929 | if (var_field) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1930 | ref_field = create_var_ref(hist_data, var_field, |
| 1931 | system, event_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1932 | |
| 1933 | if (!ref_field) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1934 | hist_err(tr, HIST_ERR_VAR_NOT_FOUND, errpos(var_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1935 | |
| 1936 | return ref_field; |
| 1937 | } |
| 1938 | |
| 1939 | static struct ftrace_event_field * |
| 1940 | parse_field(struct hist_trigger_data *hist_data, struct trace_event_file *file, |
| 1941 | char *field_str, unsigned long *flags) |
| 1942 | { |
| 1943 | struct ftrace_event_field *field = NULL; |
| 1944 | char *field_name, *modifier, *str; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1945 | struct trace_array *tr = file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1946 | |
| 1947 | modifier = str = kstrdup(field_str, GFP_KERNEL); |
| 1948 | if (!modifier) |
| 1949 | return ERR_PTR(-ENOMEM); |
| 1950 | |
| 1951 | field_name = strsep(&modifier, "."); |
| 1952 | if (modifier) { |
| 1953 | if (strcmp(modifier, "hex") == 0) |
| 1954 | *flags |= HIST_FIELD_FL_HEX; |
| 1955 | else if (strcmp(modifier, "sym") == 0) |
| 1956 | *flags |= HIST_FIELD_FL_SYM; |
| 1957 | else if (strcmp(modifier, "sym-offset") == 0) |
| 1958 | *flags |= HIST_FIELD_FL_SYM_OFFSET; |
| 1959 | else if ((strcmp(modifier, "execname") == 0) && |
| 1960 | (strcmp(field_name, "common_pid") == 0)) |
| 1961 | *flags |= HIST_FIELD_FL_EXECNAME; |
| 1962 | else if (strcmp(modifier, "syscall") == 0) |
| 1963 | *flags |= HIST_FIELD_FL_SYSCALL; |
| 1964 | else if (strcmp(modifier, "log2") == 0) |
| 1965 | *flags |= HIST_FIELD_FL_LOG2; |
| 1966 | else if (strcmp(modifier, "usecs") == 0) |
| 1967 | *flags |= HIST_FIELD_FL_TIMESTAMP_USECS; |
| 1968 | else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1969 | hist_err(tr, HIST_ERR_BAD_FIELD_MODIFIER, errpos(modifier)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1970 | field = ERR_PTR(-EINVAL); |
| 1971 | goto out; |
| 1972 | } |
| 1973 | } |
| 1974 | |
| 1975 | if (strcmp(field_name, "common_timestamp") == 0) { |
| 1976 | *flags |= HIST_FIELD_FL_TIMESTAMP; |
| 1977 | hist_data->enable_timestamps = true; |
| 1978 | if (*flags & HIST_FIELD_FL_TIMESTAMP_USECS) |
| 1979 | hist_data->attrs->ts_in_usecs = true; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1980 | } else if (strcmp(field_name, "common_cpu") == 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1981 | *flags |= HIST_FIELD_FL_CPU; |
| 1982 | else { |
| 1983 | field = trace_find_event_field(file->event_call, field_name); |
| 1984 | if (!field || !field->size) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1985 | /* |
| 1986 | * For backward compatibility, if field_name |
| 1987 | * was "cpu", then we treat this the same as |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1988 | * common_cpu. This also works for "CPU". |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1989 | */ |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 1990 | if (field && field->filter_type == FILTER_CPU) { |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 1991 | *flags |= HIST_FIELD_FL_CPU; |
| 1992 | } else { |
| 1993 | hist_err(tr, HIST_ERR_FIELD_NOT_FOUND, |
| 1994 | errpos(field_name)); |
| 1995 | field = ERR_PTR(-EINVAL); |
| 1996 | goto out; |
| 1997 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 1998 | } |
| 1999 | } |
| 2000 | out: |
| 2001 | kfree(str); |
| 2002 | |
| 2003 | return field; |
| 2004 | } |
| 2005 | |
| 2006 | static struct hist_field *create_alias(struct hist_trigger_data *hist_data, |
| 2007 | struct hist_field *var_ref, |
| 2008 | char *var_name) |
| 2009 | { |
| 2010 | struct hist_field *alias = NULL; |
| 2011 | unsigned long flags = HIST_FIELD_FL_ALIAS | HIST_FIELD_FL_VAR; |
| 2012 | |
| 2013 | alias = create_hist_field(hist_data, NULL, flags, var_name); |
| 2014 | if (!alias) |
| 2015 | return NULL; |
| 2016 | |
| 2017 | alias->fn = var_ref->fn; |
| 2018 | alias->operands[0] = var_ref; |
| 2019 | |
| 2020 | if (init_var_ref(alias, var_ref, var_ref->system, var_ref->event_name)) { |
| 2021 | destroy_hist_field(alias, 0); |
| 2022 | return NULL; |
| 2023 | } |
| 2024 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2025 | alias->var_ref_idx = var_ref->var_ref_idx; |
| 2026 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2027 | return alias; |
| 2028 | } |
| 2029 | |
| 2030 | static struct hist_field *parse_atom(struct hist_trigger_data *hist_data, |
| 2031 | struct trace_event_file *file, char *str, |
| 2032 | unsigned long *flags, char *var_name) |
| 2033 | { |
| 2034 | char *s, *ref_system = NULL, *ref_event = NULL, *ref_var = str; |
| 2035 | struct ftrace_event_field *field = NULL; |
| 2036 | struct hist_field *hist_field = NULL; |
| 2037 | int ret = 0; |
| 2038 | |
| 2039 | s = strchr(str, '.'); |
| 2040 | if (s) { |
| 2041 | s = strchr(++s, '.'); |
| 2042 | if (s) { |
| 2043 | ref_system = strsep(&str, "."); |
| 2044 | if (!str) { |
| 2045 | ret = -EINVAL; |
| 2046 | goto out; |
| 2047 | } |
| 2048 | ref_event = strsep(&str, "."); |
| 2049 | if (!str) { |
| 2050 | ret = -EINVAL; |
| 2051 | goto out; |
| 2052 | } |
| 2053 | ref_var = str; |
| 2054 | } |
| 2055 | } |
| 2056 | |
| 2057 | s = local_field_var_ref(hist_data, ref_system, ref_event, ref_var); |
| 2058 | if (!s) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2059 | hist_field = parse_var_ref(hist_data, ref_system, |
| 2060 | ref_event, ref_var); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2061 | if (hist_field) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2062 | if (var_name) { |
| 2063 | hist_field = create_alias(hist_data, hist_field, var_name); |
| 2064 | if (!hist_field) { |
| 2065 | ret = -ENOMEM; |
| 2066 | goto out; |
| 2067 | } |
| 2068 | } |
| 2069 | return hist_field; |
| 2070 | } |
| 2071 | } else |
| 2072 | str = s; |
| 2073 | |
| 2074 | field = parse_field(hist_data, file, str, flags); |
| 2075 | if (IS_ERR(field)) { |
| 2076 | ret = PTR_ERR(field); |
| 2077 | goto out; |
| 2078 | } |
| 2079 | |
| 2080 | hist_field = create_hist_field(hist_data, field, *flags, var_name); |
| 2081 | if (!hist_field) { |
| 2082 | ret = -ENOMEM; |
| 2083 | goto out; |
| 2084 | } |
| 2085 | |
| 2086 | return hist_field; |
| 2087 | out: |
| 2088 | return ERR_PTR(ret); |
| 2089 | } |
| 2090 | |
| 2091 | static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, |
| 2092 | struct trace_event_file *file, |
| 2093 | char *str, unsigned long flags, |
| 2094 | char *var_name, unsigned int level); |
| 2095 | |
| 2096 | static struct hist_field *parse_unary(struct hist_trigger_data *hist_data, |
| 2097 | struct trace_event_file *file, |
| 2098 | char *str, unsigned long flags, |
| 2099 | char *var_name, unsigned int level) |
| 2100 | { |
| 2101 | struct hist_field *operand1, *expr = NULL; |
| 2102 | unsigned long operand_flags; |
| 2103 | int ret = 0; |
| 2104 | char *s; |
| 2105 | |
| 2106 | /* we support only -(xxx) i.e. explicit parens required */ |
| 2107 | |
| 2108 | if (level > 3) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2109 | hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2110 | ret = -EINVAL; |
| 2111 | goto free; |
| 2112 | } |
| 2113 | |
| 2114 | str++; /* skip leading '-' */ |
| 2115 | |
| 2116 | s = strchr(str, '('); |
| 2117 | if (s) |
| 2118 | str++; |
| 2119 | else { |
| 2120 | ret = -EINVAL; |
| 2121 | goto free; |
| 2122 | } |
| 2123 | |
| 2124 | s = strrchr(str, ')'); |
| 2125 | if (s) |
| 2126 | *s = '\0'; |
| 2127 | else { |
| 2128 | ret = -EINVAL; /* no closing ')' */ |
| 2129 | goto free; |
| 2130 | } |
| 2131 | |
| 2132 | flags |= HIST_FIELD_FL_EXPR; |
| 2133 | expr = create_hist_field(hist_data, NULL, flags, var_name); |
| 2134 | if (!expr) { |
| 2135 | ret = -ENOMEM; |
| 2136 | goto free; |
| 2137 | } |
| 2138 | |
| 2139 | operand_flags = 0; |
| 2140 | operand1 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level); |
| 2141 | if (IS_ERR(operand1)) { |
| 2142 | ret = PTR_ERR(operand1); |
| 2143 | goto free; |
| 2144 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2145 | if (operand1->flags & HIST_FIELD_FL_STRING) { |
| 2146 | /* String type can not be the operand of unary operator. */ |
| 2147 | hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str)); |
| 2148 | destroy_hist_field(operand1, 0); |
| 2149 | ret = -EINVAL; |
| 2150 | goto free; |
| 2151 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2152 | |
| 2153 | expr->flags |= operand1->flags & |
| 2154 | (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); |
| 2155 | expr->fn = hist_field_unary_minus; |
| 2156 | expr->operands[0] = operand1; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2157 | expr->size = operand1->size; |
| 2158 | expr->is_signed = operand1->is_signed; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2159 | expr->operator = FIELD_OP_UNARY_MINUS; |
| 2160 | expr->name = expr_str(expr, 0); |
| 2161 | expr->type = kstrdup(operand1->type, GFP_KERNEL); |
| 2162 | if (!expr->type) { |
| 2163 | ret = -ENOMEM; |
| 2164 | goto free; |
| 2165 | } |
| 2166 | |
| 2167 | return expr; |
| 2168 | free: |
| 2169 | destroy_hist_field(expr, 0); |
| 2170 | return ERR_PTR(ret); |
| 2171 | } |
| 2172 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2173 | static int check_expr_operands(struct trace_array *tr, |
| 2174 | struct hist_field *operand1, |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2175 | struct hist_field *operand2) |
| 2176 | { |
| 2177 | unsigned long operand1_flags = operand1->flags; |
| 2178 | unsigned long operand2_flags = operand2->flags; |
| 2179 | |
| 2180 | if ((operand1_flags & HIST_FIELD_FL_VAR_REF) || |
| 2181 | (operand1_flags & HIST_FIELD_FL_ALIAS)) { |
| 2182 | struct hist_field *var; |
| 2183 | |
| 2184 | var = find_var_field(operand1->var.hist_data, operand1->name); |
| 2185 | if (!var) |
| 2186 | return -EINVAL; |
| 2187 | operand1_flags = var->flags; |
| 2188 | } |
| 2189 | |
| 2190 | if ((operand2_flags & HIST_FIELD_FL_VAR_REF) || |
| 2191 | (operand2_flags & HIST_FIELD_FL_ALIAS)) { |
| 2192 | struct hist_field *var; |
| 2193 | |
| 2194 | var = find_var_field(operand2->var.hist_data, operand2->name); |
| 2195 | if (!var) |
| 2196 | return -EINVAL; |
| 2197 | operand2_flags = var->flags; |
| 2198 | } |
| 2199 | |
| 2200 | if ((operand1_flags & HIST_FIELD_FL_TIMESTAMP_USECS) != |
| 2201 | (operand2_flags & HIST_FIELD_FL_TIMESTAMP_USECS)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2202 | hist_err(tr, HIST_ERR_TIMESTAMP_MISMATCH, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2203 | return -EINVAL; |
| 2204 | } |
| 2205 | |
| 2206 | return 0; |
| 2207 | } |
| 2208 | |
| 2209 | static struct hist_field *parse_expr(struct hist_trigger_data *hist_data, |
| 2210 | struct trace_event_file *file, |
| 2211 | char *str, unsigned long flags, |
| 2212 | char *var_name, unsigned int level) |
| 2213 | { |
| 2214 | struct hist_field *operand1 = NULL, *operand2 = NULL, *expr = NULL; |
| 2215 | unsigned long operand_flags; |
| 2216 | int field_op, ret = -EINVAL; |
| 2217 | char *sep, *operand1_str; |
| 2218 | |
| 2219 | if (level > 3) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2220 | hist_err(file->tr, HIST_ERR_TOO_MANY_SUBEXPR, errpos(str)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2221 | return ERR_PTR(-EINVAL); |
| 2222 | } |
| 2223 | |
| 2224 | field_op = contains_operator(str); |
| 2225 | |
| 2226 | if (field_op == FIELD_OP_NONE) |
| 2227 | return parse_atom(hist_data, file, str, &flags, var_name); |
| 2228 | |
| 2229 | if (field_op == FIELD_OP_UNARY_MINUS) |
| 2230 | return parse_unary(hist_data, file, str, flags, var_name, ++level); |
| 2231 | |
| 2232 | switch (field_op) { |
| 2233 | case FIELD_OP_MINUS: |
| 2234 | sep = "-"; |
| 2235 | break; |
| 2236 | case FIELD_OP_PLUS: |
| 2237 | sep = "+"; |
| 2238 | break; |
| 2239 | default: |
| 2240 | goto free; |
| 2241 | } |
| 2242 | |
| 2243 | operand1_str = strsep(&str, sep); |
| 2244 | if (!operand1_str || !str) |
| 2245 | goto free; |
| 2246 | |
| 2247 | operand_flags = 0; |
| 2248 | operand1 = parse_atom(hist_data, file, operand1_str, |
| 2249 | &operand_flags, NULL); |
| 2250 | if (IS_ERR(operand1)) { |
| 2251 | ret = PTR_ERR(operand1); |
| 2252 | operand1 = NULL; |
| 2253 | goto free; |
| 2254 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2255 | if (operand1->flags & HIST_FIELD_FL_STRING) { |
| 2256 | hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(operand1_str)); |
| 2257 | ret = -EINVAL; |
| 2258 | goto free; |
| 2259 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2260 | |
| 2261 | /* rest of string could be another expression e.g. b+c in a+b+c */ |
| 2262 | operand_flags = 0; |
| 2263 | operand2 = parse_expr(hist_data, file, str, operand_flags, NULL, ++level); |
| 2264 | if (IS_ERR(operand2)) { |
| 2265 | ret = PTR_ERR(operand2); |
| 2266 | operand2 = NULL; |
| 2267 | goto free; |
| 2268 | } |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2269 | if (operand2->flags & HIST_FIELD_FL_STRING) { |
| 2270 | hist_err(file->tr, HIST_ERR_INVALID_STR_OPERAND, errpos(str)); |
| 2271 | ret = -EINVAL; |
| 2272 | goto free; |
| 2273 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2274 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2275 | ret = check_expr_operands(file->tr, operand1, operand2); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2276 | if (ret) |
| 2277 | goto free; |
| 2278 | |
| 2279 | flags |= HIST_FIELD_FL_EXPR; |
| 2280 | |
| 2281 | flags |= operand1->flags & |
| 2282 | (HIST_FIELD_FL_TIMESTAMP | HIST_FIELD_FL_TIMESTAMP_USECS); |
| 2283 | |
| 2284 | expr = create_hist_field(hist_data, NULL, flags, var_name); |
| 2285 | if (!expr) { |
| 2286 | ret = -ENOMEM; |
| 2287 | goto free; |
| 2288 | } |
| 2289 | |
| 2290 | operand1->read_once = true; |
| 2291 | operand2->read_once = true; |
| 2292 | |
| 2293 | expr->operands[0] = operand1; |
| 2294 | expr->operands[1] = operand2; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2295 | |
| 2296 | /* The operand sizes should be the same, so just pick one */ |
| 2297 | expr->size = operand1->size; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2298 | expr->is_signed = operand1->is_signed; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2299 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2300 | expr->operator = field_op; |
| 2301 | expr->name = expr_str(expr, 0); |
| 2302 | expr->type = kstrdup(operand1->type, GFP_KERNEL); |
| 2303 | if (!expr->type) { |
| 2304 | ret = -ENOMEM; |
| 2305 | goto free; |
| 2306 | } |
| 2307 | |
| 2308 | switch (field_op) { |
| 2309 | case FIELD_OP_MINUS: |
| 2310 | expr->fn = hist_field_minus; |
| 2311 | break; |
| 2312 | case FIELD_OP_PLUS: |
| 2313 | expr->fn = hist_field_plus; |
| 2314 | break; |
| 2315 | default: |
| 2316 | ret = -EINVAL; |
| 2317 | goto free; |
| 2318 | } |
| 2319 | |
| 2320 | return expr; |
| 2321 | free: |
| 2322 | destroy_hist_field(operand1, 0); |
| 2323 | destroy_hist_field(operand2, 0); |
| 2324 | destroy_hist_field(expr, 0); |
| 2325 | |
| 2326 | return ERR_PTR(ret); |
| 2327 | } |
| 2328 | |
| 2329 | static char *find_trigger_filter(struct hist_trigger_data *hist_data, |
| 2330 | struct trace_event_file *file) |
| 2331 | { |
| 2332 | struct event_trigger_data *test; |
| 2333 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2334 | lockdep_assert_held(&event_mutex); |
| 2335 | |
| 2336 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2337 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 2338 | if (test->private_data == hist_data) |
| 2339 | return test->filter_str; |
| 2340 | } |
| 2341 | } |
| 2342 | |
| 2343 | return NULL; |
| 2344 | } |
| 2345 | |
| 2346 | static struct event_command trigger_hist_cmd; |
| 2347 | static int event_hist_trigger_func(struct event_command *cmd_ops, |
| 2348 | struct trace_event_file *file, |
| 2349 | char *glob, char *cmd, char *param); |
| 2350 | |
| 2351 | static bool compatible_keys(struct hist_trigger_data *target_hist_data, |
| 2352 | struct hist_trigger_data *hist_data, |
| 2353 | unsigned int n_keys) |
| 2354 | { |
| 2355 | struct hist_field *target_hist_field, *hist_field; |
| 2356 | unsigned int n, i, j; |
| 2357 | |
| 2358 | if (hist_data->n_fields - hist_data->n_vals != n_keys) |
| 2359 | return false; |
| 2360 | |
| 2361 | i = hist_data->n_vals; |
| 2362 | j = target_hist_data->n_vals; |
| 2363 | |
| 2364 | for (n = 0; n < n_keys; n++) { |
| 2365 | hist_field = hist_data->fields[i + n]; |
| 2366 | target_hist_field = target_hist_data->fields[j + n]; |
| 2367 | |
| 2368 | if (strcmp(hist_field->type, target_hist_field->type) != 0) |
| 2369 | return false; |
| 2370 | if (hist_field->size != target_hist_field->size) |
| 2371 | return false; |
| 2372 | if (hist_field->is_signed != target_hist_field->is_signed) |
| 2373 | return false; |
| 2374 | } |
| 2375 | |
| 2376 | return true; |
| 2377 | } |
| 2378 | |
| 2379 | static struct hist_trigger_data * |
| 2380 | find_compatible_hist(struct hist_trigger_data *target_hist_data, |
| 2381 | struct trace_event_file *file) |
| 2382 | { |
| 2383 | struct hist_trigger_data *hist_data; |
| 2384 | struct event_trigger_data *test; |
| 2385 | unsigned int n_keys; |
| 2386 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2387 | lockdep_assert_held(&event_mutex); |
| 2388 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2389 | n_keys = target_hist_data->n_fields - target_hist_data->n_vals; |
| 2390 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2391 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2392 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 2393 | hist_data = test->private_data; |
| 2394 | |
| 2395 | if (compatible_keys(target_hist_data, hist_data, n_keys)) |
| 2396 | return hist_data; |
| 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | return NULL; |
| 2401 | } |
| 2402 | |
| 2403 | static struct trace_event_file *event_file(struct trace_array *tr, |
| 2404 | char *system, char *event_name) |
| 2405 | { |
| 2406 | struct trace_event_file *file; |
| 2407 | |
| 2408 | file = __find_event_file(tr, system, event_name); |
| 2409 | if (!file) |
| 2410 | return ERR_PTR(-EINVAL); |
| 2411 | |
| 2412 | return file; |
| 2413 | } |
| 2414 | |
| 2415 | static struct hist_field * |
| 2416 | find_synthetic_field_var(struct hist_trigger_data *target_hist_data, |
| 2417 | char *system, char *event_name, char *field_name) |
| 2418 | { |
| 2419 | struct hist_field *event_var; |
| 2420 | char *synthetic_name; |
| 2421 | |
| 2422 | synthetic_name = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); |
| 2423 | if (!synthetic_name) |
| 2424 | return ERR_PTR(-ENOMEM); |
| 2425 | |
| 2426 | strcpy(synthetic_name, "synthetic_"); |
| 2427 | strcat(synthetic_name, field_name); |
| 2428 | |
| 2429 | event_var = find_event_var(target_hist_data, system, event_name, synthetic_name); |
| 2430 | |
| 2431 | kfree(synthetic_name); |
| 2432 | |
| 2433 | return event_var; |
| 2434 | } |
| 2435 | |
| 2436 | /** |
| 2437 | * create_field_var_hist - Automatically create a histogram and var for a field |
| 2438 | * @target_hist_data: The target hist trigger |
| 2439 | * @subsys_name: Optional subsystem name |
| 2440 | * @event_name: Optional event name |
| 2441 | * @field_name: The name of the field (and the resulting variable) |
| 2442 | * |
| 2443 | * Hist trigger actions fetch data from variables, not directly from |
| 2444 | * events. However, for convenience, users are allowed to directly |
| 2445 | * specify an event field in an action, which will be automatically |
| 2446 | * converted into a variable on their behalf. |
| 2447 | |
| 2448 | * If a user specifies a field on an event that isn't the event the |
| 2449 | * histogram currently being defined (the target event histogram), the |
| 2450 | * only way that can be accomplished is if a new hist trigger is |
| 2451 | * created and the field variable defined on that. |
| 2452 | * |
| 2453 | * This function creates a new histogram compatible with the target |
| 2454 | * event (meaning a histogram with the same key as the target |
| 2455 | * histogram), and creates a variable for the specified field, but |
| 2456 | * with 'synthetic_' prepended to the variable name in order to avoid |
| 2457 | * collision with normal field variables. |
| 2458 | * |
| 2459 | * Return: The variable created for the field. |
| 2460 | */ |
| 2461 | static struct hist_field * |
| 2462 | create_field_var_hist(struct hist_trigger_data *target_hist_data, |
| 2463 | char *subsys_name, char *event_name, char *field_name) |
| 2464 | { |
| 2465 | struct trace_array *tr = target_hist_data->event_file->tr; |
| 2466 | struct hist_field *event_var = ERR_PTR(-EINVAL); |
| 2467 | struct hist_trigger_data *hist_data; |
| 2468 | unsigned int i, n, first = true; |
| 2469 | struct field_var_hist *var_hist; |
| 2470 | struct trace_event_file *file; |
| 2471 | struct hist_field *key_field; |
| 2472 | char *saved_filter; |
| 2473 | char *cmd; |
| 2474 | int ret; |
| 2475 | |
| 2476 | if (target_hist_data->n_field_var_hists >= SYNTH_FIELDS_MAX) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2477 | hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2478 | return ERR_PTR(-EINVAL); |
| 2479 | } |
| 2480 | |
| 2481 | file = event_file(tr, subsys_name, event_name); |
| 2482 | |
| 2483 | if (IS_ERR(file)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2484 | hist_err(tr, HIST_ERR_EVENT_FILE_NOT_FOUND, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2485 | ret = PTR_ERR(file); |
| 2486 | return ERR_PTR(ret); |
| 2487 | } |
| 2488 | |
| 2489 | /* |
| 2490 | * Look for a histogram compatible with target. We'll use the |
| 2491 | * found histogram specification to create a new matching |
| 2492 | * histogram with our variable on it. target_hist_data is not |
| 2493 | * yet a registered histogram so we can't use that. |
| 2494 | */ |
| 2495 | hist_data = find_compatible_hist(target_hist_data, file); |
| 2496 | if (!hist_data) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2497 | hist_err(tr, HIST_ERR_HIST_NOT_FOUND, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2498 | return ERR_PTR(-EINVAL); |
| 2499 | } |
| 2500 | |
| 2501 | /* See if a synthetic field variable has already been created */ |
| 2502 | event_var = find_synthetic_field_var(target_hist_data, subsys_name, |
| 2503 | event_name, field_name); |
| 2504 | if (!IS_ERR_OR_NULL(event_var)) |
| 2505 | return event_var; |
| 2506 | |
| 2507 | var_hist = kzalloc(sizeof(*var_hist), GFP_KERNEL); |
| 2508 | if (!var_hist) |
| 2509 | return ERR_PTR(-ENOMEM); |
| 2510 | |
| 2511 | cmd = kzalloc(MAX_FILTER_STR_VAL, GFP_KERNEL); |
| 2512 | if (!cmd) { |
| 2513 | kfree(var_hist); |
| 2514 | return ERR_PTR(-ENOMEM); |
| 2515 | } |
| 2516 | |
| 2517 | /* Use the same keys as the compatible histogram */ |
| 2518 | strcat(cmd, "keys="); |
| 2519 | |
| 2520 | for_each_hist_key_field(i, hist_data) { |
| 2521 | key_field = hist_data->fields[i]; |
| 2522 | if (!first) |
| 2523 | strcat(cmd, ","); |
| 2524 | strcat(cmd, key_field->field->name); |
| 2525 | first = false; |
| 2526 | } |
| 2527 | |
| 2528 | /* Create the synthetic field variable specification */ |
| 2529 | strcat(cmd, ":synthetic_"); |
| 2530 | strcat(cmd, field_name); |
| 2531 | strcat(cmd, "="); |
| 2532 | strcat(cmd, field_name); |
| 2533 | |
| 2534 | /* Use the same filter as the compatible histogram */ |
| 2535 | saved_filter = find_trigger_filter(hist_data, file); |
| 2536 | if (saved_filter) { |
| 2537 | strcat(cmd, " if "); |
| 2538 | strcat(cmd, saved_filter); |
| 2539 | } |
| 2540 | |
| 2541 | var_hist->cmd = kstrdup(cmd, GFP_KERNEL); |
| 2542 | if (!var_hist->cmd) { |
| 2543 | kfree(cmd); |
| 2544 | kfree(var_hist); |
| 2545 | return ERR_PTR(-ENOMEM); |
| 2546 | } |
| 2547 | |
| 2548 | /* Save the compatible histogram information */ |
| 2549 | var_hist->hist_data = hist_data; |
| 2550 | |
| 2551 | /* Create the new histogram with our variable */ |
| 2552 | ret = event_hist_trigger_func(&trigger_hist_cmd, file, |
| 2553 | "", "hist", cmd); |
| 2554 | if (ret) { |
| 2555 | kfree(cmd); |
| 2556 | kfree(var_hist->cmd); |
| 2557 | kfree(var_hist); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2558 | hist_err(tr, HIST_ERR_HIST_CREATE_FAIL, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2559 | return ERR_PTR(ret); |
| 2560 | } |
| 2561 | |
| 2562 | kfree(cmd); |
| 2563 | |
| 2564 | /* If we can't find the variable, something went wrong */ |
| 2565 | event_var = find_synthetic_field_var(target_hist_data, subsys_name, |
| 2566 | event_name, field_name); |
| 2567 | if (IS_ERR_OR_NULL(event_var)) { |
| 2568 | kfree(var_hist->cmd); |
| 2569 | kfree(var_hist); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2570 | hist_err(tr, HIST_ERR_SYNTH_VAR_NOT_FOUND, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2571 | return ERR_PTR(-EINVAL); |
| 2572 | } |
| 2573 | |
| 2574 | n = target_hist_data->n_field_var_hists; |
| 2575 | target_hist_data->field_var_hists[n] = var_hist; |
| 2576 | target_hist_data->n_field_var_hists++; |
| 2577 | |
| 2578 | return event_var; |
| 2579 | } |
| 2580 | |
| 2581 | static struct hist_field * |
| 2582 | find_target_event_var(struct hist_trigger_data *hist_data, |
| 2583 | char *subsys_name, char *event_name, char *var_name) |
| 2584 | { |
| 2585 | struct trace_event_file *file = hist_data->event_file; |
| 2586 | struct hist_field *hist_field = NULL; |
| 2587 | |
| 2588 | if (subsys_name) { |
| 2589 | struct trace_event_call *call; |
| 2590 | |
| 2591 | if (!event_name) |
| 2592 | return NULL; |
| 2593 | |
| 2594 | call = file->event_call; |
| 2595 | |
| 2596 | if (strcmp(subsys_name, call->class->system) != 0) |
| 2597 | return NULL; |
| 2598 | |
| 2599 | if (strcmp(event_name, trace_event_name(call)) != 0) |
| 2600 | return NULL; |
| 2601 | } |
| 2602 | |
| 2603 | hist_field = find_var_field(hist_data, var_name); |
| 2604 | |
| 2605 | return hist_field; |
| 2606 | } |
| 2607 | |
| 2608 | static inline void __update_field_vars(struct tracing_map_elt *elt, |
| 2609 | struct ring_buffer_event *rbe, |
| 2610 | void *rec, |
| 2611 | struct field_var **field_vars, |
| 2612 | unsigned int n_field_vars, |
| 2613 | unsigned int field_var_str_start) |
| 2614 | { |
| 2615 | struct hist_elt_data *elt_data = elt->private_data; |
| 2616 | unsigned int i, j, var_idx; |
| 2617 | u64 var_val; |
| 2618 | |
| 2619 | for (i = 0, j = field_var_str_start; i < n_field_vars; i++) { |
| 2620 | struct field_var *field_var = field_vars[i]; |
| 2621 | struct hist_field *var = field_var->var; |
| 2622 | struct hist_field *val = field_var->val; |
| 2623 | |
| 2624 | var_val = val->fn(val, elt, rbe, rec); |
| 2625 | var_idx = var->var.idx; |
| 2626 | |
| 2627 | if (val->flags & HIST_FIELD_FL_STRING) { |
| 2628 | char *str = elt_data->field_var_str[j++]; |
| 2629 | char *val_str = (char *)(uintptr_t)var_val; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2630 | unsigned int size; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2631 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 2632 | size = min(val->size, STR_VAR_LEN_MAX); |
| 2633 | strscpy(str, val_str, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2634 | var_val = (u64)(uintptr_t)str; |
| 2635 | } |
| 2636 | tracing_map_set_var(elt, var_idx, var_val); |
| 2637 | } |
| 2638 | } |
| 2639 | |
| 2640 | static void update_field_vars(struct hist_trigger_data *hist_data, |
| 2641 | struct tracing_map_elt *elt, |
| 2642 | struct ring_buffer_event *rbe, |
| 2643 | void *rec) |
| 2644 | { |
| 2645 | __update_field_vars(elt, rbe, rec, hist_data->field_vars, |
| 2646 | hist_data->n_field_vars, 0); |
| 2647 | } |
| 2648 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2649 | static void save_track_data_vars(struct hist_trigger_data *hist_data, |
| 2650 | struct tracing_map_elt *elt, void *rec, |
| 2651 | struct ring_buffer_event *rbe, void *key, |
| 2652 | struct action_data *data, u64 *var_ref_vals) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2653 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2654 | __update_field_vars(elt, rbe, rec, hist_data->save_vars, |
| 2655 | hist_data->n_save_vars, hist_data->n_field_var_str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2656 | } |
| 2657 | |
| 2658 | static struct hist_field *create_var(struct hist_trigger_data *hist_data, |
| 2659 | struct trace_event_file *file, |
| 2660 | char *name, int size, const char *type) |
| 2661 | { |
| 2662 | struct hist_field *var; |
| 2663 | int idx; |
| 2664 | |
| 2665 | if (find_var(hist_data, file, name) && !hist_data->remove) { |
| 2666 | var = ERR_PTR(-EINVAL); |
| 2667 | goto out; |
| 2668 | } |
| 2669 | |
| 2670 | var = kzalloc(sizeof(struct hist_field), GFP_KERNEL); |
| 2671 | if (!var) { |
| 2672 | var = ERR_PTR(-ENOMEM); |
| 2673 | goto out; |
| 2674 | } |
| 2675 | |
| 2676 | idx = tracing_map_add_var(hist_data->map); |
| 2677 | if (idx < 0) { |
| 2678 | kfree(var); |
| 2679 | var = ERR_PTR(-EINVAL); |
| 2680 | goto out; |
| 2681 | } |
| 2682 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 2683 | var->ref = 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2684 | var->flags = HIST_FIELD_FL_VAR; |
| 2685 | var->var.idx = idx; |
| 2686 | var->var.hist_data = var->hist_data = hist_data; |
| 2687 | var->size = size; |
| 2688 | var->var.name = kstrdup(name, GFP_KERNEL); |
| 2689 | var->type = kstrdup(type, GFP_KERNEL); |
| 2690 | if (!var->var.name || !var->type) { |
| 2691 | kfree(var->var.name); |
| 2692 | kfree(var->type); |
| 2693 | kfree(var); |
| 2694 | var = ERR_PTR(-ENOMEM); |
| 2695 | } |
| 2696 | out: |
| 2697 | return var; |
| 2698 | } |
| 2699 | |
| 2700 | static struct field_var *create_field_var(struct hist_trigger_data *hist_data, |
| 2701 | struct trace_event_file *file, |
| 2702 | char *field_name) |
| 2703 | { |
| 2704 | struct hist_field *val = NULL, *var = NULL; |
| 2705 | unsigned long flags = HIST_FIELD_FL_VAR; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2706 | struct trace_array *tr = file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2707 | struct field_var *field_var; |
| 2708 | int ret = 0; |
| 2709 | |
| 2710 | if (hist_data->n_field_vars >= SYNTH_FIELDS_MAX) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2711 | hist_err(tr, HIST_ERR_TOO_MANY_FIELD_VARS, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2712 | ret = -EINVAL; |
| 2713 | goto err; |
| 2714 | } |
| 2715 | |
| 2716 | val = parse_atom(hist_data, file, field_name, &flags, NULL); |
| 2717 | if (IS_ERR(val)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2718 | hist_err(tr, HIST_ERR_FIELD_VAR_PARSE_FAIL, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2719 | ret = PTR_ERR(val); |
| 2720 | goto err; |
| 2721 | } |
| 2722 | |
| 2723 | var = create_var(hist_data, file, field_name, val->size, val->type); |
| 2724 | if (IS_ERR(var)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2725 | hist_err(tr, HIST_ERR_VAR_CREATE_FIND_FAIL, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2726 | kfree(val); |
| 2727 | ret = PTR_ERR(var); |
| 2728 | goto err; |
| 2729 | } |
| 2730 | |
| 2731 | field_var = kzalloc(sizeof(struct field_var), GFP_KERNEL); |
| 2732 | if (!field_var) { |
| 2733 | kfree(val); |
| 2734 | kfree(var); |
| 2735 | ret = -ENOMEM; |
| 2736 | goto err; |
| 2737 | } |
| 2738 | |
| 2739 | field_var->var = var; |
| 2740 | field_var->val = val; |
| 2741 | out: |
| 2742 | return field_var; |
| 2743 | err: |
| 2744 | field_var = ERR_PTR(ret); |
| 2745 | goto out; |
| 2746 | } |
| 2747 | |
| 2748 | /** |
| 2749 | * create_target_field_var - Automatically create a variable for a field |
| 2750 | * @target_hist_data: The target hist trigger |
| 2751 | * @subsys_name: Optional subsystem name |
| 2752 | * @event_name: Optional event name |
| 2753 | * @var_name: The name of the field (and the resulting variable) |
| 2754 | * |
| 2755 | * Hist trigger actions fetch data from variables, not directly from |
| 2756 | * events. However, for convenience, users are allowed to directly |
| 2757 | * specify an event field in an action, which will be automatically |
| 2758 | * converted into a variable on their behalf. |
| 2759 | |
| 2760 | * This function creates a field variable with the name var_name on |
| 2761 | * the hist trigger currently being defined on the target event. If |
| 2762 | * subsys_name and event_name are specified, this function simply |
| 2763 | * verifies that they do in fact match the target event subsystem and |
| 2764 | * event name. |
| 2765 | * |
| 2766 | * Return: The variable created for the field. |
| 2767 | */ |
| 2768 | static struct field_var * |
| 2769 | create_target_field_var(struct hist_trigger_data *target_hist_data, |
| 2770 | char *subsys_name, char *event_name, char *var_name) |
| 2771 | { |
| 2772 | struct trace_event_file *file = target_hist_data->event_file; |
| 2773 | |
| 2774 | if (subsys_name) { |
| 2775 | struct trace_event_call *call; |
| 2776 | |
| 2777 | if (!event_name) |
| 2778 | return NULL; |
| 2779 | |
| 2780 | call = file->event_call; |
| 2781 | |
| 2782 | if (strcmp(subsys_name, call->class->system) != 0) |
| 2783 | return NULL; |
| 2784 | |
| 2785 | if (strcmp(event_name, trace_event_name(call)) != 0) |
| 2786 | return NULL; |
| 2787 | } |
| 2788 | |
| 2789 | return create_field_var(target_hist_data, file, var_name); |
| 2790 | } |
| 2791 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2792 | static bool check_track_val_max(u64 track_val, u64 var_val) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2793 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2794 | if (var_val <= track_val) |
| 2795 | return false; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2796 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2797 | return true; |
| 2798 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2799 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2800 | static bool check_track_val_changed(u64 track_val, u64 var_val) |
| 2801 | { |
| 2802 | if (var_val == track_val) |
| 2803 | return false; |
| 2804 | |
| 2805 | return true; |
| 2806 | } |
| 2807 | |
| 2808 | static u64 get_track_val(struct hist_trigger_data *hist_data, |
| 2809 | struct tracing_map_elt *elt, |
| 2810 | struct action_data *data) |
| 2811 | { |
| 2812 | unsigned int track_var_idx = data->track_data.track_var->var.idx; |
| 2813 | u64 track_val; |
| 2814 | |
| 2815 | track_val = tracing_map_read_var(elt, track_var_idx); |
| 2816 | |
| 2817 | return track_val; |
| 2818 | } |
| 2819 | |
| 2820 | static void save_track_val(struct hist_trigger_data *hist_data, |
| 2821 | struct tracing_map_elt *elt, |
| 2822 | struct action_data *data, u64 var_val) |
| 2823 | { |
| 2824 | unsigned int track_var_idx = data->track_data.track_var->var.idx; |
| 2825 | |
| 2826 | tracing_map_set_var(elt, track_var_idx, var_val); |
| 2827 | } |
| 2828 | |
| 2829 | static void save_track_data(struct hist_trigger_data *hist_data, |
| 2830 | struct tracing_map_elt *elt, void *rec, |
| 2831 | struct ring_buffer_event *rbe, void *key, |
| 2832 | struct action_data *data, u64 *var_ref_vals) |
| 2833 | { |
| 2834 | if (data->track_data.save_data) |
| 2835 | data->track_data.save_data(hist_data, elt, rec, rbe, key, data, var_ref_vals); |
| 2836 | } |
| 2837 | |
| 2838 | static bool check_track_val(struct tracing_map_elt *elt, |
| 2839 | struct action_data *data, |
| 2840 | u64 var_val) |
| 2841 | { |
| 2842 | struct hist_trigger_data *hist_data; |
| 2843 | u64 track_val; |
| 2844 | |
| 2845 | hist_data = data->track_data.track_var->hist_data; |
| 2846 | track_val = get_track_val(hist_data, elt, data); |
| 2847 | |
| 2848 | return data->track_data.check_val(track_val, var_val); |
| 2849 | } |
| 2850 | |
| 2851 | #ifdef CONFIG_TRACER_SNAPSHOT |
| 2852 | static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) |
| 2853 | { |
| 2854 | /* called with tr->max_lock held */ |
| 2855 | struct track_data *track_data = tr->cond_snapshot->cond_data; |
| 2856 | struct hist_elt_data *elt_data, *track_elt_data; |
| 2857 | struct snapshot_context *context = cond_data; |
| 2858 | struct action_data *action; |
| 2859 | u64 track_val; |
| 2860 | |
| 2861 | if (!track_data) |
| 2862 | return false; |
| 2863 | |
| 2864 | action = track_data->action_data; |
| 2865 | |
| 2866 | track_val = get_track_val(track_data->hist_data, context->elt, |
| 2867 | track_data->action_data); |
| 2868 | |
| 2869 | if (!action->track_data.check_val(track_data->track_val, track_val)) |
| 2870 | return false; |
| 2871 | |
| 2872 | track_data->track_val = track_val; |
| 2873 | memcpy(track_data->key, context->key, track_data->key_len); |
| 2874 | |
| 2875 | elt_data = context->elt->private_data; |
| 2876 | track_elt_data = track_data->elt.private_data; |
| 2877 | if (elt_data->comm) |
| 2878 | strncpy(track_elt_data->comm, elt_data->comm, TASK_COMM_LEN); |
| 2879 | |
| 2880 | track_data->updated = true; |
| 2881 | |
| 2882 | return true; |
| 2883 | } |
| 2884 | |
| 2885 | static void save_track_data_snapshot(struct hist_trigger_data *hist_data, |
| 2886 | struct tracing_map_elt *elt, void *rec, |
| 2887 | struct ring_buffer_event *rbe, void *key, |
| 2888 | struct action_data *data, |
| 2889 | u64 *var_ref_vals) |
| 2890 | { |
| 2891 | struct trace_event_file *file = hist_data->event_file; |
| 2892 | struct snapshot_context context; |
| 2893 | |
| 2894 | context.elt = elt; |
| 2895 | context.key = key; |
| 2896 | |
| 2897 | tracing_snapshot_cond(file->tr, &context); |
| 2898 | } |
| 2899 | |
| 2900 | static void hist_trigger_print_key(struct seq_file *m, |
| 2901 | struct hist_trigger_data *hist_data, |
| 2902 | void *key, |
| 2903 | struct tracing_map_elt *elt); |
| 2904 | |
| 2905 | static struct action_data *snapshot_action(struct hist_trigger_data *hist_data) |
| 2906 | { |
| 2907 | unsigned int i; |
| 2908 | |
| 2909 | if (!hist_data->n_actions) |
| 2910 | return NULL; |
| 2911 | |
| 2912 | for (i = 0; i < hist_data->n_actions; i++) { |
| 2913 | struct action_data *data = hist_data->actions[i]; |
| 2914 | |
| 2915 | if (data->action == ACTION_SNAPSHOT) |
| 2916 | return data; |
| 2917 | } |
| 2918 | |
| 2919 | return NULL; |
| 2920 | } |
| 2921 | |
| 2922 | static void track_data_snapshot_print(struct seq_file *m, |
| 2923 | struct hist_trigger_data *hist_data) |
| 2924 | { |
| 2925 | struct trace_event_file *file = hist_data->event_file; |
| 2926 | struct track_data *track_data; |
| 2927 | struct action_data *action; |
| 2928 | |
| 2929 | track_data = tracing_cond_snapshot_data(file->tr); |
| 2930 | if (!track_data) |
| 2931 | return; |
| 2932 | |
| 2933 | if (!track_data->updated) |
| 2934 | return; |
| 2935 | |
| 2936 | action = snapshot_action(hist_data); |
| 2937 | if (!action) |
| 2938 | return; |
| 2939 | |
| 2940 | seq_puts(m, "\nSnapshot taken (see tracing/snapshot). Details:\n"); |
| 2941 | seq_printf(m, "\ttriggering value { %s(%s) }: %10llu", |
| 2942 | action->handler == HANDLER_ONMAX ? "onmax" : "onchange", |
| 2943 | action->track_data.var_str, track_data->track_val); |
| 2944 | |
| 2945 | seq_puts(m, "\ttriggered by event with key: "); |
| 2946 | hist_trigger_print_key(m, hist_data, track_data->key, &track_data->elt); |
| 2947 | seq_putc(m, '\n'); |
| 2948 | } |
| 2949 | #else |
| 2950 | static bool cond_snapshot_update(struct trace_array *tr, void *cond_data) |
| 2951 | { |
| 2952 | return false; |
| 2953 | } |
| 2954 | static void save_track_data_snapshot(struct hist_trigger_data *hist_data, |
| 2955 | struct tracing_map_elt *elt, void *rec, |
| 2956 | struct ring_buffer_event *rbe, void *key, |
| 2957 | struct action_data *data, |
| 2958 | u64 *var_ref_vals) {} |
| 2959 | static void track_data_snapshot_print(struct seq_file *m, |
| 2960 | struct hist_trigger_data *hist_data) {} |
| 2961 | #endif /* CONFIG_TRACER_SNAPSHOT */ |
| 2962 | |
| 2963 | static void track_data_print(struct seq_file *m, |
| 2964 | struct hist_trigger_data *hist_data, |
| 2965 | struct tracing_map_elt *elt, |
| 2966 | struct action_data *data) |
| 2967 | { |
| 2968 | u64 track_val = get_track_val(hist_data, elt, data); |
| 2969 | unsigned int i, save_var_idx; |
| 2970 | |
| 2971 | if (data->handler == HANDLER_ONMAX) |
| 2972 | seq_printf(m, "\n\tmax: %10llu", track_val); |
| 2973 | else if (data->handler == HANDLER_ONCHANGE) |
| 2974 | seq_printf(m, "\n\tchanged: %10llu", track_val); |
| 2975 | |
| 2976 | if (data->action == ACTION_SNAPSHOT) |
| 2977 | return; |
| 2978 | |
| 2979 | for (i = 0; i < hist_data->n_save_vars; i++) { |
| 2980 | struct hist_field *save_val = hist_data->save_vars[i]->val; |
| 2981 | struct hist_field *save_var = hist_data->save_vars[i]->var; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2982 | u64 val; |
| 2983 | |
| 2984 | save_var_idx = save_var->var.idx; |
| 2985 | |
| 2986 | val = tracing_map_read_var(elt, save_var_idx); |
| 2987 | |
| 2988 | if (save_val->flags & HIST_FIELD_FL_STRING) { |
| 2989 | seq_printf(m, " %s: %-32s", save_var->var.name, |
| 2990 | (char *)(uintptr_t)(val)); |
| 2991 | } else |
| 2992 | seq_printf(m, " %s: %10llu", save_var->var.name, val); |
| 2993 | } |
| 2994 | } |
| 2995 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 2996 | static void ontrack_action(struct hist_trigger_data *hist_data, |
| 2997 | struct tracing_map_elt *elt, void *rec, |
| 2998 | struct ring_buffer_event *rbe, void *key, |
| 2999 | struct action_data *data, u64 *var_ref_vals) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3000 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3001 | u64 var_val = var_ref_vals[data->track_data.var_ref->var_ref_idx]; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3002 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3003 | if (check_track_val(elt, data, var_val)) { |
| 3004 | save_track_val(hist_data, elt, data, var_val); |
| 3005 | save_track_data(hist_data, elt, rec, rbe, key, data, var_ref_vals); |
| 3006 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3009 | static void action_data_destroy(struct action_data *data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3010 | { |
| 3011 | unsigned int i; |
| 3012 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3013 | lockdep_assert_held(&event_mutex); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3014 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3015 | kfree(data->action_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3016 | |
| 3017 | for (i = 0; i < data->n_params; i++) |
| 3018 | kfree(data->params[i]); |
| 3019 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3020 | if (data->synth_event) |
| 3021 | data->synth_event->ref--; |
| 3022 | |
| 3023 | kfree(data->synth_event_name); |
| 3024 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3025 | kfree(data); |
| 3026 | } |
| 3027 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3028 | static void track_data_destroy(struct hist_trigger_data *hist_data, |
| 3029 | struct action_data *data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3030 | { |
| 3031 | struct trace_event_file *file = hist_data->event_file; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3032 | |
| 3033 | destroy_hist_field(data->track_data.track_var, 0); |
| 3034 | |
| 3035 | if (data->action == ACTION_SNAPSHOT) { |
| 3036 | struct track_data *track_data; |
| 3037 | |
| 3038 | track_data = tracing_cond_snapshot_data(file->tr); |
| 3039 | if (track_data && track_data->hist_data == hist_data) { |
| 3040 | tracing_snapshot_cond_disable(file->tr); |
| 3041 | track_data_free(track_data); |
| 3042 | } |
| 3043 | } |
| 3044 | |
| 3045 | kfree(data->track_data.var_str); |
| 3046 | |
| 3047 | action_data_destroy(data); |
| 3048 | } |
| 3049 | |
| 3050 | static int action_create(struct hist_trigger_data *hist_data, |
| 3051 | struct action_data *data); |
| 3052 | |
| 3053 | static int track_data_create(struct hist_trigger_data *hist_data, |
| 3054 | struct action_data *data) |
| 3055 | { |
| 3056 | struct hist_field *var_field, *ref_field, *track_var = NULL; |
| 3057 | struct trace_event_file *file = hist_data->event_file; |
| 3058 | struct trace_array *tr = file->tr; |
| 3059 | char *track_data_var_str; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3060 | int ret = 0; |
| 3061 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3062 | track_data_var_str = data->track_data.var_str; |
| 3063 | if (track_data_var_str[0] != '$') { |
| 3064 | hist_err(tr, HIST_ERR_ONX_NOT_VAR, errpos(track_data_var_str)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3065 | return -EINVAL; |
| 3066 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3067 | track_data_var_str++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3068 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3069 | var_field = find_target_event_var(hist_data, NULL, NULL, track_data_var_str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3070 | if (!var_field) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3071 | hist_err(tr, HIST_ERR_ONX_VAR_NOT_FOUND, errpos(track_data_var_str)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3072 | return -EINVAL; |
| 3073 | } |
| 3074 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3075 | ref_field = create_var_ref(hist_data, var_field, NULL, NULL); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3076 | if (!ref_field) |
| 3077 | return -ENOMEM; |
| 3078 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3079 | data->track_data.var_ref = ref_field; |
| 3080 | |
| 3081 | if (data->handler == HANDLER_ONMAX) |
| 3082 | track_var = create_var(hist_data, file, "__max", sizeof(u64), "u64"); |
| 3083 | if (IS_ERR(track_var)) { |
| 3084 | hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); |
| 3085 | ret = PTR_ERR(track_var); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3086 | goto out; |
| 3087 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3088 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3089 | if (data->handler == HANDLER_ONCHANGE) |
| 3090 | track_var = create_var(hist_data, file, "__change", sizeof(u64), "u64"); |
| 3091 | if (IS_ERR(track_var)) { |
| 3092 | hist_err(tr, HIST_ERR_ONX_VAR_CREATE_FAIL, 0); |
| 3093 | ret = PTR_ERR(track_var); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3094 | goto out; |
| 3095 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3096 | data->track_data.track_var = track_var; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3097 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3098 | ret = action_create(hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3099 | out: |
| 3100 | return ret; |
| 3101 | } |
| 3102 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3103 | static int parse_action_params(struct trace_array *tr, char *params, |
| 3104 | struct action_data *data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3105 | { |
| 3106 | char *param, *saved_param; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3107 | bool first_param = true; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3108 | int ret = 0; |
| 3109 | |
| 3110 | while (params) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3111 | if (data->n_params >= SYNTH_FIELDS_MAX) { |
| 3112 | hist_err(tr, HIST_ERR_TOO_MANY_PARAMS, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3113 | goto out; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3114 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3115 | |
| 3116 | param = strsep(¶ms, ","); |
| 3117 | if (!param) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3118 | hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3119 | ret = -EINVAL; |
| 3120 | goto out; |
| 3121 | } |
| 3122 | |
| 3123 | param = strstrip(param); |
| 3124 | if (strlen(param) < 2) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3125 | hist_err(tr, HIST_ERR_INVALID_PARAM, errpos(param)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3126 | ret = -EINVAL; |
| 3127 | goto out; |
| 3128 | } |
| 3129 | |
| 3130 | saved_param = kstrdup(param, GFP_KERNEL); |
| 3131 | if (!saved_param) { |
| 3132 | ret = -ENOMEM; |
| 3133 | goto out; |
| 3134 | } |
| 3135 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3136 | if (first_param && data->use_trace_keyword) { |
| 3137 | data->synth_event_name = saved_param; |
| 3138 | first_param = false; |
| 3139 | continue; |
| 3140 | } |
| 3141 | first_param = false; |
| 3142 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3143 | data->params[data->n_params++] = saved_param; |
| 3144 | } |
| 3145 | out: |
| 3146 | return ret; |
| 3147 | } |
| 3148 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3149 | static int action_parse(struct trace_array *tr, char *str, struct action_data *data, |
| 3150 | enum handler_id handler) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3151 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3152 | char *action_name; |
| 3153 | int ret = 0; |
| 3154 | |
| 3155 | strsep(&str, "."); |
| 3156 | if (!str) { |
| 3157 | hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); |
| 3158 | ret = -EINVAL; |
| 3159 | goto out; |
| 3160 | } |
| 3161 | |
| 3162 | action_name = strsep(&str, "("); |
| 3163 | if (!action_name || !str) { |
| 3164 | hist_err(tr, HIST_ERR_ACTION_NOT_FOUND, 0); |
| 3165 | ret = -EINVAL; |
| 3166 | goto out; |
| 3167 | } |
| 3168 | |
| 3169 | if (str_has_prefix(action_name, "save")) { |
| 3170 | char *params = strsep(&str, ")"); |
| 3171 | |
| 3172 | if (!params) { |
| 3173 | hist_err(tr, HIST_ERR_NO_SAVE_PARAMS, 0); |
| 3174 | ret = -EINVAL; |
| 3175 | goto out; |
| 3176 | } |
| 3177 | |
| 3178 | ret = parse_action_params(tr, params, data); |
| 3179 | if (ret) |
| 3180 | goto out; |
| 3181 | |
| 3182 | if (handler == HANDLER_ONMAX) |
| 3183 | data->track_data.check_val = check_track_val_max; |
| 3184 | else if (handler == HANDLER_ONCHANGE) |
| 3185 | data->track_data.check_val = check_track_val_changed; |
| 3186 | else { |
| 3187 | hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); |
| 3188 | ret = -EINVAL; |
| 3189 | goto out; |
| 3190 | } |
| 3191 | |
| 3192 | data->track_data.save_data = save_track_data_vars; |
| 3193 | data->fn = ontrack_action; |
| 3194 | data->action = ACTION_SAVE; |
| 3195 | } else if (str_has_prefix(action_name, "snapshot")) { |
| 3196 | char *params = strsep(&str, ")"); |
| 3197 | |
| 3198 | if (!str) { |
| 3199 | hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(params)); |
| 3200 | ret = -EINVAL; |
| 3201 | goto out; |
| 3202 | } |
| 3203 | |
| 3204 | if (handler == HANDLER_ONMAX) |
| 3205 | data->track_data.check_val = check_track_val_max; |
| 3206 | else if (handler == HANDLER_ONCHANGE) |
| 3207 | data->track_data.check_val = check_track_val_changed; |
| 3208 | else { |
| 3209 | hist_err(tr, HIST_ERR_ACTION_MISMATCH, errpos(action_name)); |
| 3210 | ret = -EINVAL; |
| 3211 | goto out; |
| 3212 | } |
| 3213 | |
| 3214 | data->track_data.save_data = save_track_data_snapshot; |
| 3215 | data->fn = ontrack_action; |
| 3216 | data->action = ACTION_SNAPSHOT; |
| 3217 | } else { |
| 3218 | char *params = strsep(&str, ")"); |
| 3219 | |
| 3220 | if (str_has_prefix(action_name, "trace")) |
| 3221 | data->use_trace_keyword = true; |
| 3222 | |
| 3223 | if (params) { |
| 3224 | ret = parse_action_params(tr, params, data); |
| 3225 | if (ret) |
| 3226 | goto out; |
| 3227 | } |
| 3228 | |
| 3229 | if (handler == HANDLER_ONMAX) |
| 3230 | data->track_data.check_val = check_track_val_max; |
| 3231 | else if (handler == HANDLER_ONCHANGE) |
| 3232 | data->track_data.check_val = check_track_val_changed; |
| 3233 | |
| 3234 | if (handler != HANDLER_ONMATCH) { |
| 3235 | data->track_data.save_data = action_trace; |
| 3236 | data->fn = ontrack_action; |
| 3237 | } else |
| 3238 | data->fn = action_trace; |
| 3239 | |
| 3240 | data->action = ACTION_TRACE; |
| 3241 | } |
| 3242 | |
| 3243 | data->action_name = kstrdup(action_name, GFP_KERNEL); |
| 3244 | if (!data->action_name) { |
| 3245 | ret = -ENOMEM; |
| 3246 | goto out; |
| 3247 | } |
| 3248 | |
| 3249 | data->handler = handler; |
| 3250 | out: |
| 3251 | return ret; |
| 3252 | } |
| 3253 | |
| 3254 | static struct action_data *track_data_parse(struct hist_trigger_data *hist_data, |
| 3255 | char *str, enum handler_id handler) |
| 3256 | { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3257 | struct action_data *data; |
| 3258 | int ret = -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3259 | char *var_str; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3260 | |
| 3261 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 3262 | if (!data) |
| 3263 | return ERR_PTR(-ENOMEM); |
| 3264 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3265 | var_str = strsep(&str, ")"); |
| 3266 | if (!var_str || !str) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3267 | ret = -EINVAL; |
| 3268 | goto free; |
| 3269 | } |
| 3270 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3271 | data->track_data.var_str = kstrdup(var_str, GFP_KERNEL); |
| 3272 | if (!data->track_data.var_str) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3273 | ret = -ENOMEM; |
| 3274 | goto free; |
| 3275 | } |
| 3276 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3277 | ret = action_parse(hist_data->event_file->tr, str, data, handler); |
| 3278 | if (ret) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3279 | goto free; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3280 | out: |
| 3281 | return data; |
| 3282 | free: |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3283 | track_data_destroy(hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3284 | data = ERR_PTR(ret); |
| 3285 | goto out; |
| 3286 | } |
| 3287 | |
| 3288 | static void onmatch_destroy(struct action_data *data) |
| 3289 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3290 | kfree(data->match_data.event); |
| 3291 | kfree(data->match_data.event_system); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3292 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3293 | action_data_destroy(data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3294 | } |
| 3295 | |
| 3296 | static void destroy_field_var(struct field_var *field_var) |
| 3297 | { |
| 3298 | if (!field_var) |
| 3299 | return; |
| 3300 | |
| 3301 | destroy_hist_field(field_var->var, 0); |
| 3302 | destroy_hist_field(field_var->val, 0); |
| 3303 | |
| 3304 | kfree(field_var); |
| 3305 | } |
| 3306 | |
| 3307 | static void destroy_field_vars(struct hist_trigger_data *hist_data) |
| 3308 | { |
| 3309 | unsigned int i; |
| 3310 | |
| 3311 | for (i = 0; i < hist_data->n_field_vars; i++) |
| 3312 | destroy_field_var(hist_data->field_vars[i]); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3313 | |
| 3314 | for (i = 0; i < hist_data->n_save_vars; i++) |
| 3315 | destroy_field_var(hist_data->save_vars[i]); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3316 | } |
| 3317 | |
| 3318 | static void save_field_var(struct hist_trigger_data *hist_data, |
| 3319 | struct field_var *field_var) |
| 3320 | { |
| 3321 | hist_data->field_vars[hist_data->n_field_vars++] = field_var; |
| 3322 | |
| 3323 | if (field_var->val->flags & HIST_FIELD_FL_STRING) |
| 3324 | hist_data->n_field_var_str++; |
| 3325 | } |
| 3326 | |
| 3327 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3328 | static int check_synth_field(struct synth_event *event, |
| 3329 | struct hist_field *hist_field, |
| 3330 | unsigned int field_pos) |
| 3331 | { |
| 3332 | struct synth_field *field; |
| 3333 | |
| 3334 | if (field_pos >= event->n_fields) |
| 3335 | return -EINVAL; |
| 3336 | |
| 3337 | field = event->fields[field_pos]; |
| 3338 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3339 | /* |
| 3340 | * A dynamic string synth field can accept static or |
| 3341 | * dynamic. A static string synth field can only accept a |
| 3342 | * same-sized static string, which is checked for later. |
| 3343 | */ |
| 3344 | if (strstr(hist_field->type, "char[") && field->is_string |
| 3345 | && field->is_dynamic) |
| 3346 | return 0; |
| 3347 | |
| 3348 | if (strcmp(field->type, hist_field->type) != 0) { |
| 3349 | if (field->size != hist_field->size || |
| 3350 | (!field->is_string && field->is_signed != hist_field->is_signed)) |
| 3351 | return -EINVAL; |
| 3352 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3353 | |
| 3354 | return 0; |
| 3355 | } |
| 3356 | |
| 3357 | static struct hist_field * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3358 | trace_action_find_var(struct hist_trigger_data *hist_data, |
| 3359 | struct action_data *data, |
| 3360 | char *system, char *event, char *var) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3361 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3362 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3363 | struct hist_field *hist_field; |
| 3364 | |
| 3365 | var++; /* skip '$' */ |
| 3366 | |
| 3367 | hist_field = find_target_event_var(hist_data, system, event, var); |
| 3368 | if (!hist_field) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3369 | if (!system && data->handler == HANDLER_ONMATCH) { |
| 3370 | system = data->match_data.event_system; |
| 3371 | event = data->match_data.event; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3372 | } |
| 3373 | |
| 3374 | hist_field = find_event_var(hist_data, system, event, var); |
| 3375 | } |
| 3376 | |
| 3377 | if (!hist_field) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3378 | hist_err(tr, HIST_ERR_PARAM_NOT_FOUND, errpos(var)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3379 | |
| 3380 | return hist_field; |
| 3381 | } |
| 3382 | |
| 3383 | static struct hist_field * |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3384 | trace_action_create_field_var(struct hist_trigger_data *hist_data, |
| 3385 | struct action_data *data, char *system, |
| 3386 | char *event, char *var) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3387 | { |
| 3388 | struct hist_field *hist_field = NULL; |
| 3389 | struct field_var *field_var; |
| 3390 | |
| 3391 | /* |
| 3392 | * First try to create a field var on the target event (the |
| 3393 | * currently being defined). This will create a variable for |
| 3394 | * unqualified fields on the target event, or if qualified, |
| 3395 | * target fields that have qualified names matching the target. |
| 3396 | */ |
| 3397 | field_var = create_target_field_var(hist_data, system, event, var); |
| 3398 | |
| 3399 | if (field_var && !IS_ERR(field_var)) { |
| 3400 | save_field_var(hist_data, field_var); |
| 3401 | hist_field = field_var->var; |
| 3402 | } else { |
| 3403 | field_var = NULL; |
| 3404 | /* |
| 3405 | * If no explicit system.event is specfied, default to |
| 3406 | * looking for fields on the onmatch(system.event.xxx) |
| 3407 | * event. |
| 3408 | */ |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3409 | if (!system && data->handler == HANDLER_ONMATCH) { |
| 3410 | system = data->match_data.event_system; |
| 3411 | event = data->match_data.event; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3412 | } |
| 3413 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3414 | if (!event) |
| 3415 | goto free; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3416 | /* |
| 3417 | * At this point, we're looking at a field on another |
| 3418 | * event. Because we can't modify a hist trigger on |
| 3419 | * another event to add a variable for a field, we need |
| 3420 | * to create a new trigger on that event and create the |
| 3421 | * variable at the same time. |
| 3422 | */ |
| 3423 | hist_field = create_field_var_hist(hist_data, system, event, var); |
| 3424 | if (IS_ERR(hist_field)) |
| 3425 | goto free; |
| 3426 | } |
| 3427 | out: |
| 3428 | return hist_field; |
| 3429 | free: |
| 3430 | destroy_field_var(field_var); |
| 3431 | hist_field = NULL; |
| 3432 | goto out; |
| 3433 | } |
| 3434 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3435 | static int trace_action_create(struct hist_trigger_data *hist_data, |
| 3436 | struct action_data *data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3437 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3438 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3439 | char *event_name, *param, *system = NULL; |
| 3440 | struct hist_field *hist_field, *var_ref; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3441 | unsigned int i; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3442 | unsigned int field_pos = 0; |
| 3443 | struct synth_event *event; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3444 | char *synth_event_name; |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3445 | int var_ref_idx, ret = 0; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3446 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3447 | lockdep_assert_held(&event_mutex); |
| 3448 | |
| 3449 | if (data->use_trace_keyword) |
| 3450 | synth_event_name = data->synth_event_name; |
| 3451 | else |
| 3452 | synth_event_name = data->action_name; |
| 3453 | |
| 3454 | event = find_synth_event(synth_event_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3455 | if (!event) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3456 | hist_err(tr, HIST_ERR_SYNTH_EVENT_NOT_FOUND, errpos(synth_event_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3457 | return -EINVAL; |
| 3458 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3459 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3460 | event->ref++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3461 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3462 | for (i = 0; i < data->n_params; i++) { |
| 3463 | char *p; |
| 3464 | |
| 3465 | p = param = kstrdup(data->params[i], GFP_KERNEL); |
| 3466 | if (!param) { |
| 3467 | ret = -ENOMEM; |
| 3468 | goto err; |
| 3469 | } |
| 3470 | |
| 3471 | system = strsep(¶m, "."); |
| 3472 | if (!param) { |
| 3473 | param = (char *)system; |
| 3474 | system = event_name = NULL; |
| 3475 | } else { |
| 3476 | event_name = strsep(¶m, "."); |
| 3477 | if (!param) { |
| 3478 | kfree(p); |
| 3479 | ret = -EINVAL; |
| 3480 | goto err; |
| 3481 | } |
| 3482 | } |
| 3483 | |
| 3484 | if (param[0] == '$') |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3485 | hist_field = trace_action_find_var(hist_data, data, |
| 3486 | system, event_name, |
| 3487 | param); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3488 | else |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3489 | hist_field = trace_action_create_field_var(hist_data, |
| 3490 | data, |
| 3491 | system, |
| 3492 | event_name, |
| 3493 | param); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3494 | |
| 3495 | if (!hist_field) { |
| 3496 | kfree(p); |
| 3497 | ret = -EINVAL; |
| 3498 | goto err; |
| 3499 | } |
| 3500 | |
| 3501 | if (check_synth_field(event, hist_field, field_pos) == 0) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3502 | var_ref = create_var_ref(hist_data, hist_field, |
| 3503 | system, event_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3504 | if (!var_ref) { |
| 3505 | kfree(p); |
| 3506 | ret = -ENOMEM; |
| 3507 | goto err; |
| 3508 | } |
| 3509 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3510 | var_ref_idx = find_var_ref_idx(hist_data, var_ref); |
| 3511 | if (WARN_ON(var_ref_idx < 0)) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3512 | kfree(p); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 3513 | ret = var_ref_idx; |
| 3514 | goto err; |
| 3515 | } |
| 3516 | |
| 3517 | data->var_ref_idx[i] = var_ref_idx; |
| 3518 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3519 | field_pos++; |
| 3520 | kfree(p); |
| 3521 | continue; |
| 3522 | } |
| 3523 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3524 | hist_err(tr, HIST_ERR_SYNTH_TYPE_MISMATCH, errpos(param)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3525 | kfree(p); |
| 3526 | ret = -EINVAL; |
| 3527 | goto err; |
| 3528 | } |
| 3529 | |
| 3530 | if (field_pos != event->n_fields) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3531 | hist_err(tr, HIST_ERR_SYNTH_COUNT_MISMATCH, errpos(event->name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3532 | ret = -EINVAL; |
| 3533 | goto err; |
| 3534 | } |
| 3535 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3536 | data->synth_event = event; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3537 | out: |
| 3538 | return ret; |
| 3539 | err: |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3540 | event->ref--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3541 | |
| 3542 | goto out; |
| 3543 | } |
| 3544 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3545 | static int action_create(struct hist_trigger_data *hist_data, |
| 3546 | struct action_data *data) |
| 3547 | { |
| 3548 | struct trace_event_file *file = hist_data->event_file; |
| 3549 | struct trace_array *tr = file->tr; |
| 3550 | struct track_data *track_data; |
| 3551 | struct field_var *field_var; |
| 3552 | unsigned int i; |
| 3553 | char *param; |
| 3554 | int ret = 0; |
| 3555 | |
| 3556 | if (data->action == ACTION_TRACE) |
| 3557 | return trace_action_create(hist_data, data); |
| 3558 | |
| 3559 | if (data->action == ACTION_SNAPSHOT) { |
| 3560 | track_data = track_data_alloc(hist_data->key_size, data, hist_data); |
| 3561 | if (IS_ERR(track_data)) { |
| 3562 | ret = PTR_ERR(track_data); |
| 3563 | goto out; |
| 3564 | } |
| 3565 | |
| 3566 | ret = tracing_snapshot_cond_enable(file->tr, track_data, |
| 3567 | cond_snapshot_update); |
| 3568 | if (ret) |
| 3569 | track_data_free(track_data); |
| 3570 | |
| 3571 | goto out; |
| 3572 | } |
| 3573 | |
| 3574 | if (data->action == ACTION_SAVE) { |
| 3575 | if (hist_data->n_save_vars) { |
| 3576 | ret = -EEXIST; |
| 3577 | hist_err(tr, HIST_ERR_TOO_MANY_SAVE_ACTIONS, 0); |
| 3578 | goto out; |
| 3579 | } |
| 3580 | |
| 3581 | for (i = 0; i < data->n_params; i++) { |
| 3582 | param = kstrdup(data->params[i], GFP_KERNEL); |
| 3583 | if (!param) { |
| 3584 | ret = -ENOMEM; |
| 3585 | goto out; |
| 3586 | } |
| 3587 | |
| 3588 | field_var = create_target_field_var(hist_data, NULL, NULL, param); |
| 3589 | if (IS_ERR(field_var)) { |
| 3590 | hist_err(tr, HIST_ERR_FIELD_VAR_CREATE_FAIL, |
| 3591 | errpos(param)); |
| 3592 | ret = PTR_ERR(field_var); |
| 3593 | kfree(param); |
| 3594 | goto out; |
| 3595 | } |
| 3596 | |
| 3597 | hist_data->save_vars[hist_data->n_save_vars++] = field_var; |
| 3598 | if (field_var->val->flags & HIST_FIELD_FL_STRING) |
| 3599 | hist_data->n_save_var_str++; |
| 3600 | kfree(param); |
| 3601 | } |
| 3602 | } |
| 3603 | out: |
| 3604 | return ret; |
| 3605 | } |
| 3606 | |
| 3607 | static int onmatch_create(struct hist_trigger_data *hist_data, |
| 3608 | struct action_data *data) |
| 3609 | { |
| 3610 | return action_create(hist_data, data); |
| 3611 | } |
| 3612 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3613 | static struct action_data *onmatch_parse(struct trace_array *tr, char *str) |
| 3614 | { |
| 3615 | char *match_event, *match_event_system; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3616 | struct action_data *data; |
| 3617 | int ret = -EINVAL; |
| 3618 | |
| 3619 | data = kzalloc(sizeof(*data), GFP_KERNEL); |
| 3620 | if (!data) |
| 3621 | return ERR_PTR(-ENOMEM); |
| 3622 | |
| 3623 | match_event = strsep(&str, ")"); |
| 3624 | if (!match_event || !str) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3625 | hist_err(tr, HIST_ERR_NO_CLOSING_PAREN, errpos(match_event)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3626 | goto free; |
| 3627 | } |
| 3628 | |
| 3629 | match_event_system = strsep(&match_event, "."); |
| 3630 | if (!match_event) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3631 | hist_err(tr, HIST_ERR_SUBSYS_NOT_FOUND, errpos(match_event_system)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3632 | goto free; |
| 3633 | } |
| 3634 | |
| 3635 | if (IS_ERR(event_file(tr, match_event_system, match_event))) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3636 | hist_err(tr, HIST_ERR_INVALID_SUBSYS_EVENT, errpos(match_event)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3637 | goto free; |
| 3638 | } |
| 3639 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3640 | data->match_data.event = kstrdup(match_event, GFP_KERNEL); |
| 3641 | if (!data->match_data.event) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3642 | ret = -ENOMEM; |
| 3643 | goto free; |
| 3644 | } |
| 3645 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3646 | data->match_data.event_system = kstrdup(match_event_system, GFP_KERNEL); |
| 3647 | if (!data->match_data.event_system) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3648 | ret = -ENOMEM; |
| 3649 | goto free; |
| 3650 | } |
| 3651 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3652 | ret = action_parse(tr, str, data, HANDLER_ONMATCH); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3653 | if (ret) |
| 3654 | goto free; |
| 3655 | out: |
| 3656 | return data; |
| 3657 | free: |
| 3658 | onmatch_destroy(data); |
| 3659 | data = ERR_PTR(ret); |
| 3660 | goto out; |
| 3661 | } |
| 3662 | |
| 3663 | static int create_hitcount_val(struct hist_trigger_data *hist_data) |
| 3664 | { |
| 3665 | hist_data->fields[HITCOUNT_IDX] = |
| 3666 | create_hist_field(hist_data, NULL, HIST_FIELD_FL_HITCOUNT, NULL); |
| 3667 | if (!hist_data->fields[HITCOUNT_IDX]) |
| 3668 | return -ENOMEM; |
| 3669 | |
| 3670 | hist_data->n_vals++; |
| 3671 | hist_data->n_fields++; |
| 3672 | |
| 3673 | if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX)) |
| 3674 | return -EINVAL; |
| 3675 | |
| 3676 | return 0; |
| 3677 | } |
| 3678 | |
| 3679 | static int __create_val_field(struct hist_trigger_data *hist_data, |
| 3680 | unsigned int val_idx, |
| 3681 | struct trace_event_file *file, |
| 3682 | char *var_name, char *field_str, |
| 3683 | unsigned long flags) |
| 3684 | { |
| 3685 | struct hist_field *hist_field; |
| 3686 | int ret = 0; |
| 3687 | |
| 3688 | hist_field = parse_expr(hist_data, file, field_str, flags, var_name, 0); |
| 3689 | if (IS_ERR(hist_field)) { |
| 3690 | ret = PTR_ERR(hist_field); |
| 3691 | goto out; |
| 3692 | } |
| 3693 | |
| 3694 | hist_data->fields[val_idx] = hist_field; |
| 3695 | |
| 3696 | ++hist_data->n_vals; |
| 3697 | ++hist_data->n_fields; |
| 3698 | |
| 3699 | if (WARN_ON(hist_data->n_vals > TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) |
| 3700 | ret = -EINVAL; |
| 3701 | out: |
| 3702 | return ret; |
| 3703 | } |
| 3704 | |
| 3705 | static int create_val_field(struct hist_trigger_data *hist_data, |
| 3706 | unsigned int val_idx, |
| 3707 | struct trace_event_file *file, |
| 3708 | char *field_str) |
| 3709 | { |
| 3710 | if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX)) |
| 3711 | return -EINVAL; |
| 3712 | |
| 3713 | return __create_val_field(hist_data, val_idx, file, NULL, field_str, 0); |
| 3714 | } |
| 3715 | |
| 3716 | static int create_var_field(struct hist_trigger_data *hist_data, |
| 3717 | unsigned int val_idx, |
| 3718 | struct trace_event_file *file, |
| 3719 | char *var_name, char *expr_str) |
| 3720 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3721 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3722 | unsigned long flags = 0; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3723 | int ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3724 | |
| 3725 | if (WARN_ON(val_idx >= TRACING_MAP_VALS_MAX + TRACING_MAP_VARS_MAX)) |
| 3726 | return -EINVAL; |
| 3727 | |
| 3728 | if (find_var(hist_data, file, var_name) && !hist_data->remove) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3729 | hist_err(tr, HIST_ERR_DUPLICATE_VAR, errpos(var_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3730 | return -EINVAL; |
| 3731 | } |
| 3732 | |
| 3733 | flags |= HIST_FIELD_FL_VAR; |
| 3734 | hist_data->n_vars++; |
| 3735 | if (WARN_ON(hist_data->n_vars > TRACING_MAP_VARS_MAX)) |
| 3736 | return -EINVAL; |
| 3737 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3738 | ret = __create_val_field(hist_data, val_idx, file, var_name, expr_str, flags); |
| 3739 | |
| 3740 | if (!ret && hist_data->fields[val_idx]->flags & HIST_FIELD_FL_STRING) |
| 3741 | hist_data->fields[val_idx]->var_str_idx = hist_data->n_var_str++; |
| 3742 | |
| 3743 | return ret; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3744 | } |
| 3745 | |
| 3746 | static int create_val_fields(struct hist_trigger_data *hist_data, |
| 3747 | struct trace_event_file *file) |
| 3748 | { |
| 3749 | char *fields_str, *field_str; |
| 3750 | unsigned int i, j = 1; |
| 3751 | int ret; |
| 3752 | |
| 3753 | ret = create_hitcount_val(hist_data); |
| 3754 | if (ret) |
| 3755 | goto out; |
| 3756 | |
| 3757 | fields_str = hist_data->attrs->vals_str; |
| 3758 | if (!fields_str) |
| 3759 | goto out; |
| 3760 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3761 | for (i = 0, j = 1; i < TRACING_MAP_VALS_MAX && |
| 3762 | j < TRACING_MAP_VALS_MAX; i++) { |
| 3763 | field_str = strsep(&fields_str, ","); |
| 3764 | if (!field_str) |
| 3765 | break; |
| 3766 | |
| 3767 | if (strcmp(field_str, "hitcount") == 0) |
| 3768 | continue; |
| 3769 | |
| 3770 | ret = create_val_field(hist_data, j++, file, field_str); |
| 3771 | if (ret) |
| 3772 | goto out; |
| 3773 | } |
| 3774 | |
| 3775 | if (fields_str && (strcmp(fields_str, "hitcount") != 0)) |
| 3776 | ret = -EINVAL; |
| 3777 | out: |
| 3778 | return ret; |
| 3779 | } |
| 3780 | |
| 3781 | static int create_key_field(struct hist_trigger_data *hist_data, |
| 3782 | unsigned int key_idx, |
| 3783 | unsigned int key_offset, |
| 3784 | struct trace_event_file *file, |
| 3785 | char *field_str) |
| 3786 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3787 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3788 | struct hist_field *hist_field = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3789 | unsigned long flags = 0; |
| 3790 | unsigned int key_size; |
| 3791 | int ret = 0; |
| 3792 | |
| 3793 | if (WARN_ON(key_idx >= HIST_FIELDS_MAX)) |
| 3794 | return -EINVAL; |
| 3795 | |
| 3796 | flags |= HIST_FIELD_FL_KEY; |
| 3797 | |
| 3798 | if (strcmp(field_str, "stacktrace") == 0) { |
| 3799 | flags |= HIST_FIELD_FL_STACKTRACE; |
| 3800 | key_size = sizeof(unsigned long) * HIST_STACKTRACE_DEPTH; |
| 3801 | hist_field = create_hist_field(hist_data, NULL, flags, NULL); |
| 3802 | } else { |
| 3803 | hist_field = parse_expr(hist_data, file, field_str, flags, |
| 3804 | NULL, 0); |
| 3805 | if (IS_ERR(hist_field)) { |
| 3806 | ret = PTR_ERR(hist_field); |
| 3807 | goto out; |
| 3808 | } |
| 3809 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3810 | if (field_has_hist_vars(hist_field, 0)) { |
| 3811 | hist_err(tr, HIST_ERR_INVALID_REF_KEY, errpos(field_str)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3812 | destroy_hist_field(hist_field, 0); |
| 3813 | ret = -EINVAL; |
| 3814 | goto out; |
| 3815 | } |
| 3816 | |
| 3817 | key_size = hist_field->size; |
| 3818 | } |
| 3819 | |
| 3820 | hist_data->fields[key_idx] = hist_field; |
| 3821 | |
| 3822 | key_size = ALIGN(key_size, sizeof(u64)); |
| 3823 | hist_data->fields[key_idx]->size = key_size; |
| 3824 | hist_data->fields[key_idx]->offset = key_offset; |
| 3825 | |
| 3826 | hist_data->key_size += key_size; |
| 3827 | |
| 3828 | if (hist_data->key_size > HIST_KEY_SIZE_MAX) { |
| 3829 | ret = -EINVAL; |
| 3830 | goto out; |
| 3831 | } |
| 3832 | |
| 3833 | hist_data->n_keys++; |
| 3834 | hist_data->n_fields++; |
| 3835 | |
| 3836 | if (WARN_ON(hist_data->n_keys > TRACING_MAP_KEYS_MAX)) |
| 3837 | return -EINVAL; |
| 3838 | |
| 3839 | ret = key_size; |
| 3840 | out: |
| 3841 | return ret; |
| 3842 | } |
| 3843 | |
| 3844 | static int create_key_fields(struct hist_trigger_data *hist_data, |
| 3845 | struct trace_event_file *file) |
| 3846 | { |
| 3847 | unsigned int i, key_offset = 0, n_vals = hist_data->n_vals; |
| 3848 | char *fields_str, *field_str; |
| 3849 | int ret = -EINVAL; |
| 3850 | |
| 3851 | fields_str = hist_data->attrs->keys_str; |
| 3852 | if (!fields_str) |
| 3853 | goto out; |
| 3854 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3855 | for (i = n_vals; i < n_vals + TRACING_MAP_KEYS_MAX; i++) { |
| 3856 | field_str = strsep(&fields_str, ","); |
| 3857 | if (!field_str) |
| 3858 | break; |
| 3859 | ret = create_key_field(hist_data, i, key_offset, |
| 3860 | file, field_str); |
| 3861 | if (ret < 0) |
| 3862 | goto out; |
| 3863 | key_offset += ret; |
| 3864 | } |
| 3865 | if (fields_str) { |
| 3866 | ret = -EINVAL; |
| 3867 | goto out; |
| 3868 | } |
| 3869 | ret = 0; |
| 3870 | out: |
| 3871 | return ret; |
| 3872 | } |
| 3873 | |
| 3874 | static int create_var_fields(struct hist_trigger_data *hist_data, |
| 3875 | struct trace_event_file *file) |
| 3876 | { |
| 3877 | unsigned int i, j = hist_data->n_vals; |
| 3878 | int ret = 0; |
| 3879 | |
| 3880 | unsigned int n_vars = hist_data->attrs->var_defs.n_vars; |
| 3881 | |
| 3882 | for (i = 0; i < n_vars; i++) { |
| 3883 | char *var_name = hist_data->attrs->var_defs.name[i]; |
| 3884 | char *expr = hist_data->attrs->var_defs.expr[i]; |
| 3885 | |
| 3886 | ret = create_var_field(hist_data, j++, file, var_name, expr); |
| 3887 | if (ret) |
| 3888 | goto out; |
| 3889 | } |
| 3890 | out: |
| 3891 | return ret; |
| 3892 | } |
| 3893 | |
| 3894 | static void free_var_defs(struct hist_trigger_data *hist_data) |
| 3895 | { |
| 3896 | unsigned int i; |
| 3897 | |
| 3898 | for (i = 0; i < hist_data->attrs->var_defs.n_vars; i++) { |
| 3899 | kfree(hist_data->attrs->var_defs.name[i]); |
| 3900 | kfree(hist_data->attrs->var_defs.expr[i]); |
| 3901 | } |
| 3902 | |
| 3903 | hist_data->attrs->var_defs.n_vars = 0; |
| 3904 | } |
| 3905 | |
| 3906 | static int parse_var_defs(struct hist_trigger_data *hist_data) |
| 3907 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3908 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3909 | char *s, *str, *var_name, *field_str; |
| 3910 | unsigned int i, j, n_vars = 0; |
| 3911 | int ret = 0; |
| 3912 | |
| 3913 | for (i = 0; i < hist_data->attrs->n_assignments; i++) { |
| 3914 | str = hist_data->attrs->assignment_str[i]; |
| 3915 | for (j = 0; j < TRACING_MAP_VARS_MAX; j++) { |
| 3916 | field_str = strsep(&str, ","); |
| 3917 | if (!field_str) |
| 3918 | break; |
| 3919 | |
| 3920 | var_name = strsep(&field_str, "="); |
| 3921 | if (!var_name || !field_str) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3922 | hist_err(tr, HIST_ERR_MALFORMED_ASSIGNMENT, |
| 3923 | errpos(var_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3924 | ret = -EINVAL; |
| 3925 | goto free; |
| 3926 | } |
| 3927 | |
| 3928 | if (n_vars == TRACING_MAP_VARS_MAX) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 3929 | hist_err(tr, HIST_ERR_TOO_MANY_VARS, errpos(var_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3930 | ret = -EINVAL; |
| 3931 | goto free; |
| 3932 | } |
| 3933 | |
| 3934 | s = kstrdup(var_name, GFP_KERNEL); |
| 3935 | if (!s) { |
| 3936 | ret = -ENOMEM; |
| 3937 | goto free; |
| 3938 | } |
| 3939 | hist_data->attrs->var_defs.name[n_vars] = s; |
| 3940 | |
| 3941 | s = kstrdup(field_str, GFP_KERNEL); |
| 3942 | if (!s) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3943 | ret = -ENOMEM; |
| 3944 | goto free; |
| 3945 | } |
| 3946 | hist_data->attrs->var_defs.expr[n_vars++] = s; |
| 3947 | |
| 3948 | hist_data->attrs->var_defs.n_vars = n_vars; |
| 3949 | } |
| 3950 | } |
| 3951 | |
| 3952 | return ret; |
| 3953 | free: |
| 3954 | free_var_defs(hist_data); |
| 3955 | |
| 3956 | return ret; |
| 3957 | } |
| 3958 | |
| 3959 | static int create_hist_fields(struct hist_trigger_data *hist_data, |
| 3960 | struct trace_event_file *file) |
| 3961 | { |
| 3962 | int ret; |
| 3963 | |
| 3964 | ret = parse_var_defs(hist_data); |
| 3965 | if (ret) |
| 3966 | goto out; |
| 3967 | |
| 3968 | ret = create_val_fields(hist_data, file); |
| 3969 | if (ret) |
| 3970 | goto out; |
| 3971 | |
| 3972 | ret = create_var_fields(hist_data, file); |
| 3973 | if (ret) |
| 3974 | goto out; |
| 3975 | |
| 3976 | ret = create_key_fields(hist_data, file); |
| 3977 | if (ret) |
| 3978 | goto out; |
| 3979 | out: |
| 3980 | free_var_defs(hist_data); |
| 3981 | |
| 3982 | return ret; |
| 3983 | } |
| 3984 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3985 | static int is_descending(struct trace_array *tr, const char *str) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3986 | { |
| 3987 | if (!str) |
| 3988 | return 0; |
| 3989 | |
| 3990 | if (strcmp(str, "descending") == 0) |
| 3991 | return 1; |
| 3992 | |
| 3993 | if (strcmp(str, "ascending") == 0) |
| 3994 | return 0; |
| 3995 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 3996 | hist_err(tr, HIST_ERR_INVALID_SORT_MODIFIER, errpos((char *)str)); |
| 3997 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 3998 | return -EINVAL; |
| 3999 | } |
| 4000 | |
| 4001 | static int create_sort_keys(struct hist_trigger_data *hist_data) |
| 4002 | { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4003 | struct trace_array *tr = hist_data->event_file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4004 | char *fields_str = hist_data->attrs->sort_key_str; |
| 4005 | struct tracing_map_sort_key *sort_key; |
| 4006 | int descending, ret = 0; |
| 4007 | unsigned int i, j, k; |
| 4008 | |
| 4009 | hist_data->n_sort_keys = 1; /* we always have at least one, hitcount */ |
| 4010 | |
| 4011 | if (!fields_str) |
| 4012 | goto out; |
| 4013 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4014 | for (i = 0; i < TRACING_MAP_SORT_KEYS_MAX; i++) { |
| 4015 | struct hist_field *hist_field; |
| 4016 | char *field_str, *field_name; |
| 4017 | const char *test_name; |
| 4018 | |
| 4019 | sort_key = &hist_data->sort_keys[i]; |
| 4020 | |
| 4021 | field_str = strsep(&fields_str, ","); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4022 | if (!field_str) |
| 4023 | break; |
| 4024 | |
| 4025 | if (!*field_str) { |
| 4026 | ret = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4027 | hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort=")); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4028 | break; |
| 4029 | } |
| 4030 | |
| 4031 | if ((i == TRACING_MAP_SORT_KEYS_MAX - 1) && fields_str) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4032 | hist_err(tr, HIST_ERR_TOO_MANY_SORT_FIELDS, errpos("sort=")); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4033 | ret = -EINVAL; |
| 4034 | break; |
| 4035 | } |
| 4036 | |
| 4037 | field_name = strsep(&field_str, "."); |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4038 | if (!field_name || !*field_name) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4039 | ret = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4040 | hist_err(tr, HIST_ERR_EMPTY_SORT_FIELD, errpos("sort=")); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4041 | break; |
| 4042 | } |
| 4043 | |
| 4044 | if (strcmp(field_name, "hitcount") == 0) { |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4045 | descending = is_descending(tr, field_str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4046 | if (descending < 0) { |
| 4047 | ret = descending; |
| 4048 | break; |
| 4049 | } |
| 4050 | sort_key->descending = descending; |
| 4051 | continue; |
| 4052 | } |
| 4053 | |
| 4054 | for (j = 1, k = 1; j < hist_data->n_fields; j++) { |
| 4055 | unsigned int idx; |
| 4056 | |
| 4057 | hist_field = hist_data->fields[j]; |
| 4058 | if (hist_field->flags & HIST_FIELD_FL_VAR) |
| 4059 | continue; |
| 4060 | |
| 4061 | idx = k++; |
| 4062 | |
| 4063 | test_name = hist_field_name(hist_field, 0); |
| 4064 | |
| 4065 | if (strcmp(field_name, test_name) == 0) { |
| 4066 | sort_key->field_idx = idx; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4067 | descending = is_descending(tr, field_str); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4068 | if (descending < 0) { |
| 4069 | ret = descending; |
| 4070 | goto out; |
| 4071 | } |
| 4072 | sort_key->descending = descending; |
| 4073 | break; |
| 4074 | } |
| 4075 | } |
| 4076 | if (j == hist_data->n_fields) { |
| 4077 | ret = -EINVAL; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4078 | hist_err(tr, HIST_ERR_INVALID_SORT_FIELD, errpos(field_name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4079 | break; |
| 4080 | } |
| 4081 | } |
| 4082 | |
| 4083 | hist_data->n_sort_keys = i; |
| 4084 | out: |
| 4085 | return ret; |
| 4086 | } |
| 4087 | |
| 4088 | static void destroy_actions(struct hist_trigger_data *hist_data) |
| 4089 | { |
| 4090 | unsigned int i; |
| 4091 | |
| 4092 | for (i = 0; i < hist_data->n_actions; i++) { |
| 4093 | struct action_data *data = hist_data->actions[i]; |
| 4094 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4095 | if (data->handler == HANDLER_ONMATCH) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4096 | onmatch_destroy(data); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4097 | else if (data->handler == HANDLER_ONMAX || |
| 4098 | data->handler == HANDLER_ONCHANGE) |
| 4099 | track_data_destroy(hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4100 | else |
| 4101 | kfree(data); |
| 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | static int parse_actions(struct hist_trigger_data *hist_data) |
| 4106 | { |
| 4107 | struct trace_array *tr = hist_data->event_file->tr; |
| 4108 | struct action_data *data; |
| 4109 | unsigned int i; |
| 4110 | int ret = 0; |
| 4111 | char *str; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4112 | int len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4113 | |
| 4114 | for (i = 0; i < hist_data->attrs->n_actions; i++) { |
| 4115 | str = hist_data->attrs->action_str[i]; |
| 4116 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4117 | if ((len = str_has_prefix(str, "onmatch("))) { |
| 4118 | char *action_str = str + len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4119 | |
| 4120 | data = onmatch_parse(tr, action_str); |
| 4121 | if (IS_ERR(data)) { |
| 4122 | ret = PTR_ERR(data); |
| 4123 | break; |
| 4124 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4125 | } else if ((len = str_has_prefix(str, "onmax("))) { |
| 4126 | char *action_str = str + len; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4127 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4128 | data = track_data_parse(hist_data, action_str, |
| 4129 | HANDLER_ONMAX); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4130 | if (IS_ERR(data)) { |
| 4131 | ret = PTR_ERR(data); |
| 4132 | break; |
| 4133 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4134 | } else if ((len = str_has_prefix(str, "onchange("))) { |
| 4135 | char *action_str = str + len; |
| 4136 | |
| 4137 | data = track_data_parse(hist_data, action_str, |
| 4138 | HANDLER_ONCHANGE); |
| 4139 | if (IS_ERR(data)) { |
| 4140 | ret = PTR_ERR(data); |
| 4141 | break; |
| 4142 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4143 | } else { |
| 4144 | ret = -EINVAL; |
| 4145 | break; |
| 4146 | } |
| 4147 | |
| 4148 | hist_data->actions[hist_data->n_actions++] = data; |
| 4149 | } |
| 4150 | |
| 4151 | return ret; |
| 4152 | } |
| 4153 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4154 | static int create_actions(struct hist_trigger_data *hist_data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4155 | { |
| 4156 | struct action_data *data; |
| 4157 | unsigned int i; |
| 4158 | int ret = 0; |
| 4159 | |
| 4160 | for (i = 0; i < hist_data->attrs->n_actions; i++) { |
| 4161 | data = hist_data->actions[i]; |
| 4162 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4163 | if (data->handler == HANDLER_ONMATCH) { |
| 4164 | ret = onmatch_create(hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4165 | if (ret) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4166 | break; |
| 4167 | } else if (data->handler == HANDLER_ONMAX || |
| 4168 | data->handler == HANDLER_ONCHANGE) { |
| 4169 | ret = track_data_create(hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4170 | if (ret) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4171 | break; |
| 4172 | } else { |
| 4173 | ret = -EINVAL; |
| 4174 | break; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4175 | } |
| 4176 | } |
| 4177 | |
| 4178 | return ret; |
| 4179 | } |
| 4180 | |
| 4181 | static void print_actions(struct seq_file *m, |
| 4182 | struct hist_trigger_data *hist_data, |
| 4183 | struct tracing_map_elt *elt) |
| 4184 | { |
| 4185 | unsigned int i; |
| 4186 | |
| 4187 | for (i = 0; i < hist_data->n_actions; i++) { |
| 4188 | struct action_data *data = hist_data->actions[i]; |
| 4189 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4190 | if (data->action == ACTION_SNAPSHOT) |
| 4191 | continue; |
| 4192 | |
| 4193 | if (data->handler == HANDLER_ONMAX || |
| 4194 | data->handler == HANDLER_ONCHANGE) |
| 4195 | track_data_print(m, hist_data, elt, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4196 | } |
| 4197 | } |
| 4198 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4199 | static void print_action_spec(struct seq_file *m, |
| 4200 | struct hist_trigger_data *hist_data, |
| 4201 | struct action_data *data) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4202 | { |
| 4203 | unsigned int i; |
| 4204 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4205 | if (data->action == ACTION_SAVE) { |
| 4206 | for (i = 0; i < hist_data->n_save_vars; i++) { |
| 4207 | seq_printf(m, "%s", hist_data->save_vars[i]->var->var.name); |
| 4208 | if (i < hist_data->n_save_vars - 1) |
| 4209 | seq_puts(m, ","); |
| 4210 | } |
| 4211 | } else if (data->action == ACTION_TRACE) { |
| 4212 | if (data->use_trace_keyword) |
| 4213 | seq_printf(m, "%s", data->synth_event_name); |
| 4214 | for (i = 0; i < data->n_params; i++) { |
| 4215 | if (i || data->use_trace_keyword) |
| 4216 | seq_puts(m, ","); |
| 4217 | seq_printf(m, "%s", data->params[i]); |
| 4218 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4219 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4220 | } |
| 4221 | |
| 4222 | static void print_track_data_spec(struct seq_file *m, |
| 4223 | struct hist_trigger_data *hist_data, |
| 4224 | struct action_data *data) |
| 4225 | { |
| 4226 | if (data->handler == HANDLER_ONMAX) |
| 4227 | seq_puts(m, ":onmax("); |
| 4228 | else if (data->handler == HANDLER_ONCHANGE) |
| 4229 | seq_puts(m, ":onchange("); |
| 4230 | seq_printf(m, "%s", data->track_data.var_str); |
| 4231 | seq_printf(m, ").%s(", data->action_name); |
| 4232 | |
| 4233 | print_action_spec(m, hist_data, data); |
| 4234 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4235 | seq_puts(m, ")"); |
| 4236 | } |
| 4237 | |
| 4238 | static void print_onmatch_spec(struct seq_file *m, |
| 4239 | struct hist_trigger_data *hist_data, |
| 4240 | struct action_data *data) |
| 4241 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4242 | seq_printf(m, ":onmatch(%s.%s).", data->match_data.event_system, |
| 4243 | data->match_data.event); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4244 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4245 | seq_printf(m, "%s(", data->action_name); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4246 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4247 | print_action_spec(m, hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4248 | |
| 4249 | seq_puts(m, ")"); |
| 4250 | } |
| 4251 | |
| 4252 | static bool actions_match(struct hist_trigger_data *hist_data, |
| 4253 | struct hist_trigger_data *hist_data_test) |
| 4254 | { |
| 4255 | unsigned int i, j; |
| 4256 | |
| 4257 | if (hist_data->n_actions != hist_data_test->n_actions) |
| 4258 | return false; |
| 4259 | |
| 4260 | for (i = 0; i < hist_data->n_actions; i++) { |
| 4261 | struct action_data *data = hist_data->actions[i]; |
| 4262 | struct action_data *data_test = hist_data_test->actions[i]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4263 | char *action_name, *action_name_test; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4264 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4265 | if (data->handler != data_test->handler) |
| 4266 | return false; |
| 4267 | if (data->action != data_test->action) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4268 | return false; |
| 4269 | |
| 4270 | if (data->n_params != data_test->n_params) |
| 4271 | return false; |
| 4272 | |
| 4273 | for (j = 0; j < data->n_params; j++) { |
| 4274 | if (strcmp(data->params[j], data_test->params[j]) != 0) |
| 4275 | return false; |
| 4276 | } |
| 4277 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4278 | if (data->use_trace_keyword) |
| 4279 | action_name = data->synth_event_name; |
| 4280 | else |
| 4281 | action_name = data->action_name; |
| 4282 | |
| 4283 | if (data_test->use_trace_keyword) |
| 4284 | action_name_test = data_test->synth_event_name; |
| 4285 | else |
| 4286 | action_name_test = data_test->action_name; |
| 4287 | |
| 4288 | if (strcmp(action_name, action_name_test) != 0) |
| 4289 | return false; |
| 4290 | |
| 4291 | if (data->handler == HANDLER_ONMATCH) { |
| 4292 | if (strcmp(data->match_data.event_system, |
| 4293 | data_test->match_data.event_system) != 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4294 | return false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4295 | if (strcmp(data->match_data.event, |
| 4296 | data_test->match_data.event) != 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4297 | return false; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4298 | } else if (data->handler == HANDLER_ONMAX || |
| 4299 | data->handler == HANDLER_ONCHANGE) { |
| 4300 | if (strcmp(data->track_data.var_str, |
| 4301 | data_test->track_data.var_str) != 0) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4302 | return false; |
| 4303 | } |
| 4304 | } |
| 4305 | |
| 4306 | return true; |
| 4307 | } |
| 4308 | |
| 4309 | |
| 4310 | static void print_actions_spec(struct seq_file *m, |
| 4311 | struct hist_trigger_data *hist_data) |
| 4312 | { |
| 4313 | unsigned int i; |
| 4314 | |
| 4315 | for (i = 0; i < hist_data->n_actions; i++) { |
| 4316 | struct action_data *data = hist_data->actions[i]; |
| 4317 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4318 | if (data->handler == HANDLER_ONMATCH) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4319 | print_onmatch_spec(m, hist_data, data); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4320 | else if (data->handler == HANDLER_ONMAX || |
| 4321 | data->handler == HANDLER_ONCHANGE) |
| 4322 | print_track_data_spec(m, hist_data, data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4323 | } |
| 4324 | } |
| 4325 | |
| 4326 | static void destroy_field_var_hists(struct hist_trigger_data *hist_data) |
| 4327 | { |
| 4328 | unsigned int i; |
| 4329 | |
| 4330 | for (i = 0; i < hist_data->n_field_var_hists; i++) { |
| 4331 | kfree(hist_data->field_var_hists[i]->cmd); |
| 4332 | kfree(hist_data->field_var_hists[i]); |
| 4333 | } |
| 4334 | } |
| 4335 | |
| 4336 | static void destroy_hist_data(struct hist_trigger_data *hist_data) |
| 4337 | { |
| 4338 | if (!hist_data) |
| 4339 | return; |
| 4340 | |
| 4341 | destroy_hist_trigger_attrs(hist_data->attrs); |
| 4342 | destroy_hist_fields(hist_data); |
| 4343 | tracing_map_destroy(hist_data->map); |
| 4344 | |
| 4345 | destroy_actions(hist_data); |
| 4346 | destroy_field_vars(hist_data); |
| 4347 | destroy_field_var_hists(hist_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4348 | |
| 4349 | kfree(hist_data); |
| 4350 | } |
| 4351 | |
| 4352 | static int create_tracing_map_fields(struct hist_trigger_data *hist_data) |
| 4353 | { |
| 4354 | struct tracing_map *map = hist_data->map; |
| 4355 | struct ftrace_event_field *field; |
| 4356 | struct hist_field *hist_field; |
| 4357 | int i, idx = 0; |
| 4358 | |
| 4359 | for_each_hist_field(i, hist_data) { |
| 4360 | hist_field = hist_data->fields[i]; |
| 4361 | if (hist_field->flags & HIST_FIELD_FL_KEY) { |
| 4362 | tracing_map_cmp_fn_t cmp_fn; |
| 4363 | |
| 4364 | field = hist_field->field; |
| 4365 | |
| 4366 | if (hist_field->flags & HIST_FIELD_FL_STACKTRACE) |
| 4367 | cmp_fn = tracing_map_cmp_none; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4368 | else if (!field || hist_field->flags & HIST_FIELD_FL_CPU) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4369 | cmp_fn = tracing_map_cmp_num(hist_field->size, |
| 4370 | hist_field->is_signed); |
| 4371 | else if (is_string_field(field)) |
| 4372 | cmp_fn = tracing_map_cmp_string; |
| 4373 | else |
| 4374 | cmp_fn = tracing_map_cmp_num(field->size, |
| 4375 | field->is_signed); |
| 4376 | idx = tracing_map_add_key_field(map, |
| 4377 | hist_field->offset, |
| 4378 | cmp_fn); |
| 4379 | } else if (!(hist_field->flags & HIST_FIELD_FL_VAR)) |
| 4380 | idx = tracing_map_add_sum_field(map); |
| 4381 | |
| 4382 | if (idx < 0) |
| 4383 | return idx; |
| 4384 | |
| 4385 | if (hist_field->flags & HIST_FIELD_FL_VAR) { |
| 4386 | idx = tracing_map_add_var(map); |
| 4387 | if (idx < 0) |
| 4388 | return idx; |
| 4389 | hist_field->var.idx = idx; |
| 4390 | hist_field->var.hist_data = hist_data; |
| 4391 | } |
| 4392 | } |
| 4393 | |
| 4394 | return 0; |
| 4395 | } |
| 4396 | |
| 4397 | static struct hist_trigger_data * |
| 4398 | create_hist_data(unsigned int map_bits, |
| 4399 | struct hist_trigger_attrs *attrs, |
| 4400 | struct trace_event_file *file, |
| 4401 | bool remove) |
| 4402 | { |
| 4403 | const struct tracing_map_ops *map_ops = NULL; |
| 4404 | struct hist_trigger_data *hist_data; |
| 4405 | int ret = 0; |
| 4406 | |
| 4407 | hist_data = kzalloc(sizeof(*hist_data), GFP_KERNEL); |
| 4408 | if (!hist_data) |
| 4409 | return ERR_PTR(-ENOMEM); |
| 4410 | |
| 4411 | hist_data->attrs = attrs; |
| 4412 | hist_data->remove = remove; |
| 4413 | hist_data->event_file = file; |
| 4414 | |
| 4415 | ret = parse_actions(hist_data); |
| 4416 | if (ret) |
| 4417 | goto free; |
| 4418 | |
| 4419 | ret = create_hist_fields(hist_data, file); |
| 4420 | if (ret) |
| 4421 | goto free; |
| 4422 | |
| 4423 | ret = create_sort_keys(hist_data); |
| 4424 | if (ret) |
| 4425 | goto free; |
| 4426 | |
| 4427 | map_ops = &hist_trigger_elt_data_ops; |
| 4428 | |
| 4429 | hist_data->map = tracing_map_create(map_bits, hist_data->key_size, |
| 4430 | map_ops, hist_data); |
| 4431 | if (IS_ERR(hist_data->map)) { |
| 4432 | ret = PTR_ERR(hist_data->map); |
| 4433 | hist_data->map = NULL; |
| 4434 | goto free; |
| 4435 | } |
| 4436 | |
| 4437 | ret = create_tracing_map_fields(hist_data); |
| 4438 | if (ret) |
| 4439 | goto free; |
| 4440 | out: |
| 4441 | return hist_data; |
| 4442 | free: |
| 4443 | hist_data->attrs = NULL; |
| 4444 | |
| 4445 | destroy_hist_data(hist_data); |
| 4446 | |
| 4447 | hist_data = ERR_PTR(ret); |
| 4448 | |
| 4449 | goto out; |
| 4450 | } |
| 4451 | |
| 4452 | static void hist_trigger_elt_update(struct hist_trigger_data *hist_data, |
| 4453 | struct tracing_map_elt *elt, void *rec, |
| 4454 | struct ring_buffer_event *rbe, |
| 4455 | u64 *var_ref_vals) |
| 4456 | { |
| 4457 | struct hist_elt_data *elt_data; |
| 4458 | struct hist_field *hist_field; |
| 4459 | unsigned int i, var_idx; |
| 4460 | u64 hist_val; |
| 4461 | |
| 4462 | elt_data = elt->private_data; |
| 4463 | elt_data->var_ref_vals = var_ref_vals; |
| 4464 | |
| 4465 | for_each_hist_val_field(i, hist_data) { |
| 4466 | hist_field = hist_data->fields[i]; |
| 4467 | hist_val = hist_field->fn(hist_field, elt, rbe, rec); |
| 4468 | if (hist_field->flags & HIST_FIELD_FL_VAR) { |
| 4469 | var_idx = hist_field->var.idx; |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4470 | |
| 4471 | if (hist_field->flags & HIST_FIELD_FL_STRING) { |
| 4472 | unsigned int str_start, var_str_idx, idx; |
| 4473 | char *str, *val_str; |
| 4474 | unsigned int size; |
| 4475 | |
| 4476 | str_start = hist_data->n_field_var_str + |
| 4477 | hist_data->n_save_var_str; |
| 4478 | var_str_idx = hist_field->var_str_idx; |
| 4479 | idx = str_start + var_str_idx; |
| 4480 | |
| 4481 | str = elt_data->field_var_str[idx]; |
| 4482 | val_str = (char *)(uintptr_t)hist_val; |
| 4483 | |
| 4484 | size = min(hist_field->size, STR_VAR_LEN_MAX); |
| 4485 | strscpy(str, val_str, size); |
| 4486 | |
| 4487 | hist_val = (u64)(uintptr_t)str; |
| 4488 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4489 | tracing_map_set_var(elt, var_idx, hist_val); |
| 4490 | continue; |
| 4491 | } |
| 4492 | tracing_map_update_sum(elt, i, hist_val); |
| 4493 | } |
| 4494 | |
| 4495 | for_each_hist_key_field(i, hist_data) { |
| 4496 | hist_field = hist_data->fields[i]; |
| 4497 | if (hist_field->flags & HIST_FIELD_FL_VAR) { |
| 4498 | hist_val = hist_field->fn(hist_field, elt, rbe, rec); |
| 4499 | var_idx = hist_field->var.idx; |
| 4500 | tracing_map_set_var(elt, var_idx, hist_val); |
| 4501 | } |
| 4502 | } |
| 4503 | |
| 4504 | update_field_vars(hist_data, elt, rbe, rec); |
| 4505 | } |
| 4506 | |
| 4507 | static inline void add_to_key(char *compound_key, void *key, |
| 4508 | struct hist_field *key_field, void *rec) |
| 4509 | { |
| 4510 | size_t size = key_field->size; |
| 4511 | |
| 4512 | if (key_field->flags & HIST_FIELD_FL_STRING) { |
| 4513 | struct ftrace_event_field *field; |
| 4514 | |
| 4515 | field = key_field->field; |
| 4516 | if (field->filter_type == FILTER_DYN_STRING) |
| 4517 | size = *(u32 *)(rec + field->offset) >> 16; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4518 | else if (field->filter_type == FILTER_STATIC_STRING) |
| 4519 | size = field->size; |
| 4520 | |
| 4521 | /* ensure NULL-termination */ |
| 4522 | if (size > key_field->size - 1) |
| 4523 | size = key_field->size - 1; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4524 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4525 | strncpy(compound_key + key_field->offset, (char *)key, size); |
| 4526 | } else |
| 4527 | memcpy(compound_key + key_field->offset, key, size); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4528 | } |
| 4529 | |
| 4530 | static void |
| 4531 | hist_trigger_actions(struct hist_trigger_data *hist_data, |
| 4532 | struct tracing_map_elt *elt, void *rec, |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4533 | struct ring_buffer_event *rbe, void *key, |
| 4534 | u64 *var_ref_vals) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4535 | { |
| 4536 | struct action_data *data; |
| 4537 | unsigned int i; |
| 4538 | |
| 4539 | for (i = 0; i < hist_data->n_actions; i++) { |
| 4540 | data = hist_data->actions[i]; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4541 | data->fn(hist_data, elt, rec, rbe, key, data, var_ref_vals); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4542 | } |
| 4543 | } |
| 4544 | |
| 4545 | static void event_hist_trigger(struct event_trigger_data *data, void *rec, |
| 4546 | struct ring_buffer_event *rbe) |
| 4547 | { |
| 4548 | struct hist_trigger_data *hist_data = data->private_data; |
| 4549 | bool use_compound_key = (hist_data->n_keys > 1); |
| 4550 | unsigned long entries[HIST_STACKTRACE_DEPTH]; |
| 4551 | u64 var_ref_vals[TRACING_MAP_VARS_MAX]; |
| 4552 | char compound_key[HIST_KEY_SIZE_MAX]; |
| 4553 | struct tracing_map_elt *elt = NULL; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4554 | struct hist_field *key_field; |
| 4555 | u64 field_contents; |
| 4556 | void *key = NULL; |
| 4557 | unsigned int i; |
| 4558 | |
| 4559 | memset(compound_key, 0, hist_data->key_size); |
| 4560 | |
| 4561 | for_each_hist_key_field(i, hist_data) { |
| 4562 | key_field = hist_data->fields[i]; |
| 4563 | |
| 4564 | if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4565 | memset(entries, 0, HIST_STACKTRACE_SIZE); |
| 4566 | stack_trace_save(entries, HIST_STACKTRACE_DEPTH, |
| 4567 | HIST_STACKTRACE_SKIP); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4568 | key = entries; |
| 4569 | } else { |
| 4570 | field_contents = key_field->fn(key_field, elt, rbe, rec); |
| 4571 | if (key_field->flags & HIST_FIELD_FL_STRING) { |
| 4572 | key = (void *)(unsigned long)field_contents; |
| 4573 | use_compound_key = true; |
| 4574 | } else |
| 4575 | key = (void *)&field_contents; |
| 4576 | } |
| 4577 | |
| 4578 | if (use_compound_key) |
| 4579 | add_to_key(compound_key, key, key_field, rec); |
| 4580 | } |
| 4581 | |
| 4582 | if (use_compound_key) |
| 4583 | key = compound_key; |
| 4584 | |
| 4585 | if (hist_data->n_var_refs && |
| 4586 | !resolve_var_refs(hist_data, key, var_ref_vals, false)) |
| 4587 | return; |
| 4588 | |
| 4589 | elt = tracing_map_insert(hist_data->map, key); |
| 4590 | if (!elt) |
| 4591 | return; |
| 4592 | |
| 4593 | hist_trigger_elt_update(hist_data, elt, rec, rbe, var_ref_vals); |
| 4594 | |
| 4595 | if (resolve_var_refs(hist_data, key, var_ref_vals, true)) |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4596 | hist_trigger_actions(hist_data, elt, rec, rbe, key, var_ref_vals); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4597 | } |
| 4598 | |
| 4599 | static void hist_trigger_stacktrace_print(struct seq_file *m, |
| 4600 | unsigned long *stacktrace_entries, |
| 4601 | unsigned int max_entries) |
| 4602 | { |
| 4603 | char str[KSYM_SYMBOL_LEN]; |
| 4604 | unsigned int spaces = 8; |
| 4605 | unsigned int i; |
| 4606 | |
| 4607 | for (i = 0; i < max_entries; i++) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4608 | if (!stacktrace_entries[i]) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4609 | return; |
| 4610 | |
| 4611 | seq_printf(m, "%*c", 1 + spaces, ' '); |
| 4612 | sprint_symbol(str, stacktrace_entries[i]); |
| 4613 | seq_printf(m, "%s\n", str); |
| 4614 | } |
| 4615 | } |
| 4616 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4617 | static void hist_trigger_print_key(struct seq_file *m, |
| 4618 | struct hist_trigger_data *hist_data, |
| 4619 | void *key, |
| 4620 | struct tracing_map_elt *elt) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4621 | { |
| 4622 | struct hist_field *key_field; |
| 4623 | char str[KSYM_SYMBOL_LEN]; |
| 4624 | bool multiline = false; |
| 4625 | const char *field_name; |
| 4626 | unsigned int i; |
| 4627 | u64 uval; |
| 4628 | |
| 4629 | seq_puts(m, "{ "); |
| 4630 | |
| 4631 | for_each_hist_key_field(i, hist_data) { |
| 4632 | key_field = hist_data->fields[i]; |
| 4633 | |
| 4634 | if (i > hist_data->n_vals) |
| 4635 | seq_puts(m, ", "); |
| 4636 | |
| 4637 | field_name = hist_field_name(key_field, 0); |
| 4638 | |
| 4639 | if (key_field->flags & HIST_FIELD_FL_HEX) { |
| 4640 | uval = *(u64 *)(key + key_field->offset); |
| 4641 | seq_printf(m, "%s: %llx", field_name, uval); |
| 4642 | } else if (key_field->flags & HIST_FIELD_FL_SYM) { |
| 4643 | uval = *(u64 *)(key + key_field->offset); |
| 4644 | sprint_symbol_no_offset(str, uval); |
| 4645 | seq_printf(m, "%s: [%llx] %-45s", field_name, |
| 4646 | uval, str); |
| 4647 | } else if (key_field->flags & HIST_FIELD_FL_SYM_OFFSET) { |
| 4648 | uval = *(u64 *)(key + key_field->offset); |
| 4649 | sprint_symbol(str, uval); |
| 4650 | seq_printf(m, "%s: [%llx] %-55s", field_name, |
| 4651 | uval, str); |
| 4652 | } else if (key_field->flags & HIST_FIELD_FL_EXECNAME) { |
| 4653 | struct hist_elt_data *elt_data = elt->private_data; |
| 4654 | char *comm; |
| 4655 | |
| 4656 | if (WARN_ON_ONCE(!elt_data)) |
| 4657 | return; |
| 4658 | |
| 4659 | comm = elt_data->comm; |
| 4660 | |
| 4661 | uval = *(u64 *)(key + key_field->offset); |
| 4662 | seq_printf(m, "%s: %-16s[%10llu]", field_name, |
| 4663 | comm, uval); |
| 4664 | } else if (key_field->flags & HIST_FIELD_FL_SYSCALL) { |
| 4665 | const char *syscall_name; |
| 4666 | |
| 4667 | uval = *(u64 *)(key + key_field->offset); |
| 4668 | syscall_name = get_syscall_name(uval); |
| 4669 | if (!syscall_name) |
| 4670 | syscall_name = "unknown_syscall"; |
| 4671 | |
| 4672 | seq_printf(m, "%s: %-30s[%3llu]", field_name, |
| 4673 | syscall_name, uval); |
| 4674 | } else if (key_field->flags & HIST_FIELD_FL_STACKTRACE) { |
| 4675 | seq_puts(m, "stacktrace:\n"); |
| 4676 | hist_trigger_stacktrace_print(m, |
| 4677 | key + key_field->offset, |
| 4678 | HIST_STACKTRACE_DEPTH); |
| 4679 | multiline = true; |
| 4680 | } else if (key_field->flags & HIST_FIELD_FL_LOG2) { |
| 4681 | seq_printf(m, "%s: ~ 2^%-2llu", field_name, |
| 4682 | *(u64 *)(key + key_field->offset)); |
| 4683 | } else if (key_field->flags & HIST_FIELD_FL_STRING) { |
| 4684 | seq_printf(m, "%s: %-50s", field_name, |
| 4685 | (char *)(key + key_field->offset)); |
| 4686 | } else { |
| 4687 | uval = *(u64 *)(key + key_field->offset); |
| 4688 | seq_printf(m, "%s: %10llu", field_name, uval); |
| 4689 | } |
| 4690 | } |
| 4691 | |
| 4692 | if (!multiline) |
| 4693 | seq_puts(m, " "); |
| 4694 | |
| 4695 | seq_puts(m, "}"); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4696 | } |
| 4697 | |
| 4698 | static void hist_trigger_entry_print(struct seq_file *m, |
| 4699 | struct hist_trigger_data *hist_data, |
| 4700 | void *key, |
| 4701 | struct tracing_map_elt *elt) |
| 4702 | { |
| 4703 | const char *field_name; |
| 4704 | unsigned int i; |
| 4705 | |
| 4706 | hist_trigger_print_key(m, hist_data, key, elt); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4707 | |
| 4708 | seq_printf(m, " hitcount: %10llu", |
| 4709 | tracing_map_read_sum(elt, HITCOUNT_IDX)); |
| 4710 | |
| 4711 | for (i = 1; i < hist_data->n_vals; i++) { |
| 4712 | field_name = hist_field_name(hist_data->fields[i], 0); |
| 4713 | |
| 4714 | if (hist_data->fields[i]->flags & HIST_FIELD_FL_VAR || |
| 4715 | hist_data->fields[i]->flags & HIST_FIELD_FL_EXPR) |
| 4716 | continue; |
| 4717 | |
| 4718 | if (hist_data->fields[i]->flags & HIST_FIELD_FL_HEX) { |
| 4719 | seq_printf(m, " %s: %10llx", field_name, |
| 4720 | tracing_map_read_sum(elt, i)); |
| 4721 | } else { |
| 4722 | seq_printf(m, " %s: %10llu", field_name, |
| 4723 | tracing_map_read_sum(elt, i)); |
| 4724 | } |
| 4725 | } |
| 4726 | |
| 4727 | print_actions(m, hist_data, elt); |
| 4728 | |
| 4729 | seq_puts(m, "\n"); |
| 4730 | } |
| 4731 | |
| 4732 | static int print_entries(struct seq_file *m, |
| 4733 | struct hist_trigger_data *hist_data) |
| 4734 | { |
| 4735 | struct tracing_map_sort_entry **sort_entries = NULL; |
| 4736 | struct tracing_map *map = hist_data->map; |
| 4737 | int i, n_entries; |
| 4738 | |
| 4739 | n_entries = tracing_map_sort_entries(map, hist_data->sort_keys, |
| 4740 | hist_data->n_sort_keys, |
| 4741 | &sort_entries); |
| 4742 | if (n_entries < 0) |
| 4743 | return n_entries; |
| 4744 | |
| 4745 | for (i = 0; i < n_entries; i++) |
| 4746 | hist_trigger_entry_print(m, hist_data, |
| 4747 | sort_entries[i]->key, |
| 4748 | sort_entries[i]->elt); |
| 4749 | |
| 4750 | tracing_map_destroy_sort_entries(sort_entries, n_entries); |
| 4751 | |
| 4752 | return n_entries; |
| 4753 | } |
| 4754 | |
| 4755 | static void hist_trigger_show(struct seq_file *m, |
| 4756 | struct event_trigger_data *data, int n) |
| 4757 | { |
| 4758 | struct hist_trigger_data *hist_data; |
| 4759 | int n_entries; |
| 4760 | |
| 4761 | if (n > 0) |
| 4762 | seq_puts(m, "\n\n"); |
| 4763 | |
| 4764 | seq_puts(m, "# event histogram\n#\n# trigger info: "); |
| 4765 | data->ops->print(m, data->ops, data); |
| 4766 | seq_puts(m, "#\n\n"); |
| 4767 | |
| 4768 | hist_data = data->private_data; |
| 4769 | n_entries = print_entries(m, hist_data); |
| 4770 | if (n_entries < 0) |
| 4771 | n_entries = 0; |
| 4772 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4773 | track_data_snapshot_print(m, hist_data); |
| 4774 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4775 | seq_printf(m, "\nTotals:\n Hits: %llu\n Entries: %u\n Dropped: %llu\n", |
| 4776 | (u64)atomic64_read(&hist_data->map->hits), |
| 4777 | n_entries, (u64)atomic64_read(&hist_data->map->drops)); |
| 4778 | } |
| 4779 | |
| 4780 | static int hist_show(struct seq_file *m, void *v) |
| 4781 | { |
| 4782 | struct event_trigger_data *data; |
| 4783 | struct trace_event_file *event_file; |
| 4784 | int n = 0, ret = 0; |
| 4785 | |
| 4786 | mutex_lock(&event_mutex); |
| 4787 | |
| 4788 | event_file = event_file_data(m->private); |
| 4789 | if (unlikely(!event_file)) { |
| 4790 | ret = -ENODEV; |
| 4791 | goto out_unlock; |
| 4792 | } |
| 4793 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 4794 | list_for_each_entry(data, &event_file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4795 | if (data->cmd_ops->trigger_type == ETT_EVENT_HIST) |
| 4796 | hist_trigger_show(m, data, n++); |
| 4797 | } |
| 4798 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4799 | out_unlock: |
| 4800 | mutex_unlock(&event_mutex); |
| 4801 | |
| 4802 | return ret; |
| 4803 | } |
| 4804 | |
| 4805 | static int event_hist_open(struct inode *inode, struct file *file) |
| 4806 | { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 4807 | int ret; |
| 4808 | |
| 4809 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
| 4810 | if (ret) |
| 4811 | return ret; |
| 4812 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 4813 | return single_open(file, hist_show, file); |
| 4814 | } |
| 4815 | |
| 4816 | const struct file_operations event_hist_fops = { |
| 4817 | .open = event_hist_open, |
| 4818 | .read = seq_read, |
| 4819 | .llseek = seq_lseek, |
| 4820 | .release = single_release, |
| 4821 | }; |
| 4822 | |
Olivier Deprez | 157378f | 2022-04-04 15:47:50 +0200 | [diff] [blame^] | 4823 | #ifdef CONFIG_HIST_TRIGGERS_DEBUG |
| 4824 | static void hist_field_debug_show_flags(struct seq_file *m, |
| 4825 | unsigned long flags) |
| 4826 | { |
| 4827 | seq_puts(m, " flags:\n"); |
| 4828 | |
| 4829 | if (flags & HIST_FIELD_FL_KEY) |
| 4830 | seq_puts(m, " HIST_FIELD_FL_KEY\n"); |
| 4831 | else if (flags & HIST_FIELD_FL_HITCOUNT) |
| 4832 | seq_puts(m, " VAL: HIST_FIELD_FL_HITCOUNT\n"); |
| 4833 | else if (flags & HIST_FIELD_FL_VAR) |
| 4834 | seq_puts(m, " HIST_FIELD_FL_VAR\n"); |
| 4835 | else if (flags & HIST_FIELD_FL_VAR_REF) |
| 4836 | seq_puts(m, " HIST_FIELD_FL_VAR_REF\n"); |
| 4837 | else |
| 4838 | seq_puts(m, " VAL: normal u64 value\n"); |
| 4839 | |
| 4840 | if (flags & HIST_FIELD_FL_ALIAS) |
| 4841 | seq_puts(m, " HIST_FIELD_FL_ALIAS\n"); |
| 4842 | } |
| 4843 | |
| 4844 | static int hist_field_debug_show(struct seq_file *m, |
| 4845 | struct hist_field *field, unsigned long flags) |
| 4846 | { |
| 4847 | if ((field->flags & flags) != flags) { |
| 4848 | seq_printf(m, "ERROR: bad flags - %lx\n", flags); |
| 4849 | return -EINVAL; |
| 4850 | } |
| 4851 | |
| 4852 | hist_field_debug_show_flags(m, field->flags); |
| 4853 | if (field->field) |
| 4854 | seq_printf(m, " ftrace_event_field name: %s\n", |
| 4855 | field->field->name); |
| 4856 | |
| 4857 | if (field->flags & HIST_FIELD_FL_VAR) { |
| 4858 | seq_printf(m, " var.name: %s\n", field->var.name); |
| 4859 | seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n", |
| 4860 | field->var.idx); |
| 4861 | } |
| 4862 | |
| 4863 | if (field->flags & HIST_FIELD_FL_ALIAS) |
| 4864 | seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n", |
| 4865 | field->var_ref_idx); |
| 4866 | |
| 4867 | if (field->flags & HIST_FIELD_FL_VAR_REF) { |
| 4868 | seq_printf(m, " name: %s\n", field->name); |
| 4869 | seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n", |
| 4870 | field->var.idx); |
| 4871 | seq_printf(m, " var.hist_data: %p\n", field->var.hist_data); |
| 4872 | seq_printf(m, " var_ref_idx (into hist_data->var_refs[]): %u\n", |
| 4873 | field->var_ref_idx); |
| 4874 | if (field->system) |
| 4875 | seq_printf(m, " system: %s\n", field->system); |
| 4876 | if (field->event_name) |
| 4877 | seq_printf(m, " event_name: %s\n", field->event_name); |
| 4878 | } |
| 4879 | |
| 4880 | seq_printf(m, " type: %s\n", field->type); |
| 4881 | seq_printf(m, " size: %u\n", field->size); |
| 4882 | seq_printf(m, " is_signed: %u\n", field->is_signed); |
| 4883 | |
| 4884 | return 0; |
| 4885 | } |
| 4886 | |
| 4887 | static int field_var_debug_show(struct seq_file *m, |
| 4888 | struct field_var *field_var, unsigned int i, |
| 4889 | bool save_vars) |
| 4890 | { |
| 4891 | const char *vars_name = save_vars ? "save_vars" : "field_vars"; |
| 4892 | struct hist_field *field; |
| 4893 | int ret = 0; |
| 4894 | |
| 4895 | seq_printf(m, "\n hist_data->%s[%d]:\n", vars_name, i); |
| 4896 | |
| 4897 | field = field_var->var; |
| 4898 | |
| 4899 | seq_printf(m, "\n %s[%d].var:\n", vars_name, i); |
| 4900 | |
| 4901 | hist_field_debug_show_flags(m, field->flags); |
| 4902 | seq_printf(m, " var.name: %s\n", field->var.name); |
| 4903 | seq_printf(m, " var.idx (into tracing_map_elt.vars[]): %u\n", |
| 4904 | field->var.idx); |
| 4905 | |
| 4906 | field = field_var->val; |
| 4907 | |
| 4908 | seq_printf(m, "\n %s[%d].val:\n", vars_name, i); |
| 4909 | if (field->field) |
| 4910 | seq_printf(m, " ftrace_event_field name: %s\n", |
| 4911 | field->field->name); |
| 4912 | else { |
| 4913 | ret = -EINVAL; |
| 4914 | goto out; |
| 4915 | } |
| 4916 | |
| 4917 | seq_printf(m, " type: %s\n", field->type); |
| 4918 | seq_printf(m, " size: %u\n", field->size); |
| 4919 | seq_printf(m, " is_signed: %u\n", field->is_signed); |
| 4920 | out: |
| 4921 | return ret; |
| 4922 | } |
| 4923 | |
| 4924 | static int hist_action_debug_show(struct seq_file *m, |
| 4925 | struct action_data *data, int i) |
| 4926 | { |
| 4927 | int ret = 0; |
| 4928 | |
| 4929 | if (data->handler == HANDLER_ONMAX || |
| 4930 | data->handler == HANDLER_ONCHANGE) { |
| 4931 | seq_printf(m, "\n hist_data->actions[%d].track_data.var_ref:\n", i); |
| 4932 | ret = hist_field_debug_show(m, data->track_data.var_ref, |
| 4933 | HIST_FIELD_FL_VAR_REF); |
| 4934 | if (ret) |
| 4935 | goto out; |
| 4936 | |
| 4937 | seq_printf(m, "\n hist_data->actions[%d].track_data.track_var:\n", i); |
| 4938 | ret = hist_field_debug_show(m, data->track_data.track_var, |
| 4939 | HIST_FIELD_FL_VAR); |
| 4940 | if (ret) |
| 4941 | goto out; |
| 4942 | } |
| 4943 | |
| 4944 | if (data->handler == HANDLER_ONMATCH) { |
| 4945 | seq_printf(m, "\n hist_data->actions[%d].match_data.event_system: %s\n", |
| 4946 | i, data->match_data.event_system); |
| 4947 | seq_printf(m, " hist_data->actions[%d].match_data.event: %s\n", |
| 4948 | i, data->match_data.event); |
| 4949 | } |
| 4950 | out: |
| 4951 | return ret; |
| 4952 | } |
| 4953 | |
| 4954 | static int hist_actions_debug_show(struct seq_file *m, |
| 4955 | struct hist_trigger_data *hist_data) |
| 4956 | { |
| 4957 | int i, ret = 0; |
| 4958 | |
| 4959 | if (hist_data->n_actions) |
| 4960 | seq_puts(m, "\n action tracking variables (for onmax()/onchange()/onmatch()):\n"); |
| 4961 | |
| 4962 | for (i = 0; i < hist_data->n_actions; i++) { |
| 4963 | struct action_data *action = hist_data->actions[i]; |
| 4964 | |
| 4965 | ret = hist_action_debug_show(m, action, i); |
| 4966 | if (ret) |
| 4967 | goto out; |
| 4968 | } |
| 4969 | |
| 4970 | if (hist_data->n_save_vars) |
| 4971 | seq_puts(m, "\n save action variables (save() params):\n"); |
| 4972 | |
| 4973 | for (i = 0; i < hist_data->n_save_vars; i++) { |
| 4974 | ret = field_var_debug_show(m, hist_data->save_vars[i], i, true); |
| 4975 | if (ret) |
| 4976 | goto out; |
| 4977 | } |
| 4978 | out: |
| 4979 | return ret; |
| 4980 | } |
| 4981 | |
| 4982 | static void hist_trigger_debug_show(struct seq_file *m, |
| 4983 | struct event_trigger_data *data, int n) |
| 4984 | { |
| 4985 | struct hist_trigger_data *hist_data; |
| 4986 | int i, ret; |
| 4987 | |
| 4988 | if (n > 0) |
| 4989 | seq_puts(m, "\n\n"); |
| 4990 | |
| 4991 | seq_puts(m, "# event histogram\n#\n# trigger info: "); |
| 4992 | data->ops->print(m, data->ops, data); |
| 4993 | seq_puts(m, "#\n\n"); |
| 4994 | |
| 4995 | hist_data = data->private_data; |
| 4996 | |
| 4997 | seq_printf(m, "hist_data: %p\n\n", hist_data); |
| 4998 | seq_printf(m, " n_vals: %u\n", hist_data->n_vals); |
| 4999 | seq_printf(m, " n_keys: %u\n", hist_data->n_keys); |
| 5000 | seq_printf(m, " n_fields: %u\n", hist_data->n_fields); |
| 5001 | |
| 5002 | seq_puts(m, "\n val fields:\n\n"); |
| 5003 | |
| 5004 | seq_puts(m, " hist_data->fields[0]:\n"); |
| 5005 | ret = hist_field_debug_show(m, hist_data->fields[0], |
| 5006 | HIST_FIELD_FL_HITCOUNT); |
| 5007 | if (ret) |
| 5008 | return; |
| 5009 | |
| 5010 | for (i = 1; i < hist_data->n_vals; i++) { |
| 5011 | seq_printf(m, "\n hist_data->fields[%d]:\n", i); |
| 5012 | ret = hist_field_debug_show(m, hist_data->fields[i], 0); |
| 5013 | if (ret) |
| 5014 | return; |
| 5015 | } |
| 5016 | |
| 5017 | seq_puts(m, "\n key fields:\n"); |
| 5018 | |
| 5019 | for (i = hist_data->n_vals; i < hist_data->n_fields; i++) { |
| 5020 | seq_printf(m, "\n hist_data->fields[%d]:\n", i); |
| 5021 | ret = hist_field_debug_show(m, hist_data->fields[i], |
| 5022 | HIST_FIELD_FL_KEY); |
| 5023 | if (ret) |
| 5024 | return; |
| 5025 | } |
| 5026 | |
| 5027 | if (hist_data->n_var_refs) |
| 5028 | seq_puts(m, "\n variable reference fields:\n"); |
| 5029 | |
| 5030 | for (i = 0; i < hist_data->n_var_refs; i++) { |
| 5031 | seq_printf(m, "\n hist_data->var_refs[%d]:\n", i); |
| 5032 | ret = hist_field_debug_show(m, hist_data->var_refs[i], |
| 5033 | HIST_FIELD_FL_VAR_REF); |
| 5034 | if (ret) |
| 5035 | return; |
| 5036 | } |
| 5037 | |
| 5038 | if (hist_data->n_field_vars) |
| 5039 | seq_puts(m, "\n field variables:\n"); |
| 5040 | |
| 5041 | for (i = 0; i < hist_data->n_field_vars; i++) { |
| 5042 | ret = field_var_debug_show(m, hist_data->field_vars[i], i, false); |
| 5043 | if (ret) |
| 5044 | return; |
| 5045 | } |
| 5046 | |
| 5047 | ret = hist_actions_debug_show(m, hist_data); |
| 5048 | if (ret) |
| 5049 | return; |
| 5050 | } |
| 5051 | |
| 5052 | static int hist_debug_show(struct seq_file *m, void *v) |
| 5053 | { |
| 5054 | struct event_trigger_data *data; |
| 5055 | struct trace_event_file *event_file; |
| 5056 | int n = 0, ret = 0; |
| 5057 | |
| 5058 | mutex_lock(&event_mutex); |
| 5059 | |
| 5060 | event_file = event_file_data(m->private); |
| 5061 | if (unlikely(!event_file)) { |
| 5062 | ret = -ENODEV; |
| 5063 | goto out_unlock; |
| 5064 | } |
| 5065 | |
| 5066 | list_for_each_entry(data, &event_file->triggers, list) { |
| 5067 | if (data->cmd_ops->trigger_type == ETT_EVENT_HIST) |
| 5068 | hist_trigger_debug_show(m, data, n++); |
| 5069 | } |
| 5070 | |
| 5071 | out_unlock: |
| 5072 | mutex_unlock(&event_mutex); |
| 5073 | |
| 5074 | return ret; |
| 5075 | } |
| 5076 | |
| 5077 | static int event_hist_debug_open(struct inode *inode, struct file *file) |
| 5078 | { |
| 5079 | int ret; |
| 5080 | |
| 5081 | ret = security_locked_down(LOCKDOWN_TRACEFS); |
| 5082 | if (ret) |
| 5083 | return ret; |
| 5084 | |
| 5085 | return single_open(file, hist_debug_show, file); |
| 5086 | } |
| 5087 | |
| 5088 | const struct file_operations event_hist_debug_fops = { |
| 5089 | .open = event_hist_debug_open, |
| 5090 | .read = seq_read, |
| 5091 | .llseek = seq_lseek, |
| 5092 | .release = single_release, |
| 5093 | }; |
| 5094 | #endif |
| 5095 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5096 | static void hist_field_print(struct seq_file *m, struct hist_field *hist_field) |
| 5097 | { |
| 5098 | const char *field_name = hist_field_name(hist_field, 0); |
| 5099 | |
| 5100 | if (hist_field->var.name) |
| 5101 | seq_printf(m, "%s=", hist_field->var.name); |
| 5102 | |
| 5103 | if (hist_field->flags & HIST_FIELD_FL_CPU) |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5104 | seq_puts(m, "common_cpu"); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5105 | else if (field_name) { |
| 5106 | if (hist_field->flags & HIST_FIELD_FL_VAR_REF || |
| 5107 | hist_field->flags & HIST_FIELD_FL_ALIAS) |
| 5108 | seq_putc(m, '$'); |
| 5109 | seq_printf(m, "%s", field_name); |
| 5110 | } else if (hist_field->flags & HIST_FIELD_FL_TIMESTAMP) |
| 5111 | seq_puts(m, "common_timestamp"); |
| 5112 | |
| 5113 | if (hist_field->flags) { |
| 5114 | if (!(hist_field->flags & HIST_FIELD_FL_VAR_REF) && |
| 5115 | !(hist_field->flags & HIST_FIELD_FL_EXPR)) { |
| 5116 | const char *flags = get_hist_field_flags(hist_field); |
| 5117 | |
| 5118 | if (flags) |
| 5119 | seq_printf(m, ".%s", flags); |
| 5120 | } |
| 5121 | } |
| 5122 | } |
| 5123 | |
| 5124 | static int event_hist_trigger_print(struct seq_file *m, |
| 5125 | struct event_trigger_ops *ops, |
| 5126 | struct event_trigger_data *data) |
| 5127 | { |
| 5128 | struct hist_trigger_data *hist_data = data->private_data; |
| 5129 | struct hist_field *field; |
| 5130 | bool have_var = false; |
| 5131 | unsigned int i; |
| 5132 | |
| 5133 | seq_puts(m, "hist:"); |
| 5134 | |
| 5135 | if (data->name) |
| 5136 | seq_printf(m, "%s:", data->name); |
| 5137 | |
| 5138 | seq_puts(m, "keys="); |
| 5139 | |
| 5140 | for_each_hist_key_field(i, hist_data) { |
| 5141 | field = hist_data->fields[i]; |
| 5142 | |
| 5143 | if (i > hist_data->n_vals) |
| 5144 | seq_puts(m, ","); |
| 5145 | |
| 5146 | if (field->flags & HIST_FIELD_FL_STACKTRACE) |
| 5147 | seq_puts(m, "stacktrace"); |
| 5148 | else |
| 5149 | hist_field_print(m, field); |
| 5150 | } |
| 5151 | |
| 5152 | seq_puts(m, ":vals="); |
| 5153 | |
| 5154 | for_each_hist_val_field(i, hist_data) { |
| 5155 | field = hist_data->fields[i]; |
| 5156 | if (field->flags & HIST_FIELD_FL_VAR) { |
| 5157 | have_var = true; |
| 5158 | continue; |
| 5159 | } |
| 5160 | |
| 5161 | if (i == HITCOUNT_IDX) |
| 5162 | seq_puts(m, "hitcount"); |
| 5163 | else { |
| 5164 | seq_puts(m, ","); |
| 5165 | hist_field_print(m, field); |
| 5166 | } |
| 5167 | } |
| 5168 | |
| 5169 | if (have_var) { |
| 5170 | unsigned int n = 0; |
| 5171 | |
| 5172 | seq_puts(m, ":"); |
| 5173 | |
| 5174 | for_each_hist_val_field(i, hist_data) { |
| 5175 | field = hist_data->fields[i]; |
| 5176 | |
| 5177 | if (field->flags & HIST_FIELD_FL_VAR) { |
| 5178 | if (n++) |
| 5179 | seq_puts(m, ","); |
| 5180 | hist_field_print(m, field); |
| 5181 | } |
| 5182 | } |
| 5183 | } |
| 5184 | |
| 5185 | seq_puts(m, ":sort="); |
| 5186 | |
| 5187 | for (i = 0; i < hist_data->n_sort_keys; i++) { |
| 5188 | struct tracing_map_sort_key *sort_key; |
| 5189 | unsigned int idx, first_key_idx; |
| 5190 | |
| 5191 | /* skip VAR vals */ |
| 5192 | first_key_idx = hist_data->n_vals - hist_data->n_vars; |
| 5193 | |
| 5194 | sort_key = &hist_data->sort_keys[i]; |
| 5195 | idx = sort_key->field_idx; |
| 5196 | |
| 5197 | if (WARN_ON(idx >= HIST_FIELDS_MAX)) |
| 5198 | return -EINVAL; |
| 5199 | |
| 5200 | if (i > 0) |
| 5201 | seq_puts(m, ","); |
| 5202 | |
| 5203 | if (idx == HITCOUNT_IDX) |
| 5204 | seq_puts(m, "hitcount"); |
| 5205 | else { |
| 5206 | if (idx >= first_key_idx) |
| 5207 | idx += hist_data->n_vars; |
| 5208 | hist_field_print(m, hist_data->fields[idx]); |
| 5209 | } |
| 5210 | |
| 5211 | if (sort_key->descending) |
| 5212 | seq_puts(m, ".descending"); |
| 5213 | } |
| 5214 | seq_printf(m, ":size=%u", (1 << hist_data->map->map_bits)); |
| 5215 | if (hist_data->enable_timestamps) |
| 5216 | seq_printf(m, ":clock=%s", hist_data->attrs->clock); |
| 5217 | |
| 5218 | print_actions_spec(m, hist_data); |
| 5219 | |
| 5220 | if (data->filter_str) |
| 5221 | seq_printf(m, " if %s", data->filter_str); |
| 5222 | |
| 5223 | if (data->paused) |
| 5224 | seq_puts(m, " [paused]"); |
| 5225 | else |
| 5226 | seq_puts(m, " [active]"); |
| 5227 | |
| 5228 | seq_putc(m, '\n'); |
| 5229 | |
| 5230 | return 0; |
| 5231 | } |
| 5232 | |
| 5233 | static int event_hist_trigger_init(struct event_trigger_ops *ops, |
| 5234 | struct event_trigger_data *data) |
| 5235 | { |
| 5236 | struct hist_trigger_data *hist_data = data->private_data; |
| 5237 | |
| 5238 | if (!data->ref && hist_data->attrs->name) |
| 5239 | save_named_trigger(hist_data->attrs->name, data); |
| 5240 | |
| 5241 | data->ref++; |
| 5242 | |
| 5243 | return 0; |
| 5244 | } |
| 5245 | |
| 5246 | static void unregister_field_var_hists(struct hist_trigger_data *hist_data) |
| 5247 | { |
| 5248 | struct trace_event_file *file; |
| 5249 | unsigned int i; |
| 5250 | char *cmd; |
| 5251 | int ret; |
| 5252 | |
| 5253 | for (i = 0; i < hist_data->n_field_var_hists; i++) { |
| 5254 | file = hist_data->field_var_hists[i]->hist_data->event_file; |
| 5255 | cmd = hist_data->field_var_hists[i]->cmd; |
| 5256 | ret = event_hist_trigger_func(&trigger_hist_cmd, file, |
| 5257 | "!hist", "hist", cmd); |
| 5258 | } |
| 5259 | } |
| 5260 | |
| 5261 | static void event_hist_trigger_free(struct event_trigger_ops *ops, |
| 5262 | struct event_trigger_data *data) |
| 5263 | { |
| 5264 | struct hist_trigger_data *hist_data = data->private_data; |
| 5265 | |
| 5266 | if (WARN_ON_ONCE(data->ref <= 0)) |
| 5267 | return; |
| 5268 | |
| 5269 | data->ref--; |
| 5270 | if (!data->ref) { |
| 5271 | if (data->name) |
| 5272 | del_named_trigger(data); |
| 5273 | |
| 5274 | trigger_data_free(data); |
| 5275 | |
| 5276 | remove_hist_vars(hist_data); |
| 5277 | |
| 5278 | unregister_field_var_hists(hist_data); |
| 5279 | |
| 5280 | destroy_hist_data(hist_data); |
| 5281 | } |
| 5282 | } |
| 5283 | |
| 5284 | static struct event_trigger_ops event_hist_trigger_ops = { |
| 5285 | .func = event_hist_trigger, |
| 5286 | .print = event_hist_trigger_print, |
| 5287 | .init = event_hist_trigger_init, |
| 5288 | .free = event_hist_trigger_free, |
| 5289 | }; |
| 5290 | |
| 5291 | static int event_hist_trigger_named_init(struct event_trigger_ops *ops, |
| 5292 | struct event_trigger_data *data) |
| 5293 | { |
| 5294 | data->ref++; |
| 5295 | |
| 5296 | save_named_trigger(data->named_data->name, data); |
| 5297 | |
| 5298 | event_hist_trigger_init(ops, data->named_data); |
| 5299 | |
| 5300 | return 0; |
| 5301 | } |
| 5302 | |
| 5303 | static void event_hist_trigger_named_free(struct event_trigger_ops *ops, |
| 5304 | struct event_trigger_data *data) |
| 5305 | { |
| 5306 | if (WARN_ON_ONCE(data->ref <= 0)) |
| 5307 | return; |
| 5308 | |
| 5309 | event_hist_trigger_free(ops, data->named_data); |
| 5310 | |
| 5311 | data->ref--; |
| 5312 | if (!data->ref) { |
| 5313 | del_named_trigger(data); |
| 5314 | trigger_data_free(data); |
| 5315 | } |
| 5316 | } |
| 5317 | |
| 5318 | static struct event_trigger_ops event_hist_trigger_named_ops = { |
| 5319 | .func = event_hist_trigger, |
| 5320 | .print = event_hist_trigger_print, |
| 5321 | .init = event_hist_trigger_named_init, |
| 5322 | .free = event_hist_trigger_named_free, |
| 5323 | }; |
| 5324 | |
| 5325 | static struct event_trigger_ops *event_hist_get_trigger_ops(char *cmd, |
| 5326 | char *param) |
| 5327 | { |
| 5328 | return &event_hist_trigger_ops; |
| 5329 | } |
| 5330 | |
| 5331 | static void hist_clear(struct event_trigger_data *data) |
| 5332 | { |
| 5333 | struct hist_trigger_data *hist_data = data->private_data; |
| 5334 | |
| 5335 | if (data->name) |
| 5336 | pause_named_trigger(data); |
| 5337 | |
| 5338 | tracepoint_synchronize_unregister(); |
| 5339 | |
| 5340 | tracing_map_clear(hist_data->map); |
| 5341 | |
| 5342 | if (data->name) |
| 5343 | unpause_named_trigger(data); |
| 5344 | } |
| 5345 | |
| 5346 | static bool compatible_field(struct ftrace_event_field *field, |
| 5347 | struct ftrace_event_field *test_field) |
| 5348 | { |
| 5349 | if (field == test_field) |
| 5350 | return true; |
| 5351 | if (field == NULL || test_field == NULL) |
| 5352 | return false; |
| 5353 | if (strcmp(field->name, test_field->name) != 0) |
| 5354 | return false; |
| 5355 | if (strcmp(field->type, test_field->type) != 0) |
| 5356 | return false; |
| 5357 | if (field->size != test_field->size) |
| 5358 | return false; |
| 5359 | if (field->is_signed != test_field->is_signed) |
| 5360 | return false; |
| 5361 | |
| 5362 | return true; |
| 5363 | } |
| 5364 | |
| 5365 | static bool hist_trigger_match(struct event_trigger_data *data, |
| 5366 | struct event_trigger_data *data_test, |
| 5367 | struct event_trigger_data *named_data, |
| 5368 | bool ignore_filter) |
| 5369 | { |
| 5370 | struct tracing_map_sort_key *sort_key, *sort_key_test; |
| 5371 | struct hist_trigger_data *hist_data, *hist_data_test; |
| 5372 | struct hist_field *key_field, *key_field_test; |
| 5373 | unsigned int i; |
| 5374 | |
| 5375 | if (named_data && (named_data != data_test) && |
| 5376 | (named_data != data_test->named_data)) |
| 5377 | return false; |
| 5378 | |
| 5379 | if (!named_data && is_named_trigger(data_test)) |
| 5380 | return false; |
| 5381 | |
| 5382 | hist_data = data->private_data; |
| 5383 | hist_data_test = data_test->private_data; |
| 5384 | |
| 5385 | if (hist_data->n_vals != hist_data_test->n_vals || |
| 5386 | hist_data->n_fields != hist_data_test->n_fields || |
| 5387 | hist_data->n_sort_keys != hist_data_test->n_sort_keys) |
| 5388 | return false; |
| 5389 | |
| 5390 | if (!ignore_filter) { |
| 5391 | if ((data->filter_str && !data_test->filter_str) || |
| 5392 | (!data->filter_str && data_test->filter_str)) |
| 5393 | return false; |
| 5394 | } |
| 5395 | |
| 5396 | for_each_hist_field(i, hist_data) { |
| 5397 | key_field = hist_data->fields[i]; |
| 5398 | key_field_test = hist_data_test->fields[i]; |
| 5399 | |
| 5400 | if (key_field->flags != key_field_test->flags) |
| 5401 | return false; |
| 5402 | if (!compatible_field(key_field->field, key_field_test->field)) |
| 5403 | return false; |
| 5404 | if (key_field->offset != key_field_test->offset) |
| 5405 | return false; |
| 5406 | if (key_field->size != key_field_test->size) |
| 5407 | return false; |
| 5408 | if (key_field->is_signed != key_field_test->is_signed) |
| 5409 | return false; |
| 5410 | if (!!key_field->var.name != !!key_field_test->var.name) |
| 5411 | return false; |
| 5412 | if (key_field->var.name && |
| 5413 | strcmp(key_field->var.name, key_field_test->var.name) != 0) |
| 5414 | return false; |
| 5415 | } |
| 5416 | |
| 5417 | for (i = 0; i < hist_data->n_sort_keys; i++) { |
| 5418 | sort_key = &hist_data->sort_keys[i]; |
| 5419 | sort_key_test = &hist_data_test->sort_keys[i]; |
| 5420 | |
| 5421 | if (sort_key->field_idx != sort_key_test->field_idx || |
| 5422 | sort_key->descending != sort_key_test->descending) |
| 5423 | return false; |
| 5424 | } |
| 5425 | |
| 5426 | if (!ignore_filter && data->filter_str && |
| 5427 | (strcmp(data->filter_str, data_test->filter_str) != 0)) |
| 5428 | return false; |
| 5429 | |
| 5430 | if (!actions_match(hist_data, hist_data_test)) |
| 5431 | return false; |
| 5432 | |
| 5433 | return true; |
| 5434 | } |
| 5435 | |
| 5436 | static int hist_register_trigger(char *glob, struct event_trigger_ops *ops, |
| 5437 | struct event_trigger_data *data, |
| 5438 | struct trace_event_file *file) |
| 5439 | { |
| 5440 | struct hist_trigger_data *hist_data = data->private_data; |
| 5441 | struct event_trigger_data *test, *named_data = NULL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5442 | struct trace_array *tr = file->tr; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5443 | int ret = 0; |
| 5444 | |
| 5445 | if (hist_data->attrs->name) { |
| 5446 | named_data = find_named_trigger(hist_data->attrs->name); |
| 5447 | if (named_data) { |
| 5448 | if (!hist_trigger_match(data, named_data, named_data, |
| 5449 | true)) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5450 | hist_err(tr, HIST_ERR_NAMED_MISMATCH, errpos(hist_data->attrs->name)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5451 | ret = -EINVAL; |
| 5452 | goto out; |
| 5453 | } |
| 5454 | } |
| 5455 | } |
| 5456 | |
| 5457 | if (hist_data->attrs->name && !named_data) |
| 5458 | goto new; |
| 5459 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5460 | lockdep_assert_held(&event_mutex); |
| 5461 | |
| 5462 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5463 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5464 | if (!hist_trigger_match(data, test, named_data, false)) |
| 5465 | continue; |
| 5466 | if (hist_data->attrs->pause) |
| 5467 | test->paused = true; |
| 5468 | else if (hist_data->attrs->cont) |
| 5469 | test->paused = false; |
| 5470 | else if (hist_data->attrs->clear) |
| 5471 | hist_clear(test); |
| 5472 | else { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5473 | hist_err(tr, HIST_ERR_TRIGGER_EEXIST, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5474 | ret = -EEXIST; |
| 5475 | } |
| 5476 | goto out; |
| 5477 | } |
| 5478 | } |
| 5479 | new: |
| 5480 | if (hist_data->attrs->cont || hist_data->attrs->clear) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5481 | hist_err(tr, HIST_ERR_TRIGGER_ENOENT_CLEAR, 0); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5482 | ret = -ENOENT; |
| 5483 | goto out; |
| 5484 | } |
| 5485 | |
| 5486 | if (hist_data->attrs->pause) |
| 5487 | data->paused = true; |
| 5488 | |
| 5489 | if (named_data) { |
| 5490 | data->private_data = named_data->private_data; |
| 5491 | set_named_trigger_data(data, named_data); |
| 5492 | data->ops = &event_hist_trigger_named_ops; |
| 5493 | } |
| 5494 | |
| 5495 | if (data->ops->init) { |
| 5496 | ret = data->ops->init(data->ops, data); |
| 5497 | if (ret < 0) |
| 5498 | goto out; |
| 5499 | } |
| 5500 | |
| 5501 | if (hist_data->enable_timestamps) { |
| 5502 | char *clock = hist_data->attrs->clock; |
| 5503 | |
| 5504 | ret = tracing_set_clock(file->tr, hist_data->attrs->clock); |
| 5505 | if (ret) { |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5506 | hist_err(tr, HIST_ERR_SET_CLOCK_FAIL, errpos(clock)); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5507 | goto out; |
| 5508 | } |
| 5509 | |
| 5510 | tracing_set_time_stamp_abs(file->tr, true); |
| 5511 | } |
| 5512 | |
| 5513 | if (named_data) |
| 5514 | destroy_hist_data(hist_data); |
| 5515 | |
| 5516 | ret++; |
| 5517 | out: |
| 5518 | return ret; |
| 5519 | } |
| 5520 | |
| 5521 | static int hist_trigger_enable(struct event_trigger_data *data, |
| 5522 | struct trace_event_file *file) |
| 5523 | { |
| 5524 | int ret = 0; |
| 5525 | |
| 5526 | list_add_tail_rcu(&data->list, &file->triggers); |
| 5527 | |
| 5528 | update_cond_flag(file); |
| 5529 | |
| 5530 | if (trace_event_trigger_enable_disable(file, 1) < 0) { |
| 5531 | list_del_rcu(&data->list); |
| 5532 | update_cond_flag(file); |
| 5533 | ret--; |
| 5534 | } |
| 5535 | |
| 5536 | return ret; |
| 5537 | } |
| 5538 | |
| 5539 | static bool have_hist_trigger_match(struct event_trigger_data *data, |
| 5540 | struct trace_event_file *file) |
| 5541 | { |
| 5542 | struct hist_trigger_data *hist_data = data->private_data; |
| 5543 | struct event_trigger_data *test, *named_data = NULL; |
| 5544 | bool match = false; |
| 5545 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5546 | lockdep_assert_held(&event_mutex); |
| 5547 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5548 | if (hist_data->attrs->name) |
| 5549 | named_data = find_named_trigger(hist_data->attrs->name); |
| 5550 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5551 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5552 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5553 | if (hist_trigger_match(data, test, named_data, false)) { |
| 5554 | match = true; |
| 5555 | break; |
| 5556 | } |
| 5557 | } |
| 5558 | } |
| 5559 | |
| 5560 | return match; |
| 5561 | } |
| 5562 | |
| 5563 | static bool hist_trigger_check_refs(struct event_trigger_data *data, |
| 5564 | struct trace_event_file *file) |
| 5565 | { |
| 5566 | struct hist_trigger_data *hist_data = data->private_data; |
| 5567 | struct event_trigger_data *test, *named_data = NULL; |
| 5568 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5569 | lockdep_assert_held(&event_mutex); |
| 5570 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5571 | if (hist_data->attrs->name) |
| 5572 | named_data = find_named_trigger(hist_data->attrs->name); |
| 5573 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5574 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5575 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5576 | if (!hist_trigger_match(data, test, named_data, false)) |
| 5577 | continue; |
| 5578 | hist_data = test->private_data; |
| 5579 | if (check_var_refs(hist_data)) |
| 5580 | return true; |
| 5581 | break; |
| 5582 | } |
| 5583 | } |
| 5584 | |
| 5585 | return false; |
| 5586 | } |
| 5587 | |
| 5588 | static void hist_unregister_trigger(char *glob, struct event_trigger_ops *ops, |
| 5589 | struct event_trigger_data *data, |
| 5590 | struct trace_event_file *file) |
| 5591 | { |
| 5592 | struct hist_trigger_data *hist_data = data->private_data; |
| 5593 | struct event_trigger_data *test, *named_data = NULL; |
| 5594 | bool unregistered = false; |
| 5595 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5596 | lockdep_assert_held(&event_mutex); |
| 5597 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5598 | if (hist_data->attrs->name) |
| 5599 | named_data = find_named_trigger(hist_data->attrs->name); |
| 5600 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5601 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5602 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5603 | if (!hist_trigger_match(data, test, named_data, false)) |
| 5604 | continue; |
| 5605 | unregistered = true; |
| 5606 | list_del_rcu(&test->list); |
| 5607 | trace_event_trigger_enable_disable(file, 0); |
| 5608 | update_cond_flag(file); |
| 5609 | break; |
| 5610 | } |
| 5611 | } |
| 5612 | |
| 5613 | if (unregistered && test->ops->free) |
| 5614 | test->ops->free(test->ops, test); |
| 5615 | |
| 5616 | if (hist_data->enable_timestamps) { |
| 5617 | if (!hist_data->remove || unregistered) |
| 5618 | tracing_set_time_stamp_abs(file->tr, false); |
| 5619 | } |
| 5620 | } |
| 5621 | |
| 5622 | static bool hist_file_check_refs(struct trace_event_file *file) |
| 5623 | { |
| 5624 | struct hist_trigger_data *hist_data; |
| 5625 | struct event_trigger_data *test; |
| 5626 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5627 | lockdep_assert_held(&event_mutex); |
| 5628 | |
| 5629 | list_for_each_entry(test, &file->triggers, list) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5630 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5631 | hist_data = test->private_data; |
| 5632 | if (check_var_refs(hist_data)) |
| 5633 | return true; |
| 5634 | } |
| 5635 | } |
| 5636 | |
| 5637 | return false; |
| 5638 | } |
| 5639 | |
| 5640 | static void hist_unreg_all(struct trace_event_file *file) |
| 5641 | { |
| 5642 | struct event_trigger_data *test, *n; |
| 5643 | struct hist_trigger_data *hist_data; |
| 5644 | struct synth_event *se; |
| 5645 | const char *se_name; |
| 5646 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5647 | lockdep_assert_held(&event_mutex); |
| 5648 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5649 | if (hist_file_check_refs(file)) |
| 5650 | return; |
| 5651 | |
| 5652 | list_for_each_entry_safe(test, n, &file->triggers, list) { |
| 5653 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5654 | hist_data = test->private_data; |
| 5655 | list_del_rcu(&test->list); |
| 5656 | trace_event_trigger_enable_disable(file, 0); |
| 5657 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5658 | se_name = trace_event_name(file->event_call); |
| 5659 | se = find_synth_event(se_name); |
| 5660 | if (se) |
| 5661 | se->ref--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5662 | |
| 5663 | update_cond_flag(file); |
| 5664 | if (hist_data->enable_timestamps) |
| 5665 | tracing_set_time_stamp_abs(file->tr, false); |
| 5666 | if (test->ops->free) |
| 5667 | test->ops->free(test->ops, test); |
| 5668 | } |
| 5669 | } |
| 5670 | } |
| 5671 | |
| 5672 | static int event_hist_trigger_func(struct event_command *cmd_ops, |
| 5673 | struct trace_event_file *file, |
| 5674 | char *glob, char *cmd, char *param) |
| 5675 | { |
| 5676 | unsigned int hist_trigger_bits = TRACING_MAP_BITS_DEFAULT; |
| 5677 | struct event_trigger_data *trigger_data; |
| 5678 | struct hist_trigger_attrs *attrs; |
| 5679 | struct event_trigger_ops *trigger_ops; |
| 5680 | struct hist_trigger_data *hist_data; |
| 5681 | struct synth_event *se; |
| 5682 | const char *se_name; |
| 5683 | bool remove = false; |
| 5684 | char *trigger, *p; |
| 5685 | int ret = 0; |
| 5686 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5687 | lockdep_assert_held(&event_mutex); |
| 5688 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5689 | if (glob && strlen(glob)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5690 | hist_err_clear(); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5691 | last_cmd_set(file, param); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5692 | } |
| 5693 | |
| 5694 | if (!param) |
| 5695 | return -EINVAL; |
| 5696 | |
| 5697 | if (glob[0] == '!') |
| 5698 | remove = true; |
| 5699 | |
| 5700 | /* |
| 5701 | * separate the trigger from the filter (k:v [if filter]) |
| 5702 | * allowing for whitespace in the trigger |
| 5703 | */ |
| 5704 | p = trigger = param; |
| 5705 | do { |
| 5706 | p = strstr(p, "if"); |
| 5707 | if (!p) |
| 5708 | break; |
| 5709 | if (p == param) |
| 5710 | return -EINVAL; |
| 5711 | if (*(p - 1) != ' ' && *(p - 1) != '\t') { |
| 5712 | p++; |
| 5713 | continue; |
| 5714 | } |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5715 | if (p >= param + strlen(param) - (sizeof("if") - 1) - 1) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5716 | return -EINVAL; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5717 | if (*(p + sizeof("if") - 1) != ' ' && *(p + sizeof("if") - 1) != '\t') { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5718 | p++; |
| 5719 | continue; |
| 5720 | } |
| 5721 | break; |
| 5722 | } while (p); |
| 5723 | |
| 5724 | if (!p) |
| 5725 | param = NULL; |
| 5726 | else { |
| 5727 | *(p - 1) = '\0'; |
| 5728 | param = strstrip(p); |
| 5729 | trigger = strstrip(trigger); |
| 5730 | } |
| 5731 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5732 | attrs = parse_hist_trigger_attrs(file->tr, trigger); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5733 | if (IS_ERR(attrs)) |
| 5734 | return PTR_ERR(attrs); |
| 5735 | |
| 5736 | if (attrs->map_bits) |
| 5737 | hist_trigger_bits = attrs->map_bits; |
| 5738 | |
| 5739 | hist_data = create_hist_data(hist_trigger_bits, attrs, file, remove); |
| 5740 | if (IS_ERR(hist_data)) { |
| 5741 | destroy_hist_trigger_attrs(attrs); |
| 5742 | return PTR_ERR(hist_data); |
| 5743 | } |
| 5744 | |
| 5745 | trigger_ops = cmd_ops->get_trigger_ops(cmd, trigger); |
| 5746 | |
| 5747 | trigger_data = kzalloc(sizeof(*trigger_data), GFP_KERNEL); |
| 5748 | if (!trigger_data) { |
| 5749 | ret = -ENOMEM; |
| 5750 | goto out_free; |
| 5751 | } |
| 5752 | |
| 5753 | trigger_data->count = -1; |
| 5754 | trigger_data->ops = trigger_ops; |
| 5755 | trigger_data->cmd_ops = cmd_ops; |
| 5756 | |
| 5757 | INIT_LIST_HEAD(&trigger_data->list); |
| 5758 | RCU_INIT_POINTER(trigger_data->filter, NULL); |
| 5759 | |
| 5760 | trigger_data->private_data = hist_data; |
| 5761 | |
| 5762 | /* if param is non-empty, it's supposed to be a filter */ |
| 5763 | if (param && cmd_ops->set_filter) { |
| 5764 | ret = cmd_ops->set_filter(param, trigger_data, file); |
| 5765 | if (ret < 0) |
| 5766 | goto out_free; |
| 5767 | } |
| 5768 | |
| 5769 | if (remove) { |
| 5770 | if (!have_hist_trigger_match(trigger_data, file)) |
| 5771 | goto out_free; |
| 5772 | |
| 5773 | if (hist_trigger_check_refs(trigger_data, file)) { |
| 5774 | ret = -EBUSY; |
| 5775 | goto out_free; |
| 5776 | } |
| 5777 | |
| 5778 | cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5779 | se_name = trace_event_name(file->event_call); |
| 5780 | se = find_synth_event(se_name); |
| 5781 | if (se) |
| 5782 | se->ref--; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5783 | ret = 0; |
| 5784 | goto out_free; |
| 5785 | } |
| 5786 | |
| 5787 | ret = cmd_ops->reg(glob, trigger_ops, trigger_data, file); |
| 5788 | /* |
| 5789 | * The above returns on success the # of triggers registered, |
| 5790 | * but if it didn't register any it returns zero. Consider no |
| 5791 | * triggers registered a failure too. |
| 5792 | */ |
| 5793 | if (!ret) { |
| 5794 | if (!(attrs->pause || attrs->cont || attrs->clear)) |
| 5795 | ret = -ENOENT; |
| 5796 | goto out_free; |
| 5797 | } else if (ret < 0) |
| 5798 | goto out_free; |
| 5799 | |
| 5800 | if (get_named_trigger_data(trigger_data)) |
| 5801 | goto enable; |
| 5802 | |
| 5803 | if (has_hist_vars(hist_data)) |
| 5804 | save_hist_vars(hist_data); |
| 5805 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 5806 | ret = create_actions(hist_data); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5807 | if (ret) |
| 5808 | goto out_unreg; |
| 5809 | |
| 5810 | ret = tracing_map_init(hist_data->map); |
| 5811 | if (ret) |
| 5812 | goto out_unreg; |
| 5813 | enable: |
| 5814 | ret = hist_trigger_enable(trigger_data, file); |
| 5815 | if (ret) |
| 5816 | goto out_unreg; |
| 5817 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5818 | se_name = trace_event_name(file->event_call); |
| 5819 | se = find_synth_event(se_name); |
| 5820 | if (se) |
| 5821 | se->ref++; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5822 | /* Just return zero, not the number of registered triggers */ |
| 5823 | ret = 0; |
| 5824 | out: |
| 5825 | if (ret == 0) |
| 5826 | hist_err_clear(); |
| 5827 | |
| 5828 | return ret; |
| 5829 | out_unreg: |
| 5830 | cmd_ops->unreg(glob+1, trigger_ops, trigger_data, file); |
| 5831 | out_free: |
| 5832 | if (cmd_ops->set_filter) |
| 5833 | cmd_ops->set_filter(NULL, trigger_data, NULL); |
| 5834 | |
| 5835 | remove_hist_vars(hist_data); |
| 5836 | |
| 5837 | kfree(trigger_data); |
| 5838 | |
| 5839 | destroy_hist_data(hist_data); |
| 5840 | goto out; |
| 5841 | } |
| 5842 | |
| 5843 | static struct event_command trigger_hist_cmd = { |
| 5844 | .name = "hist", |
| 5845 | .trigger_type = ETT_EVENT_HIST, |
| 5846 | .flags = EVENT_CMD_FL_NEEDS_REC, |
| 5847 | .func = event_hist_trigger_func, |
| 5848 | .reg = hist_register_trigger, |
| 5849 | .unreg = hist_unregister_trigger, |
| 5850 | .unreg_all = hist_unreg_all, |
| 5851 | .get_trigger_ops = event_hist_get_trigger_ops, |
| 5852 | .set_filter = set_trigger_filter, |
| 5853 | }; |
| 5854 | |
| 5855 | __init int register_trigger_hist_cmd(void) |
| 5856 | { |
| 5857 | int ret; |
| 5858 | |
| 5859 | ret = register_event_command(&trigger_hist_cmd); |
| 5860 | WARN_ON(ret < 0); |
| 5861 | |
| 5862 | return ret; |
| 5863 | } |
| 5864 | |
| 5865 | static void |
| 5866 | hist_enable_trigger(struct event_trigger_data *data, void *rec, |
| 5867 | struct ring_buffer_event *event) |
| 5868 | { |
| 5869 | struct enable_trigger_data *enable_data = data->private_data; |
| 5870 | struct event_trigger_data *test; |
| 5871 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame] | 5872 | list_for_each_entry_rcu(test, &enable_data->file->triggers, list, |
| 5873 | lockdep_is_held(&event_mutex)) { |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 5874 | if (test->cmd_ops->trigger_type == ETT_EVENT_HIST) { |
| 5875 | if (enable_data->enable) |
| 5876 | test->paused = false; |
| 5877 | else |
| 5878 | test->paused = true; |
| 5879 | } |
| 5880 | } |
| 5881 | } |
| 5882 | |
| 5883 | static void |
| 5884 | hist_enable_count_trigger(struct event_trigger_data *data, void *rec, |
| 5885 | struct ring_buffer_event *event) |
| 5886 | { |
| 5887 | if (!data->count) |
| 5888 | return; |
| 5889 | |
| 5890 | if (data->count != -1) |
| 5891 | (data->count)--; |
| 5892 | |
| 5893 | hist_enable_trigger(data, rec, event); |
| 5894 | } |
| 5895 | |
| 5896 | static struct event_trigger_ops hist_enable_trigger_ops = { |
| 5897 | .func = hist_enable_trigger, |
| 5898 | .print = event_enable_trigger_print, |
| 5899 | .init = event_trigger_init, |
| 5900 | .free = event_enable_trigger_free, |
| 5901 | }; |
| 5902 | |
| 5903 | static struct event_trigger_ops hist_enable_count_trigger_ops = { |
| 5904 | .func = hist_enable_count_trigger, |
| 5905 | .print = event_enable_trigger_print, |
| 5906 | .init = event_trigger_init, |
| 5907 | .free = event_enable_trigger_free, |
| 5908 | }; |
| 5909 | |
| 5910 | static struct event_trigger_ops hist_disable_trigger_ops = { |
| 5911 | .func = hist_enable_trigger, |
| 5912 | .print = event_enable_trigger_print, |
| 5913 | .init = event_trigger_init, |
| 5914 | .free = event_enable_trigger_free, |
| 5915 | }; |
| 5916 | |
| 5917 | static struct event_trigger_ops hist_disable_count_trigger_ops = { |
| 5918 | .func = hist_enable_count_trigger, |
| 5919 | .print = event_enable_trigger_print, |
| 5920 | .init = event_trigger_init, |
| 5921 | .free = event_enable_trigger_free, |
| 5922 | }; |
| 5923 | |
| 5924 | static struct event_trigger_ops * |
| 5925 | hist_enable_get_trigger_ops(char *cmd, char *param) |
| 5926 | { |
| 5927 | struct event_trigger_ops *ops; |
| 5928 | bool enable; |
| 5929 | |
| 5930 | enable = (strcmp(cmd, ENABLE_HIST_STR) == 0); |
| 5931 | |
| 5932 | if (enable) |
| 5933 | ops = param ? &hist_enable_count_trigger_ops : |
| 5934 | &hist_enable_trigger_ops; |
| 5935 | else |
| 5936 | ops = param ? &hist_disable_count_trigger_ops : |
| 5937 | &hist_disable_trigger_ops; |
| 5938 | |
| 5939 | return ops; |
| 5940 | } |
| 5941 | |
| 5942 | static void hist_enable_unreg_all(struct trace_event_file *file) |
| 5943 | { |
| 5944 | struct event_trigger_data *test, *n; |
| 5945 | |
| 5946 | list_for_each_entry_safe(test, n, &file->triggers, list) { |
| 5947 | if (test->cmd_ops->trigger_type == ETT_HIST_ENABLE) { |
| 5948 | list_del_rcu(&test->list); |
| 5949 | update_cond_flag(file); |
| 5950 | trace_event_trigger_enable_disable(file, 0); |
| 5951 | if (test->ops->free) |
| 5952 | test->ops->free(test->ops, test); |
| 5953 | } |
| 5954 | } |
| 5955 | } |
| 5956 | |
| 5957 | static struct event_command trigger_hist_enable_cmd = { |
| 5958 | .name = ENABLE_HIST_STR, |
| 5959 | .trigger_type = ETT_HIST_ENABLE, |
| 5960 | .func = event_enable_trigger_func, |
| 5961 | .reg = event_enable_register_trigger, |
| 5962 | .unreg = event_enable_unregister_trigger, |
| 5963 | .unreg_all = hist_enable_unreg_all, |
| 5964 | .get_trigger_ops = hist_enable_get_trigger_ops, |
| 5965 | .set_filter = set_trigger_filter, |
| 5966 | }; |
| 5967 | |
| 5968 | static struct event_command trigger_hist_disable_cmd = { |
| 5969 | .name = DISABLE_HIST_STR, |
| 5970 | .trigger_type = ETT_HIST_ENABLE, |
| 5971 | .func = event_enable_trigger_func, |
| 5972 | .reg = event_enable_register_trigger, |
| 5973 | .unreg = event_enable_unregister_trigger, |
| 5974 | .unreg_all = hist_enable_unreg_all, |
| 5975 | .get_trigger_ops = hist_enable_get_trigger_ops, |
| 5976 | .set_filter = set_trigger_filter, |
| 5977 | }; |
| 5978 | |
| 5979 | static __init void unregister_trigger_hist_enable_disable_cmds(void) |
| 5980 | { |
| 5981 | unregister_event_command(&trigger_hist_enable_cmd); |
| 5982 | unregister_event_command(&trigger_hist_disable_cmd); |
| 5983 | } |
| 5984 | |
| 5985 | __init int register_trigger_hist_enable_disable_cmds(void) |
| 5986 | { |
| 5987 | int ret; |
| 5988 | |
| 5989 | ret = register_event_command(&trigger_hist_enable_cmd); |
| 5990 | if (WARN_ON(ret < 0)) |
| 5991 | return ret; |
| 5992 | ret = register_event_command(&trigger_hist_disable_cmd); |
| 5993 | if (WARN_ON(ret < 0)) |
| 5994 | unregister_trigger_hist_enable_disable_cmds(); |
| 5995 | |
| 5996 | return ret; |
| 5997 | } |