Skip to content

Commit 478325f

Browse files
committed
tracing: Clean up and document pred_funcs_##type creation and use
The pred_funcs_##type arrays consist of five functions that are assigned based on the ops. The array must be in the same order of the ops each function represents. The PRED_FUNC_START macro denotes the op enum that starts the op that maps to the pred_funcs_##type arrays. This is all very subtle and prone to bugs if the code is changed. Add comments describing how PRED_FUNC_START and pred_funcs_##type array is used, and also a PRED_FUNC_MAX that is the maximum number of functions in the arrays. Clean up select_comparison_fn() that assigns the predicates to the pred_funcs_##type array function as well as add protection in case an op is passed in that does not map correctly to the array. Reviewed-by: Masami Hiramatsu <mhiramat@kernel.org> Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org>
1 parent e9baef0 commit 478325f

File tree

1 file changed

+32
-14
lines changed

1 file changed

+32
-14
lines changed

kernel/trace/trace_events_filter.c

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,13 @@ struct filter_op {
6565

6666
static struct filter_op filter_ops[] = { OPS };
6767

68+
/*
69+
* pred functions are OP_LT, OP_LE, OP_GT, OP_GE, and OP_BAND
70+
* pred_funcs_##type below must match the order of them above.
71+
*/
72+
#define PRED_FUNC_START OP_LT
73+
#define PRED_FUNC_MAX (OP_BAND - PRED_FUNC_START)
74+
6875
#define ERRORS \
6976
C( NONE, "No error"), \
7077
C( INVALID_OP, "Invalid operator"), \
@@ -172,8 +179,6 @@ static const filter_pred_fn_t pred_funcs_##type[] = { \
172179
filter_pred_BAND_##type, \
173180
};
174181

175-
#define PRED_FUNC_START OP_LT
176-
177182
#define DEFINE_EQUALITY_PRED(size) \
178183
static int filter_pred_##size(struct filter_pred *pred, void *event) \
179184
{ \
@@ -946,39 +951,52 @@ static filter_pred_fn_t select_comparison_fn(enum filter_op_ids op,
946951
int field_size, int field_is_signed)
947952
{
948953
filter_pred_fn_t fn = NULL;
954+
int pred_func_index = -1;
955+
956+
switch (op) {
957+
case OP_EQ:
958+
case OP_NE:
959+
break;
960+
default:
961+
if (WARN_ON_ONCE(op < PRED_FUNC_START))
962+
return NULL;
963+
pred_func_index = op - PRED_FUNC_START;
964+
if (WARN_ON_ONCE(pred_func_index > PRED_FUNC_MAX))
965+
return NULL;
966+
}
949967

950968
switch (field_size) {
951969
case 8:
952-
if (op == OP_EQ || op == OP_NE)
970+
if (pred_func_index < 0)
953971
fn = filter_pred_64;
954972
else if (field_is_signed)
955-
fn = pred_funcs_s64[op - PRED_FUNC_START];
973+
fn = pred_funcs_s64[pred_func_index];
956974
else
957-
fn = pred_funcs_u64[op - PRED_FUNC_START];
975+
fn = pred_funcs_u64[pred_func_index];
958976
break;
959977
case 4:
960-
if (op == OP_EQ || op == OP_NE)
978+
if (pred_func_index < 0)
961979
fn = filter_pred_32;
962980
else if (field_is_signed)
963-
fn = pred_funcs_s32[op - PRED_FUNC_START];
981+
fn = pred_funcs_s32[pred_func_index];
964982
else
965-
fn = pred_funcs_u32[op - PRED_FUNC_START];
983+
fn = pred_funcs_u32[pred_func_index];
966984
break;
967985
case 2:
968-
if (op == OP_EQ || op == OP_NE)
986+
if (pred_func_index < 0)
969987
fn = filter_pred_16;
970988
else if (field_is_signed)
971-
fn = pred_funcs_s16[op - PRED_FUNC_START];
989+
fn = pred_funcs_s16[pred_func_index];
972990
else
973-
fn = pred_funcs_u16[op - PRED_FUNC_START];
991+
fn = pred_funcs_u16[pred_func_index];
974992
break;
975993
case 1:
976-
if (op == OP_EQ || op == OP_NE)
994+
if (pred_func_index < 0)
977995
fn = filter_pred_8;
978996
else if (field_is_signed)
979-
fn = pred_funcs_s8[op - PRED_FUNC_START];
997+
fn = pred_funcs_s8[pred_func_index];
980998
else
981-
fn = pred_funcs_u8[op - PRED_FUNC_START];
999+
fn = pred_funcs_u8[pred_func_index];
9821000
break;
9831001
}
9841002

0 commit comments

Comments
 (0)