Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

bpf/verifier: Optimize ID mapping reset in states_equal

Currently, reset_idmap_scratch() performs a 4.7KB memset() in every
states_equal() call. Optimize this by using a counter to track used
ID mappings, replacing the O(N) memset() with an O(1) reset and
bounding the search loop in check_ids().

Signed-off-by: Qiliang Yuan <realwujing@gmail.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20260120023234.77673-1-realwujing@gmail.com

authored by

Qiliang Yuan and committed by
Andrii Nakryiko
f81c07a6 713edc71

+15 -9
+1
include/linux/bpf_verifier.h
··· 692 692 693 693 struct bpf_idmap { 694 694 u32 tmp_id_gen; 695 + u32 cnt; 695 696 struct bpf_id_pair map[BPF_ID_MAP_SIZE]; 696 697 }; 697 698
+14 -9
kernel/bpf/verifier.c
··· 19000 19000 if (old_id == 0) /* cur_id == 0 as well */ 19001 19001 return true; 19002 19002 19003 - for (i = 0; i < BPF_ID_MAP_SIZE; i++) { 19004 - if (!map[i].old) { 19005 - /* Reached an empty slot; haven't seen this id before */ 19006 - map[i].old = old_id; 19007 - map[i].cur = cur_id; 19008 - return true; 19009 - } 19003 + for (i = 0; i < idmap->cnt; i++) { 19010 19004 if (map[i].old == old_id) 19011 19005 return map[i].cur == cur_id; 19012 19006 if (map[i].cur == cur_id) 19013 19007 return false; 19014 19008 } 19009 + 19010 + /* Reached the end of known mappings; haven't seen this id before */ 19011 + if (idmap->cnt < BPF_ID_MAP_SIZE) { 19012 + map[idmap->cnt].old = old_id; 19013 + map[idmap->cnt].cur = cur_id; 19014 + idmap->cnt++; 19015 + return true; 19016 + } 19017 + 19015 19018 /* We ran out of idmap slots, which should be impossible */ 19016 19019 WARN_ON_ONCE(1); 19017 19020 return false; ··· 19523 19520 19524 19521 static void reset_idmap_scratch(struct bpf_verifier_env *env) 19525 19522 { 19526 - env->idmap_scratch.tmp_id_gen = env->id_gen; 19527 - memset(&env->idmap_scratch.map, 0, sizeof(env->idmap_scratch.map)); 19523 + struct bpf_idmap *idmap = &env->idmap_scratch; 19524 + 19525 + idmap->tmp_id_gen = env->id_gen; 19526 + idmap->cnt = 0; 19528 19527 } 19529 19528 19530 19529 static bool states_equal(struct bpf_verifier_env *env,