bpf: fix incorrect kmalloc usage in lpm_trie MAP_GET_NEXT_KEY rcu region
Subsystems (3)
PATCH 1/1 bpf: fix incorrect kmalloc usage in lpm_trie MAP_GET_NEXT_KEY rcu region
In commit b471f2f1de8b ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE map"), the implemented MAP_GET_NEXT_KEY callback function is guarded with rcu read lock. In the function body, "kmalloc(size, GFP_USER | __GFP_NOWARN)" is used which may sleep and violate rcu read lock region requirements. This patch fixed the issue by using GFP_ATOMIC instead to avoid blocking kmalloc. Tested with CONFIG_DEBUG_ATOMIC_SLEEP=y as suggested by Eric Dumazet.
--- kernel/bpf/lpm_trie.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/bpf/lpm_trie.c +1 -1
@@ -624,7 +624,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key) } node_stack =kmalloc (trie->max_prefixlen *sizeof (struct lpm_trie_node *), -GFP_USER | __GFP_NOWARN); +GFP_ATOMIC | __GFP_NOWARN);if (!node_stack)return -ENOMEM ;
Signature · 1 line
2.9.5
Quoted 9 lines · Yonghong Song
In commit
b471f2f1de8b("bpf: implementMAP_GET_NEXT_KEYcommand forLPM_TRIEmap"), the implementedMAP_GET_NEXT_KEYcallback function is guarded with rcu read lock. In the function body, "kmalloc(size, GFP_USER | __GFP_NOWARN)" is used which may sleep and violate rcu read lock region requirements. This patch fixed the issue by usingGFP_ATOMICinstead to avoid blocking kmalloc. Tested withCONFIG_DEBUG_ATOMIC_SLEEP=yas suggested by Eric Dumazet.Fixes b471f2f1de8b ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE map")Signed-off-by Yonghong Song <[email protected]>
Thanks.
Quoted 17 lines
--- kernel/bpf/lpm_trie.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)kernel/bpf/lpm_trie.c +1
-1@@ -624,7 +624,7 @@ static int trie_get_next_key(struct bpf_map *map, void *_key, void *_next_key) } node_stack =kmalloc (trie->max_prefixlen *sizeof (struct lpm_trie_node *), -GFP_USER | __GFP_NOWARN); +GFP_ATOMIC | __GFP_NOWARN);if (!node_stack)return -ENOMEM ;
Quoted 8 lines · Eric Dumazet
Quoted 7 lines · Yonghong Song
In commit
b471f2f1de8b("bpf: implementMAP_GET_NEXT_KEYcommand forLPM_TRIEmap"), the implementedMAP_GET_NEXT_KEYcallback function is guarded with rcu read lock. In the function body, "kmalloc(size, GFP_USER | __GFP_NOWARN)" is used which may sleep and violate rcu read lock region requirements. This patch fixed the issue by usingGFP_ATOMICinstead to avoid blocking kmalloc. Tested withCONFIG_DEBUG_ATOMIC_SLEEP=yas suggested by Eric Dumazet.
Quoted 5 lines
Quoted 2 lines
Fixes b471f2f1de8b ("bpf: implement MAP_GET_NEXT_KEY command for LPM_TRIE map")Signed-off-by Yonghong Song <[email protected]>Reported-by syzbot <[email protected]>Reviewed-by Eric Dumazet <[email protected]>
Applied to bpf-next, thanks everyone!