bpf: fix rcu lockdep warning for lpm_trie map_free callback
Subsystems (3)
PATCH 1/1 bpf: fix rcu lockdep warning for lpm_trie map_free callback
Commit 9a3efb6b661f ("bpf: fix memory leak in lpm_trie map_free callback function") fixed a memory leak and removed unnecessary locks in map_free callback function. Unfortrunately, it introduced a lockdep warning. When lockdep checking is turned on, running tools/testing/selftests/bpf/test_lpm_map will have:
[ 98.294321] =============================
[ 98.294807] WARNING: suspicious RCU usage
[ 98.295359] 4.16.0-rc2+ #193 Not tainted
[ 98.295907] -----------------------------
[ 98.296486] /home/yhs/work/bpf/kernel/bpf/lpm_trie.c:572 suspicious rcu_dereference_check() usage!
[ 98.297657]
[ 98.297657] other info that might help us debug this:
[ 98.297657]
[ 98.298663]
[ 98.298663] rcu_scheduler_active = 2, debug_locks = 1
[ 98.299536] 2 locks held by kworker/2:1/54:
[ 98.300152] #0: ((wq_completion)"events"){+.+.}, at: [<00000000196bc1f0>] process_one_work+0x157/0x5c0
[ 98.301381] #1: ((work_completion)(&map->work)){+.+.}, at: [<00000000196bc1f0>] process_one_work+0x157/0x5c0Since actual trie tree removal happens only after no other accesses to the tree are possible, this patch simply converted all rcu protected pointer access to normal access, which removed the above warning.
--- kernel/bpf/lpm_trie.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)
kernel/bpf/lpm_trie.c +5 -6
@@ -552,7 +552,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)static void trie_free (struct bpf_map *map) { struct lpm_trie *trie = container_of(map, struct lpm_trie, map); -struct lpm_trie_node __rcu **slot; +struct lpm_trie_node **slot;struct lpm_trie_node *node;/* Wait for outstanding programs to complete
@@ -569,23 +569,22 @@ static void trie_free(struct bpf_map *map) slot = &trie->root;for (;;) { - node =rcu_dereference_protected (*slot, -lockdep_is_held (&trie->lock)); + node = *slot;if (!node) goto out; -if (rcu_access_pointer (node->child[0 ])) { +if (node->child[0 ]) { slot = &node->child[0 ];continue ; } -if (rcu_access_pointer (node->child[1 ])) { +if (node->child[1 ]) { slot = &node->child[1 ];continue ; }kfree (node); -RCU_INIT_POINTER (*slot,NULL ); + *slot =NULL ;break ; } }
Signature · 1 line
2.9.5
Quoted 45 lines · Yonghong Song
Commit
9a3efb6b661f("bpf: fix memory leak inlpm_triemap_freecallback function") fixed a memory leak and removed unnecessary locks inmap_freecallback function. Unfortrunately, it introduced a lockdep warning. When lockdep checking is turned on, runningtools/testing/selftests/bpf/test_lpm_mapwill have:[ 98.294321] ============================= [ 98.294807] WARNING: suspicious RCU usage [ 98.295359] 4.16.0-rc2+ #193 Not tainted [ 98.295907] ----------------------------- [ 98.296486] /home/yhs/work/bpf/kernel/bpf/lpm_trie.c:572 suspicious rcu_dereference_check() usage! [ 98.297657] [ 98.297657] other info that might help us debug this: [ 98.297657] [ 98.298663] [ 98.298663] rcu_scheduler_active = 2, debug_locks = 1 [ 98.299536] 2 locks held by kworker/2:1/54: [ 98.300152] #0: ((wq_completion)"events"){+.+.}, at: [<00000000196bc1f0>] process_one_work+0x157/0x5c0 [ 98.301381] #1: ((work_completion)(&map->work)){+.+.}, at: [<00000000196bc1f0>] process_one_work+0x157/0x5c0Since actual trie tree removal happens only after no other accesses to the tree are possible, this patch simply converted all rcu protected pointer access to normal access, which removed the above warning.
Fixes 9a3efb6b661f ("bpf: fix memory leak in lpm_trie map_free callback function")Reported-by Eric Dumazet <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/lpm_trie.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)kernel/bpf/lpm_trie.c +1
-1@@ -552,7 +552,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)static void trie_free (struct bpf_map *map) { struct lpm_trie *trie = container_of(map, struct lpm_trie, map); -struct lpm_trie_node __rcu **slot; +struct lpm_trie_node **slot;struct lpm_trie_node *node;/* Wait for outstanding programs to complete
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 572
Quoted 6 lines
kernel/bpf/lpm_trie.c +1
-2@@ -569,23 +569,22 @@ static void trie_free(struct bpf_map *map) slot = &trie->root;for (;;) { - node =rcu_dereference_protected (*slot, -lockdep_is_held (&trie->lock)); + node = *slot;
Hi Yonghong
It is not sparse compliant.
kernel/bpf/lpm_trie.c:573:30: warning: incorrect type in assignment (different address spaces) kernel/bpf/lpm_trie.c:573:30: expected struct lpm_trie_node *node kernel/bpf/lpm_trie.c:573:30: got struct lpm_trie_node [noderef] <asn:4>*<noident>
In my local tree, I simply did
node = rcu_dereference_protected(*slot, 1);
Since we are the last user of the whole tree after the prior synchronize_rcu();
Quoted 62 lines · Eric Dumazet
Quoted 45 lines · Yonghong Song
Commit
9a3efb6b661f("bpf: fix memory leak inlpm_triemap_freecallback function") fixed a memory leak and removed unnecessary locks inmap_freecallback function. Unfortrunately, it introduced a lockdep warning. When lockdep checking is turned on, runningtools/testing/selftests/bpf/test_lpm_mapwill have:[ 98.294321] ============================= [ 98.294807] WARNING: suspicious RCU usage [ 98.295359] 4.16.0-rc2+ #193 Not tainted [ 98.295907] ----------------------------- [ 98.296486] /home/yhs/work/bpf/kernel/bpf/lpm_trie.c:572 suspicious rcu_dereference_check() usage! [ 98.297657] [ 98.297657] other info that might help us debug this: [ 98.297657] [ 98.298663] [ 98.298663] rcu_scheduler_active = 2, debug_locks = 1 [ 98.299536] 2 locks held by kworker/2:1/54: [ 98.300152] #0: ((wq_completion)"events"){+.+.}, at: [<00000000196bc1f0>] process_one_work+0x157/0x5c0 [ 98.301381] #1: ((work_completion)(&map->work)){+.+.}, at: [<00000000196bc1f0>] process_one_work+0x157/0x5c0Since actual trie tree removal happens only after no other accesses to the tree are possible, this patch simply converted all rcu protected pointer access to normal access, which removed the above warning.
Fixes 9a3efb6b661f ("bpf: fix memory leak in lpm_trie map_free callback function")Reported-by Eric Dumazet <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/lpm_trie.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-)kernel/bpf/lpm_trie.c +1
-1@@ -552,7 +552,7 @@ static struct bpf_map *trie_alloc(union bpf_attr *attr)static void trie_free (struct bpf_map *map) { struct lpm_trie *trie = container_of(map, struct lpm_trie, map); -struct lpm_trie_node __rcu **slot; +struct lpm_trie_node **slot;struct lpm_trie_node *node;/* Wait for outstanding programs to complete kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 572
Quoted 6 lines
kernel/bpf/lpm_trie.c +1
-2@@ -569,23 +569,22 @@ static void trie_free(struct bpf_map *map) slot = &trie->root;for (;;) { - node =rcu_dereference_protected (*slot, -lockdep_is_held (&trie->lock)); + node = *slot;Hi Yonghong
It is not sparse compliant.
kernel/bpf/lpm_trie.c:573:30: warning: incorrect type in assignment (different address spaces) kernel/bpf/lpm_trie.c:573:30: expected structlpm_trie_node*node kernel/bpf/lpm_trie.c:573:30: got structlpm_trie_node[noderef] <asn:4>*<noident>
Quoted 5 lines
In my local tree, I simply did
node = rcu_dereference_protected(*slot, 1);Since we are the last user of the whole tree after the prior
synchronize_rcu();
Emic,
Thanks for the fix suggestion. It does make sense.
I indeed ran sparse before my patch send-email. Unfortunately, my dev machine (sparse 0.5.0 + gcc 4.8.5) didn't issue warning like the above.
With the same kernel config and kernel tree, I just tried on another machine (a FC27 VM, sparse 0.5.1 + gcc 7.3.1), I did see the warning and the above suggested fix makes warning went away.
Need to figure out why sparse is not happy with my dev machine.
Will send a follow patch soon.
Thanks!
Yonghong
Quoted 2 lines
kbuild test robot
build botThank you for the patch! Perhaps something to improve:
Hi Yonghong,
Thank you for the patch! Perhaps something to improve:
[auto build test WARNING on bpf/master]
url: https://github.com/0day-ci/linux/commits/Yonghong-Song/bpf-fix-rcu-lockdep-warning-for-lpm_trie-map_free-callback/20180222-202658 base: https://git.kernel.org/pub/scm/linux/kernel/git/bpf/bpf.git master reproduce: # apt-get install sparse make ARCH=x86_64 allmodconfig make C=1 CF=-D__CHECK_ENDIAN__
sparse warnings: (new ones prefixed by >>)
include/linux/init.h:134:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/init.h:135:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/init.h:268:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/init.h:269:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/printk.h:200:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:32:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:34:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:37:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:38:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:40:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:42:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:43:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:45:5: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:46:5: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/mem_encrypt.h:49:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/qspinlock.h:53:32: sparse: attribute 'indirect_branch': unknown attribute include/linux/workqueue.h:646:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/workqueue.h:647:5: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/numa.h:34:12: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/numa.h:35:13: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/numa.h:62:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/vmalloc.h:64:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/vmalloc.h:173:8: sparse: attribute 'indirect_branch': unknown attribute include/linux/vmalloc.h:174:8: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/fixmap.h:174:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/fixmap.h:176:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/fixmap.h:178:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/fixmap.h:180:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/apic.h:254:13: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/apic.h:430:13: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/io_apic.h:184:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/smp.h:113:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/smp.h:125:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/smp.h:126:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/percpu.h:110:33: sparse: attribute 'indirect_branch': unknown attribute include/linux/percpu.h:112:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/percpu.h:114:12: sparse: attribute 'indirect_branch': unknown attribute include/linux/percpu.h:118:12: sparse: attribute 'indirect_branch': unknown attribute include/linux/percpu.h:126:12: sparse: attribute 'indirect_branch': unknown attribute include/linux/memory_hotplug.h:221:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/mmzone.h:1292:15: sparse: attribute 'indirect_branch': unknown attribute include/linux/kmemleak.h:29:33: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/kasan.h:29:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/kasan.h:30:6: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/pgtable.h:28:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/slab.h:135:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/slab.h:716:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/wait_bit.h:41:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:63:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:64:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:65:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:66:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:2421:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:2422:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/fs.h:3329:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/hrtimer.h:497:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/mm.h:1753:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/mm.h:1941:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/mm.h:2083:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/mm.h:2671:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/swiotlb.h:39:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/swiotlb.h:124:13: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/swiotlb.h:9:12: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/swiotlb.h:10:12: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/swiotlb.h:11:13: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/swiotlb.h:12:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/dma-contiguous.h:85:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/cred.h:167:13: sparse: attribute 'indirect_branch': unknown attribute include/linux/nsproxy.h:74:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/io.h:47:6: sparse: attribute 'indirect_branch': unknown attribute include/linux/netdevice.h:302:5: sparse: attribute 'indirect_branch': unknown attribute include/linux/netdevice.h:4056:5: sparse: attribute 'indirect_branch': unknown attribute arch/x86/include/asm/vdso.h:44:13: sparse: attribute 'indirect_branch': unknown attribute
>> kernel/bpf/lpm_trie.c:569:22: sparse: incorrect type in assignment (different address spaces) @@ expected struct lpm_trie_node @@ got struct lpm_trie_nstruct lpm_trie_node @@
kernel/bpf/lpm_trie.c:569:22: expected struct lpm_trie_node
kernel/bpf/lpm_trie.c:569:22: got struct lpm_trie_node
kernel/bpf/lpm_trie.c:577:38: sparse: incorrect type in assignment (different address spaces) @@ expected struct lpm_trie_node @@ got struct lpm_trie_nstruct lpm_trie_node @@ kernel/bpf/lpm_trie.c:577:38: expected struct lpm_trie_node
kernel/bpf/lpm_trie.c:577:38: got struct lpm_trie_node
kernel/bpf/lpm_trie.c:582:38: sparse: incorrect type in assignment (different address spaces) @@ expected struct lpm_trie_node @@ got struct lpm_trie_nstruct lpm_trie_node @@ kernel/bpf/lpm_trie.c:582:38: expected struct lpm_trie_node
kernel/bpf/lpm_trie.c:582:38: got struct lpm_trie_node
vim +569 kernel/bpf/lpm_trie.c
b95a5c4db Daniel Mack 2017-01-21 551
b95a5c4db Daniel Mack 2017-01-21 552 static void trie_free(struct bpf_map *map)
b95a5c4db Daniel Mack 2017-01-21 553 {
b95a5c4db Daniel Mack 2017-01-21 554 struct lpm_trie *trie = container_of(map, struct lpm_trie, map);
ca073514f Yonghong Song 2018-02-21 555 struct lpm_trie_node **slot;
b95a5c4db Daniel Mack 2017-01-21 556 struct lpm_trie_node *node;
b95a5c4db Daniel Mack 2017-01-21 557
9a3efb6b6 Yonghong Song 2018-02-13 558 /* Wait for outstanding programs to complete
9a3efb6b6 Yonghong Song 2018-02-13 559 * update/lookup/delete/get_next_key and free the trie.
9a3efb6b6 Yonghong Song 2018-02-13 560 */
9a3efb6b6 Yonghong Song 2018-02-13 561 synchronize_rcu();
b95a5c4db Daniel Mack 2017-01-21 562
b95a5c4db Daniel Mack 2017-01-21 563 /* Always start at the root and walk down to a node that has no
b95a5c4db Daniel Mack 2017-01-21 564 * children. Then free that node, nullify its reference in the parent
b95a5c4db Daniel Mack 2017-01-21 565 * and start over.
b95a5c4db Daniel Mack 2017-01-21 566 */
b95a5c4db Daniel Mack 2017-01-21 567
b95a5c4db Daniel Mack 2017-01-21 568 for (;;) {
b95a5c4db Daniel Mack 2017-01-21 @569 slot = &trie->root;
b95a5c4db Daniel Mack 2017-01-21 570
b95a5c4db Daniel Mack 2017-01-21 571 for (;;) {
ca073514f Yonghong Song 2018-02-21 572 node = *slot;
b95a5c4db Daniel Mack 2017-01-21 573 if (!node)
9a3efb6b6 Yonghong Song 2018-02-13 574 goto out;
b95a5c4db Daniel Mack 2017-01-21 575
ca073514f Yonghong Song 2018-02-21 576 if (node->child[0]) {
b95a5c4db Daniel Mack 2017-01-21 577 slot = &node->child[0];
b95a5c4db Daniel Mack 2017-01-21 578 continue;
b95a5c4db Daniel Mack 2017-01-21 579 }
b95a5c4db Daniel Mack 2017-01-21 580
ca073514f Yonghong Song 2018-02-21 581 if (node->child[1]) {
b95a5c4db Daniel Mack 2017-01-21 582 slot = &node->child[1];
b95a5c4db Daniel Mack 2017-01-21 583 continue;
b95a5c4db Daniel Mack 2017-01-21 584 }
b95a5c4db Daniel Mack 2017-01-21 585
b95a5c4db Daniel Mack 2017-01-21 586 kfree(node);
ca073514f Yonghong Song 2018-02-21 587 *slot = NULL;
b95a5c4db Daniel Mack 2017-01-21 588 break;
b95a5c4db Daniel Mack 2017-01-21 589 }
b95a5c4db Daniel Mack 2017-01-21 590 }
b95a5c4db Daniel Mack 2017-01-21 591
9a3efb6b6 Yonghong Song 2018-02-13 592 out:
9a3efb6b6 Yonghong Song 2018-02-13 593 kfree(trie);
b95a5c4db Daniel Mack 2017-01-21 594 }
b95a5c4db Daniel Mack 2017-01-21 595 :::::: The code at line 569 was first introduced by commit
:::::: b95a5c4db09bc7c253636cb84dc9b12c577fd5a0 bpf: add a longest prefix match trie map implementation
:::::: TO: Daniel Mack <[email protected]>
:::::: CC: David S. Miller <[email protected]>
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation