| 1 | // SPDX-License-Identifier: GPL-2.0 | 
|---|
| 2 | /* | 
|---|
| 3 | * trace_events_filter - generic event filtering | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright (C) 2009 Tom Zanussi <tzanussi@gmail.com> | 
|---|
| 6 | */ | 
|---|
| 7 |  | 
|---|
| 8 | #include <linux/uaccess.h> | 
|---|
| 9 | #include <linux/module.h> | 
|---|
| 10 | #include <linux/ctype.h> | 
|---|
| 11 | #include <linux/mutex.h> | 
|---|
| 12 | #include <linux/perf_event.h> | 
|---|
| 13 | #include <linux/slab.h> | 
|---|
| 14 |  | 
|---|
| 15 | #include "trace.h" | 
|---|
| 16 | #include "trace_output.h" | 
|---|
| 17 |  | 
|---|
| 18 | #define DEFAULT_SYS_FILTER_MESSAGE					\ | 
|---|
| 19 | "### global filter ###\n"					\ | 
|---|
| 20 | "# Use this to set filters for multiple events.\n"		\ | 
|---|
| 21 | "# Only events with the given fields will be affected.\n"	\ | 
|---|
| 22 | "# If no events are modified, an error message will be displayed here" | 
|---|
| 23 |  | 
|---|
| 24 | /* Due to token parsing '<=' must be before '<' and '>=' must be before '>' */ | 
|---|
| 25 | #define OPS					\ | 
|---|
| 26 | C( OP_GLOB,	"~"  ),			\ | 
|---|
| 27 | C( OP_NE,	"!=" ),			\ | 
|---|
| 28 | C( OP_EQ,	"==" ),			\ | 
|---|
| 29 | C( OP_LE,	"<=" ),			\ | 
|---|
| 30 | C( OP_LT,	"<"  ),			\ | 
|---|
| 31 | C( OP_GE,	">=" ),			\ | 
|---|
| 32 | C( OP_GT,	">"  ),			\ | 
|---|
| 33 | C( OP_BAND,	"&"  ),			\ | 
|---|
| 34 | C( OP_MAX,	NULL ) | 
|---|
| 35 |  | 
|---|
| 36 | #undef C | 
|---|
| 37 | #define C(a, b)	a | 
|---|
| 38 |  | 
|---|
| 39 | enum filter_op_ids { OPS }; | 
|---|
| 40 |  | 
|---|
| 41 | #undef C | 
|---|
| 42 | #define C(a, b)	b | 
|---|
| 43 |  | 
|---|
| 44 | static const char * ops[] = { OPS }; | 
|---|
| 45 |  | 
|---|
| 46 | enum filter_pred_fn { | 
|---|
| 47 | FILTER_PRED_FN_NOP, | 
|---|
| 48 | FILTER_PRED_FN_64, | 
|---|
| 49 | FILTER_PRED_FN_64_CPUMASK, | 
|---|
| 50 | FILTER_PRED_FN_S64, | 
|---|
| 51 | FILTER_PRED_FN_U64, | 
|---|
| 52 | FILTER_PRED_FN_32, | 
|---|
| 53 | FILTER_PRED_FN_32_CPUMASK, | 
|---|
| 54 | FILTER_PRED_FN_S32, | 
|---|
| 55 | FILTER_PRED_FN_U32, | 
|---|
| 56 | FILTER_PRED_FN_16, | 
|---|
| 57 | FILTER_PRED_FN_16_CPUMASK, | 
|---|
| 58 | FILTER_PRED_FN_S16, | 
|---|
| 59 | FILTER_PRED_FN_U16, | 
|---|
| 60 | FILTER_PRED_FN_8, | 
|---|
| 61 | FILTER_PRED_FN_8_CPUMASK, | 
|---|
| 62 | FILTER_PRED_FN_S8, | 
|---|
| 63 | FILTER_PRED_FN_U8, | 
|---|
| 64 | FILTER_PRED_FN_COMM, | 
|---|
| 65 | FILTER_PRED_FN_STRING, | 
|---|
| 66 | FILTER_PRED_FN_STRLOC, | 
|---|
| 67 | FILTER_PRED_FN_STRRELLOC, | 
|---|
| 68 | FILTER_PRED_FN_PCHAR_USER, | 
|---|
| 69 | FILTER_PRED_FN_PCHAR, | 
|---|
| 70 | FILTER_PRED_FN_CPU, | 
|---|
| 71 | FILTER_PRED_FN_CPU_CPUMASK, | 
|---|
| 72 | FILTER_PRED_FN_CPUMASK, | 
|---|
| 73 | FILTER_PRED_FN_CPUMASK_CPU, | 
|---|
| 74 | FILTER_PRED_FN_FUNCTION, | 
|---|
| 75 | FILTER_PRED_FN_, | 
|---|
| 76 | FILTER_PRED_TEST_VISITED, | 
|---|
| 77 | }; | 
|---|
| 78 |  | 
|---|
| 79 | struct filter_pred { | 
|---|
| 80 | struct regex		*regex; | 
|---|
| 81 | struct cpumask          *mask; | 
|---|
| 82 | unsigned short		*ops; | 
|---|
| 83 | struct ftrace_event_field *field; | 
|---|
| 84 | u64			val; | 
|---|
| 85 | u64			val2; | 
|---|
| 86 | enum filter_pred_fn	fn_num; | 
|---|
| 87 | int			offset; | 
|---|
| 88 | int			not; | 
|---|
| 89 | int			op; | 
|---|
| 90 | }; | 
|---|
| 91 |  | 
|---|
| 92 | /* | 
|---|
| 93 | * pred functions are OP_LE, OP_LT, OP_GE, OP_GT, and OP_BAND | 
|---|
| 94 | * pred_funcs_##type below must match the order of them above. | 
|---|
| 95 | */ | 
|---|
| 96 | #define PRED_FUNC_START			OP_LE | 
|---|
| 97 | #define PRED_FUNC_MAX			(OP_BAND - PRED_FUNC_START) | 
|---|
| 98 |  | 
|---|
| 99 | #define ERRORS								\ | 
|---|
| 100 | C(NONE,			"No error"),				\ | 
|---|
| 101 | C(INVALID_OP,		"Invalid operator"),			\ | 
|---|
| 102 | C(TOO_MANY_OPEN,	"Too many '('"),			\ | 
|---|
| 103 | C(TOO_MANY_CLOSE,	"Too few '('"),				\ | 
|---|
| 104 | C(MISSING_QUOTE,	"Missing matching quote"),		\ | 
|---|
| 105 | C(MISSING_BRACE_OPEN,   "Missing '{'"),				\ | 
|---|
| 106 | C(MISSING_BRACE_CLOSE,  "Missing '}'"),				\ | 
|---|
| 107 | C(OPERAND_TOO_LONG,	"Operand too long"),			\ | 
|---|
| 108 | C(EXPECT_STRING,	"Expecting string field"),		\ | 
|---|
| 109 | C(EXPECT_DIGIT,		"Expecting numeric field"),		\ | 
|---|
| 110 | C(ILLEGAL_FIELD_OP,	"Illegal operation for field type"),	\ | 
|---|
| 111 | C(FIELD_NOT_FOUND,	"Field not found"),			\ | 
|---|
| 112 | C(ILLEGAL_INTVAL,	"Illegal integer value"),		\ | 
|---|
| 113 | C(BAD_SUBSYS_FILTER,	"Couldn't find or set field in one of a subsystem's events"), \ | 
|---|
| 114 | C(TOO_MANY_PREDS,	"Too many terms in predicate expression"), \ | 
|---|
| 115 | C(INVALID_FILTER,	"Meaningless filter expression"),	\ | 
|---|
| 116 | C(INVALID_CPULIST,	"Invalid cpulist"),	\ | 
|---|
| 117 | C(IP_FIELD_ONLY,	"Only 'ip' field is supported for function trace"), \ | 
|---|
| 118 | C(INVALID_VALUE,	"Invalid value (did you forget quotes)?"), \ | 
|---|
| 119 | C(NO_FUNCTION,		"Function not found"),			\ | 
|---|
| 120 | C(ERRNO,		"Error"),				\ | 
|---|
| 121 | C(NO_FILTER,		"No filter found") | 
|---|
| 122 |  | 
|---|
| 123 | #undef C | 
|---|
| 124 | #define C(a, b)		FILT_ERR_##a | 
|---|
| 125 |  | 
|---|
| 126 | enum { ERRORS }; | 
|---|
| 127 |  | 
|---|
| 128 | #undef C | 
|---|
| 129 | #define C(a, b)		b | 
|---|
| 130 |  | 
|---|
| 131 | static const char *err_text[] = { ERRORS }; | 
|---|
| 132 |  | 
|---|
| 133 | /* Called after a '!' character but "!=" and "!~" are not "not"s */ | 
|---|
| 134 | static bool is_not(const char *str) | 
|---|
| 135 | { | 
|---|
| 136 | switch (str[1]) { | 
|---|
| 137 | case '=': | 
|---|
| 138 | case '~': | 
|---|
| 139 | return false; | 
|---|
| 140 | } | 
|---|
| 141 | return true; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | /** | 
|---|
| 145 | * struct prog_entry - a singe entry in the filter program | 
|---|
| 146 | * @target:	     Index to jump to on a branch (actually one minus the index) | 
|---|
| 147 | * @when_to_branch:  The value of the result of the predicate to do a branch | 
|---|
| 148 | * @pred:	     The predicate to execute. | 
|---|
| 149 | */ | 
|---|
| 150 | struct prog_entry { | 
|---|
| 151 | int			target; | 
|---|
| 152 | int			when_to_branch; | 
|---|
| 153 | struct filter_pred	*pred; | 
|---|
| 154 | }; | 
|---|
| 155 |  | 
|---|
| 156 | /** | 
|---|
| 157 | * update_preds - assign a program entry a label target | 
|---|
| 158 | * @prog: The program array | 
|---|
| 159 | * @N: The index of the current entry in @prog | 
|---|
| 160 | * @invert: What to assign a program entry for its branch condition | 
|---|
| 161 | * | 
|---|
| 162 | * The program entry at @N has a target that points to the index of a program | 
|---|
| 163 | * entry that can have its target and when_to_branch fields updated. | 
|---|
| 164 | * Update the current program entry denoted by index @N target field to be | 
|---|
| 165 | * that of the updated entry. This will denote the entry to update if | 
|---|
| 166 | * we are processing an "||" after an "&&". | 
|---|
| 167 | */ | 
|---|
| 168 | static void update_preds(struct prog_entry *prog, int N, int invert) | 
|---|
| 169 | { | 
|---|
| 170 | int t, s; | 
|---|
| 171 |  | 
|---|
| 172 | t = prog[N].target; | 
|---|
| 173 | s = prog[t].target; | 
|---|
| 174 | prog[t].when_to_branch = invert; | 
|---|
| 175 | prog[t].target = N; | 
|---|
| 176 | prog[N].target = s; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | struct filter_parse_error { | 
|---|
| 180 | int lasterr; | 
|---|
| 181 | int lasterr_pos; | 
|---|
| 182 | }; | 
|---|
| 183 |  | 
|---|
| 184 | static void parse_error(struct filter_parse_error *pe, int err, int pos) | 
|---|
| 185 | { | 
|---|
| 186 | pe->lasterr = err; | 
|---|
| 187 | pe->lasterr_pos = pos; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | typedef int (*parse_pred_fn)(const char *str, void *data, int pos, | 
|---|
| 191 | struct filter_parse_error *pe, | 
|---|
| 192 | struct filter_pred **pred); | 
|---|
| 193 |  | 
|---|
| 194 | enum { | 
|---|
| 195 | INVERT		= 1, | 
|---|
| 196 | PROCESS_AND	= 2, | 
|---|
| 197 | PROCESS_OR	= 4, | 
|---|
| 198 | }; | 
|---|
| 199 |  | 
|---|
| 200 | static void free_predicate(struct filter_pred *pred) | 
|---|
| 201 | { | 
|---|
| 202 | if (pred) { | 
|---|
| 203 | kfree(objp: pred->regex); | 
|---|
| 204 | kfree(objp: pred->mask); | 
|---|
| 205 | kfree(objp: pred); | 
|---|
| 206 | } | 
|---|
| 207 | } | 
|---|
| 208 |  | 
|---|
| 209 | /* | 
|---|
| 210 | * Without going into a formal proof, this explains the method that is used in | 
|---|
| 211 | * parsing the logical expressions. | 
|---|
| 212 | * | 
|---|
| 213 | * For example, if we have: "a && !(!b || (c && g)) || d || e && !f" | 
|---|
| 214 | * The first pass will convert it into the following program: | 
|---|
| 215 | * | 
|---|
| 216 | * n1: r=a;       l1: if (!r) goto l4; | 
|---|
| 217 | * n2: r=b;       l2: if (!r) goto l4; | 
|---|
| 218 | * n3: r=c; r=!r; l3: if (r) goto l4; | 
|---|
| 219 | * n4: r=g; r=!r; l4: if (r) goto l5; | 
|---|
| 220 | * n5: r=d;       l5: if (r) goto T | 
|---|
| 221 | * n6: r=e;       l6: if (!r) goto l7; | 
|---|
| 222 | * n7: r=f; r=!r; l7: if (!r) goto F | 
|---|
| 223 | * T: return TRUE | 
|---|
| 224 | * F: return FALSE | 
|---|
| 225 | * | 
|---|
| 226 | * To do this, we use a data structure to represent each of the above | 
|---|
| 227 | * predicate and conditions that has: | 
|---|
| 228 | * | 
|---|
| 229 | *  predicate, when_to_branch, invert, target | 
|---|
| 230 | * | 
|---|
| 231 | * The "predicate" will hold the function to determine the result "r". | 
|---|
| 232 | * The "when_to_branch" denotes what "r" should be if a branch is to be taken | 
|---|
| 233 | * "&&" would contain "!r" or (0) and "||" would contain "r" or (1). | 
|---|
| 234 | * The "invert" holds whether the value should be reversed before testing. | 
|---|
| 235 | * The "target" contains the label "l#" to jump to. | 
|---|
| 236 | * | 
|---|
| 237 | * A stack is created to hold values when parentheses are used. | 
|---|
| 238 | * | 
|---|
| 239 | * To simplify the logic, the labels will start at 0 and not 1. | 
|---|
| 240 | * | 
|---|
| 241 | * The possible invert values are 1 and 0. The number of "!"s that are in scope | 
|---|
| 242 | * before the predicate determines the invert value, if the number is odd then | 
|---|
| 243 | * the invert value is 1 and 0 otherwise. This means the invert value only | 
|---|
| 244 | * needs to be toggled when a new "!" is introduced compared to what is stored | 
|---|
| 245 | * on the stack, where parentheses were used. | 
|---|
| 246 | * | 
|---|
| 247 | * The top of the stack and "invert" are initialized to zero. | 
|---|
| 248 | * | 
|---|
| 249 | * ** FIRST PASS ** | 
|---|
| 250 | * | 
|---|
| 251 | * #1 A loop through all the tokens is done: | 
|---|
| 252 | * | 
|---|
| 253 | * #2 If the token is an "(", the stack is push, and the current stack value | 
|---|
| 254 | *    gets the current invert value, and the loop continues to the next token. | 
|---|
| 255 | *    The top of the stack saves the "invert" value to keep track of what | 
|---|
| 256 | *    the current inversion is. As "!(a && !b || c)" would require all | 
|---|
| 257 | *    predicates being affected separately by the "!" before the parentheses. | 
|---|
| 258 | *    And that would end up being equivalent to "(!a || b) && !c" | 
|---|
| 259 | * | 
|---|
| 260 | * #3 If the token is an "!", the current "invert" value gets inverted, and | 
|---|
| 261 | *    the loop continues. Note, if the next token is a predicate, then | 
|---|
| 262 | *    this "invert" value is only valid for the current program entry, | 
|---|
| 263 | *    and does not affect other predicates later on. | 
|---|
| 264 | * | 
|---|
| 265 | * The only other acceptable token is the predicate string. | 
|---|
| 266 | * | 
|---|
| 267 | * #4 A new entry into the program is added saving: the predicate and the | 
|---|
| 268 | *    current value of "invert". The target is currently assigned to the | 
|---|
| 269 | *    previous program index (this will not be its final value). | 
|---|
| 270 | * | 
|---|
| 271 | * #5 We now enter another loop and look at the next token. The only valid | 
|---|
| 272 | *    tokens are ")", "&&", "||" or end of the input string "\0". | 
|---|
| 273 | * | 
|---|
| 274 | * #6 The invert variable is reset to the current value saved on the top of | 
|---|
| 275 | *    the stack. | 
|---|
| 276 | * | 
|---|
| 277 | * #7 The top of the stack holds not only the current invert value, but also | 
|---|
| 278 | *    if a "&&" or "||" needs to be processed. Note, the "&&" takes higher | 
|---|
| 279 | *    precedence than "||". That is "a && b || c && d" is equivalent to | 
|---|
| 280 | *    "(a && b) || (c && d)". Thus the first thing to do is to see if "&&" needs | 
|---|
| 281 | *    to be processed. This is the case if an "&&" was the last token. If it was | 
|---|
| 282 | *    then we call update_preds(). This takes the program, the current index in | 
|---|
| 283 | *    the program, and the current value of "invert".  More will be described | 
|---|
| 284 | *    below about this function. | 
|---|
| 285 | * | 
|---|
| 286 | * #8 If the next token is "&&" then we set a flag in the top of the stack | 
|---|
| 287 | *    that denotes that "&&" needs to be processed, break out of this loop | 
|---|
| 288 | *    and continue with the outer loop. | 
|---|
| 289 | * | 
|---|
| 290 | * #9 Otherwise, if a "||" needs to be processed then update_preds() is called. | 
|---|
| 291 | *    This is called with the program, the current index in the program, but | 
|---|
| 292 | *    this time with an inverted value of "invert" (that is !invert). This is | 
|---|
| 293 | *    because the value taken will become the "when_to_branch" value of the | 
|---|
| 294 | *    program. | 
|---|
| 295 | *    Note, this is called when the next token is not an "&&". As stated before, | 
|---|
| 296 | *    "&&" takes higher precedence, and "||" should not be processed yet if the | 
|---|
| 297 | *    next logical operation is "&&". | 
|---|
| 298 | * | 
|---|
| 299 | * #10 If the next token is "||" then we set a flag in the top of the stack | 
|---|
| 300 | *     that denotes that "||" needs to be processed, break out of this loop | 
|---|
| 301 | *     and continue with the outer loop. | 
|---|
| 302 | * | 
|---|
| 303 | * #11 If this is the end of the input string "\0" then we break out of both | 
|---|
| 304 | *     loops. | 
|---|
| 305 | * | 
|---|
| 306 | * #12 Otherwise, the next token is ")", where we pop the stack and continue | 
|---|
| 307 | *     this inner loop. | 
|---|
| 308 | * | 
|---|
| 309 | * Now to discuss the update_pred() function, as that is key to the setting up | 
|---|
| 310 | * of the program. Remember the "target" of the program is initialized to the | 
|---|
| 311 | * previous index and not the "l" label. The target holds the index into the | 
|---|
| 312 | * program that gets affected by the operand. Thus if we have something like | 
|---|
| 313 | *  "a || b && c", when we process "a" the target will be "-1" (undefined). | 
|---|
| 314 | * When we process "b", its target is "0", which is the index of "a", as that's | 
|---|
| 315 | * the predicate that is affected by "||". But because the next token after "b" | 
|---|
| 316 | * is "&&" we don't call update_preds(). Instead continue to "c". As the | 
|---|
| 317 | * next token after "c" is not "&&" but the end of input, we first process the | 
|---|
| 318 | * "&&" by calling update_preds() for the "&&" then we process the "||" by | 
|---|
| 319 | * calling updates_preds() with the values for processing "||". | 
|---|
| 320 | * | 
|---|
| 321 | * What does that mean? What update_preds() does is to first save the "target" | 
|---|
| 322 | * of the program entry indexed by the current program entry's "target" | 
|---|
| 323 | * (remember the "target" is initialized to previous program entry), and then | 
|---|
| 324 | * sets that "target" to the current index which represents the label "l#". | 
|---|
| 325 | * That entry's "when_to_branch" is set to the value passed in (the "invert" | 
|---|
| 326 | * or "!invert"). Then it sets the current program entry's target to the saved | 
|---|
| 327 | * "target" value (the old value of the program that had its "target" updated | 
|---|
| 328 | * to the label). | 
|---|
| 329 | * | 
|---|
| 330 | * Looking back at "a || b && c", we have the following steps: | 
|---|
| 331 | *  "a"  - prog[0] = { "a", X, -1 } // pred, when_to_branch, target | 
|---|
| 332 | *  "||" - flag that we need to process "||"; continue outer loop | 
|---|
| 333 | *  "b"  - prog[1] = { "b", X, 0 } | 
|---|
| 334 | *  "&&" - flag that we need to process "&&"; continue outer loop | 
|---|
| 335 | * (Notice we did not process "||") | 
|---|
| 336 | *  "c"  - prog[2] = { "c", X, 1 } | 
|---|
| 337 | *  update_preds(prog, 2, 0); // invert = 0 as we are processing "&&" | 
|---|
| 338 | *    t = prog[2].target; // t = 1 | 
|---|
| 339 | *    s = prog[t].target; // s = 0 | 
|---|
| 340 | *    prog[t].target = 2; // Set target to "l2" | 
|---|
| 341 | *    prog[t].when_to_branch = 0; | 
|---|
| 342 | *    prog[2].target = s; | 
|---|
| 343 | * update_preds(prog, 2, 1); // invert = 1 as we are now processing "||" | 
|---|
| 344 | *    t = prog[2].target; // t = 0 | 
|---|
| 345 | *    s = prog[t].target; // s = -1 | 
|---|
| 346 | *    prog[t].target = 2; // Set target to "l2" | 
|---|
| 347 | *    prog[t].when_to_branch = 1; | 
|---|
| 348 | *    prog[2].target = s; | 
|---|
| 349 | * | 
|---|
| 350 | * #13 Which brings us to the final step of the first pass, which is to set | 
|---|
| 351 | *     the last program entry's when_to_branch and target, which will be | 
|---|
| 352 | *     when_to_branch = 0; target = N; ( the label after the program entry after | 
|---|
| 353 | *     the last program entry processed above). | 
|---|
| 354 | * | 
|---|
| 355 | * If we denote "TRUE" to be the entry after the last program entry processed, | 
|---|
| 356 | * and "FALSE" the program entry after that, we are now done with the first | 
|---|
| 357 | * pass. | 
|---|
| 358 | * | 
|---|
| 359 | * Making the above "a || b && c" have a program of: | 
|---|
| 360 | *  prog[0] = { "a", 1, 2 } | 
|---|
| 361 | *  prog[1] = { "b", 0, 2 } | 
|---|
| 362 | *  prog[2] = { "c", 0, 3 } | 
|---|
| 363 | * | 
|---|
| 364 | * Which translates into: | 
|---|
| 365 | * n0: r = a; l0: if (r) goto l2; | 
|---|
| 366 | * n1: r = b; l1: if (!r) goto l2; | 
|---|
| 367 | * n2: r = c; l2: if (!r) goto l3;  // Which is the same as "goto F;" | 
|---|
| 368 | * T: return TRUE; l3: | 
|---|
| 369 | * F: return FALSE | 
|---|
| 370 | * | 
|---|
| 371 | * Although, after the first pass, the program is correct, it is | 
|---|
| 372 | * inefficient. The simple sample of "a || b && c" could be easily been | 
|---|
| 373 | * converted into: | 
|---|
| 374 | * n0: r = a; if (r) goto T | 
|---|
| 375 | * n1: r = b; if (!r) goto F | 
|---|
| 376 | * n2: r = c; if (!r) goto F | 
|---|
| 377 | * T: return TRUE; | 
|---|
| 378 | * F: return FALSE; | 
|---|
| 379 | * | 
|---|
| 380 | * The First Pass is over the input string. The next too passes are over | 
|---|
| 381 | * the program itself. | 
|---|
| 382 | * | 
|---|
| 383 | * ** SECOND PASS ** | 
|---|
| 384 | * | 
|---|
| 385 | * Which brings us to the second pass. If a jump to a label has the | 
|---|
| 386 | * same condition as that label, it can instead jump to its target. | 
|---|
| 387 | * The original example of "a && !(!b || (c && g)) || d || e && !f" | 
|---|
| 388 | * where the first pass gives us: | 
|---|
| 389 | * | 
|---|
| 390 | * n1: r=a;       l1: if (!r) goto l4; | 
|---|
| 391 | * n2: r=b;       l2: if (!r) goto l4; | 
|---|
| 392 | * n3: r=c; r=!r; l3: if (r) goto l4; | 
|---|
| 393 | * n4: r=g; r=!r; l4: if (r) goto l5; | 
|---|
| 394 | * n5: r=d;       l5: if (r) goto T | 
|---|
| 395 | * n6: r=e;       l6: if (!r) goto l7; | 
|---|
| 396 | * n7: r=f; r=!r; l7: if (!r) goto F: | 
|---|
| 397 | * T: return TRUE; | 
|---|
| 398 | * F: return FALSE | 
|---|
| 399 | * | 
|---|
| 400 | * We can see that "l3: if (r) goto l4;" and at l4, we have "if (r) goto l5;". | 
|---|
| 401 | * And "l5: if (r) goto T", we could optimize this by converting l3 and l4 | 
|---|
| 402 | * to go directly to T. To accomplish this, we start from the last | 
|---|
| 403 | * entry in the program and work our way back. If the target of the entry | 
|---|
| 404 | * has the same "when_to_branch" then we could use that entry's target. | 
|---|
| 405 | * Doing this, the above would end up as: | 
|---|
| 406 | * | 
|---|
| 407 | * n1: r=a;       l1: if (!r) goto l4; | 
|---|
| 408 | * n2: r=b;       l2: if (!r) goto l4; | 
|---|
| 409 | * n3: r=c; r=!r; l3: if (r) goto T; | 
|---|
| 410 | * n4: r=g; r=!r; l4: if (r) goto T; | 
|---|
| 411 | * n5: r=d;       l5: if (r) goto T; | 
|---|
| 412 | * n6: r=e;       l6: if (!r) goto F; | 
|---|
| 413 | * n7: r=f; r=!r; l7: if (!r) goto F; | 
|---|
| 414 | * T: return TRUE | 
|---|
| 415 | * F: return FALSE | 
|---|
| 416 | * | 
|---|
| 417 | * In that same pass, if the "when_to_branch" doesn't match, we can simply | 
|---|
| 418 | * go to the program entry after the label. That is, "l2: if (!r) goto l4;" | 
|---|
| 419 | * where "l4: if (r) goto T;", then we can convert l2 to be: | 
|---|
| 420 | * "l2: if (!r) goto n5;". | 
|---|
| 421 | * | 
|---|
| 422 | * This will have the second pass give us: | 
|---|
| 423 | * n1: r=a;       l1: if (!r) goto n5; | 
|---|
| 424 | * n2: r=b;       l2: if (!r) goto n5; | 
|---|
| 425 | * n3: r=c; r=!r; l3: if (r) goto T; | 
|---|
| 426 | * n4: r=g; r=!r; l4: if (r) goto T; | 
|---|
| 427 | * n5: r=d;       l5: if (r) goto T | 
|---|
| 428 | * n6: r=e;       l6: if (!r) goto F; | 
|---|
| 429 | * n7: r=f; r=!r; l7: if (!r) goto F | 
|---|
| 430 | * T: return TRUE | 
|---|
| 431 | * F: return FALSE | 
|---|
| 432 | * | 
|---|
| 433 | * Notice, all the "l#" labels are no longer used, and they can now | 
|---|
| 434 | * be discarded. | 
|---|
| 435 | * | 
|---|
| 436 | * ** THIRD PASS ** | 
|---|
| 437 | * | 
|---|
| 438 | * For the third pass we deal with the inverts. As they simply just | 
|---|
| 439 | * make the "when_to_branch" get inverted, a simple loop over the | 
|---|
| 440 | * program to that does: "when_to_branch ^= invert;" will do the | 
|---|
| 441 | * job, leaving us with: | 
|---|
| 442 | * n1: r=a; if (!r) goto n5; | 
|---|
| 443 | * n2: r=b; if (!r) goto n5; | 
|---|
| 444 | * n3: r=c: if (!r) goto T; | 
|---|
| 445 | * n4: r=g; if (!r) goto T; | 
|---|
| 446 | * n5: r=d; if (r) goto T | 
|---|
| 447 | * n6: r=e; if (!r) goto F; | 
|---|
| 448 | * n7: r=f; if (r) goto F | 
|---|
| 449 | * T: return TRUE | 
|---|
| 450 | * F: return FALSE | 
|---|
| 451 | * | 
|---|
| 452 | * As "r = a; if (!r) goto n5;" is obviously the same as | 
|---|
| 453 | * "if (!a) goto n5;" without doing anything we can interpret the | 
|---|
| 454 | * program as: | 
|---|
| 455 | * n1: if (!a) goto n5; | 
|---|
| 456 | * n2: if (!b) goto n5; | 
|---|
| 457 | * n3: if (!c) goto T; | 
|---|
| 458 | * n4: if (!g) goto T; | 
|---|
| 459 | * n5: if (d) goto T | 
|---|
| 460 | * n6: if (!e) goto F; | 
|---|
| 461 | * n7: if (f) goto F | 
|---|
| 462 | * T: return TRUE | 
|---|
| 463 | * F: return FALSE | 
|---|
| 464 | * | 
|---|
| 465 | * Since the inverts are discarded at the end, there's no reason to store | 
|---|
| 466 | * them in the program array (and waste memory). A separate array to hold | 
|---|
| 467 | * the inverts is used and freed at the end. | 
|---|
| 468 | */ | 
|---|
| 469 | static struct prog_entry * | 
|---|
| 470 | predicate_parse(const char *str, int nr_parens, int nr_preds, | 
|---|
| 471 | parse_pred_fn parse_pred, void *data, | 
|---|
| 472 | struct filter_parse_error *pe) | 
|---|
| 473 | { | 
|---|
| 474 | struct prog_entry *prog_stack; | 
|---|
| 475 | struct prog_entry *prog; | 
|---|
| 476 | const char *ptr = str; | 
|---|
| 477 | char *inverts = NULL; | 
|---|
| 478 | int *op_stack; | 
|---|
| 479 | int *top; | 
|---|
| 480 | int invert = 0; | 
|---|
| 481 | int ret = -ENOMEM; | 
|---|
| 482 | int len; | 
|---|
| 483 | int N = 0; | 
|---|
| 484 | int i; | 
|---|
| 485 |  | 
|---|
| 486 | nr_preds += 2; /* For TRUE and FALSE */ | 
|---|
| 487 |  | 
|---|
| 488 | op_stack = kmalloc_array(nr_parens, sizeof(*op_stack), GFP_KERNEL); | 
|---|
| 489 | if (!op_stack) | 
|---|
| 490 | return ERR_PTR(error: -ENOMEM); | 
|---|
| 491 | prog_stack = kcalloc(nr_preds, sizeof(*prog_stack), GFP_KERNEL); | 
|---|
| 492 | if (!prog_stack) { | 
|---|
| 493 | parse_error(pe, err: -ENOMEM, pos: 0); | 
|---|
| 494 | goto out_free; | 
|---|
| 495 | } | 
|---|
| 496 | inverts = kmalloc_array(nr_preds, sizeof(*inverts), GFP_KERNEL); | 
|---|
| 497 | if (!inverts) { | 
|---|
| 498 | parse_error(pe, err: -ENOMEM, pos: 0); | 
|---|
| 499 | goto out_free; | 
|---|
| 500 | } | 
|---|
| 501 |  | 
|---|
| 502 | top = op_stack; | 
|---|
| 503 | prog = prog_stack; | 
|---|
| 504 | *top = 0; | 
|---|
| 505 |  | 
|---|
| 506 | /* First pass */ | 
|---|
| 507 | while (*ptr) {						/* #1 */ | 
|---|
| 508 | const char *next = ptr++; | 
|---|
| 509 |  | 
|---|
| 510 | if (isspace(*next)) | 
|---|
| 511 | continue; | 
|---|
| 512 |  | 
|---|
| 513 | switch (*next) { | 
|---|
| 514 | case '(':					/* #2 */ | 
|---|
| 515 | if (top - op_stack > nr_parens) { | 
|---|
| 516 | ret = -EINVAL; | 
|---|
| 517 | goto out_free; | 
|---|
| 518 | } | 
|---|
| 519 | *(++top) = invert; | 
|---|
| 520 | continue; | 
|---|
| 521 | case '!':					/* #3 */ | 
|---|
| 522 | if (!is_not(str: next)) | 
|---|
| 523 | break; | 
|---|
| 524 | invert = !invert; | 
|---|
| 525 | continue; | 
|---|
| 526 | } | 
|---|
| 527 |  | 
|---|
| 528 | if (N >= nr_preds) { | 
|---|
| 529 | parse_error(pe, err: FILT_ERR_TOO_MANY_PREDS, pos: next - str); | 
|---|
| 530 | goto out_free; | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | inverts[N] = invert;				/* #4 */ | 
|---|
| 534 | prog[N].target = N-1; | 
|---|
| 535 |  | 
|---|
| 536 | len = parse_pred(next, data, ptr - str, pe, &prog[N].pred); | 
|---|
| 537 | if (len < 0) { | 
|---|
| 538 | ret = len; | 
|---|
| 539 | goto out_free; | 
|---|
| 540 | } | 
|---|
| 541 | ptr = next + len; | 
|---|
| 542 |  | 
|---|
| 543 | N++; | 
|---|
| 544 |  | 
|---|
| 545 | ret = -1; | 
|---|
| 546 | while (1) {					/* #5 */ | 
|---|
| 547 | next = ptr++; | 
|---|
| 548 | if (isspace(*next)) | 
|---|
| 549 | continue; | 
|---|
| 550 |  | 
|---|
| 551 | switch (*next) { | 
|---|
| 552 | case ')': | 
|---|
| 553 | case '\0': | 
|---|
| 554 | break; | 
|---|
| 555 | case '&': | 
|---|
| 556 | case '|': | 
|---|
| 557 | /* accepting only "&&" or "||" */ | 
|---|
| 558 | if (next[1] == next[0]) { | 
|---|
| 559 | ptr++; | 
|---|
| 560 | break; | 
|---|
| 561 | } | 
|---|
| 562 | fallthrough; | 
|---|
| 563 | default: | 
|---|
| 564 | parse_error(pe, err: FILT_ERR_TOO_MANY_PREDS, | 
|---|
| 565 | pos: next - str); | 
|---|
| 566 | goto out_free; | 
|---|
| 567 | } | 
|---|
| 568 |  | 
|---|
| 569 | invert = *top & INVERT; | 
|---|
| 570 |  | 
|---|
| 571 | if (*top & PROCESS_AND) {		/* #7 */ | 
|---|
| 572 | update_preds(prog, N: N - 1, invert); | 
|---|
| 573 | *top &= ~PROCESS_AND; | 
|---|
| 574 | } | 
|---|
| 575 | if (*next == '&') {			/* #8 */ | 
|---|
| 576 | *top |= PROCESS_AND; | 
|---|
| 577 | break; | 
|---|
| 578 | } | 
|---|
| 579 | if (*top & PROCESS_OR) {		/* #9 */ | 
|---|
| 580 | update_preds(prog, N: N - 1, invert: !invert); | 
|---|
| 581 | *top &= ~PROCESS_OR; | 
|---|
| 582 | } | 
|---|
| 583 | if (*next == '|') {			/* #10 */ | 
|---|
| 584 | *top |= PROCESS_OR; | 
|---|
| 585 | break; | 
|---|
| 586 | } | 
|---|
| 587 | if (!*next)				/* #11 */ | 
|---|
| 588 | goto out; | 
|---|
| 589 |  | 
|---|
| 590 | if (top == op_stack) { | 
|---|
| 591 | ret = -1; | 
|---|
| 592 | /* Too few '(' */ | 
|---|
| 593 | parse_error(pe, err: FILT_ERR_TOO_MANY_CLOSE, pos: ptr - str); | 
|---|
| 594 | goto out_free; | 
|---|
| 595 | } | 
|---|
| 596 | top--;					/* #12 */ | 
|---|
| 597 | } | 
|---|
| 598 | } | 
|---|
| 599 | out: | 
|---|
| 600 | if (top != op_stack) { | 
|---|
| 601 | /* Too many '(' */ | 
|---|
| 602 | parse_error(pe, err: FILT_ERR_TOO_MANY_OPEN, pos: ptr - str); | 
|---|
| 603 | goto out_free; | 
|---|
| 604 | } | 
|---|
| 605 |  | 
|---|
| 606 | if (!N) { | 
|---|
| 607 | /* No program? */ | 
|---|
| 608 | ret = -EINVAL; | 
|---|
| 609 | parse_error(pe, err: FILT_ERR_NO_FILTER, pos: ptr - str); | 
|---|
| 610 | goto out_free; | 
|---|
| 611 | } | 
|---|
| 612 |  | 
|---|
| 613 | prog[N].pred = NULL;					/* #13 */ | 
|---|
| 614 | prog[N].target = 1;		/* TRUE */ | 
|---|
| 615 | prog[N+1].pred = NULL; | 
|---|
| 616 | prog[N+1].target = 0;		/* FALSE */ | 
|---|
| 617 | prog[N-1].target = N; | 
|---|
| 618 | prog[N-1].when_to_branch = false; | 
|---|
| 619 |  | 
|---|
| 620 | /* Second Pass */ | 
|---|
| 621 | for (i = N-1 ; i--; ) { | 
|---|
| 622 | int target = prog[i].target; | 
|---|
| 623 | if (prog[i].when_to_branch == prog[target].when_to_branch) | 
|---|
| 624 | prog[i].target = prog[target].target; | 
|---|
| 625 | } | 
|---|
| 626 |  | 
|---|
| 627 | /* Third Pass */ | 
|---|
| 628 | for (i = 0; i < N; i++) { | 
|---|
| 629 | invert = inverts[i] ^ prog[i].when_to_branch; | 
|---|
| 630 | prog[i].when_to_branch = invert; | 
|---|
| 631 | /* Make sure the program always moves forward */ | 
|---|
| 632 | if (WARN_ON(prog[i].target <= i)) { | 
|---|
| 633 | ret = -EINVAL; | 
|---|
| 634 | goto out_free; | 
|---|
| 635 | } | 
|---|
| 636 | } | 
|---|
| 637 |  | 
|---|
| 638 | kfree(objp: op_stack); | 
|---|
| 639 | kfree(objp: inverts); | 
|---|
| 640 | return prog; | 
|---|
| 641 | out_free: | 
|---|
| 642 | kfree(objp: op_stack); | 
|---|
| 643 | kfree(objp: inverts); | 
|---|
| 644 | if (prog_stack) { | 
|---|
| 645 | for (i = 0; prog_stack[i].pred; i++) | 
|---|
| 646 | free_predicate(pred: prog_stack[i].pred); | 
|---|
| 647 | kfree(objp: prog_stack); | 
|---|
| 648 | } | 
|---|
| 649 | return ERR_PTR(error: ret); | 
|---|
| 650 | } | 
|---|
| 651 |  | 
|---|
| 652 | static inline int | 
|---|
| 653 | do_filter_cpumask(int op, const struct cpumask *mask, const struct cpumask *cmp) | 
|---|
| 654 | { | 
|---|
| 655 | switch (op) { | 
|---|
| 656 | case OP_EQ: | 
|---|
| 657 | return cpumask_equal(src1p: mask, src2p: cmp); | 
|---|
| 658 | case OP_NE: | 
|---|
| 659 | return !cpumask_equal(src1p: mask, src2p: cmp); | 
|---|
| 660 | case OP_BAND: | 
|---|
| 661 | return cpumask_intersects(src1p: mask, src2p: cmp); | 
|---|
| 662 | default: | 
|---|
| 663 | return 0; | 
|---|
| 664 | } | 
|---|
| 665 | } | 
|---|
| 666 |  | 
|---|
| 667 | /* Optimisation of do_filter_cpumask() for scalar fields */ | 
|---|
| 668 | static inline int | 
|---|
| 669 | do_filter_scalar_cpumask(int op, unsigned int cpu, const struct cpumask *mask) | 
|---|
| 670 | { | 
|---|
| 671 | /* | 
|---|
| 672 | * Per the weight-of-one cpumask optimisations, the mask passed in this | 
|---|
| 673 | * function has a weight >= 2, so it is never equal to a single scalar. | 
|---|
| 674 | */ | 
|---|
| 675 | switch (op) { | 
|---|
| 676 | case OP_EQ: | 
|---|
| 677 | return false; | 
|---|
| 678 | case OP_NE: | 
|---|
| 679 | return true; | 
|---|
| 680 | case OP_BAND: | 
|---|
| 681 | return cpumask_test_cpu(cpu, cpumask: mask); | 
|---|
| 682 | default: | 
|---|
| 683 | return 0; | 
|---|
| 684 | } | 
|---|
| 685 | } | 
|---|
| 686 |  | 
|---|
| 687 | static inline int | 
|---|
| 688 | do_filter_cpumask_scalar(int op, const struct cpumask *mask, unsigned int cpu) | 
|---|
| 689 | { | 
|---|
| 690 | switch (op) { | 
|---|
| 691 | case OP_EQ: | 
|---|
| 692 | return cpumask_test_cpu(cpu, cpumask: mask) && | 
|---|
| 693 | cpumask_nth(cpu: 1, srcp: mask) >= nr_cpu_ids; | 
|---|
| 694 | case OP_NE: | 
|---|
| 695 | return !cpumask_test_cpu(cpu, cpumask: mask) || | 
|---|
| 696 | cpumask_nth(cpu: 1, srcp: mask) < nr_cpu_ids; | 
|---|
| 697 | case OP_BAND: | 
|---|
| 698 | return cpumask_test_cpu(cpu, cpumask: mask); | 
|---|
| 699 | default: | 
|---|
| 700 | return 0; | 
|---|
| 701 | } | 
|---|
| 702 | } | 
|---|
| 703 |  | 
|---|
| 704 | enum pred_cmp_types { | 
|---|
| 705 | PRED_CMP_TYPE_NOP, | 
|---|
| 706 | PRED_CMP_TYPE_LT, | 
|---|
| 707 | PRED_CMP_TYPE_LE, | 
|---|
| 708 | PRED_CMP_TYPE_GT, | 
|---|
| 709 | PRED_CMP_TYPE_GE, | 
|---|
| 710 | PRED_CMP_TYPE_BAND, | 
|---|
| 711 | }; | 
|---|
| 712 |  | 
|---|
| 713 | #define DEFINE_COMPARISON_PRED(type)					\ | 
|---|
| 714 | static int filter_pred_##type(struct filter_pred *pred, void *event)	\ | 
|---|
| 715 | {									\ | 
|---|
| 716 | switch (pred->op) {						\ | 
|---|
| 717 | case OP_LT: {							\ | 
|---|
| 718 | type *addr = (type *)(event + pred->offset);		\ | 
|---|
| 719 | type val = (type)pred->val;				\ | 
|---|
| 720 | return *addr < val;					\ | 
|---|
| 721 | }								\ | 
|---|
| 722 | case OP_LE: {					\ | 
|---|
| 723 | type *addr = (type *)(event + pred->offset);		\ | 
|---|
| 724 | type val = (type)pred->val;				\ | 
|---|
| 725 | return *addr <= val;					\ | 
|---|
| 726 | }								\ | 
|---|
| 727 | case OP_GT: {					\ | 
|---|
| 728 | type *addr = (type *)(event + pred->offset);		\ | 
|---|
| 729 | type val = (type)pred->val;				\ | 
|---|
| 730 | return *addr > val;					\ | 
|---|
| 731 | }								\ | 
|---|
| 732 | case OP_GE: {					\ | 
|---|
| 733 | type *addr = (type *)(event + pred->offset);		\ | 
|---|
| 734 | type val = (type)pred->val;				\ | 
|---|
| 735 | return *addr >= val;					\ | 
|---|
| 736 | }								\ | 
|---|
| 737 | case OP_BAND: {					\ | 
|---|
| 738 | type *addr = (type *)(event + pred->offset);		\ | 
|---|
| 739 | type val = (type)pred->val;				\ | 
|---|
| 740 | return !!(*addr & val);					\ | 
|---|
| 741 | }								\ | 
|---|
| 742 | default:							\ | 
|---|
| 743 | return 0;						\ | 
|---|
| 744 | }								\ | 
|---|
| 745 | } | 
|---|
| 746 |  | 
|---|
| 747 | #define DEFINE_CPUMASK_COMPARISON_PRED(size)					\ | 
|---|
| 748 | static int filter_pred_##size##_cpumask(struct filter_pred *pred, void *event)	\ | 
|---|
| 749 | {										\ | 
|---|
| 750 | u##size *addr = (u##size *)(event + pred->offset);			\ | 
|---|
| 751 | unsigned int cpu = *addr;						\ | 
|---|
| 752 | \ | 
|---|
| 753 | if (cpu >= nr_cpu_ids)							\ | 
|---|
| 754 | return 0;							\ | 
|---|
| 755 | \ | 
|---|
| 756 | return do_filter_scalar_cpumask(pred->op, cpu, pred->mask);		\ | 
|---|
| 757 | } | 
|---|
| 758 |  | 
|---|
| 759 | #define DEFINE_EQUALITY_PRED(size)					\ | 
|---|
| 760 | static int filter_pred_##size(struct filter_pred *pred, void *event)	\ | 
|---|
| 761 | {									\ | 
|---|
| 762 | u##size *addr = (u##size *)(event + pred->offset);		\ | 
|---|
| 763 | u##size val = (u##size)pred->val;				\ | 
|---|
| 764 | int match;							\ | 
|---|
| 765 | \ | 
|---|
| 766 | match = (val == *addr) ^ pred->not;				\ | 
|---|
| 767 | \ | 
|---|
| 768 | return match;							\ | 
|---|
| 769 | } | 
|---|
| 770 |  | 
|---|
| 771 | DEFINE_COMPARISON_PRED(s64); | 
|---|
| 772 | DEFINE_COMPARISON_PRED(u64); | 
|---|
| 773 | DEFINE_COMPARISON_PRED(s32); | 
|---|
| 774 | DEFINE_COMPARISON_PRED(u32); | 
|---|
| 775 | DEFINE_COMPARISON_PRED(s16); | 
|---|
| 776 | DEFINE_COMPARISON_PRED(u16); | 
|---|
| 777 | DEFINE_COMPARISON_PRED(s8); | 
|---|
| 778 | DEFINE_COMPARISON_PRED(u8); | 
|---|
| 779 |  | 
|---|
| 780 | DEFINE_CPUMASK_COMPARISON_PRED(64); | 
|---|
| 781 | DEFINE_CPUMASK_COMPARISON_PRED(32); | 
|---|
| 782 | DEFINE_CPUMASK_COMPARISON_PRED(16); | 
|---|
| 783 | DEFINE_CPUMASK_COMPARISON_PRED(8); | 
|---|
| 784 |  | 
|---|
| 785 | DEFINE_EQUALITY_PRED(64); | 
|---|
| 786 | DEFINE_EQUALITY_PRED(32); | 
|---|
| 787 | DEFINE_EQUALITY_PRED(16); | 
|---|
| 788 | DEFINE_EQUALITY_PRED(8); | 
|---|
| 789 |  | 
|---|
| 790 | /* user space strings temp buffer */ | 
|---|
| 791 | #define USTRING_BUF_SIZE	1024 | 
|---|
| 792 |  | 
|---|
| 793 | struct ustring_buffer { | 
|---|
| 794 | char		buffer[USTRING_BUF_SIZE]; | 
|---|
| 795 | }; | 
|---|
| 796 |  | 
|---|
| 797 | static __percpu struct ustring_buffer *ustring_per_cpu; | 
|---|
| 798 |  | 
|---|
| 799 | static __always_inline char *test_string(char *str) | 
|---|
| 800 | { | 
|---|
| 801 | struct ustring_buffer *ubuf; | 
|---|
| 802 | char *kstr; | 
|---|
| 803 |  | 
|---|
| 804 | if (!ustring_per_cpu) | 
|---|
| 805 | return NULL; | 
|---|
| 806 |  | 
|---|
| 807 | ubuf = this_cpu_ptr(ustring_per_cpu); | 
|---|
| 808 | kstr = ubuf->buffer; | 
|---|
| 809 |  | 
|---|
| 810 | /* For safety, do not trust the string pointer */ | 
|---|
| 811 | if (strncpy_from_kernel_nofault(dst: kstr, unsafe_addr: str, USTRING_BUF_SIZE) < 0) | 
|---|
| 812 | return NULL; | 
|---|
| 813 | return kstr; | 
|---|
| 814 | } | 
|---|
| 815 |  | 
|---|
| 816 | static __always_inline char *test_ustring(char *str) | 
|---|
| 817 | { | 
|---|
| 818 | struct ustring_buffer *ubuf; | 
|---|
| 819 | char __user *ustr; | 
|---|
| 820 | char *kstr; | 
|---|
| 821 |  | 
|---|
| 822 | if (!ustring_per_cpu) | 
|---|
| 823 | return NULL; | 
|---|
| 824 |  | 
|---|
| 825 | ubuf = this_cpu_ptr(ustring_per_cpu); | 
|---|
| 826 | kstr = ubuf->buffer; | 
|---|
| 827 |  | 
|---|
| 828 | /* user space address? */ | 
|---|
| 829 | ustr = (char __user *)str; | 
|---|
| 830 | if (strncpy_from_user_nofault(dst: kstr, unsafe_addr: ustr, USTRING_BUF_SIZE) < 0) | 
|---|
| 831 | return NULL; | 
|---|
| 832 |  | 
|---|
| 833 | return kstr; | 
|---|
| 834 | } | 
|---|
| 835 |  | 
|---|
| 836 | /* Filter predicate for fixed sized arrays of characters */ | 
|---|
| 837 | static int filter_pred_string(struct filter_pred *pred, void *event) | 
|---|
| 838 | { | 
|---|
| 839 | char *addr = (char *)(event + pred->offset); | 
|---|
| 840 | int cmp, match; | 
|---|
| 841 |  | 
|---|
| 842 | cmp = pred->regex->match(addr, pred->regex, pred->regex->field_len); | 
|---|
| 843 |  | 
|---|
| 844 | match = cmp ^ pred->not; | 
|---|
| 845 |  | 
|---|
| 846 | return match; | 
|---|
| 847 | } | 
|---|
| 848 |  | 
|---|
| 849 | static __always_inline int filter_pchar(struct filter_pred *pred, char *str) | 
|---|
| 850 | { | 
|---|
| 851 | int cmp, match; | 
|---|
| 852 | int len; | 
|---|
| 853 |  | 
|---|
| 854 | len = strlen(str) + 1;	/* including tailing '\0' */ | 
|---|
| 855 | cmp = pred->regex->match(str, pred->regex, len); | 
|---|
| 856 |  | 
|---|
| 857 | match = cmp ^ pred->not; | 
|---|
| 858 |  | 
|---|
| 859 | return match; | 
|---|
| 860 | } | 
|---|
| 861 | /* Filter predicate for char * pointers */ | 
|---|
| 862 | static int filter_pred_pchar(struct filter_pred *pred, void *event) | 
|---|
| 863 | { | 
|---|
| 864 | char **addr = (char **)(event + pred->offset); | 
|---|
| 865 | char *str; | 
|---|
| 866 |  | 
|---|
| 867 | str = test_string(str: *addr); | 
|---|
| 868 | if (!str) | 
|---|
| 869 | return 0; | 
|---|
| 870 |  | 
|---|
| 871 | return filter_pchar(pred, str); | 
|---|
| 872 | } | 
|---|
| 873 |  | 
|---|
| 874 | /* Filter predicate for char * pointers in user space*/ | 
|---|
| 875 | static int filter_pred_pchar_user(struct filter_pred *pred, void *event) | 
|---|
| 876 | { | 
|---|
| 877 | char **addr = (char **)(event + pred->offset); | 
|---|
| 878 | char *str; | 
|---|
| 879 |  | 
|---|
| 880 | str = test_ustring(str: *addr); | 
|---|
| 881 | if (!str) | 
|---|
| 882 | return 0; | 
|---|
| 883 |  | 
|---|
| 884 | return filter_pchar(pred, str); | 
|---|
| 885 | } | 
|---|
| 886 |  | 
|---|
| 887 | /* | 
|---|
| 888 | * Filter predicate for dynamic sized arrays of characters. | 
|---|
| 889 | * These are implemented through a list of strings at the end | 
|---|
| 890 | * of the entry. | 
|---|
| 891 | * Also each of these strings have a field in the entry which | 
|---|
| 892 | * contains its offset from the beginning of the entry. | 
|---|
| 893 | * We have then first to get this field, dereference it | 
|---|
| 894 | * and add it to the address of the entry, and at last we have | 
|---|
| 895 | * the address of the string. | 
|---|
| 896 | */ | 
|---|
| 897 | static int filter_pred_strloc(struct filter_pred *pred, void *event) | 
|---|
| 898 | { | 
|---|
| 899 | u32 str_item = *(u32 *)(event + pred->offset); | 
|---|
| 900 | int str_loc = str_item & 0xffff; | 
|---|
| 901 | int str_len = str_item >> 16; | 
|---|
| 902 | char *addr = (char *)(event + str_loc); | 
|---|
| 903 | int cmp, match; | 
|---|
| 904 |  | 
|---|
| 905 | cmp = pred->regex->match(addr, pred->regex, str_len); | 
|---|
| 906 |  | 
|---|
| 907 | match = cmp ^ pred->not; | 
|---|
| 908 |  | 
|---|
| 909 | return match; | 
|---|
| 910 | } | 
|---|
| 911 |  | 
|---|
| 912 | /* | 
|---|
| 913 | * Filter predicate for relative dynamic sized arrays of characters. | 
|---|
| 914 | * These are implemented through a list of strings at the end | 
|---|
| 915 | * of the entry as same as dynamic string. | 
|---|
| 916 | * The difference is that the relative one records the location offset | 
|---|
| 917 | * from the field itself, not the event entry. | 
|---|
| 918 | */ | 
|---|
| 919 | static int filter_pred_strrelloc(struct filter_pred *pred, void *event) | 
|---|
| 920 | { | 
|---|
| 921 | u32 *item = (u32 *)(event + pred->offset); | 
|---|
| 922 | u32 str_item = *item; | 
|---|
| 923 | int str_loc = str_item & 0xffff; | 
|---|
| 924 | int str_len = str_item >> 16; | 
|---|
| 925 | char *addr = (char *)(&item[1]) + str_loc; | 
|---|
| 926 | int cmp, match; | 
|---|
| 927 |  | 
|---|
| 928 | cmp = pred->regex->match(addr, pred->regex, str_len); | 
|---|
| 929 |  | 
|---|
| 930 | match = cmp ^ pred->not; | 
|---|
| 931 |  | 
|---|
| 932 | return match; | 
|---|
| 933 | } | 
|---|
| 934 |  | 
|---|
| 935 | /* Filter predicate for CPUs. */ | 
|---|
| 936 | static int filter_pred_cpu(struct filter_pred *pred, void *event) | 
|---|
| 937 | { | 
|---|
| 938 | int cpu, cmp; | 
|---|
| 939 |  | 
|---|
| 940 | cpu = raw_smp_processor_id(); | 
|---|
| 941 | cmp = pred->val; | 
|---|
| 942 |  | 
|---|
| 943 | switch (pred->op) { | 
|---|
| 944 | case OP_EQ: | 
|---|
| 945 | return cpu == cmp; | 
|---|
| 946 | case OP_NE: | 
|---|
| 947 | return cpu != cmp; | 
|---|
| 948 | case OP_LT: | 
|---|
| 949 | return cpu < cmp; | 
|---|
| 950 | case OP_LE: | 
|---|
| 951 | return cpu <= cmp; | 
|---|
| 952 | case OP_GT: | 
|---|
| 953 | return cpu > cmp; | 
|---|
| 954 | case OP_GE: | 
|---|
| 955 | return cpu >= cmp; | 
|---|
| 956 | default: | 
|---|
| 957 | return 0; | 
|---|
| 958 | } | 
|---|
| 959 | } | 
|---|
| 960 |  | 
|---|
| 961 | /* Filter predicate for current CPU vs user-provided cpumask */ | 
|---|
| 962 | static int filter_pred_cpu_cpumask(struct filter_pred *pred, void *event) | 
|---|
| 963 | { | 
|---|
| 964 | int cpu = raw_smp_processor_id(); | 
|---|
| 965 |  | 
|---|
| 966 | return do_filter_scalar_cpumask(op: pred->op, cpu, mask: pred->mask); | 
|---|
| 967 | } | 
|---|
| 968 |  | 
|---|
| 969 | /* Filter predicate for cpumask field vs user-provided cpumask */ | 
|---|
| 970 | static int filter_pred_cpumask(struct filter_pred *pred, void *event) | 
|---|
| 971 | { | 
|---|
| 972 | u32 item = *(u32 *)(event + pred->offset); | 
|---|
| 973 | int loc = item & 0xffff; | 
|---|
| 974 | const struct cpumask *mask = (event + loc); | 
|---|
| 975 | const struct cpumask *cmp = pred->mask; | 
|---|
| 976 |  | 
|---|
| 977 | return do_filter_cpumask(op: pred->op, mask, cmp); | 
|---|
| 978 | } | 
|---|
| 979 |  | 
|---|
| 980 | /* Filter predicate for cpumask field vs user-provided scalar  */ | 
|---|
| 981 | static int filter_pred_cpumask_cpu(struct filter_pred *pred, void *event) | 
|---|
| 982 | { | 
|---|
| 983 | u32 item = *(u32 *)(event + pred->offset); | 
|---|
| 984 | int loc = item & 0xffff; | 
|---|
| 985 | const struct cpumask *mask = (event + loc); | 
|---|
| 986 | unsigned int cpu = pred->val; | 
|---|
| 987 |  | 
|---|
| 988 | return do_filter_cpumask_scalar(op: pred->op, mask, cpu); | 
|---|
| 989 | } | 
|---|
| 990 |  | 
|---|
| 991 | /* Filter predicate for COMM. */ | 
|---|
| 992 | static int filter_pred_comm(struct filter_pred *pred, void *event) | 
|---|
| 993 | { | 
|---|
| 994 | int cmp; | 
|---|
| 995 |  | 
|---|
| 996 | cmp = pred->regex->match(current->comm, pred->regex, | 
|---|
| 997 | TASK_COMM_LEN); | 
|---|
| 998 | return cmp ^ pred->not; | 
|---|
| 999 | } | 
|---|
| 1000 |  | 
|---|
| 1001 | /* Filter predicate for functions. */ | 
|---|
| 1002 | static int filter_pred_function(struct filter_pred *pred, void *event) | 
|---|
| 1003 | { | 
|---|
| 1004 | unsigned long *addr = (unsigned long *)(event + pred->offset); | 
|---|
| 1005 | unsigned long start = (unsigned long)pred->val; | 
|---|
| 1006 | unsigned long end = (unsigned long)pred->val2; | 
|---|
| 1007 | int ret = *addr >= start && *addr < end; | 
|---|
| 1008 |  | 
|---|
| 1009 | return pred->op == OP_EQ ? ret : !ret; | 
|---|
| 1010 | } | 
|---|
| 1011 |  | 
|---|
| 1012 | /* | 
|---|
| 1013 | * regex_match_foo - Basic regex callbacks | 
|---|
| 1014 | * | 
|---|
| 1015 | * @str: the string to be searched | 
|---|
| 1016 | * @r:   the regex structure containing the pattern string | 
|---|
| 1017 | * @len: the length of the string to be searched (including '\0') | 
|---|
| 1018 | * | 
|---|
| 1019 | * Note: | 
|---|
| 1020 | * - @str might not be NULL-terminated if it's of type DYN_STRING | 
|---|
| 1021 | *   RDYN_STRING, or STATIC_STRING, unless @len is zero. | 
|---|
| 1022 | */ | 
|---|
| 1023 |  | 
|---|
| 1024 | static int regex_match_full(char *str, struct regex *r, int len) | 
|---|
| 1025 | { | 
|---|
| 1026 | /* len of zero means str is dynamic and ends with '\0' */ | 
|---|
| 1027 | if (!len) | 
|---|
| 1028 | return strcmp(str, r->pattern) == 0; | 
|---|
| 1029 |  | 
|---|
| 1030 | return strncmp(str, r->pattern, len) == 0; | 
|---|
| 1031 | } | 
|---|
| 1032 |  | 
|---|
| 1033 | static int regex_match_front(char *str, struct regex *r, int len) | 
|---|
| 1034 | { | 
|---|
| 1035 | if (len && len < r->len) | 
|---|
| 1036 | return 0; | 
|---|
| 1037 |  | 
|---|
| 1038 | return strncmp(str, r->pattern, r->len) == 0; | 
|---|
| 1039 | } | 
|---|
| 1040 |  | 
|---|
| 1041 | static int regex_match_middle(char *str, struct regex *r, int len) | 
|---|
| 1042 | { | 
|---|
| 1043 | if (!len) | 
|---|
| 1044 | return strstr(str, r->pattern) != NULL; | 
|---|
| 1045 |  | 
|---|
| 1046 | return strnstr(str, r->pattern, len) != NULL; | 
|---|
| 1047 | } | 
|---|
| 1048 |  | 
|---|
| 1049 | static int regex_match_end(char *str, struct regex *r, int len) | 
|---|
| 1050 | { | 
|---|
| 1051 | int strlen = len - 1; | 
|---|
| 1052 |  | 
|---|
| 1053 | if (strlen >= r->len && | 
|---|
| 1054 | memcmp(str + strlen - r->len, r->pattern, r->len) == 0) | 
|---|
| 1055 | return 1; | 
|---|
| 1056 | return 0; | 
|---|
| 1057 | } | 
|---|
| 1058 |  | 
|---|
| 1059 | static int regex_match_glob(char *str, struct regex *r, int len __maybe_unused) | 
|---|
| 1060 | { | 
|---|
| 1061 | if (glob_match(pat: r->pattern, str)) | 
|---|
| 1062 | return 1; | 
|---|
| 1063 | return 0; | 
|---|
| 1064 | } | 
|---|
| 1065 |  | 
|---|
| 1066 | /** | 
|---|
| 1067 | * filter_parse_regex - parse a basic regex | 
|---|
| 1068 | * @buff:   the raw regex | 
|---|
| 1069 | * @len:    length of the regex | 
|---|
| 1070 | * @search: will point to the beginning of the string to compare | 
|---|
| 1071 | * @not:    tell whether the match will have to be inverted | 
|---|
| 1072 | * | 
|---|
| 1073 | * This passes in a buffer containing a regex and this function will | 
|---|
| 1074 | * set search to point to the search part of the buffer and | 
|---|
| 1075 | * return the type of search it is (see enum above). | 
|---|
| 1076 | * This does modify buff. | 
|---|
| 1077 | * | 
|---|
| 1078 | * Returns enum type. | 
|---|
| 1079 | *  search returns the pointer to use for comparison. | 
|---|
| 1080 | *  not returns 1 if buff started with a '!' | 
|---|
| 1081 | *     0 otherwise. | 
|---|
| 1082 | */ | 
|---|
| 1083 | enum regex_type filter_parse_regex(char *buff, int len, char **search, int *not) | 
|---|
| 1084 | { | 
|---|
| 1085 | int type = MATCH_FULL; | 
|---|
| 1086 | int i; | 
|---|
| 1087 |  | 
|---|
| 1088 | if (buff[0] == '!') { | 
|---|
| 1089 | *not = 1; | 
|---|
| 1090 | buff++; | 
|---|
| 1091 | len--; | 
|---|
| 1092 | } else | 
|---|
| 1093 | *not = 0; | 
|---|
| 1094 |  | 
|---|
| 1095 | *search = buff; | 
|---|
| 1096 |  | 
|---|
| 1097 | if (isdigit(c: buff[0])) | 
|---|
| 1098 | return MATCH_INDEX; | 
|---|
| 1099 |  | 
|---|
| 1100 | for (i = 0; i < len; i++) { | 
|---|
| 1101 | if (buff[i] == '*') { | 
|---|
| 1102 | if (!i) { | 
|---|
| 1103 | type = MATCH_END_ONLY; | 
|---|
| 1104 | } else if (i == len - 1) { | 
|---|
| 1105 | if (type == MATCH_END_ONLY) | 
|---|
| 1106 | type = MATCH_MIDDLE_ONLY; | 
|---|
| 1107 | else | 
|---|
| 1108 | type = MATCH_FRONT_ONLY; | 
|---|
| 1109 | buff[i] = 0; | 
|---|
| 1110 | break; | 
|---|
| 1111 | } else {	/* pattern continues, use full glob */ | 
|---|
| 1112 | return MATCH_GLOB; | 
|---|
| 1113 | } | 
|---|
| 1114 | } else if (strchr( "[?\\", buff[i])) { | 
|---|
| 1115 | return MATCH_GLOB; | 
|---|
| 1116 | } | 
|---|
| 1117 | } | 
|---|
| 1118 | if (buff[0] == '*') | 
|---|
| 1119 | *search = buff + 1; | 
|---|
| 1120 |  | 
|---|
| 1121 | return type; | 
|---|
| 1122 | } | 
|---|
| 1123 |  | 
|---|
| 1124 | static void filter_build_regex(struct filter_pred *pred) | 
|---|
| 1125 | { | 
|---|
| 1126 | struct regex *r = pred->regex; | 
|---|
| 1127 | char *search; | 
|---|
| 1128 | enum regex_type type = MATCH_FULL; | 
|---|
| 1129 |  | 
|---|
| 1130 | if (pred->op == OP_GLOB) { | 
|---|
| 1131 | type = filter_parse_regex(buff: r->pattern, len: r->len, search: &search, not: &pred->not); | 
|---|
| 1132 | r->len = strlen(search); | 
|---|
| 1133 | memmove(dest: r->pattern, src: search, count: r->len+1); | 
|---|
| 1134 | } | 
|---|
| 1135 |  | 
|---|
| 1136 | switch (type) { | 
|---|
| 1137 | /* MATCH_INDEX should not happen, but if it does, match full */ | 
|---|
| 1138 | case MATCH_INDEX: | 
|---|
| 1139 | case MATCH_FULL: | 
|---|
| 1140 | r->match = regex_match_full; | 
|---|
| 1141 | break; | 
|---|
| 1142 | case MATCH_FRONT_ONLY: | 
|---|
| 1143 | r->match = regex_match_front; | 
|---|
| 1144 | break; | 
|---|
| 1145 | case MATCH_MIDDLE_ONLY: | 
|---|
| 1146 | r->match = regex_match_middle; | 
|---|
| 1147 | break; | 
|---|
| 1148 | case MATCH_END_ONLY: | 
|---|
| 1149 | r->match = regex_match_end; | 
|---|
| 1150 | break; | 
|---|
| 1151 | case MATCH_GLOB: | 
|---|
| 1152 | r->match = regex_match_glob; | 
|---|
| 1153 | break; | 
|---|
| 1154 | } | 
|---|
| 1155 | } | 
|---|
| 1156 |  | 
|---|
| 1157 |  | 
|---|
| 1158 | #ifdef CONFIG_FTRACE_STARTUP_TEST | 
|---|
| 1159 | static int test_pred_visited_fn(struct filter_pred *pred, void *event); | 
|---|
| 1160 | #else | 
|---|
| 1161 | static int test_pred_visited_fn(struct filter_pred *pred, void *event) | 
|---|
| 1162 | { | 
|---|
| 1163 | return 0; | 
|---|
| 1164 | } | 
|---|
| 1165 | #endif | 
|---|
| 1166 |  | 
|---|
| 1167 |  | 
|---|
| 1168 | static int filter_pred_fn_call(struct filter_pred *pred, void *event); | 
|---|
| 1169 |  | 
|---|
| 1170 | /* return 1 if event matches, 0 otherwise (discard) */ | 
|---|
| 1171 | int filter_match_preds(struct event_filter *filter, void *rec) | 
|---|
| 1172 | { | 
|---|
| 1173 | struct prog_entry *prog; | 
|---|
| 1174 | int i; | 
|---|
| 1175 |  | 
|---|
| 1176 | /* no filter is considered a match */ | 
|---|
| 1177 | if (!filter) | 
|---|
| 1178 | return 1; | 
|---|
| 1179 |  | 
|---|
| 1180 | /* Protected by either SRCU(tracepoint_srcu) or preempt_disable */ | 
|---|
| 1181 | prog = rcu_dereference_raw(filter->prog); | 
|---|
| 1182 | if (!prog) | 
|---|
| 1183 | return 1; | 
|---|
| 1184 |  | 
|---|
| 1185 | for (i = 0; prog[i].pred; i++) { | 
|---|
| 1186 | struct filter_pred *pred = prog[i].pred; | 
|---|
| 1187 | int match = filter_pred_fn_call(pred, event: rec); | 
|---|
| 1188 | if (match == prog[i].when_to_branch) | 
|---|
| 1189 | i = prog[i].target; | 
|---|
| 1190 | } | 
|---|
| 1191 | return prog[i].target; | 
|---|
| 1192 | } | 
|---|
| 1193 | EXPORT_SYMBOL_GPL(filter_match_preds); | 
|---|
| 1194 |  | 
|---|
| 1195 | static void remove_filter_string(struct event_filter *filter) | 
|---|
| 1196 | { | 
|---|
| 1197 | if (!filter) | 
|---|
| 1198 | return; | 
|---|
| 1199 |  | 
|---|
| 1200 | kfree(objp: filter->filter_string); | 
|---|
| 1201 | filter->filter_string = NULL; | 
|---|
| 1202 | } | 
|---|
| 1203 |  | 
|---|
| 1204 | static void append_filter_err(struct trace_array *tr, | 
|---|
| 1205 | struct filter_parse_error *pe, | 
|---|
| 1206 | struct event_filter *filter) | 
|---|
| 1207 | { | 
|---|
| 1208 | struct trace_seq *s; | 
|---|
| 1209 | int pos = pe->lasterr_pos; | 
|---|
| 1210 | char *buf; | 
|---|
| 1211 | int len; | 
|---|
| 1212 |  | 
|---|
| 1213 | if (WARN_ON(!filter->filter_string)) | 
|---|
| 1214 | return; | 
|---|
| 1215 |  | 
|---|
| 1216 | s = kmalloc(sizeof(*s), GFP_KERNEL); | 
|---|
| 1217 | if (!s) | 
|---|
| 1218 | return; | 
|---|
| 1219 | trace_seq_init(s); | 
|---|
| 1220 |  | 
|---|
| 1221 | len = strlen(filter->filter_string); | 
|---|
| 1222 | if (pos > len) | 
|---|
| 1223 | pos = len; | 
|---|
| 1224 |  | 
|---|
| 1225 | /* indexing is off by one */ | 
|---|
| 1226 | if (pos) | 
|---|
| 1227 | pos++; | 
|---|
| 1228 |  | 
|---|
| 1229 | trace_seq_puts(s, str: filter->filter_string); | 
|---|
| 1230 | if (pe->lasterr > 0) { | 
|---|
| 1231 | trace_seq_printf(s, fmt: "\n%*s", pos, "^"); | 
|---|
| 1232 | trace_seq_printf(s, fmt: "\nparse_error: %s\n", err_text[pe->lasterr]); | 
|---|
| 1233 | tracing_log_err(tr, loc: "event filter parse error", | 
|---|
| 1234 | cmd: filter->filter_string, errs: err_text, | 
|---|
| 1235 | type: pe->lasterr, pos: pe->lasterr_pos); | 
|---|
| 1236 | } else { | 
|---|
| 1237 | trace_seq_printf(s, fmt: "\nError: (%d)\n", pe->lasterr); | 
|---|
| 1238 | tracing_log_err(tr, loc: "event filter parse error", | 
|---|
| 1239 | cmd: filter->filter_string, errs: err_text, | 
|---|
| 1240 | type: FILT_ERR_ERRNO, pos: 0); | 
|---|
| 1241 | } | 
|---|
| 1242 | trace_seq_putc(s, c: 0); | 
|---|
| 1243 | buf = kmemdup_nul(s: s->buffer, len: s->seq.len, GFP_KERNEL); | 
|---|
| 1244 | if (buf) { | 
|---|
| 1245 | kfree(objp: filter->filter_string); | 
|---|
| 1246 | filter->filter_string = buf; | 
|---|
| 1247 | } | 
|---|
| 1248 | kfree(objp: s); | 
|---|
| 1249 | } | 
|---|
| 1250 |  | 
|---|
| 1251 | static inline struct event_filter *event_filter(struct trace_event_file *file) | 
|---|
| 1252 | { | 
|---|
| 1253 | return rcu_dereference_protected(file->filter, | 
|---|
| 1254 | lockdep_is_held(&event_mutex)); | 
|---|
| 1255 |  | 
|---|
| 1256 | } | 
|---|
| 1257 |  | 
|---|
| 1258 | /* caller must hold event_mutex */ | 
|---|
| 1259 | void print_event_filter(struct trace_event_file *file, struct trace_seq *s) | 
|---|
| 1260 | { | 
|---|
| 1261 | struct event_filter *filter = event_filter(file); | 
|---|
| 1262 |  | 
|---|
| 1263 | if (filter && filter->filter_string) | 
|---|
| 1264 | trace_seq_printf(s, fmt: "%s\n", filter->filter_string); | 
|---|
| 1265 | else | 
|---|
| 1266 | trace_seq_puts(s, str: "none\n"); | 
|---|
| 1267 | } | 
|---|
| 1268 |  | 
|---|
| 1269 | void print_subsystem_event_filter(struct event_subsystem *system, | 
|---|
| 1270 | struct trace_seq *s) | 
|---|
| 1271 | { | 
|---|
| 1272 | struct event_filter *filter; | 
|---|
| 1273 |  | 
|---|
| 1274 | mutex_lock(lock: &event_mutex); | 
|---|
| 1275 | filter = system->filter; | 
|---|
| 1276 | if (filter && filter->filter_string) | 
|---|
| 1277 | trace_seq_printf(s, fmt: "%s\n", filter->filter_string); | 
|---|
| 1278 | else | 
|---|
| 1279 | trace_seq_puts(s, DEFAULT_SYS_FILTER_MESSAGE "\n"); | 
|---|
| 1280 | mutex_unlock(lock: &event_mutex); | 
|---|
| 1281 | } | 
|---|
| 1282 |  | 
|---|
| 1283 | static void free_prog(struct event_filter *filter) | 
|---|
| 1284 | { | 
|---|
| 1285 | struct prog_entry *prog; | 
|---|
| 1286 | int i; | 
|---|
| 1287 |  | 
|---|
| 1288 | prog = rcu_access_pointer(filter->prog); | 
|---|
| 1289 | if (!prog) | 
|---|
| 1290 | return; | 
|---|
| 1291 |  | 
|---|
| 1292 | for (i = 0; prog[i].pred; i++) | 
|---|
| 1293 | free_predicate(pred: prog[i].pred); | 
|---|
| 1294 | kfree(objp: prog); | 
|---|
| 1295 | } | 
|---|
| 1296 |  | 
|---|
| 1297 | static void filter_disable(struct trace_event_file *file) | 
|---|
| 1298 | { | 
|---|
| 1299 | unsigned long old_flags = file->flags; | 
|---|
| 1300 |  | 
|---|
| 1301 | file->flags &= ~EVENT_FILE_FL_FILTERED; | 
|---|
| 1302 |  | 
|---|
| 1303 | if (old_flags != file->flags) | 
|---|
| 1304 | trace_buffered_event_disable(); | 
|---|
| 1305 | } | 
|---|
| 1306 |  | 
|---|
| 1307 | static void __free_filter(struct event_filter *filter) | 
|---|
| 1308 | { | 
|---|
| 1309 | if (!filter) | 
|---|
| 1310 | return; | 
|---|
| 1311 |  | 
|---|
| 1312 | free_prog(filter); | 
|---|
| 1313 | kfree(objp: filter->filter_string); | 
|---|
| 1314 | kfree(objp: filter); | 
|---|
| 1315 | } | 
|---|
| 1316 |  | 
|---|
| 1317 | void free_event_filter(struct event_filter *filter) | 
|---|
| 1318 | { | 
|---|
| 1319 | __free_filter(filter); | 
|---|
| 1320 | } | 
|---|
| 1321 |  | 
|---|
| 1322 | static inline void __remove_filter(struct trace_event_file *file) | 
|---|
| 1323 | { | 
|---|
| 1324 | filter_disable(file); | 
|---|
| 1325 | remove_filter_string(filter: event_filter(file)); | 
|---|
| 1326 | } | 
|---|
| 1327 |  | 
|---|
| 1328 | static void filter_free_subsystem_preds(struct trace_subsystem_dir *dir, | 
|---|
| 1329 | struct trace_array *tr) | 
|---|
| 1330 | { | 
|---|
| 1331 | struct trace_event_file *file; | 
|---|
| 1332 |  | 
|---|
| 1333 | list_for_each_entry(file, &tr->events, list) { | 
|---|
| 1334 | if (file->system != dir) | 
|---|
| 1335 | continue; | 
|---|
| 1336 | __remove_filter(file); | 
|---|
| 1337 | } | 
|---|
| 1338 | } | 
|---|
| 1339 |  | 
|---|
| 1340 | struct filter_list { | 
|---|
| 1341 | struct list_head	list; | 
|---|
| 1342 | struct event_filter	*filter; | 
|---|
| 1343 | }; | 
|---|
| 1344 |  | 
|---|
| 1345 | struct filter_head { | 
|---|
| 1346 | struct list_head	list; | 
|---|
| 1347 | union { | 
|---|
| 1348 | struct rcu_head		rcu; | 
|---|
| 1349 | struct rcu_work		rwork; | 
|---|
| 1350 | }; | 
|---|
| 1351 | }; | 
|---|
| 1352 |  | 
|---|
| 1353 | static void free_filter_list(struct filter_head *filter_list) | 
|---|
| 1354 | { | 
|---|
| 1355 | struct filter_list *filter_item, *tmp; | 
|---|
| 1356 |  | 
|---|
| 1357 | list_for_each_entry_safe(filter_item, tmp, &filter_list->list, list) { | 
|---|
| 1358 | __free_filter(filter: filter_item->filter); | 
|---|
| 1359 | list_del(entry: &filter_item->list); | 
|---|
| 1360 | kfree(objp: filter_item); | 
|---|
| 1361 | } | 
|---|
| 1362 | kfree(objp: filter_list); | 
|---|
| 1363 | } | 
|---|
| 1364 |  | 
|---|
| 1365 | static void free_filter_list_work(struct work_struct *work) | 
|---|
| 1366 | { | 
|---|
| 1367 | struct filter_head *filter_list; | 
|---|
| 1368 |  | 
|---|
| 1369 | filter_list = container_of(to_rcu_work(work), struct filter_head, rwork); | 
|---|
| 1370 | free_filter_list(filter_list); | 
|---|
| 1371 | } | 
|---|
| 1372 |  | 
|---|
| 1373 | static void free_filter_list_tasks(struct rcu_head *rhp) | 
|---|
| 1374 | { | 
|---|
| 1375 | struct filter_head *filter_list = container_of(rhp, struct filter_head, rcu); | 
|---|
| 1376 |  | 
|---|
| 1377 | INIT_RCU_WORK(&filter_list->rwork, free_filter_list_work); | 
|---|
| 1378 | queue_rcu_work(wq: system_wq, rwork: &filter_list->rwork); | 
|---|
| 1379 | } | 
|---|
| 1380 |  | 
|---|
| 1381 | /* | 
|---|
| 1382 | * The tracepoint_synchronize_unregister() is a double rcu call. | 
|---|
| 1383 | * It calls synchronize_rcu_tasks_trace() followed by synchronize_rcu(). | 
|---|
| 1384 | * Instead of waiting for it, simply call these via the call_rcu*() | 
|---|
| 1385 | * variants. | 
|---|
| 1386 | */ | 
|---|
| 1387 | static void delay_free_filter(struct filter_head *head) | 
|---|
| 1388 | { | 
|---|
| 1389 | call_rcu_tasks_trace(rhp: &head->rcu, func: free_filter_list_tasks); | 
|---|
| 1390 | } | 
|---|
| 1391 |  | 
|---|
| 1392 | static void try_delay_free_filter(struct event_filter *filter) | 
|---|
| 1393 | { | 
|---|
| 1394 | struct filter_head *head; | 
|---|
| 1395 | struct filter_list *item; | 
|---|
| 1396 |  | 
|---|
| 1397 | head = kmalloc(sizeof(*head), GFP_KERNEL); | 
|---|
| 1398 | if (!head) | 
|---|
| 1399 | goto free_now; | 
|---|
| 1400 |  | 
|---|
| 1401 | INIT_LIST_HEAD(list: &head->list); | 
|---|
| 1402 |  | 
|---|
| 1403 | item = kmalloc(sizeof(*item), GFP_KERNEL); | 
|---|
| 1404 | if (!item) { | 
|---|
| 1405 | kfree(objp: head); | 
|---|
| 1406 | goto free_now; | 
|---|
| 1407 | } | 
|---|
| 1408 |  | 
|---|
| 1409 | item->filter = filter; | 
|---|
| 1410 | list_add_tail(new: &item->list, head: &head->list); | 
|---|
| 1411 | delay_free_filter(head); | 
|---|
| 1412 | return; | 
|---|
| 1413 |  | 
|---|
| 1414 | free_now: | 
|---|
| 1415 | /* Make sure the filter is not being used */ | 
|---|
| 1416 | tracepoint_synchronize_unregister(); | 
|---|
| 1417 | __free_filter(filter); | 
|---|
| 1418 | } | 
|---|
| 1419 |  | 
|---|
| 1420 | static inline void __free_subsystem_filter(struct trace_event_file *file) | 
|---|
| 1421 | { | 
|---|
| 1422 | __free_filter(filter: event_filter(file)); | 
|---|
| 1423 | file->filter = NULL; | 
|---|
| 1424 | } | 
|---|
| 1425 |  | 
|---|
| 1426 | static inline void event_set_filter(struct trace_event_file *file, | 
|---|
| 1427 | struct event_filter *filter) | 
|---|
| 1428 | { | 
|---|
| 1429 | rcu_assign_pointer(file->filter, filter); | 
|---|
| 1430 | } | 
|---|
| 1431 |  | 
|---|
| 1432 | static inline void event_clear_filter(struct trace_event_file *file) | 
|---|
| 1433 | { | 
|---|
| 1434 | RCU_INIT_POINTER(file->filter, NULL); | 
|---|
| 1435 | } | 
|---|
| 1436 |  | 
|---|
| 1437 | static void filter_free_subsystem_filters(struct trace_subsystem_dir *dir, | 
|---|
| 1438 | struct trace_array *tr, | 
|---|
| 1439 | struct event_filter *filter) | 
|---|
| 1440 | { | 
|---|
| 1441 | struct trace_event_file *file; | 
|---|
| 1442 | struct filter_head *head; | 
|---|
| 1443 | struct filter_list *item; | 
|---|
| 1444 |  | 
|---|
| 1445 | head = kmalloc(sizeof(*head), GFP_KERNEL); | 
|---|
| 1446 | if (!head) | 
|---|
| 1447 | goto free_now; | 
|---|
| 1448 |  | 
|---|
| 1449 | INIT_LIST_HEAD(list: &head->list); | 
|---|
| 1450 |  | 
|---|
| 1451 | list_for_each_entry(file, &tr->events, list) { | 
|---|
| 1452 | if (file->system != dir) | 
|---|
| 1453 | continue; | 
|---|
| 1454 | item = kmalloc(sizeof(*item), GFP_KERNEL); | 
|---|
| 1455 | if (!item) | 
|---|
| 1456 | goto free_now; | 
|---|
| 1457 | item->filter = event_filter(file); | 
|---|
| 1458 | list_add_tail(new: &item->list, head: &head->list); | 
|---|
| 1459 | event_clear_filter(file); | 
|---|
| 1460 | } | 
|---|
| 1461 |  | 
|---|
| 1462 | item = kmalloc(sizeof(*item), GFP_KERNEL); | 
|---|
| 1463 | if (!item) | 
|---|
| 1464 | goto free_now; | 
|---|
| 1465 |  | 
|---|
| 1466 | item->filter = filter; | 
|---|
| 1467 | list_add_tail(new: &item->list, head: &head->list); | 
|---|
| 1468 |  | 
|---|
| 1469 | delay_free_filter(head); | 
|---|
| 1470 | return; | 
|---|
| 1471 | free_now: | 
|---|
| 1472 | tracepoint_synchronize_unregister(); | 
|---|
| 1473 |  | 
|---|
| 1474 | if (head) | 
|---|
| 1475 | free_filter_list(filter_list: head); | 
|---|
| 1476 |  | 
|---|
| 1477 | list_for_each_entry(file, &tr->events, list) { | 
|---|
| 1478 | if (file->system != dir || !file->filter) | 
|---|
| 1479 | continue; | 
|---|
| 1480 | __free_subsystem_filter(file); | 
|---|
| 1481 | } | 
|---|
| 1482 | __free_filter(filter); | 
|---|
| 1483 | } | 
|---|
| 1484 |  | 
|---|
| 1485 | int filter_assign_type(const char *type) | 
|---|
| 1486 | { | 
|---|
| 1487 | if (strstr(type, "__data_loc")) { | 
|---|
| 1488 | if (strstr(type, "char")) | 
|---|
| 1489 | return FILTER_DYN_STRING; | 
|---|
| 1490 | if (strstr(type, "cpumask_t")) | 
|---|
| 1491 | return FILTER_CPUMASK; | 
|---|
| 1492 | } | 
|---|
| 1493 |  | 
|---|
| 1494 | if (strstr(type, "__rel_loc") && strstr(type, "char")) | 
|---|
| 1495 | return FILTER_RDYN_STRING; | 
|---|
| 1496 |  | 
|---|
| 1497 | if (strchr(type, '[') && strstr(type, "char")) | 
|---|
| 1498 | return FILTER_STATIC_STRING; | 
|---|
| 1499 |  | 
|---|
| 1500 | if (strcmp(type, "char *") == 0 || strcmp(type, "const char *") == 0) | 
|---|
| 1501 | return FILTER_PTR_STRING; | 
|---|
| 1502 |  | 
|---|
| 1503 | return FILTER_OTHER; | 
|---|
| 1504 | } | 
|---|
| 1505 |  | 
|---|
| 1506 | static enum filter_pred_fn select_comparison_fn(enum filter_op_ids op, | 
|---|
| 1507 | int field_size, int field_is_signed) | 
|---|
| 1508 | { | 
|---|
| 1509 | enum filter_pred_fn fn = FILTER_PRED_FN_NOP; | 
|---|
| 1510 | int pred_func_index = -1; | 
|---|
| 1511 |  | 
|---|
| 1512 | switch (op) { | 
|---|
| 1513 | case OP_EQ: | 
|---|
| 1514 | case OP_NE: | 
|---|
| 1515 | break; | 
|---|
| 1516 | default: | 
|---|
| 1517 | if (WARN_ON_ONCE(op < PRED_FUNC_START)) | 
|---|
| 1518 | return fn; | 
|---|
| 1519 | pred_func_index = op - PRED_FUNC_START; | 
|---|
| 1520 | if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX)) | 
|---|
| 1521 | return fn; | 
|---|
| 1522 | } | 
|---|
| 1523 |  | 
|---|
| 1524 | switch (field_size) { | 
|---|
| 1525 | case 8: | 
|---|
| 1526 | if (pred_func_index < 0) | 
|---|
| 1527 | fn = FILTER_PRED_FN_64; | 
|---|
| 1528 | else if (field_is_signed) | 
|---|
| 1529 | fn = FILTER_PRED_FN_S64; | 
|---|
| 1530 | else | 
|---|
| 1531 | fn = FILTER_PRED_FN_U64; | 
|---|
| 1532 | break; | 
|---|
| 1533 | case 4: | 
|---|
| 1534 | if (pred_func_index < 0) | 
|---|
| 1535 | fn = FILTER_PRED_FN_32; | 
|---|
| 1536 | else if (field_is_signed) | 
|---|
| 1537 | fn = FILTER_PRED_FN_S32; | 
|---|
| 1538 | else | 
|---|
| 1539 | fn = FILTER_PRED_FN_U32; | 
|---|
| 1540 | break; | 
|---|
| 1541 | case 2: | 
|---|
| 1542 | if (pred_func_index < 0) | 
|---|
| 1543 | fn = FILTER_PRED_FN_16; | 
|---|
| 1544 | else if (field_is_signed) | 
|---|
| 1545 | fn = FILTER_PRED_FN_S16; | 
|---|
| 1546 | else | 
|---|
| 1547 | fn = FILTER_PRED_FN_U16; | 
|---|
| 1548 | break; | 
|---|
| 1549 | case 1: | 
|---|
| 1550 | if (pred_func_index < 0) | 
|---|
| 1551 | fn = FILTER_PRED_FN_8; | 
|---|
| 1552 | else if (field_is_signed) | 
|---|
| 1553 | fn = FILTER_PRED_FN_S8; | 
|---|
| 1554 | else | 
|---|
| 1555 | fn = FILTER_PRED_FN_U8; | 
|---|
| 1556 | break; | 
|---|
| 1557 | } | 
|---|
| 1558 |  | 
|---|
| 1559 | return fn; | 
|---|
| 1560 | } | 
|---|
| 1561 |  | 
|---|
| 1562 |  | 
|---|
| 1563 | static int filter_pred_fn_call(struct filter_pred *pred, void *event) | 
|---|
| 1564 | { | 
|---|
| 1565 | switch (pred->fn_num) { | 
|---|
| 1566 | case FILTER_PRED_FN_64: | 
|---|
| 1567 | return filter_pred_64(pred, event); | 
|---|
| 1568 | case FILTER_PRED_FN_64_CPUMASK: | 
|---|
| 1569 | return filter_pred_64_cpumask(pred, event); | 
|---|
| 1570 | case FILTER_PRED_FN_S64: | 
|---|
| 1571 | return filter_pred_s64(pred, event); | 
|---|
| 1572 | case FILTER_PRED_FN_U64: | 
|---|
| 1573 | return filter_pred_u64(pred, event); | 
|---|
| 1574 | case FILTER_PRED_FN_32: | 
|---|
| 1575 | return filter_pred_32(pred, event); | 
|---|
| 1576 | case FILTER_PRED_FN_32_CPUMASK: | 
|---|
| 1577 | return filter_pred_32_cpumask(pred, event); | 
|---|
| 1578 | case FILTER_PRED_FN_S32: | 
|---|
| 1579 | return filter_pred_s32(pred, event); | 
|---|
| 1580 | case FILTER_PRED_FN_U32: | 
|---|
| 1581 | return filter_pred_u32(pred, event); | 
|---|
| 1582 | case FILTER_PRED_FN_16: | 
|---|
| 1583 | return filter_pred_16(pred, event); | 
|---|
| 1584 | case FILTER_PRED_FN_16_CPUMASK: | 
|---|
| 1585 | return filter_pred_16_cpumask(pred, event); | 
|---|
| 1586 | case FILTER_PRED_FN_S16: | 
|---|
| 1587 | return filter_pred_s16(pred, event); | 
|---|
| 1588 | case FILTER_PRED_FN_U16: | 
|---|
| 1589 | return filter_pred_u16(pred, event); | 
|---|
| 1590 | case FILTER_PRED_FN_8: | 
|---|
| 1591 | return filter_pred_8(pred, event); | 
|---|
| 1592 | case FILTER_PRED_FN_8_CPUMASK: | 
|---|
| 1593 | return filter_pred_8_cpumask(pred, event); | 
|---|
| 1594 | case FILTER_PRED_FN_S8: | 
|---|
| 1595 | return filter_pred_s8(pred, event); | 
|---|
| 1596 | case FILTER_PRED_FN_U8: | 
|---|
| 1597 | return filter_pred_u8(pred, event); | 
|---|
| 1598 | case FILTER_PRED_FN_COMM: | 
|---|
| 1599 | return filter_pred_comm(pred, event); | 
|---|
| 1600 | case FILTER_PRED_FN_STRING: | 
|---|
| 1601 | return filter_pred_string(pred, event); | 
|---|
| 1602 | case FILTER_PRED_FN_STRLOC: | 
|---|
| 1603 | return filter_pred_strloc(pred, event); | 
|---|
| 1604 | case FILTER_PRED_FN_STRRELLOC: | 
|---|
| 1605 | return filter_pred_strrelloc(pred, event); | 
|---|
| 1606 | case FILTER_PRED_FN_PCHAR_USER: | 
|---|
| 1607 | return filter_pred_pchar_user(pred, event); | 
|---|
| 1608 | case FILTER_PRED_FN_PCHAR: | 
|---|
| 1609 | return filter_pred_pchar(pred, event); | 
|---|
| 1610 | case FILTER_PRED_FN_CPU: | 
|---|
| 1611 | return filter_pred_cpu(pred, event); | 
|---|
| 1612 | case FILTER_PRED_FN_CPU_CPUMASK: | 
|---|
| 1613 | return filter_pred_cpu_cpumask(pred, event); | 
|---|
| 1614 | case FILTER_PRED_FN_CPUMASK: | 
|---|
| 1615 | return filter_pred_cpumask(pred, event); | 
|---|
| 1616 | case FILTER_PRED_FN_CPUMASK_CPU: | 
|---|
| 1617 | return filter_pred_cpumask_cpu(pred, event); | 
|---|
| 1618 | case FILTER_PRED_FN_FUNCTION: | 
|---|
| 1619 | return filter_pred_function(pred, event); | 
|---|
| 1620 | case FILTER_PRED_TEST_VISITED: | 
|---|
| 1621 | return test_pred_visited_fn(pred, event); | 
|---|
| 1622 | default: | 
|---|
| 1623 | return 0; | 
|---|
| 1624 | } | 
|---|
| 1625 | } | 
|---|
| 1626 |  | 
|---|
| 1627 | /* Called when a predicate is encountered by predicate_parse() */ | 
|---|
| 1628 | static int parse_pred(const char *str, void *data, | 
|---|
| 1629 | int pos, struct filter_parse_error *pe, | 
|---|
| 1630 | struct filter_pred **pred_ptr) | 
|---|
| 1631 | { | 
|---|
| 1632 | struct trace_event_call *call = data; | 
|---|
| 1633 | struct ftrace_event_field *field; | 
|---|
| 1634 | struct filter_pred *pred = NULL; | 
|---|
| 1635 | unsigned long offset; | 
|---|
| 1636 | unsigned long size; | 
|---|
| 1637 | unsigned long ip; | 
|---|
| 1638 | char num_buf[24];	/* Big enough to hold an address */ | 
|---|
| 1639 | char *field_name; | 
|---|
| 1640 | char *name; | 
|---|
| 1641 | bool function = false; | 
|---|
| 1642 | bool ustring = false; | 
|---|
| 1643 | char q; | 
|---|
| 1644 | u64 val; | 
|---|
| 1645 | int len; | 
|---|
| 1646 | int ret; | 
|---|
| 1647 | int op; | 
|---|
| 1648 | int s; | 
|---|
| 1649 | int i = 0; | 
|---|
| 1650 |  | 
|---|
| 1651 | /* First find the field to associate to */ | 
|---|
| 1652 | while (isspace(str[i])) | 
|---|
| 1653 | i++; | 
|---|
| 1654 | s = i; | 
|---|
| 1655 |  | 
|---|
| 1656 | while (isalnum(str[i]) || str[i] == '_') | 
|---|
| 1657 | i++; | 
|---|
| 1658 |  | 
|---|
| 1659 | len = i - s; | 
|---|
| 1660 |  | 
|---|
| 1661 | if (!len) | 
|---|
| 1662 | return -1; | 
|---|
| 1663 |  | 
|---|
| 1664 | field_name = kmemdup_nul(s: str + s, len, GFP_KERNEL); | 
|---|
| 1665 | if (!field_name) | 
|---|
| 1666 | return -ENOMEM; | 
|---|
| 1667 |  | 
|---|
| 1668 | /* Make sure that the field exists */ | 
|---|
| 1669 |  | 
|---|
| 1670 | field = trace_find_event_field(call, name: field_name); | 
|---|
| 1671 | kfree(objp: field_name); | 
|---|
| 1672 | if (!field) { | 
|---|
| 1673 | parse_error(pe, err: FILT_ERR_FIELD_NOT_FOUND, pos: pos + i); | 
|---|
| 1674 | return -EINVAL; | 
|---|
| 1675 | } | 
|---|
| 1676 |  | 
|---|
| 1677 | /* See if the field is a user space string */ | 
|---|
| 1678 | if ((len = str_has_prefix(str: str + i, prefix: ".ustring"))) { | 
|---|
| 1679 | ustring = true; | 
|---|
| 1680 | i += len; | 
|---|
| 1681 | } | 
|---|
| 1682 |  | 
|---|
| 1683 | /* See if the field is a kernel function name */ | 
|---|
| 1684 | if ((len = str_has_prefix(str: str + i, prefix: ".function"))) { | 
|---|
| 1685 | function = true; | 
|---|
| 1686 | i += len; | 
|---|
| 1687 | } | 
|---|
| 1688 |  | 
|---|
| 1689 | while (isspace(str[i])) | 
|---|
| 1690 | i++; | 
|---|
| 1691 |  | 
|---|
| 1692 | /* Make sure this op is supported */ | 
|---|
| 1693 | for (op = 0; ops[op]; op++) { | 
|---|
| 1694 | /* This is why '<=' must come before '<' in ops[] */ | 
|---|
| 1695 | if (strncmp(str + i, ops[op], strlen(ops[op])) == 0) | 
|---|
| 1696 | break; | 
|---|
| 1697 | } | 
|---|
| 1698 |  | 
|---|
| 1699 | if (!ops[op]) { | 
|---|
| 1700 | parse_error(pe, err: FILT_ERR_INVALID_OP, pos: pos + i); | 
|---|
| 1701 | goto err_free; | 
|---|
| 1702 | } | 
|---|
| 1703 |  | 
|---|
| 1704 | i += strlen(ops[op]); | 
|---|
| 1705 |  | 
|---|
| 1706 | while (isspace(str[i])) | 
|---|
| 1707 | i++; | 
|---|
| 1708 |  | 
|---|
| 1709 | s = i; | 
|---|
| 1710 |  | 
|---|
| 1711 | pred = kzalloc(sizeof(*pred), GFP_KERNEL); | 
|---|
| 1712 | if (!pred) | 
|---|
| 1713 | return -ENOMEM; | 
|---|
| 1714 |  | 
|---|
| 1715 | pred->field = field; | 
|---|
| 1716 | pred->offset = field->offset; | 
|---|
| 1717 | pred->op = op; | 
|---|
| 1718 |  | 
|---|
| 1719 | if (function) { | 
|---|
| 1720 | /* The field must be the same size as long */ | 
|---|
| 1721 | if (field->size != sizeof(long)) { | 
|---|
| 1722 | parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i); | 
|---|
| 1723 | goto err_free; | 
|---|
| 1724 | } | 
|---|
| 1725 |  | 
|---|
| 1726 | /* Function only works with '==' or '!=' and an unquoted string */ | 
|---|
| 1727 | switch (op) { | 
|---|
| 1728 | case OP_NE: | 
|---|
| 1729 | case OP_EQ: | 
|---|
| 1730 | break; | 
|---|
| 1731 | default: | 
|---|
| 1732 | parse_error(pe, err: FILT_ERR_INVALID_OP, pos: pos + i); | 
|---|
| 1733 | goto err_free; | 
|---|
| 1734 | } | 
|---|
| 1735 |  | 
|---|
| 1736 | if (isdigit(c: str[i])) { | 
|---|
| 1737 | /* We allow 0xDEADBEEF */ | 
|---|
| 1738 | while (isalnum(str[i])) | 
|---|
| 1739 | i++; | 
|---|
| 1740 |  | 
|---|
| 1741 | len = i - s; | 
|---|
| 1742 | /* 0xfeedfacedeadbeef is 18 chars max */ | 
|---|
| 1743 | if (len >= sizeof(num_buf)) { | 
|---|
| 1744 | parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i); | 
|---|
| 1745 | goto err_free; | 
|---|
| 1746 | } | 
|---|
| 1747 |  | 
|---|
| 1748 | memcpy(to: num_buf, from: str + s, len); | 
|---|
| 1749 | num_buf[len] = 0; | 
|---|
| 1750 |  | 
|---|
| 1751 | ret = kstrtoul(s: num_buf, base: 0, res: &ip); | 
|---|
| 1752 | if (ret) { | 
|---|
| 1753 | parse_error(pe, err: FILT_ERR_INVALID_VALUE, pos: pos + i); | 
|---|
| 1754 | goto err_free; | 
|---|
| 1755 | } | 
|---|
| 1756 | } else { | 
|---|
| 1757 | s = i; | 
|---|
| 1758 | for (; str[i] && !isspace(str[i]); i++) | 
|---|
| 1759 | ; | 
|---|
| 1760 |  | 
|---|
| 1761 | len = i - s; | 
|---|
| 1762 | name = kmemdup_nul(s: str + s, len, GFP_KERNEL); | 
|---|
| 1763 | if (!name) | 
|---|
| 1764 | goto err_mem; | 
|---|
| 1765 | ip = kallsyms_lookup_name(name); | 
|---|
| 1766 | kfree(objp: name); | 
|---|
| 1767 | if (!ip) { | 
|---|
| 1768 | parse_error(pe, err: FILT_ERR_NO_FUNCTION, pos: pos + i); | 
|---|
| 1769 | goto err_free; | 
|---|
| 1770 | } | 
|---|
| 1771 | } | 
|---|
| 1772 |  | 
|---|
| 1773 | /* Now find the function start and end address */ | 
|---|
| 1774 | if (!kallsyms_lookup_size_offset(addr: ip, symbolsize: &size, offset: &offset)) { | 
|---|
| 1775 | parse_error(pe, err: FILT_ERR_NO_FUNCTION, pos: pos + i); | 
|---|
| 1776 | goto err_free; | 
|---|
| 1777 | } | 
|---|
| 1778 |  | 
|---|
| 1779 | pred->fn_num = FILTER_PRED_FN_FUNCTION; | 
|---|
| 1780 | pred->val = ip - offset; | 
|---|
| 1781 | pred->val2 = pred->val + size; | 
|---|
| 1782 |  | 
|---|
| 1783 | } else if (ftrace_event_is_function(call)) { | 
|---|
| 1784 | /* | 
|---|
| 1785 | * Perf does things different with function events. | 
|---|
| 1786 | * It only allows an "ip" field, and expects a string. | 
|---|
| 1787 | * But the string does not need to be surrounded by quotes. | 
|---|
| 1788 | * If it is a string, the assigned function as a nop, | 
|---|
| 1789 | * (perf doesn't use it) and grab everything. | 
|---|
| 1790 | */ | 
|---|
| 1791 | if (strcmp(field->name, "ip") != 0) { | 
|---|
| 1792 | parse_error(pe, err: FILT_ERR_IP_FIELD_ONLY, pos: pos + i); | 
|---|
| 1793 | goto err_free; | 
|---|
| 1794 | } | 
|---|
| 1795 | pred->fn_num = FILTER_PRED_FN_NOP; | 
|---|
| 1796 |  | 
|---|
| 1797 | /* | 
|---|
| 1798 | * Quotes are not required, but if they exist then we need | 
|---|
| 1799 | * to read them till we hit a matching one. | 
|---|
| 1800 | */ | 
|---|
| 1801 | if (str[i] == '\'' || str[i] == '"') | 
|---|
| 1802 | q = str[i]; | 
|---|
| 1803 | else | 
|---|
| 1804 | q = 0; | 
|---|
| 1805 |  | 
|---|
| 1806 | for (i++; str[i]; i++) { | 
|---|
| 1807 | if (q && str[i] == q) | 
|---|
| 1808 | break; | 
|---|
| 1809 | if (!q && (str[i] == ')' || str[i] == '&' || | 
|---|
| 1810 | str[i] == '|')) | 
|---|
| 1811 | break; | 
|---|
| 1812 | } | 
|---|
| 1813 | /* Skip quotes */ | 
|---|
| 1814 | if (q) | 
|---|
| 1815 | s++; | 
|---|
| 1816 | len = i - s; | 
|---|
| 1817 | if (len >= MAX_FILTER_STR_VAL) { | 
|---|
| 1818 | parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i); | 
|---|
| 1819 | goto err_free; | 
|---|
| 1820 | } | 
|---|
| 1821 |  | 
|---|
| 1822 | pred->regex = kzalloc(sizeof(*pred->regex), GFP_KERNEL); | 
|---|
| 1823 | if (!pred->regex) | 
|---|
| 1824 | goto err_mem; | 
|---|
| 1825 | pred->regex->len = len; | 
|---|
| 1826 | memcpy(to: pred->regex->pattern, from: str + s, len); | 
|---|
| 1827 | pred->regex->pattern[len] = 0; | 
|---|
| 1828 |  | 
|---|
| 1829 | } else if (!strncmp(str + i, "CPUS", 4)) { | 
|---|
| 1830 | unsigned int maskstart; | 
|---|
| 1831 | bool single; | 
|---|
| 1832 | char *tmp; | 
|---|
| 1833 |  | 
|---|
| 1834 | switch (field->filter_type) { | 
|---|
| 1835 | case FILTER_CPUMASK: | 
|---|
| 1836 | case FILTER_CPU: | 
|---|
| 1837 | case FILTER_OTHER: | 
|---|
| 1838 | break; | 
|---|
| 1839 | default: | 
|---|
| 1840 | parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i); | 
|---|
| 1841 | goto err_free; | 
|---|
| 1842 | } | 
|---|
| 1843 |  | 
|---|
| 1844 | switch (op) { | 
|---|
| 1845 | case OP_EQ: | 
|---|
| 1846 | case OP_NE: | 
|---|
| 1847 | case OP_BAND: | 
|---|
| 1848 | break; | 
|---|
| 1849 | default: | 
|---|
| 1850 | parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i); | 
|---|
| 1851 | goto err_free; | 
|---|
| 1852 | } | 
|---|
| 1853 |  | 
|---|
| 1854 | /* Skip CPUS */ | 
|---|
| 1855 | i += 4; | 
|---|
| 1856 | if (str[i++] != '{') { | 
|---|
| 1857 | parse_error(pe, err: FILT_ERR_MISSING_BRACE_OPEN, pos: pos + i); | 
|---|
| 1858 | goto err_free; | 
|---|
| 1859 | } | 
|---|
| 1860 | maskstart = i; | 
|---|
| 1861 |  | 
|---|
| 1862 | /* Walk the cpulist until closing } */ | 
|---|
| 1863 | for (; str[i] && str[i] != '}'; i++) | 
|---|
| 1864 | ; | 
|---|
| 1865 |  | 
|---|
| 1866 | if (str[i] != '}') { | 
|---|
| 1867 | parse_error(pe, err: FILT_ERR_MISSING_BRACE_CLOSE, pos: pos + i); | 
|---|
| 1868 | goto err_free; | 
|---|
| 1869 | } | 
|---|
| 1870 |  | 
|---|
| 1871 | if (maskstart == i) { | 
|---|
| 1872 | parse_error(pe, err: FILT_ERR_INVALID_CPULIST, pos: pos + i); | 
|---|
| 1873 | goto err_free; | 
|---|
| 1874 | } | 
|---|
| 1875 |  | 
|---|
| 1876 | /* Copy the cpulist between { and } */ | 
|---|
| 1877 | tmp = kmalloc((i - maskstart) + 1, GFP_KERNEL); | 
|---|
| 1878 | if (!tmp) | 
|---|
| 1879 | goto err_mem; | 
|---|
| 1880 |  | 
|---|
| 1881 | strscpy(tmp, str + maskstart, (i - maskstart) + 1); | 
|---|
| 1882 | pred->mask = kzalloc(cpumask_size(), GFP_KERNEL); | 
|---|
| 1883 | if (!pred->mask) { | 
|---|
| 1884 | kfree(objp: tmp); | 
|---|
| 1885 | goto err_mem; | 
|---|
| 1886 | } | 
|---|
| 1887 |  | 
|---|
| 1888 | /* Now parse it */ | 
|---|
| 1889 | if (cpulist_parse(buf: tmp, dstp: pred->mask)) { | 
|---|
| 1890 | kfree(objp: tmp); | 
|---|
| 1891 | parse_error(pe, err: FILT_ERR_INVALID_CPULIST, pos: pos + i); | 
|---|
| 1892 | goto err_free; | 
|---|
| 1893 | } | 
|---|
| 1894 | kfree(objp: tmp); | 
|---|
| 1895 |  | 
|---|
| 1896 | /* Move along */ | 
|---|
| 1897 | i++; | 
|---|
| 1898 |  | 
|---|
| 1899 | /* | 
|---|
| 1900 | * Optimisation: if the user-provided mask has a weight of one | 
|---|
| 1901 | * then we can treat it as a scalar input. | 
|---|
| 1902 | */ | 
|---|
| 1903 | single = cpumask_weight(srcp: pred->mask) == 1; | 
|---|
| 1904 | if (single) { | 
|---|
| 1905 | pred->val = cpumask_first(srcp: pred->mask); | 
|---|
| 1906 | kfree(objp: pred->mask); | 
|---|
| 1907 | pred->mask = NULL; | 
|---|
| 1908 | } | 
|---|
| 1909 |  | 
|---|
| 1910 | if (field->filter_type == FILTER_CPUMASK) { | 
|---|
| 1911 | pred->fn_num = single ? | 
|---|
| 1912 | FILTER_PRED_FN_CPUMASK_CPU : | 
|---|
| 1913 | FILTER_PRED_FN_CPUMASK; | 
|---|
| 1914 | } else if (field->filter_type == FILTER_CPU) { | 
|---|
| 1915 | if (single) { | 
|---|
| 1916 | if (pred->op == OP_BAND) | 
|---|
| 1917 | pred->op = OP_EQ; | 
|---|
| 1918 |  | 
|---|
| 1919 | pred->fn_num = FILTER_PRED_FN_CPU; | 
|---|
| 1920 | } else { | 
|---|
| 1921 | pred->fn_num = FILTER_PRED_FN_CPU_CPUMASK; | 
|---|
| 1922 | } | 
|---|
| 1923 | } else if (single) { | 
|---|
| 1924 | if (pred->op == OP_BAND) | 
|---|
| 1925 | pred->op = OP_EQ; | 
|---|
| 1926 |  | 
|---|
| 1927 | pred->fn_num = select_comparison_fn(op: pred->op, field_size: field->size, field_is_signed: false); | 
|---|
| 1928 | if (pred->op == OP_NE) | 
|---|
| 1929 | pred->not = 1; | 
|---|
| 1930 | } else { | 
|---|
| 1931 | switch (field->size) { | 
|---|
| 1932 | case 8: | 
|---|
| 1933 | pred->fn_num = FILTER_PRED_FN_64_CPUMASK; | 
|---|
| 1934 | break; | 
|---|
| 1935 | case 4: | 
|---|
| 1936 | pred->fn_num = FILTER_PRED_FN_32_CPUMASK; | 
|---|
| 1937 | break; | 
|---|
| 1938 | case 2: | 
|---|
| 1939 | pred->fn_num = FILTER_PRED_FN_16_CPUMASK; | 
|---|
| 1940 | break; | 
|---|
| 1941 | case 1: | 
|---|
| 1942 | pred->fn_num = FILTER_PRED_FN_8_CPUMASK; | 
|---|
| 1943 | break; | 
|---|
| 1944 | } | 
|---|
| 1945 | } | 
|---|
| 1946 |  | 
|---|
| 1947 | /* This is either a string, or an integer */ | 
|---|
| 1948 | } else if (str[i] == '\'' || str[i] == '"') { | 
|---|
| 1949 | char q = str[i]; | 
|---|
| 1950 |  | 
|---|
| 1951 | /* Make sure the op is OK for strings */ | 
|---|
| 1952 | switch (op) { | 
|---|
| 1953 | case OP_NE: | 
|---|
| 1954 | pred->not = 1; | 
|---|
| 1955 | fallthrough; | 
|---|
| 1956 | case OP_GLOB: | 
|---|
| 1957 | case OP_EQ: | 
|---|
| 1958 | break; | 
|---|
| 1959 | default: | 
|---|
| 1960 | parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i); | 
|---|
| 1961 | goto err_free; | 
|---|
| 1962 | } | 
|---|
| 1963 |  | 
|---|
| 1964 | /* Make sure the field is OK for strings */ | 
|---|
| 1965 | if (!is_string_field(field)) { | 
|---|
| 1966 | parse_error(pe, err: FILT_ERR_EXPECT_DIGIT, pos: pos + i); | 
|---|
| 1967 | goto err_free; | 
|---|
| 1968 | } | 
|---|
| 1969 |  | 
|---|
| 1970 | for (i++; str[i]; i++) { | 
|---|
| 1971 | if (str[i] == q) | 
|---|
| 1972 | break; | 
|---|
| 1973 | } | 
|---|
| 1974 | if (!str[i]) { | 
|---|
| 1975 | parse_error(pe, err: FILT_ERR_MISSING_QUOTE, pos: pos + i); | 
|---|
| 1976 | goto err_free; | 
|---|
| 1977 | } | 
|---|
| 1978 |  | 
|---|
| 1979 | /* Skip quotes */ | 
|---|
| 1980 | s++; | 
|---|
| 1981 | len = i - s; | 
|---|
| 1982 | if (len >= MAX_FILTER_STR_VAL) { | 
|---|
| 1983 | parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i); | 
|---|
| 1984 | goto err_free; | 
|---|
| 1985 | } | 
|---|
| 1986 |  | 
|---|
| 1987 | pred->regex = kzalloc(sizeof(*pred->regex), GFP_KERNEL); | 
|---|
| 1988 | if (!pred->regex) | 
|---|
| 1989 | goto err_mem; | 
|---|
| 1990 | pred->regex->len = len; | 
|---|
| 1991 | memcpy(to: pred->regex->pattern, from: str + s, len); | 
|---|
| 1992 | pred->regex->pattern[len] = 0; | 
|---|
| 1993 |  | 
|---|
| 1994 | filter_build_regex(pred); | 
|---|
| 1995 |  | 
|---|
| 1996 | if (field->filter_type == FILTER_COMM) { | 
|---|
| 1997 | pred->fn_num = FILTER_PRED_FN_COMM; | 
|---|
| 1998 |  | 
|---|
| 1999 | } else if (field->filter_type == FILTER_STATIC_STRING) { | 
|---|
| 2000 | pred->fn_num = FILTER_PRED_FN_STRING; | 
|---|
| 2001 | pred->regex->field_len = field->size; | 
|---|
| 2002 |  | 
|---|
| 2003 | } else if (field->filter_type == FILTER_DYN_STRING) { | 
|---|
| 2004 | pred->fn_num = FILTER_PRED_FN_STRLOC; | 
|---|
| 2005 | } else if (field->filter_type == FILTER_RDYN_STRING) | 
|---|
| 2006 | pred->fn_num = FILTER_PRED_FN_STRRELLOC; | 
|---|
| 2007 | else { | 
|---|
| 2008 |  | 
|---|
| 2009 | if (!ustring_per_cpu) { | 
|---|
| 2010 | /* Once allocated, keep it around for good */ | 
|---|
| 2011 | ustring_per_cpu = alloc_percpu(struct ustring_buffer); | 
|---|
| 2012 | if (!ustring_per_cpu) | 
|---|
| 2013 | goto err_mem; | 
|---|
| 2014 | } | 
|---|
| 2015 |  | 
|---|
| 2016 | if (ustring) | 
|---|
| 2017 | pred->fn_num = FILTER_PRED_FN_PCHAR_USER; | 
|---|
| 2018 | else | 
|---|
| 2019 | pred->fn_num = FILTER_PRED_FN_PCHAR; | 
|---|
| 2020 | } | 
|---|
| 2021 | /* go past the last quote */ | 
|---|
| 2022 | i++; | 
|---|
| 2023 |  | 
|---|
| 2024 | } else if (isdigit(c: str[i]) || str[i] == '-') { | 
|---|
| 2025 |  | 
|---|
| 2026 | /* Make sure the field is not a string */ | 
|---|
| 2027 | if (is_string_field(field)) { | 
|---|
| 2028 | parse_error(pe, err: FILT_ERR_EXPECT_STRING, pos: pos + i); | 
|---|
| 2029 | goto err_free; | 
|---|
| 2030 | } | 
|---|
| 2031 |  | 
|---|
| 2032 | if (op == OP_GLOB) { | 
|---|
| 2033 | parse_error(pe, err: FILT_ERR_ILLEGAL_FIELD_OP, pos: pos + i); | 
|---|
| 2034 | goto err_free; | 
|---|
| 2035 | } | 
|---|
| 2036 |  | 
|---|
| 2037 | if (str[i] == '-') | 
|---|
| 2038 | i++; | 
|---|
| 2039 |  | 
|---|
| 2040 | /* We allow 0xDEADBEEF */ | 
|---|
| 2041 | while (isalnum(str[i])) | 
|---|
| 2042 | i++; | 
|---|
| 2043 |  | 
|---|
| 2044 | len = i - s; | 
|---|
| 2045 | /* 0xfeedfacedeadbeef is 18 chars max */ | 
|---|
| 2046 | if (len >= sizeof(num_buf)) { | 
|---|
| 2047 | parse_error(pe, err: FILT_ERR_OPERAND_TOO_LONG, pos: pos + i); | 
|---|
| 2048 | goto err_free; | 
|---|
| 2049 | } | 
|---|
| 2050 |  | 
|---|
| 2051 | memcpy(to: num_buf, from: str + s, len); | 
|---|
| 2052 | num_buf[len] = 0; | 
|---|
| 2053 |  | 
|---|
| 2054 | /* Make sure it is a value */ | 
|---|
| 2055 | if (field->is_signed) | 
|---|
| 2056 | ret = kstrtoll(s: num_buf, base: 0, res: &val); | 
|---|
| 2057 | else | 
|---|
| 2058 | ret = kstrtoull(s: num_buf, base: 0, res: &val); | 
|---|
| 2059 | if (ret) { | 
|---|
| 2060 | parse_error(pe, err: FILT_ERR_ILLEGAL_INTVAL, pos: pos + s); | 
|---|
| 2061 | goto err_free; | 
|---|
| 2062 | } | 
|---|
| 2063 |  | 
|---|
| 2064 | pred->val = val; | 
|---|
| 2065 |  | 
|---|
| 2066 | if (field->filter_type == FILTER_CPU) | 
|---|
| 2067 | pred->fn_num = FILTER_PRED_FN_CPU; | 
|---|
| 2068 | else { | 
|---|
| 2069 | pred->fn_num = select_comparison_fn(op: pred->op, field_size: field->size, | 
|---|
| 2070 | field_is_signed: field->is_signed); | 
|---|
| 2071 | if (pred->op == OP_NE) | 
|---|
| 2072 | pred->not = 1; | 
|---|
| 2073 | } | 
|---|
| 2074 |  | 
|---|
| 2075 | } else { | 
|---|
| 2076 | parse_error(pe, err: FILT_ERR_INVALID_VALUE, pos: pos + i); | 
|---|
| 2077 | goto err_free; | 
|---|
| 2078 | } | 
|---|
| 2079 |  | 
|---|
| 2080 | *pred_ptr = pred; | 
|---|
| 2081 | return i; | 
|---|
| 2082 |  | 
|---|
| 2083 | err_free: | 
|---|
| 2084 | free_predicate(pred); | 
|---|
| 2085 | return -EINVAL; | 
|---|
| 2086 | err_mem: | 
|---|
| 2087 | free_predicate(pred); | 
|---|
| 2088 | return -ENOMEM; | 
|---|
| 2089 | } | 
|---|
| 2090 |  | 
|---|
| 2091 | enum { | 
|---|
| 2092 | TOO_MANY_CLOSE		= -1, | 
|---|
| 2093 | TOO_MANY_OPEN		= -2, | 
|---|
| 2094 | MISSING_QUOTE		= -3, | 
|---|
| 2095 | }; | 
|---|
| 2096 |  | 
|---|
| 2097 | /* | 
|---|
| 2098 | * Read the filter string once to calculate the number of predicates | 
|---|
| 2099 | * as well as how deep the parentheses go. | 
|---|
| 2100 | * | 
|---|
| 2101 | * Returns: | 
|---|
| 2102 | *   0 - everything is fine (err is undefined) | 
|---|
| 2103 | *  -1 - too many ')' | 
|---|
| 2104 | *  -2 - too many '(' | 
|---|
| 2105 | *  -3 - No matching quote | 
|---|
| 2106 | */ | 
|---|
| 2107 | static int calc_stack(const char *str, int *parens, int *preds, int *err) | 
|---|
| 2108 | { | 
|---|
| 2109 | bool is_pred = false; | 
|---|
| 2110 | int nr_preds = 0; | 
|---|
| 2111 | int open = 1; /* Count the expression as "(E)" */ | 
|---|
| 2112 | int last_quote = 0; | 
|---|
| 2113 | int max_open = 1; | 
|---|
| 2114 | int quote = 0; | 
|---|
| 2115 | int i; | 
|---|
| 2116 |  | 
|---|
| 2117 | *err = 0; | 
|---|
| 2118 |  | 
|---|
| 2119 | for (i = 0; str[i]; i++) { | 
|---|
| 2120 | if (isspace(str[i])) | 
|---|
| 2121 | continue; | 
|---|
| 2122 | if (quote) { | 
|---|
| 2123 | if (str[i] == quote) | 
|---|
| 2124 | quote = 0; | 
|---|
| 2125 | continue; | 
|---|
| 2126 | } | 
|---|
| 2127 |  | 
|---|
| 2128 | switch (str[i]) { | 
|---|
| 2129 | case '\'': | 
|---|
| 2130 | case '"': | 
|---|
| 2131 | quote = str[i]; | 
|---|
| 2132 | last_quote = i; | 
|---|
| 2133 | break; | 
|---|
| 2134 | case '|': | 
|---|
| 2135 | case '&': | 
|---|
| 2136 | if (str[i+1] != str[i]) | 
|---|
| 2137 | break; | 
|---|
| 2138 | is_pred = false; | 
|---|
| 2139 | continue; | 
|---|
| 2140 | case '(': | 
|---|
| 2141 | is_pred = false; | 
|---|
| 2142 | open++; | 
|---|
| 2143 | if (open > max_open) | 
|---|
| 2144 | max_open = open; | 
|---|
| 2145 | continue; | 
|---|
| 2146 | case ')': | 
|---|
| 2147 | is_pred = false; | 
|---|
| 2148 | if (open == 1) { | 
|---|
| 2149 | *err = i; | 
|---|
| 2150 | return TOO_MANY_CLOSE; | 
|---|
| 2151 | } | 
|---|
| 2152 | open--; | 
|---|
| 2153 | continue; | 
|---|
| 2154 | } | 
|---|
| 2155 | if (!is_pred) { | 
|---|
| 2156 | nr_preds++; | 
|---|
| 2157 | is_pred = true; | 
|---|
| 2158 | } | 
|---|
| 2159 | } | 
|---|
| 2160 |  | 
|---|
| 2161 | if (quote) { | 
|---|
| 2162 | *err = last_quote; | 
|---|
| 2163 | return MISSING_QUOTE; | 
|---|
| 2164 | } | 
|---|
| 2165 |  | 
|---|
| 2166 | if (open != 1) { | 
|---|
| 2167 | int level = open; | 
|---|
| 2168 |  | 
|---|
| 2169 | /* find the bad open */ | 
|---|
| 2170 | for (i--; i; i--) { | 
|---|
| 2171 | if (quote) { | 
|---|
| 2172 | if (str[i] == quote) | 
|---|
| 2173 | quote = 0; | 
|---|
| 2174 | continue; | 
|---|
| 2175 | } | 
|---|
| 2176 | switch (str[i]) { | 
|---|
| 2177 | case '(': | 
|---|
| 2178 | if (level == open) { | 
|---|
| 2179 | *err = i; | 
|---|
| 2180 | return TOO_MANY_OPEN; | 
|---|
| 2181 | } | 
|---|
| 2182 | level--; | 
|---|
| 2183 | break; | 
|---|
| 2184 | case ')': | 
|---|
| 2185 | level++; | 
|---|
| 2186 | break; | 
|---|
| 2187 | case '\'': | 
|---|
| 2188 | case '"': | 
|---|
| 2189 | quote = str[i]; | 
|---|
| 2190 | break; | 
|---|
| 2191 | } | 
|---|
| 2192 | } | 
|---|
| 2193 | /* First character is the '(' with missing ')' */ | 
|---|
| 2194 | *err = 0; | 
|---|
| 2195 | return TOO_MANY_OPEN; | 
|---|
| 2196 | } | 
|---|
| 2197 |  | 
|---|
| 2198 | /* Set the size of the required stacks */ | 
|---|
| 2199 | *parens = max_open; | 
|---|
| 2200 | *preds = nr_preds; | 
|---|
| 2201 | return 0; | 
|---|
| 2202 | } | 
|---|
| 2203 |  | 
|---|
| 2204 | static int process_preds(struct trace_event_call *call, | 
|---|
| 2205 | const char *filter_string, | 
|---|
| 2206 | struct event_filter *filter, | 
|---|
| 2207 | struct filter_parse_error *pe) | 
|---|
| 2208 | { | 
|---|
| 2209 | struct prog_entry *prog; | 
|---|
| 2210 | int nr_parens; | 
|---|
| 2211 | int nr_preds; | 
|---|
| 2212 | int index; | 
|---|
| 2213 | int ret; | 
|---|
| 2214 |  | 
|---|
| 2215 | ret = calc_stack(str: filter_string, parens: &nr_parens, preds: &nr_preds, err: &index); | 
|---|
| 2216 | if (ret < 0) { | 
|---|
| 2217 | switch (ret) { | 
|---|
| 2218 | case MISSING_QUOTE: | 
|---|
| 2219 | parse_error(pe, err: FILT_ERR_MISSING_QUOTE, pos: index); | 
|---|
| 2220 | break; | 
|---|
| 2221 | case TOO_MANY_OPEN: | 
|---|
| 2222 | parse_error(pe, err: FILT_ERR_TOO_MANY_OPEN, pos: index); | 
|---|
| 2223 | break; | 
|---|
| 2224 | default: | 
|---|
| 2225 | parse_error(pe, err: FILT_ERR_TOO_MANY_CLOSE, pos: index); | 
|---|
| 2226 | } | 
|---|
| 2227 | return ret; | 
|---|
| 2228 | } | 
|---|
| 2229 |  | 
|---|
| 2230 | if (!nr_preds) | 
|---|
| 2231 | return -EINVAL; | 
|---|
| 2232 |  | 
|---|
| 2233 | prog = predicate_parse(str: filter_string, nr_parens, nr_preds, | 
|---|
| 2234 | parse_pred, data: call, pe); | 
|---|
| 2235 | if (IS_ERR(ptr: prog)) | 
|---|
| 2236 | return PTR_ERR(ptr: prog); | 
|---|
| 2237 |  | 
|---|
| 2238 | rcu_assign_pointer(filter->prog, prog); | 
|---|
| 2239 | return 0; | 
|---|
| 2240 | } | 
|---|
| 2241 |  | 
|---|
| 2242 | static inline void event_set_filtered_flag(struct trace_event_file *file) | 
|---|
| 2243 | { | 
|---|
| 2244 | unsigned long old_flags = file->flags; | 
|---|
| 2245 |  | 
|---|
| 2246 | file->flags |= EVENT_FILE_FL_FILTERED; | 
|---|
| 2247 |  | 
|---|
| 2248 | if (old_flags != file->flags) | 
|---|
| 2249 | trace_buffered_event_enable(); | 
|---|
| 2250 | } | 
|---|
| 2251 |  | 
|---|
| 2252 | static int process_system_preds(struct trace_subsystem_dir *dir, | 
|---|
| 2253 | struct trace_array *tr, | 
|---|
| 2254 | struct filter_parse_error *pe, | 
|---|
| 2255 | char *filter_string) | 
|---|
| 2256 | { | 
|---|
| 2257 | struct trace_event_file *file; | 
|---|
| 2258 | struct filter_list *filter_item; | 
|---|
| 2259 | struct event_filter *filter = NULL; | 
|---|
| 2260 | struct filter_head *filter_list; | 
|---|
| 2261 | bool fail = true; | 
|---|
| 2262 | int err; | 
|---|
| 2263 |  | 
|---|
| 2264 | filter_list = kmalloc(sizeof(*filter_list), GFP_KERNEL); | 
|---|
| 2265 | if (!filter_list) | 
|---|
| 2266 | return -ENOMEM; | 
|---|
| 2267 |  | 
|---|
| 2268 | INIT_LIST_HEAD(list: &filter_list->list); | 
|---|
| 2269 |  | 
|---|
| 2270 | list_for_each_entry(file, &tr->events, list) { | 
|---|
| 2271 |  | 
|---|
| 2272 | if (file->system != dir) | 
|---|
| 2273 | continue; | 
|---|
| 2274 |  | 
|---|
| 2275 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); | 
|---|
| 2276 | if (!filter) | 
|---|
| 2277 | goto fail_mem; | 
|---|
| 2278 |  | 
|---|
| 2279 | filter->filter_string = kstrdup(s: filter_string, GFP_KERNEL); | 
|---|
| 2280 | if (!filter->filter_string) | 
|---|
| 2281 | goto fail_mem; | 
|---|
| 2282 |  | 
|---|
| 2283 | err = process_preds(call: file->event_call, filter_string, filter, pe); | 
|---|
| 2284 | if (err) { | 
|---|
| 2285 | filter_disable(file); | 
|---|
| 2286 | parse_error(pe, err: FILT_ERR_BAD_SUBSYS_FILTER, pos: 0); | 
|---|
| 2287 | append_filter_err(tr, pe, filter); | 
|---|
| 2288 | } else | 
|---|
| 2289 | event_set_filtered_flag(file); | 
|---|
| 2290 |  | 
|---|
| 2291 |  | 
|---|
| 2292 | filter_item = kzalloc(sizeof(*filter_item), GFP_KERNEL); | 
|---|
| 2293 | if (!filter_item) | 
|---|
| 2294 | goto fail_mem; | 
|---|
| 2295 |  | 
|---|
| 2296 | list_add_tail(new: &filter_item->list, head: &filter_list->list); | 
|---|
| 2297 | /* | 
|---|
| 2298 | * Regardless of if this returned an error, we still | 
|---|
| 2299 | * replace the filter for the call. | 
|---|
| 2300 | */ | 
|---|
| 2301 | filter_item->filter = event_filter(file); | 
|---|
| 2302 | event_set_filter(file, filter); | 
|---|
| 2303 | filter = NULL; | 
|---|
| 2304 |  | 
|---|
| 2305 | fail = false; | 
|---|
| 2306 | } | 
|---|
| 2307 |  | 
|---|
| 2308 | if (fail) | 
|---|
| 2309 | goto fail; | 
|---|
| 2310 |  | 
|---|
| 2311 | /* | 
|---|
| 2312 | * The calls can still be using the old filters. | 
|---|
| 2313 | * Do a synchronize_rcu() and to ensure all calls are | 
|---|
| 2314 | * done with them before we free them. | 
|---|
| 2315 | */ | 
|---|
| 2316 | delay_free_filter(head: filter_list); | 
|---|
| 2317 | return 0; | 
|---|
| 2318 | fail: | 
|---|
| 2319 | /* No call succeeded */ | 
|---|
| 2320 | free_filter_list(filter_list); | 
|---|
| 2321 | parse_error(pe, err: FILT_ERR_BAD_SUBSYS_FILTER, pos: 0); | 
|---|
| 2322 | return -EINVAL; | 
|---|
| 2323 | fail_mem: | 
|---|
| 2324 | __free_filter(filter); | 
|---|
| 2325 |  | 
|---|
| 2326 | /* If any call succeeded, we still need to sync */ | 
|---|
| 2327 | if (!fail) | 
|---|
| 2328 | delay_free_filter(head: filter_list); | 
|---|
| 2329 | else | 
|---|
| 2330 | free_filter_list(filter_list); | 
|---|
| 2331 |  | 
|---|
| 2332 | return -ENOMEM; | 
|---|
| 2333 | } | 
|---|
| 2334 |  | 
|---|
| 2335 | static int create_filter_start(char *filter_string, bool set_str, | 
|---|
| 2336 | struct filter_parse_error **pse, | 
|---|
| 2337 | struct event_filter **filterp) | 
|---|
| 2338 | { | 
|---|
| 2339 | struct event_filter *filter; | 
|---|
| 2340 | struct filter_parse_error *pe = NULL; | 
|---|
| 2341 | int err = 0; | 
|---|
| 2342 |  | 
|---|
| 2343 | if (WARN_ON_ONCE(*pse || *filterp)) | 
|---|
| 2344 | return -EINVAL; | 
|---|
| 2345 |  | 
|---|
| 2346 | filter = kzalloc(sizeof(*filter), GFP_KERNEL); | 
|---|
| 2347 | if (filter && set_str) { | 
|---|
| 2348 | filter->filter_string = kstrdup(s: filter_string, GFP_KERNEL); | 
|---|
| 2349 | if (!filter->filter_string) | 
|---|
| 2350 | err = -ENOMEM; | 
|---|
| 2351 | } | 
|---|
| 2352 |  | 
|---|
| 2353 | pe = kzalloc(sizeof(*pe), GFP_KERNEL); | 
|---|
| 2354 |  | 
|---|
| 2355 | if (!filter || !pe || err) { | 
|---|
| 2356 | kfree(objp: pe); | 
|---|
| 2357 | __free_filter(filter); | 
|---|
| 2358 | return -ENOMEM; | 
|---|
| 2359 | } | 
|---|
| 2360 |  | 
|---|
| 2361 | /* we're committed to creating a new filter */ | 
|---|
| 2362 | *filterp = filter; | 
|---|
| 2363 | *pse = pe; | 
|---|
| 2364 |  | 
|---|
| 2365 | return 0; | 
|---|
| 2366 | } | 
|---|
| 2367 |  | 
|---|
| 2368 | static void create_filter_finish(struct filter_parse_error *pe) | 
|---|
| 2369 | { | 
|---|
| 2370 | kfree(objp: pe); | 
|---|
| 2371 | } | 
|---|
| 2372 |  | 
|---|
| 2373 | /** | 
|---|
| 2374 | * create_filter - create a filter for a trace_event_call | 
|---|
| 2375 | * @tr: the trace array associated with these events | 
|---|
| 2376 | * @call: trace_event_call to create a filter for | 
|---|
| 2377 | * @filter_string: filter string | 
|---|
| 2378 | * @set_str: remember @filter_str and enable detailed error in filter | 
|---|
| 2379 | * @filterp: out param for created filter (always updated on return) | 
|---|
| 2380 | *           Must be a pointer that references a NULL pointer. | 
|---|
| 2381 | * | 
|---|
| 2382 | * Creates a filter for @call with @filter_str.  If @set_str is %true, | 
|---|
| 2383 | * @filter_str is copied and recorded in the new filter. | 
|---|
| 2384 | * | 
|---|
| 2385 | * On success, returns 0 and *@filterp points to the new filter.  On | 
|---|
| 2386 | * failure, returns -errno and *@filterp may point to %NULL or to a new | 
|---|
| 2387 | * filter.  In the latter case, the returned filter contains error | 
|---|
| 2388 | * information if @set_str is %true and the caller is responsible for | 
|---|
| 2389 | * freeing it. | 
|---|
| 2390 | */ | 
|---|
| 2391 | static int create_filter(struct trace_array *tr, | 
|---|
| 2392 | struct trace_event_call *call, | 
|---|
| 2393 | char *filter_string, bool set_str, | 
|---|
| 2394 | struct event_filter **filterp) | 
|---|
| 2395 | { | 
|---|
| 2396 | struct filter_parse_error *pe = NULL; | 
|---|
| 2397 | int err; | 
|---|
| 2398 |  | 
|---|
| 2399 | /* filterp must point to NULL */ | 
|---|
| 2400 | if (WARN_ON(*filterp)) | 
|---|
| 2401 | *filterp = NULL; | 
|---|
| 2402 |  | 
|---|
| 2403 | err = create_filter_start(filter_string, set_str, pse: &pe, filterp); | 
|---|
| 2404 | if (err) | 
|---|
| 2405 | return err; | 
|---|
| 2406 |  | 
|---|
| 2407 | err = process_preds(call, filter_string, filter: *filterp, pe); | 
|---|
| 2408 | if (err && set_str) | 
|---|
| 2409 | append_filter_err(tr, pe, filter: *filterp); | 
|---|
| 2410 | create_filter_finish(pe); | 
|---|
| 2411 |  | 
|---|
| 2412 | return err; | 
|---|
| 2413 | } | 
|---|
| 2414 |  | 
|---|
| 2415 | int create_event_filter(struct trace_array *tr, | 
|---|
| 2416 | struct trace_event_call *call, | 
|---|
| 2417 | char *filter_str, bool set_str, | 
|---|
| 2418 | struct event_filter **filterp) | 
|---|
| 2419 | { | 
|---|
| 2420 | return create_filter(tr, call, filter_string: filter_str, set_str, filterp); | 
|---|
| 2421 | } | 
|---|
| 2422 |  | 
|---|
| 2423 | /** | 
|---|
| 2424 | * create_system_filter - create a filter for an event subsystem | 
|---|
| 2425 | * @dir: the descriptor for the subsystem directory | 
|---|
| 2426 | * @filter_str: filter string | 
|---|
| 2427 | * @filterp: out param for created filter (always updated on return) | 
|---|
| 2428 | * | 
|---|
| 2429 | * Identical to create_filter() except that it creates a subsystem filter | 
|---|
| 2430 | * and always remembers @filter_str. | 
|---|
| 2431 | */ | 
|---|
| 2432 | static int create_system_filter(struct trace_subsystem_dir *dir, | 
|---|
| 2433 | char *filter_str, struct event_filter **filterp) | 
|---|
| 2434 | { | 
|---|
| 2435 | struct filter_parse_error *pe = NULL; | 
|---|
| 2436 | int err; | 
|---|
| 2437 |  | 
|---|
| 2438 | err = create_filter_start(filter_string: filter_str, set_str: true, pse: &pe, filterp); | 
|---|
| 2439 | if (!err) { | 
|---|
| 2440 | err = process_system_preds(dir, tr: dir->tr, pe, filter_string: filter_str); | 
|---|
| 2441 | if (!err) { | 
|---|
| 2442 | /* System filters just show a default message */ | 
|---|
| 2443 | kfree(objp: (*filterp)->filter_string); | 
|---|
| 2444 | (*filterp)->filter_string = NULL; | 
|---|
| 2445 | } else { | 
|---|
| 2446 | append_filter_err(tr: dir->tr, pe, filter: *filterp); | 
|---|
| 2447 | } | 
|---|
| 2448 | } | 
|---|
| 2449 | create_filter_finish(pe); | 
|---|
| 2450 |  | 
|---|
| 2451 | return err; | 
|---|
| 2452 | } | 
|---|
| 2453 |  | 
|---|
| 2454 | /* caller must hold event_mutex */ | 
|---|
| 2455 | int apply_event_filter(struct trace_event_file *file, char *filter_string) | 
|---|
| 2456 | { | 
|---|
| 2457 | struct trace_event_call *call = file->event_call; | 
|---|
| 2458 | struct event_filter *filter = NULL; | 
|---|
| 2459 | int err; | 
|---|
| 2460 |  | 
|---|
| 2461 | if (file->flags & EVENT_FILE_FL_FREED) | 
|---|
| 2462 | return -ENODEV; | 
|---|
| 2463 |  | 
|---|
| 2464 | if (!strcmp(strstrip(str: filter_string), "0")) { | 
|---|
| 2465 | filter_disable(file); | 
|---|
| 2466 | filter = event_filter(file); | 
|---|
| 2467 |  | 
|---|
| 2468 | if (!filter) | 
|---|
| 2469 | return 0; | 
|---|
| 2470 |  | 
|---|
| 2471 | event_clear_filter(file); | 
|---|
| 2472 |  | 
|---|
| 2473 | try_delay_free_filter(filter); | 
|---|
| 2474 |  | 
|---|
| 2475 | return 0; | 
|---|
| 2476 | } | 
|---|
| 2477 |  | 
|---|
| 2478 | err = create_filter(tr: file->tr, call, filter_string, set_str: true, filterp: &filter); | 
|---|
| 2479 |  | 
|---|
| 2480 | /* | 
|---|
| 2481 | * Always swap the call filter with the new filter | 
|---|
| 2482 | * even if there was an error. If there was an error | 
|---|
| 2483 | * in the filter, we disable the filter and show the error | 
|---|
| 2484 | * string | 
|---|
| 2485 | */ | 
|---|
| 2486 | if (filter) { | 
|---|
| 2487 | struct event_filter *tmp; | 
|---|
| 2488 |  | 
|---|
| 2489 | tmp = event_filter(file); | 
|---|
| 2490 | if (!err) | 
|---|
| 2491 | event_set_filtered_flag(file); | 
|---|
| 2492 | else | 
|---|
| 2493 | filter_disable(file); | 
|---|
| 2494 |  | 
|---|
| 2495 | event_set_filter(file, filter); | 
|---|
| 2496 |  | 
|---|
| 2497 | if (tmp) | 
|---|
| 2498 | try_delay_free_filter(filter: tmp); | 
|---|
| 2499 | } | 
|---|
| 2500 |  | 
|---|
| 2501 | return err; | 
|---|
| 2502 | } | 
|---|
| 2503 |  | 
|---|
| 2504 | int apply_subsystem_event_filter(struct trace_subsystem_dir *dir, | 
|---|
| 2505 | char *filter_string) | 
|---|
| 2506 | { | 
|---|
| 2507 | struct event_subsystem *system = dir->subsystem; | 
|---|
| 2508 | struct trace_array *tr = dir->tr; | 
|---|
| 2509 | struct event_filter *filter = NULL; | 
|---|
| 2510 | int err = 0; | 
|---|
| 2511 |  | 
|---|
| 2512 | guard(mutex)(T: &event_mutex); | 
|---|
| 2513 |  | 
|---|
| 2514 | /* Make sure the system still has events */ | 
|---|
| 2515 | if (!dir->nr_events) | 
|---|
| 2516 | return -ENODEV; | 
|---|
| 2517 |  | 
|---|
| 2518 | if (!strcmp(strstrip(str: filter_string), "0")) { | 
|---|
| 2519 | filter_free_subsystem_preds(dir, tr); | 
|---|
| 2520 | remove_filter_string(filter: system->filter); | 
|---|
| 2521 | filter = system->filter; | 
|---|
| 2522 | system->filter = NULL; | 
|---|
| 2523 | /* Ensure all filters are no longer used */ | 
|---|
| 2524 | filter_free_subsystem_filters(dir, tr, filter); | 
|---|
| 2525 | return 0; | 
|---|
| 2526 | } | 
|---|
| 2527 |  | 
|---|
| 2528 | err = create_system_filter(dir, filter_str: filter_string, filterp: &filter); | 
|---|
| 2529 | if (filter) { | 
|---|
| 2530 | /* | 
|---|
| 2531 | * No event actually uses the system filter | 
|---|
| 2532 | * we can free it without synchronize_rcu(). | 
|---|
| 2533 | */ | 
|---|
| 2534 | __free_filter(filter: system->filter); | 
|---|
| 2535 | system->filter = filter; | 
|---|
| 2536 | } | 
|---|
| 2537 |  | 
|---|
| 2538 | return err; | 
|---|
| 2539 | } | 
|---|
| 2540 |  | 
|---|
| 2541 | #ifdef CONFIG_PERF_EVENTS | 
|---|
| 2542 |  | 
|---|
| 2543 | void ftrace_profile_free_filter(struct perf_event *event) | 
|---|
| 2544 | { | 
|---|
| 2545 | struct event_filter *filter = event->filter; | 
|---|
| 2546 |  | 
|---|
| 2547 | event->filter = NULL; | 
|---|
| 2548 | __free_filter(filter); | 
|---|
| 2549 | } | 
|---|
| 2550 |  | 
|---|
| 2551 | struct function_filter_data { | 
|---|
| 2552 | struct ftrace_ops *ops; | 
|---|
| 2553 | int first_filter; | 
|---|
| 2554 | int first_notrace; | 
|---|
| 2555 | }; | 
|---|
| 2556 |  | 
|---|
| 2557 | #ifdef CONFIG_FUNCTION_TRACER | 
|---|
| 2558 | static char ** | 
|---|
| 2559 | ftrace_function_filter_re(char *buf, int len, int *count) | 
|---|
| 2560 | { | 
|---|
| 2561 | char *str, **re; | 
|---|
| 2562 |  | 
|---|
| 2563 | str = kstrndup(buf, len, GFP_KERNEL); | 
|---|
| 2564 | if (!str) | 
|---|
| 2565 | return NULL; | 
|---|
| 2566 |  | 
|---|
| 2567 | /* | 
|---|
| 2568 | * The argv_split function takes white space | 
|---|
| 2569 | * as a separator, so convert ',' into spaces. | 
|---|
| 2570 | */ | 
|---|
| 2571 | strreplace(str, ',', ' '); | 
|---|
| 2572 |  | 
|---|
| 2573 | re = argv_split(GFP_KERNEL, str, count); | 
|---|
| 2574 | kfree(str); | 
|---|
| 2575 | return re; | 
|---|
| 2576 | } | 
|---|
| 2577 |  | 
|---|
| 2578 | static int ftrace_function_set_regexp(struct ftrace_ops *ops, int filter, | 
|---|
| 2579 | int reset, char *re, int len) | 
|---|
| 2580 | { | 
|---|
| 2581 | int ret; | 
|---|
| 2582 |  | 
|---|
| 2583 | if (filter) | 
|---|
| 2584 | ret = ftrace_set_filter(ops, re, len, reset); | 
|---|
| 2585 | else | 
|---|
| 2586 | ret = ftrace_set_notrace(ops, re, len, reset); | 
|---|
| 2587 |  | 
|---|
| 2588 | return ret; | 
|---|
| 2589 | } | 
|---|
| 2590 |  | 
|---|
| 2591 | static int __ftrace_function_set_filter(int filter, char *buf, int len, | 
|---|
| 2592 | struct function_filter_data *data) | 
|---|
| 2593 | { | 
|---|
| 2594 | int i, re_cnt, ret = -EINVAL; | 
|---|
| 2595 | int *reset; | 
|---|
| 2596 | char **re; | 
|---|
| 2597 |  | 
|---|
| 2598 | reset = filter ? &data->first_filter : &data->first_notrace; | 
|---|
| 2599 |  | 
|---|
| 2600 | /* | 
|---|
| 2601 | * The 'ip' field could have multiple filters set, separated | 
|---|
| 2602 | * either by space or comma. We first cut the filter and apply | 
|---|
| 2603 | * all pieces separately. | 
|---|
| 2604 | */ | 
|---|
| 2605 | re = ftrace_function_filter_re(buf, len, &re_cnt); | 
|---|
| 2606 | if (!re) | 
|---|
| 2607 | return -EINVAL; | 
|---|
| 2608 |  | 
|---|
| 2609 | for (i = 0; i < re_cnt; i++) { | 
|---|
| 2610 | ret = ftrace_function_set_regexp(data->ops, filter, *reset, | 
|---|
| 2611 | re[i], strlen(re[i])); | 
|---|
| 2612 | if (ret) | 
|---|
| 2613 | break; | 
|---|
| 2614 |  | 
|---|
| 2615 | if (*reset) | 
|---|
| 2616 | *reset = 0; | 
|---|
| 2617 | } | 
|---|
| 2618 |  | 
|---|
| 2619 | argv_free(re); | 
|---|
| 2620 | return ret; | 
|---|
| 2621 | } | 
|---|
| 2622 |  | 
|---|
| 2623 | static int ftrace_function_check_pred(struct filter_pred *pred) | 
|---|
| 2624 | { | 
|---|
| 2625 | struct ftrace_event_field *field = pred->field; | 
|---|
| 2626 |  | 
|---|
| 2627 | /* | 
|---|
| 2628 | * Check the predicate for function trace, verify: | 
|---|
| 2629 | *  - only '==' and '!=' is used | 
|---|
| 2630 | *  - the 'ip' field is used | 
|---|
| 2631 | */ | 
|---|
| 2632 | if ((pred->op != OP_EQ) && (pred->op != OP_NE)) | 
|---|
| 2633 | return -EINVAL; | 
|---|
| 2634 |  | 
|---|
| 2635 | if (strcmp(field->name, "ip")) | 
|---|
| 2636 | return -EINVAL; | 
|---|
| 2637 |  | 
|---|
| 2638 | return 0; | 
|---|
| 2639 | } | 
|---|
| 2640 |  | 
|---|
| 2641 | static int ftrace_function_set_filter_pred(struct filter_pred *pred, | 
|---|
| 2642 | struct function_filter_data *data) | 
|---|
| 2643 | { | 
|---|
| 2644 | int ret; | 
|---|
| 2645 |  | 
|---|
| 2646 | /* Checking the node is valid for function trace. */ | 
|---|
| 2647 | ret = ftrace_function_check_pred(pred); | 
|---|
| 2648 | if (ret) | 
|---|
| 2649 | return ret; | 
|---|
| 2650 |  | 
|---|
| 2651 | return __ftrace_function_set_filter(pred->op == OP_EQ, | 
|---|
| 2652 | pred->regex->pattern, | 
|---|
| 2653 | pred->regex->len, | 
|---|
| 2654 | data); | 
|---|
| 2655 | } | 
|---|
| 2656 |  | 
|---|
| 2657 | static bool is_or(struct prog_entry *prog, int i) | 
|---|
| 2658 | { | 
|---|
| 2659 | int target; | 
|---|
| 2660 |  | 
|---|
| 2661 | /* | 
|---|
| 2662 | * Only "||" is allowed for function events, thus, | 
|---|
| 2663 | * all true branches should jump to true, and any | 
|---|
| 2664 | * false branch should jump to false. | 
|---|
| 2665 | */ | 
|---|
| 2666 | target = prog[i].target + 1; | 
|---|
| 2667 | /* True and false have NULL preds (all prog entries should jump to one */ | 
|---|
| 2668 | if (prog[target].pred) | 
|---|
| 2669 | return false; | 
|---|
| 2670 |  | 
|---|
| 2671 | /* prog[target].target is 1 for TRUE, 0 for FALSE */ | 
|---|
| 2672 | return prog[i].when_to_branch == prog[target].target; | 
|---|
| 2673 | } | 
|---|
| 2674 |  | 
|---|
| 2675 | static int ftrace_function_set_filter(struct perf_event *event, | 
|---|
| 2676 | struct event_filter *filter) | 
|---|
| 2677 | { | 
|---|
| 2678 | struct prog_entry *prog = rcu_dereference_protected(filter->prog, | 
|---|
| 2679 | lockdep_is_held(&event_mutex)); | 
|---|
| 2680 | struct function_filter_data data = { | 
|---|
| 2681 | .first_filter  = 1, | 
|---|
| 2682 | .first_notrace = 1, | 
|---|
| 2683 | .ops           = &event->ftrace_ops, | 
|---|
| 2684 | }; | 
|---|
| 2685 | int i; | 
|---|
| 2686 |  | 
|---|
| 2687 | for (i = 0; prog[i].pred; i++) { | 
|---|
| 2688 | struct filter_pred *pred = prog[i].pred; | 
|---|
| 2689 |  | 
|---|
| 2690 | if (!is_or(prog, i)) | 
|---|
| 2691 | return -EINVAL; | 
|---|
| 2692 |  | 
|---|
| 2693 | if (ftrace_function_set_filter_pred(pred, &data) < 0) | 
|---|
| 2694 | return -EINVAL; | 
|---|
| 2695 | } | 
|---|
| 2696 | return 0; | 
|---|
| 2697 | } | 
|---|
| 2698 | #else | 
|---|
| 2699 | static int ftrace_function_set_filter(struct perf_event *event, | 
|---|
| 2700 | struct event_filter *filter) | 
|---|
| 2701 | { | 
|---|
| 2702 | return -ENODEV; | 
|---|
| 2703 | } | 
|---|
| 2704 | #endif /* CONFIG_FUNCTION_TRACER */ | 
|---|
| 2705 |  | 
|---|
| 2706 | int ftrace_profile_set_filter(struct perf_event *event, int event_id, | 
|---|
| 2707 | char *filter_str) | 
|---|
| 2708 | { | 
|---|
| 2709 | int err; | 
|---|
| 2710 | struct event_filter *filter = NULL; | 
|---|
| 2711 | struct trace_event_call *call; | 
|---|
| 2712 |  | 
|---|
| 2713 | guard(mutex)(T: &event_mutex); | 
|---|
| 2714 |  | 
|---|
| 2715 | call = event->tp_event; | 
|---|
| 2716 |  | 
|---|
| 2717 | if (!call) | 
|---|
| 2718 | return -EINVAL; | 
|---|
| 2719 |  | 
|---|
| 2720 | if (event->filter) | 
|---|
| 2721 | return -EEXIST; | 
|---|
| 2722 |  | 
|---|
| 2723 | err = create_filter(NULL, call, filter_string: filter_str, set_str: false, filterp: &filter); | 
|---|
| 2724 | if (err) | 
|---|
| 2725 | goto free_filter; | 
|---|
| 2726 |  | 
|---|
| 2727 | if (ftrace_event_is_function(call)) | 
|---|
| 2728 | err = ftrace_function_set_filter(event, filter); | 
|---|
| 2729 | else | 
|---|
| 2730 | event->filter = filter; | 
|---|
| 2731 |  | 
|---|
| 2732 | free_filter: | 
|---|
| 2733 | if (err || ftrace_event_is_function(call)) | 
|---|
| 2734 | __free_filter(filter); | 
|---|
| 2735 |  | 
|---|
| 2736 | return err; | 
|---|
| 2737 | } | 
|---|
| 2738 |  | 
|---|
| 2739 | #endif /* CONFIG_PERF_EVENTS */ | 
|---|
| 2740 |  | 
|---|
| 2741 | #ifdef CONFIG_FTRACE_STARTUP_TEST | 
|---|
| 2742 |  | 
|---|
| 2743 | #include <linux/types.h> | 
|---|
| 2744 | #include <linux/tracepoint.h> | 
|---|
| 2745 |  | 
|---|
| 2746 | #define CREATE_TRACE_POINTS | 
|---|
| 2747 | #include "trace_events_filter_test.h" | 
|---|
| 2748 |  | 
|---|
| 2749 | #define DATA_REC(m, va, vb, vc, vd, ve, vf, vg, vh, nvisit) \ | 
|---|
| 2750 | { \ | 
|---|
| 2751 | .filter = FILTER, \ | 
|---|
| 2752 | .rec    = { .a = va, .b = vb, .c = vc, .d = vd, \ | 
|---|
| 2753 | .e = ve, .f = vf, .g = vg, .h = vh }, \ | 
|---|
| 2754 | .match  = m, \ | 
|---|
| 2755 | .not_visited = nvisit, \ | 
|---|
| 2756 | } | 
|---|
| 2757 | #define YES 1 | 
|---|
| 2758 | #define NO  0 | 
|---|
| 2759 |  | 
|---|
| 2760 | static struct test_filter_data_t { | 
|---|
| 2761 | char *filter; | 
|---|
| 2762 | struct trace_event_raw_ftrace_test_filter rec; | 
|---|
| 2763 | int match; | 
|---|
| 2764 | char *not_visited; | 
|---|
| 2765 | } test_filter_data[] = { | 
|---|
| 2766 | #define FILTER "a == 1 && b == 1 && c == 1 && d == 1 && " \ | 
|---|
| 2767 | "e == 1 && f == 1 && g == 1 && h == 1" | 
|---|
| 2768 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, ""), | 
|---|
| 2769 | DATA_REC(NO,  0, 1, 1, 1, 1, 1, 1, 1, "bcdefgh"), | 
|---|
| 2770 | DATA_REC(NO,  1, 1, 1, 1, 1, 1, 1, 0, ""), | 
|---|
| 2771 | #undef FILTER | 
|---|
| 2772 | #define FILTER "a == 1 || b == 1 || c == 1 || d == 1 || " \ | 
|---|
| 2773 | "e == 1 || f == 1 || g == 1 || h == 1" | 
|---|
| 2774 | DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""), | 
|---|
| 2775 | DATA_REC(YES, 0, 0, 0, 0, 0, 0, 0, 1, ""), | 
|---|
| 2776 | DATA_REC(YES, 1, 0, 0, 0, 0, 0, 0, 0, "bcdefgh"), | 
|---|
| 2777 | #undef FILTER | 
|---|
| 2778 | #define FILTER "(a == 1 || b == 1) && (c == 1 || d == 1) && " \ | 
|---|
| 2779 | "(e == 1 || f == 1) && (g == 1 || h == 1)" | 
|---|
| 2780 | DATA_REC(NO,  0, 0, 1, 1, 1, 1, 1, 1, "dfh"), | 
|---|
| 2781 | DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), | 
|---|
| 2782 | DATA_REC(YES, 1, 0, 1, 0, 0, 1, 0, 1, "bd"), | 
|---|
| 2783 | DATA_REC(NO,  1, 0, 1, 0, 0, 1, 0, 0, "bd"), | 
|---|
| 2784 | #undef FILTER | 
|---|
| 2785 | #define FILTER "(a == 1 && b == 1) || (c == 1 && d == 1) || " \ | 
|---|
| 2786 | "(e == 1 && f == 1) || (g == 1 && h == 1)" | 
|---|
| 2787 | DATA_REC(YES, 1, 0, 1, 1, 1, 1, 1, 1, "efgh"), | 
|---|
| 2788 | DATA_REC(YES, 0, 0, 0, 0, 0, 0, 1, 1, ""), | 
|---|
| 2789 | DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""), | 
|---|
| 2790 | #undef FILTER | 
|---|
| 2791 | #define FILTER "(a == 1 && b == 1) && (c == 1 && d == 1) && " \ | 
|---|
| 2792 | "(e == 1 && f == 1) || (g == 1 && h == 1)" | 
|---|
| 2793 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 0, "gh"), | 
|---|
| 2794 | DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 1, ""), | 
|---|
| 2795 | DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, ""), | 
|---|
| 2796 | #undef FILTER | 
|---|
| 2797 | #define FILTER "((a == 1 || b == 1) || (c == 1 || d == 1) || " \ | 
|---|
| 2798 | "(e == 1 || f == 1)) && (g == 1 || h == 1)" | 
|---|
| 2799 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 0, 1, "bcdef"), | 
|---|
| 2800 | DATA_REC(NO,  0, 0, 0, 0, 0, 0, 0, 0, ""), | 
|---|
| 2801 | DATA_REC(YES, 1, 1, 1, 1, 1, 0, 1, 1, "h"), | 
|---|
| 2802 | #undef FILTER | 
|---|
| 2803 | #define FILTER "((((((((a == 1) && (b == 1)) || (c == 1)) && (d == 1)) || " \ | 
|---|
| 2804 | "(e == 1)) && (f == 1)) || (g == 1)) && (h == 1))" | 
|---|
| 2805 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "ceg"), | 
|---|
| 2806 | DATA_REC(NO,  0, 1, 0, 1, 0, 1, 0, 1, ""), | 
|---|
| 2807 | DATA_REC(NO,  1, 0, 1, 0, 1, 0, 1, 0, ""), | 
|---|
| 2808 | #undef FILTER | 
|---|
| 2809 | #define FILTER "((((((((a == 1) || (b == 1)) && (c == 1)) || (d == 1)) && " \ | 
|---|
| 2810 | "(e == 1)) || (f == 1)) && (g == 1)) || (h == 1))" | 
|---|
| 2811 | DATA_REC(YES, 1, 1, 1, 1, 1, 1, 1, 1, "bdfh"), | 
|---|
| 2812 | DATA_REC(YES, 0, 1, 0, 1, 0, 1, 0, 1, ""), | 
|---|
| 2813 | DATA_REC(YES, 1, 0, 1, 0, 1, 0, 1, 0, "bdfh"), | 
|---|
| 2814 | }; | 
|---|
| 2815 |  | 
|---|
| 2816 | #undef DATA_REC | 
|---|
| 2817 | #undef FILTER | 
|---|
| 2818 | #undef YES | 
|---|
| 2819 | #undef NO | 
|---|
| 2820 |  | 
|---|
| 2821 | #define DATA_CNT ARRAY_SIZE(test_filter_data) | 
|---|
| 2822 |  | 
|---|
| 2823 | static int test_pred_visited; | 
|---|
| 2824 |  | 
|---|
| 2825 | static int test_pred_visited_fn(struct filter_pred *pred, void *event) | 
|---|
| 2826 | { | 
|---|
| 2827 | struct ftrace_event_field *field = pred->field; | 
|---|
| 2828 |  | 
|---|
| 2829 | test_pred_visited = 1; | 
|---|
| 2830 | printk(KERN_INFO "\npred visited %s\n", field->name); | 
|---|
| 2831 | return 1; | 
|---|
| 2832 | } | 
|---|
| 2833 |  | 
|---|
| 2834 | static void update_pred_fn(struct event_filter *filter, char *fields) | 
|---|
| 2835 | { | 
|---|
| 2836 | struct prog_entry *prog = rcu_dereference_protected(filter->prog, | 
|---|
| 2837 | lockdep_is_held(&event_mutex)); | 
|---|
| 2838 | int i; | 
|---|
| 2839 |  | 
|---|
| 2840 | for (i = 0; prog[i].pred; i++) { | 
|---|
| 2841 | struct filter_pred *pred = prog[i].pred; | 
|---|
| 2842 | struct ftrace_event_field *field = pred->field; | 
|---|
| 2843 |  | 
|---|
| 2844 | WARN_ON_ONCE(pred->fn_num == FILTER_PRED_FN_NOP); | 
|---|
| 2845 |  | 
|---|
| 2846 | if (!field) { | 
|---|
| 2847 | WARN_ONCE(1, "all leafs should have field defined %d", i); | 
|---|
| 2848 | continue; | 
|---|
| 2849 | } | 
|---|
| 2850 |  | 
|---|
| 2851 | if (!strchr(fields, *field->name)) | 
|---|
| 2852 | continue; | 
|---|
| 2853 |  | 
|---|
| 2854 | pred->fn_num = FILTER_PRED_TEST_VISITED; | 
|---|
| 2855 | } | 
|---|
| 2856 | } | 
|---|
| 2857 |  | 
|---|
| 2858 | static __init int ftrace_test_event_filter(void) | 
|---|
| 2859 | { | 
|---|
| 2860 | int i; | 
|---|
| 2861 |  | 
|---|
| 2862 | printk(KERN_INFO "Testing ftrace filter: "); | 
|---|
| 2863 |  | 
|---|
| 2864 | for (i = 0; i < DATA_CNT; i++) { | 
|---|
| 2865 | struct event_filter *filter = NULL; | 
|---|
| 2866 | struct test_filter_data_t *d = &test_filter_data[i]; | 
|---|
| 2867 | int err; | 
|---|
| 2868 |  | 
|---|
| 2869 | err = create_filter(NULL, &event_ftrace_test_filter, | 
|---|
| 2870 | d->filter, false, &filter); | 
|---|
| 2871 | if (err) { | 
|---|
| 2872 | printk(KERN_INFO | 
|---|
| 2873 | "Failed to get filter for '%s', err %d\n", | 
|---|
| 2874 | d->filter, err); | 
|---|
| 2875 | __free_filter(filter); | 
|---|
| 2876 | break; | 
|---|
| 2877 | } | 
|---|
| 2878 |  | 
|---|
| 2879 | /* Needed to dereference filter->prog */ | 
|---|
| 2880 | mutex_lock(&event_mutex); | 
|---|
| 2881 | /* | 
|---|
| 2882 | * The preemption disabling is not really needed for self | 
|---|
| 2883 | * tests, but the rcu dereference will complain without it. | 
|---|
| 2884 | */ | 
|---|
| 2885 | preempt_disable(); | 
|---|
| 2886 | if (*d->not_visited) | 
|---|
| 2887 | update_pred_fn(filter, d->not_visited); | 
|---|
| 2888 |  | 
|---|
| 2889 | test_pred_visited = 0; | 
|---|
| 2890 | err = filter_match_preds(filter, &d->rec); | 
|---|
| 2891 | preempt_enable(); | 
|---|
| 2892 |  | 
|---|
| 2893 | mutex_unlock(&event_mutex); | 
|---|
| 2894 |  | 
|---|
| 2895 | __free_filter(filter); | 
|---|
| 2896 |  | 
|---|
| 2897 | if (test_pred_visited) { | 
|---|
| 2898 | printk(KERN_INFO | 
|---|
| 2899 | "Failed, unwanted pred visited for filter %s\n", | 
|---|
| 2900 | d->filter); | 
|---|
| 2901 | break; | 
|---|
| 2902 | } | 
|---|
| 2903 |  | 
|---|
| 2904 | if (err != d->match) { | 
|---|
| 2905 | printk(KERN_INFO | 
|---|
| 2906 | "Failed to match filter '%s', expected %d\n", | 
|---|
| 2907 | d->filter, d->match); | 
|---|
| 2908 | break; | 
|---|
| 2909 | } | 
|---|
| 2910 | } | 
|---|
| 2911 |  | 
|---|
| 2912 | if (i == DATA_CNT) | 
|---|
| 2913 | printk(KERN_CONT "OK\n"); | 
|---|
| 2914 |  | 
|---|
| 2915 | /* Need to call ftrace_test_filter to prevent a warning */ | 
|---|
| 2916 | if (!trace_ftrace_test_filter_enabled()) | 
|---|
| 2917 | trace_ftrace_test_filter(1, 2, 3, 4, 5, 6, 7, 8); | 
|---|
| 2918 |  | 
|---|
| 2919 | return 0; | 
|---|
| 2920 | } | 
|---|
| 2921 |  | 
|---|
| 2922 | late_initcall(ftrace_test_event_filter); | 
|---|
| 2923 |  | 
|---|
| 2924 | #endif /* CONFIG_FTRACE_STARTUP_TEST */ | 
|---|
| 2925 |  | 
|---|