The assumption that only programs attached to perf NMI events can deadlock on memory allocators is wrong. Assume the following simplified callchain: kmalloc() from regular non BPF context cache empty freelist empty lock(zone->lock); tracepoint or kprobe BPF() update_elem() lock(bucket) kmalloc() cache empty freelist empty lock(zone->lock); <- DEADLOCK There are other ways which do not involve locking to create wreckage: kmalloc() from regular non BPF context local_irq_save(); ... obj = slab_first(); kprobe() BPF() update_elem() lock(bucket) kmalloc() local_irq_save(); ... obj = slab_first(); <- Same object as above ... So preallocation _must_ be enforced for all variants of intrusive instrumentation. Unfortunately immediate enforcement would break backwards compatibility, so for now such programs still are allowed to run, but a one time warning is emitted in dmesg and the verifier emits a warning in the verifier log as well so developers are made aware about this and can fix their programs before the enforcement becomes mandatory.
Showing the changes to kernel (1 of 1 file). Show all
Unified · Split
| @@ -8143,26 +8143,43 @@ static bool is_tracing_prog_type(enum bpf_prog_type type) · 8142 unmodified lines | ||
| 8143 | 8143 | } |
| 8144 | 8144 | } |
| 8145 | 8145 | |
| 8146 | +static bool is_preallocated_map(struct bpf_map *map) | |
| 8147 | +{ | |
| 8148 | + if (!check_map_prealloc(map)) | |
| 8149 | + return false; | |
| 8150 | + if (map->inner_map_meta && !check_map_prealloc(map->inner_map_meta)) | |
| 8151 | + return false; | |
| 8152 | + return true; | |
| 8153 | +} | |
| 8154 | + | |
| 8146 | 8155 | static int check_map_prog_compatibility(struct bpf_verifier_env *env, |
| 8147 | 8156 | struct bpf_map *map, |
| 8148 | 8157 | struct bpf_prog *prog) |
| 8149 | 8158 | |
| 8150 | 8159 | { |
| 8151 | − /* Make sure that BPF_PROG_TYPE_PERF_EVENT programs only use | |
| 8152 | − * preallocated hash maps, since doing memory allocation | |
| 8153 | − * in overflow_handler can crash depending on where nmi got | |
| 8154 | − * triggered. | |
| 8160 | + /* | |
| 8161 | + * Validate that trace type programs use preallocated hash maps. | |
| 8162 | + * | |
| 8163 | + * For programs attached to PERF events this is mandatory as the | |
| 8164 | + * perf NMI can hit any arbitrary code sequence. | |
| 8165 | + * | |
| 8166 | + * All other trace types using preallocated hash maps are unsafe as | |
| 8167 | + * well because tracepoint or kprobes can be inside locked regions | |
| 8168 | + * of the memory allocator or at a place where a recursion into the | |
| 8169 | + * memory allocator would see inconsistent state. | |
| 8170 | + * | |
| 8171 | + * For now running such programs is allowed for backwards | |
| 8172 | + * compatibility reasons, but warnings are emitted so developers are | |
| 8173 | + * made aware of the unsafety and can fix their programs before this | |
| 8174 | + * is enforced. | |
| 8155 | 8175 | */ |
| 8156 | − if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
| 8157 | − if (!check_map_prealloc(map)) { | |
| 8176 | + if (is_tracing_prog_type(prog->type) && !is_preallocated_map(map)) { | |
| 8177 | + if (prog->type == BPF_PROG_TYPE_PERF_EVENT) { | |
| 8158 | 8178 | verbose(env, "perf_event programs can only use preallocated hash map\n"); |
| 8159 | 8179 | return -EINVAL; |
| 8160 | 8180 | } |
| 8161 | − if (map->inner_map_meta && | |
| 8162 | − !check_map_prealloc(map->inner_map_meta)) { | |
| 8163 | − verbose(env, "perf_event programs can only use preallocated inner hash map\n"); | |
| 8164 | − return -EINVAL; | |
| 8165 | − } | |
| 8181 | + WARN_ONCE(1, "trace type BPF program uses run-time allocation\n"); | |
| 8182 | + verbose(env, "trace type programs with run-time allocated hash maps are unsafe. Switch to preallocated hash maps.\n"); | |
| 8166 | 8183 | } |
| 8167 | 8184 | |
| 8168 | 8185 | if ((is_tracing_prog_type(prog->type) || |