Skip to content

Commit f3709f6

Browse files
joestringerborkmann
authored andcommitted
bpf: Add iterator for spilled registers
Add this iterator for spilled registers, it concentrates the details of how to get the current frame's spilled registers into a single macro while clarifying the intention of the code which is calling the macro. Signed-off-by: Joe Stringer <joe@wand.net.nz> Acked-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
1 parent 940656f commit f3709f6

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

include/linux/bpf_verifier.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,17 @@ struct bpf_verifier_state {
131131
u32 curframe;
132132
};
133133

134+
#define bpf_get_spilled_reg(slot, frame) \
135+
(((slot < frame->allocated_stack / BPF_REG_SIZE) && \
136+
(frame->stack[slot].slot_type[0] == STACK_SPILL)) \
137+
? &frame->stack[slot].spilled_ptr : NULL)
138+
139+
/* Iterate over 'frame', setting 'reg' to either NULL or a spilled register. */
140+
#define bpf_for_each_spilled_reg(iter, frame, reg) \
141+
for (iter = 0, reg = bpf_get_spilled_reg(iter, frame); \
142+
iter < frame->allocated_stack / BPF_REG_SIZE; \
143+
iter++, reg = bpf_get_spilled_reg(iter, frame))
144+
134145
/* linked list of verifier states used to prune search */
135146
struct bpf_verifier_state_list {
136147
struct bpf_verifier_state state;

kernel/bpf/verifier.c

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2252,10 +2252,9 @@ static void __clear_all_pkt_pointers(struct bpf_verifier_env *env,
22522252
if (reg_is_pkt_pointer_any(&regs[i]))
22532253
mark_reg_unknown(env, regs, i);
22542254

2255-
for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
2256-
if (state->stack[i].slot_type[0] != STACK_SPILL)
2255+
bpf_for_each_spilled_reg(i, state, reg) {
2256+
if (!reg)
22572257
continue;
2258-
reg = &state->stack[i].spilled_ptr;
22592258
if (reg_is_pkt_pointer_any(reg))
22602259
__mark_reg_unknown(reg);
22612260
}
@@ -3395,10 +3394,9 @@ static void find_good_pkt_pointers(struct bpf_verifier_state *vstate,
33953394

33963395
for (j = 0; j <= vstate->curframe; j++) {
33973396
state = vstate->frame[j];
3398-
for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3399-
if (state->stack[i].slot_type[0] != STACK_SPILL)
3397+
bpf_for_each_spilled_reg(i, state, reg) {
3398+
if (!reg)
34003399
continue;
3401-
reg = &state->stack[i].spilled_ptr;
34023400
if (reg->type == type && reg->id == dst_reg->id)
34033401
reg->range = max(reg->range, new_range);
34043402
}
@@ -3643,7 +3641,7 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
36433641
bool is_null)
36443642
{
36453643
struct bpf_func_state *state = vstate->frame[vstate->curframe];
3646-
struct bpf_reg_state *regs = state->regs;
3644+
struct bpf_reg_state *reg, *regs = state->regs;
36473645
u32 id = regs[regno].id;
36483646
int i, j;
36493647

@@ -3652,8 +3650,8 @@ static void mark_map_regs(struct bpf_verifier_state *vstate, u32 regno,
36523650

36533651
for (j = 0; j <= vstate->curframe; j++) {
36543652
state = vstate->frame[j];
3655-
for (i = 0; i < state->allocated_stack / BPF_REG_SIZE; i++) {
3656-
if (state->stack[i].slot_type[0] != STACK_SPILL)
3653+
bpf_for_each_spilled_reg(i, state, reg) {
3654+
if (!reg)
36573655
continue;
36583656
mark_map_reg(&state->stack[i].spilled_ptr, 0, id, is_null);
36593657
}

0 commit comments

Comments
 (0)