Hi, We are trying to fix some sparse warning in kernel/bpf/hashtab.c (in bpf-next tree). The sparse I use was v0.6.3-76-gf680124b. These new warnings are introduced by [1]. Before [1], hashtab.c got [...] kernel/bpf/hashtab.c:2089:19: error: subtraction of functions? Share your drugs kernel/bpf/hashtab.c:1421:9: warning: context imbalance in '__htab_map_lookup_and_delete_batch' - different lock contexts for basic block kernel/bpf/hashtab.c: note: in included file (through include/linux/workqueue.h, include/linux/bpf.h): ./include/linux/rcupdate.h:693:9: warning: context imbalance in 'bpf_hash_map_seq_find_next' - unexpected unlock ./include/linux/rcupdate.h:693:9: warning: context imbalance in 'bpf_hash_map_seq_stop' - unexpected unlock With [1], we got a few more warnings: [...] kernel/bpf/hashtab.c:2141:19: error: subtraction of functions? Share your drugs kernel/bpf/hashtab.c:1292:27: warning: context imbalance in 'htab_map_delete_elem' - unexpected unlock kernel/bpf/hashtab.c:1325:27: warning: context imbalance in 'htab_lru_map_delete_elem' - unexpected unlock kernel/bpf/hashtab.c: note: in included file (through include/linux/workqueue.h, include/linux/bpf.h): ./include/linux/rcupdate.h:693:9: warning: context imbalance in '__htab_map_lookup_and_delete_batch' - unexpected unlock ./include/linux/rcupdate.h:693:9: warning: context imbalance in 'bpf_hash_map_seq_find_next' - unexpected unlock ./include/linux/rcupdate.h:693:9: warning: context imbalance in 'bpf_hash_map_seq_stop' - unexpected unlock So htab_map_delete_elem() and htab_lru_map_delete_elem() got new warnings, and __htab_map_lookup_and_delete_batch() got a slightly different warning. After trying different annotations, including the attached foo.diff by Daniel, we found the simplest fix was something like: ====================== 8< ========================== diff --git i/kernel/bpf/hashtab.c w/kernel/bpf/hashtab.c index 23f73d4649c9c..faad2061f1167 100644 --- i/kernel/bpf/hashtab.c +++ w/kernel/bpf/hashtab.c @@ -1279,6 +1279,7 @@ static int htab_map_delete_elem(struct bpf_map *map, void *key) ret = htab_lock_bucket(htab, b, hash, &flags); if (ret) return ret; + ret = 0; l = lookup_elem_raw(head, hash, key, key_size); @@ -1314,6 +1315,7 @@ static int htab_lru_map_delete_elem(struct bpf_map *map, void *key) ret = htab_lock_bucket(htab, b, hash, &flags); if (ret) return ret; + ret = 0; l = lookup_elem_raw(head, hash, key, key_size); @@ -1476,6 +1478,7 @@ __htab_map_lookup_and_delete_batch(struct bpf_map *map, ret = htab_lock_bucket(htab, b, batch, &flags); if (ret) goto next_batch; + ret = 0; } bucket_cnt = 0; ====================== 8< ========================== These three "ret = 0;" make the sparse warning back to before [1]. However, these don't really make sense, as we know ret must be zero after the if clause. So it looks like a bug in sparse. Please help us look into this issue. Also, there are a few similar warnings from hashtab.c. Please give us guidance on how to fix them properly. Thanks in advance, Song [1] commit 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked")
Attachment:
foo.diff
Description: foo.diff