SCEV needs to process pending basic blocks in reverse postorder. Provide a small integer min-heap whose comparator can order instruction indices by their CFG ranks. Grow the backing array on demand and pass caller context to the comparator, so the heap need not encode the analysis-specific order. Signed-off-by: Eduard Zingerman --- include/linux/bpf_verifier.h | 17 +++++++++ kernel/bpf/Makefile | 2 +- kernel/bpf/heap.c | 87 ++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/include/linux/bpf_verifier.h b/include/linux/bpf_verifier.h index 555a41c5660e..8f5d35fa9aab 100644 --- a/include/linux/bpf_verifier.h +++ b/include/linux/bpf_verifier.h @@ -1938,4 +1938,21 @@ int bpf_compute_loops(struct bpf_verifier_env *env); int bpf_loop_at_index(struct bpf_verifier_env *env, u32 idx); bool bpf_is_nested_loop(struct bpf_verifier_env *env, int inner_header, int outer_header); +/* + * Simple binary heap implementation as described by + * https://en.wikipedia.org/wiki/Binary_heap + */ +struct bpf_min_heap { + int (*compare)(int, int, void *); /* ordering function for @elements */ + int *elements; /* min-heap ordered by @compare */ + void *arg; /* 3rd argument passed to @compare */ + int capacity; + int count; +}; + +void bpf_min_heap_init(struct bpf_min_heap *heap, int (*compare)(int, int, void *), void *arg); +void bpf_min_heap_free(struct bpf_min_heap *heap); +int bpf_min_heap_push(struct bpf_min_heap *heap, int elt); +bool bpf_min_heap_pop(struct bpf_min_heap *heap, int *elt); + #endif /* _LINUX_BPF_VERIFIER_H */ diff --git a/kernel/bpf/Makefile b/kernel/bpf/Makefile index 6210601eb40b..7a2c179a9059 100644 --- a/kernel/bpf/Makefile +++ b/kernel/bpf/Makefile @@ -6,7 +6,7 @@ cflags-nogcse-$(CONFIG_X86)$(CONFIG_CC_IS_GCC) := -fno-gcse endif CFLAGS_core.o += -Wno-override-init $(cflags-nogcse-yy) -obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o loops.o +obj-$(CONFIG_BPF_SYSCALL) += syscall.o verifier.o inode.o helpers.o tnum.o cnum.o log.o token.o liveness.o const_fold.o diagnostics.o loops.o heap.o obj-$(CONFIG_BPF_SYSCALL) += bpf_iter.o map_iter.o task_iter.o prog_iter.o link_iter.o obj-$(CONFIG_BPF_SYSCALL) += hashtab.o arraymap.o percpu_freelist.o bpf_lru_list.o lpm_trie.o map_in_map.o bloom_filter.o obj-$(CONFIG_BPF_SYSCALL) += local_storage.o queue_stack_maps.o ringbuf.o bpf_insn_array.o diff --git a/kernel/bpf/heap.c b/kernel/bpf/heap.c new file mode 100644 index 000000000000..029f9217b872 --- /dev/null +++ b/kernel/bpf/heap.c @@ -0,0 +1,87 @@ +// SPDX-License-Identifier: GPL-2.0-only +/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */ + +#include + +/* Indexes for binary tree encoded as an array */ +static inline int left_child(int i) { return 2 * i + 1; } +static inline int right_child(int i) { return 2 * i + 2; } +static inline int parent(int i) { return (i - 1) / 2; } + +static inline int greater(struct bpf_min_heap *heap, int a, int b) +{ + return heap->compare(a, b, heap->arg) > 0; +} + +void bpf_min_heap_init(struct bpf_min_heap *heap, int (*compare)(int, int, void *), void *arg) +{ + memset(heap, 0, sizeof(*heap)); + heap->compare = compare; + heap->arg = arg; +} + +void bpf_min_heap_free(struct bpf_min_heap *heap) +{ + kfree(heap->elements); + heap->elements = NULL; + heap->capacity = 0; + heap->count = 0; +} + +int bpf_min_heap_push(struct bpf_min_heap *heap, int elt) +{ + int new_capacity, i; + int *elements; + void *tmp; + + if (heap->count == heap->capacity) { + new_capacity = heap->capacity ? heap->capacity * 2 : 16; + tmp = krealloc(heap->elements, + sizeof(*heap->elements) * new_capacity, + GFP_KERNEL_ACCOUNT); + if (!tmp) + return -ENOMEM; + heap->elements = tmp; + heap->capacity = new_capacity; + } + + elements = heap->elements; + i = heap->count; + elements[i] = elt; + heap->count++; + while (i != 0 && greater(heap, elements[parent(i)], elements[i])) { + swap(elements[i], elements[parent(i)]); + i = parent(i); + } + return 0; +} + +static inline void sink_root(struct bpf_min_heap *heap) +{ + int *elements = heap->elements; + int i = 0; + + while ((left_child(i) < heap->count && greater(heap, elements[i], elements[left_child(i)])) || + (right_child(i) < heap->count && greater(heap, elements[i], elements[right_child(i)]))) { + if (right_child(i) >= heap->count || greater(heap, elements[right_child(i)], elements[left_child(i)])) { + swap(elements[i], elements[left_child(i)]); + i = left_child(i); + } else { + swap(elements[i], elements[right_child(i)]); + i = right_child(i); + } + } +} + +bool bpf_min_heap_pop(struct bpf_min_heap *heap, int *elt) +{ + if (heap->count == 0) + return false; + + int *elements = heap->elements; + *elt = elements[0]; + elements[0] = elements[heap->count - 1]; + --heap->count; + sink_root(heap); + return true; +} -- 2.53.0