Skip to content

Commit 07b3587

Browse files
committed
feat: sync
1 parent 80773e3 commit 07b3587

8 files changed

Lines changed: 463 additions & 236 deletions

File tree

include/linux/bpf_ir.h

Lines changed: 42 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ typedef __u64 u64;
4848
struct custom_pass_cfg;
4949
struct builtin_pass_cfg;
5050

51-
struct bpf_ir_raw_opts {
52-
u32 enable_bpf_ir;
53-
char pass_opt[300];
54-
char global_opt[200];
55-
};
56-
5751
struct bpf_ir_opts {
5852
// Enable debug mode
5953
bool debug;
@@ -841,6 +835,22 @@ struct ir_insn *bpf_ir_create_allocarray_insn_bb(struct bpf_ir_env *env,
841835
enum ir_vr_type type, u32 num,
842836
enum insert_position pos);
843837

838+
struct ir_insn *bpf_ir_create_loadimmextra_insn(
839+
struct bpf_ir_env *env, struct ir_insn *pos_insn,
840+
enum ir_loadimm_extra_type load_ty, s64 imm, enum insert_position pos);
841+
842+
struct ir_insn *bpf_ir_create_loadimmextra_insn_bb(
843+
struct bpf_ir_env *env, struct ir_basic_block *pos_bb,
844+
enum ir_loadimm_extra_type load_ty, s64 imm, enum insert_position pos);
845+
846+
struct ir_insn *bpf_ir_create_loadimmextra_insn_cg(
847+
struct bpf_ir_env *env, struct ir_insn *pos_insn,
848+
enum ir_loadimm_extra_type load_ty, s64 imm, enum insert_position pos);
849+
850+
struct ir_insn *bpf_ir_create_loadimmextra_insn_bb_cg(
851+
struct bpf_ir_env *env, struct ir_basic_block *pos_bb,
852+
enum ir_loadimm_extra_type load_ty, s64 imm, enum insert_position pos);
853+
844854
struct ir_insn *bpf_ir_create_getelemptr_insn(struct bpf_ir_env *env,
845855
struct ir_insn *pos_insn,
846856
struct ir_insn *alloca_insn,
@@ -853,6 +863,17 @@ struct ir_insn *bpf_ir_create_getelemptr_insn_bb(struct bpf_ir_env *env,
853863
struct ir_value offset,
854864
enum insert_position pos);
855865

866+
struct ir_insn *bpf_ir_create_getelemptr_insn_cg(struct bpf_ir_env *env,
867+
struct ir_insn *pos_insn,
868+
struct ir_insn *alloca_insn,
869+
struct ir_value offset,
870+
enum insert_position pos);
871+
872+
struct ir_insn *bpf_ir_create_getelemptr_insn_bb_cg(
873+
struct bpf_ir_env *env, struct ir_basic_block *pos_bb,
874+
struct ir_insn *alloca_insn, struct ir_value offset,
875+
enum insert_position pos);
876+
856877
struct ir_insn *bpf_ir_create_store_insn(struct bpf_ir_env *env,
857878
struct ir_insn *pos_insn,
858879
struct ir_insn *insn,
@@ -1066,6 +1087,7 @@ void remove_trivial_phi(struct bpf_ir_env *env, struct ir_function *fun,
10661087
void add_counter(struct bpf_ir_env *env, struct ir_function *fun, void *param);
10671088

10681089
extern const struct builtin_pass_cfg bpf_ir_kern_add_counter_pass;
1090+
extern const struct builtin_pass_cfg bpf_ir_kern_optimization_pass;
10691091

10701092
void translate_throw(struct bpf_ir_env *env, struct ir_function *fun,
10711093
void *param);
@@ -1074,7 +1096,7 @@ struct function_pass {
10741096
void (*pass)(struct bpf_ir_env *env, struct ir_function *, void *param);
10751097

10761098
bool enabled;
1077-
bool non_overridable;
1099+
bool force_enable;
10781100
char name[BPF_IR_MAX_PASS_NAME_SIZE];
10791101
};
10801102

@@ -1119,14 +1141,19 @@ struct builtin_pass_cfg {
11191141
.param_load = param_loadc, \
11201142
.param_unload = param_unloadc }
11211143

1144+
#define DEF_BUILTIN_PASS_ENABLE_CFG(namec, param_loadc, param_unloadc) \
1145+
{ .name = namec, \
1146+
.param = NULL, \
1147+
.enable = true, \
1148+
.enable_cfg = false, \
1149+
.param_load = param_loadc, \
1150+
.param_unload = param_unloadc }
1151+
11221152
#define DEF_FUNC_PASS(fun, msg, en_def) \
1123-
{ .pass = fun, \
1124-
.name = msg, \
1125-
.enabled = en_def, \
1126-
.non_overridable = false }
1153+
{ .pass = fun, .name = msg, .enabled = en_def, .force_enable = false }
11271154

1128-
#define DEF_NON_OVERRIDE_FUNC_PASS(fun, msg, en_def) \
1129-
{ .pass = fun, .name = msg, .enabled = en_def, .non_overridable = true }
1155+
#define DEF_NON_OVERRIDE_FUNC_PASS(fun, msg) \
1156+
{ .pass = fun, .name = msg, .enabled = true, .force_enable = true }
11301157

11311158
/* Passes End */
11321159

@@ -1245,8 +1272,8 @@ void bpr_ir_cg_to_cssa(struct bpf_ir_env *env, struct ir_function *fun,
12451272

12461273
/* Kern Utils Start */
12471274

1248-
int bpf_ir_init_opts(struct bpf_ir_env *env, const char *pass_opt,
1249-
const char *global_opt);
1275+
int bpf_ir_init_opts(struct bpf_ir_env *env, const char *global_opt,
1276+
const char *pass_opt);
12501277

12511278
bool bpf_ir_builtin_pass_enabled(struct bpf_ir_env *env, const char *pass_name);
12521279

kernel/bpf/ir/add_counter_pass.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static int load_param(const char *opt, void **param)
111111
return -EINVAL;
112112
}
113113
*param = malloc_proto(sizeof(int));
114-
if (!param) {
114+
if (!*param) {
115115
return -ENOMEM;
116116
}
117117
*(int *)(*param) = res;

kernel/bpf/ir/bpf_ir.c

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,13 @@ static const struct function_pass pre_passes[] = {
1818
static const struct function_pass post_passes[] = {
1919
DEF_FUNC_PASS(add_counter, "add_counter", false),
2020
/* CG Preparation Passes */
21-
DEF_NON_OVERRIDE_FUNC_PASS(translate_throw, "translate_throw", true),
22-
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_optimize_ir, "optimize_ir", true),
23-
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_change_fun_arg, "change_fun_arg",
24-
true),
25-
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_change_call_pre_cg, "change_call",
26-
true),
21+
DEF_NON_OVERRIDE_FUNC_PASS(translate_throw, "translate_throw"),
22+
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_optimize_ir, "optimize_ir"),
23+
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_change_fun_arg, "change_fun_arg"),
24+
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_change_call_pre_cg, "change_call"),
2725
DEF_NON_OVERRIDE_FUNC_PASS(bpf_ir_cg_add_stack_offset_pre_cg,
28-
"add_stack_offset", true),
29-
DEF_NON_OVERRIDE_FUNC_PASS(bpr_ir_cg_to_cssa, "to_cssa", true),
26+
"add_stack_offset"),
27+
DEF_NON_OVERRIDE_FUNC_PASS(bpr_ir_cg_to_cssa, "to_cssa"),
3028
};
3129

3230
static void write_variable(struct bpf_ir_env *env,
@@ -1142,37 +1140,27 @@ static void run_passes(struct bpf_ir_env *env, struct ir_function *fun)
11421140
{
11431141
for (size_t i = 0; i < sizeof(pre_passes) / sizeof(pre_passes[0]);
11441142
++i) {
1145-
if (pre_passes[i].non_overridable) {
1146-
if (pre_passes[i].enabled) {
1147-
run_single_pass(env, fun, &pre_passes[i], NULL);
1148-
}
1149-
} else {
1150-
bool has_override = false;
1151-
for (size_t j = 0; j < env->opts.builtin_pass_cfg_num;
1152-
++j) {
1153-
if (strcmp(env->opts.builtin_pass_cfg[j].name,
1154-
pre_passes[i].name) == 0) {
1155-
has_override = true;
1156-
if (env->opts.builtin_pass_cfg[j]
1157-
.enable) {
1158-
run_single_pass(
1159-
env, fun,
1160-
&pre_passes[i],
1161-
env->opts
1162-
.builtin_pass_cfg
1163-
[j]
1164-
.param);
1165-
}
1166-
break;
1143+
bool has_override = false;
1144+
for (size_t j = 0; j < env->opts.builtin_pass_cfg_num; ++j) {
1145+
if (strcmp(env->opts.builtin_pass_cfg[j].name,
1146+
pre_passes[i].name) == 0) {
1147+
has_override = true;
1148+
if (pre_passes[i].force_enable ||
1149+
env->opts.builtin_pass_cfg[j].enable) {
1150+
run_single_pass(
1151+
env, fun, &pre_passes[i],
1152+
env->opts.builtin_pass_cfg[j]
1153+
.param);
11671154
}
1155+
break;
11681156
}
1169-
if (!has_override) {
1170-
if (pre_passes[i].enabled) {
1171-
run_single_pass(env, fun,
1172-
&pre_passes[i], NULL);
1173-
}
1157+
}
1158+
if (!has_override) {
1159+
if (pre_passes[i].enabled) {
1160+
run_single_pass(env, fun, &pre_passes[i], NULL);
11741161
}
11751162
}
1163+
11761164
CHECK_ERR();
11771165
}
11781166
for (size_t i = 0; i < env->opts.custom_pass_num; ++i) {
@@ -1198,37 +1186,26 @@ static void run_passes(struct bpf_ir_env *env, struct ir_function *fun)
11981186
}
11991187
for (size_t i = 0; i < sizeof(post_passes) / sizeof(post_passes[0]);
12001188
++i) {
1201-
if (post_passes[i].non_overridable) {
1189+
bool has_override = false;
1190+
for (size_t j = 0; j < env->opts.builtin_pass_cfg_num; ++j) {
1191+
if (strcmp(env->opts.builtin_pass_cfg[j].name,
1192+
post_passes[i].name) == 0) {
1193+
has_override = true;
1194+
if (pre_passes[i].force_enable ||
1195+
env->opts.builtin_pass_cfg[j].enable) {
1196+
run_single_pass(
1197+
env, fun, &post_passes[i],
1198+
env->opts.builtin_pass_cfg[j]
1199+
.param);
1200+
}
1201+
break;
1202+
}
1203+
}
1204+
if (!has_override) {
12021205
if (post_passes[i].enabled) {
12031206
run_single_pass(env, fun, &post_passes[i],
12041207
NULL);
12051208
}
1206-
} else {
1207-
bool has_override = false;
1208-
for (size_t j = 0; j < env->opts.builtin_pass_cfg_num;
1209-
++j) {
1210-
if (strcmp(env->opts.builtin_pass_cfg[j].name,
1211-
post_passes[i].name) == 0) {
1212-
has_override = true;
1213-
if (env->opts.builtin_pass_cfg[j]
1214-
.enable) {
1215-
run_single_pass(
1216-
env, fun,
1217-
&post_passes[i],
1218-
env->opts
1219-
.builtin_pass_cfg
1220-
[j]
1221-
.param);
1222-
}
1223-
break;
1224-
}
1225-
}
1226-
if (!has_override) {
1227-
if (post_passes[i].enabled) {
1228-
run_single_pass(env, fun,
1229-
&post_passes[i], NULL);
1230-
}
1231-
}
12321209
}
12331210
CHECK_ERR();
12341211
}

0 commit comments

Comments
 (0)