bpf: fix memory leak in lpm_trie map_free callback function
Subsystems (3)
PATCH 1/1 bpf: fix memory leak in lpm_trie map_free callback function
There is a memory leak happening in lpm_trie map_free callback function trie_free. The trie structure itself does not get freed.
Also, trie_free function did not do synchronize_rcu before freeing various data structures. This is incorrect as some rcu_read_lock region(s) for lookup, update, delete or get_next_key may not complete yet. The fix is to add synchronize_rcu in the beginning of trie_free. The useless spin_lock is removed from this function as well.
--- kernel/bpf/lpm_trie.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)
kernel/bpf/lpm_trie.c +7 -2
@@ -555,7 +555,12 @@ static void trie_free(struct bpf_map *map)struct lpm_trie_node __rcu **slot;struct lpm_trie_node *node; -raw_spin_lock (&trie->lock); +/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, +* so the programs (can be more than one that used this map) were +* disconnected from events. Wait for outstanding programs to complete +* update/lookup/delete/get_next_key and free the trie. +*/ +synchronize_rcu ();/* Always start at the root and walk down to a node that has no * children. Then free that node, nullify its reference in the parent
@@ -588,7 +593,7 @@ static void trie_free(struct bpf_map *map) } unlock: -raw_spin_unlock (&trie->lock); +kfree (trie); }static int trie_get_next_key (struct bpf_map *map,void *_key,void *_next_key)
Signature · 1 line
2.9.5
Quoted 24 lines · Yonghong Song
There is a memory leak happening in
lpm_triemap_freecallback functiontrie_free. The trie structure itself does not get freed.Also,
trie_freefunction did not dosynchronize_rcubefore freeing various data structures. This is incorrect as somercu_read_lockregion(s) for lookup, update, delete orget_next_keymay not complete yet. The fix is to addsynchronize_rcuin the beginning oftrie_free. The uselessspin_lockis removed from this function as well.Fixes b95a5c4db09b ("bpf: add a longest prefix match trie map implementation")Reported-by Mathieu Malaterre <[email protected]>Reported-by Alexei Starovoitov <[email protected]>Tested-by Mathieu Malaterre <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/lpm_trie.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)kernel/bpf/lpm_trie.c +0
-0@@ -555,7 +555,12 @@ static void trie_free(struct bpf_map *map)struct lpm_trie_node __rcu **slot;
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 564
Quoted 10 lines
kernel/bpf/lpm_trie.c +6
-1struct lpm_trie_node *node; -raw_spin_lock (&trie->lock); +/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, +* so the programs (can be more than one that used this map) were +* disconnected from events. Wait for outstanding programs to complete +* update/lookup/delete/get_next_key and free the trie. +*/ +synchronize_rcu ();
Please do not do that.
Use kfree_rcu() instead (adding one struct rcu_head in struct lpm_trie)
Quoted 11 lines
kernel/bpf/lpm_trie.c +1
-1/* Always start at the root and walk down to a node that has no * children. Then free that node, nullify its reference in the parent @@ -588,7 +593,7 @@ static void trie_free(struct bpf_map *map) } unlock: -raw_spin_unlock (&trie->lock); +kfree (trie); }static int trie_get_next_key (struct bpf_map *map,void *_key,void *_next_key)
Quoted 38 lines · Eric Dumazet
Quoted 24 lines · Yonghong Song
There is a memory leak happening in
lpm_triemap_freecallback functiontrie_free. The trie structure itself does not get freed.Also,
trie_freefunction did not dosynchronize_rcubefore freeing various data structures. This is incorrect as somercu_read_lockregion(s) for lookup, update, delete orget_next_keymay not complete yet. The fix is to addsynchronize_rcuin the beginning oftrie_free. The uselessspin_lockis removed from this function as well.Fixes b95a5c4db09b ("bpf: add a longest prefix match trie map implementation")Reported-by Mathieu Malaterre <[email protected]>Reported-by Alexei Starovoitov <[email protected]>Tested-by Mathieu Malaterre <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/lpm_trie.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)kernel/bpf/lpm_trie.c +0
-0@@ -555,7 +555,12 @@ static void trie_free(struct bpf_map *map)struct lpm_trie_node __rcu **slot;kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 564
Quoted 10 lines
kernel/bpf/lpm_trie.c +6
-1struct lpm_trie_node *node; -raw_spin_lock (&trie->lock); +/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, +* so the programs (can be more than one that used this map) were +* disconnected from events. Wait for outstanding programs to complete +* update/lookup/delete/get_next_key and free the trie. +*/ +synchronize_rcu ();Please do not do that.
Quoted 1 line
Use
kfree_rcu()instead (adding one structrcu_headin structlpm_trie)
Oh well, I take this back. It looks we heavily use synchronize_rcu() all over the places for ->map_free() already.
Daniel Borkmann
maintainerHi Yonghong,
Quoted 23 lines · Yonghong Song
There is a memory leak happening in
lpm_triemap_freecallback functiontrie_free. The trie structure itself does not get freed.Also,
trie_freefunction did not dosynchronize_rcubefore freeing various data structures. This is incorrect as somercu_read_lockregion(s) for lookup, update, delete orget_next_keymay not complete yet. The fix is to addsynchronize_rcuin the beginning oftrie_free. The uselessspin_lockis removed from this function as well.Fixes b95a5c4db09b ("bpf: add a longest prefix match trie map implementation")Reported-by Mathieu Malaterre <[email protected]>Reported-by Alexei Starovoitov <[email protected]>Tested-by Mathieu Malaterre <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/lpm_trie.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)kernel/bpf/lpm_trie.c +0
-0
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 562
Quoted 9 lines
kernel/bpf/lpm_trie.c +5
-1@@ -555,7 +555,12 @@ static void trie_free(struct bpf_map *map)struct lpm_trie_node __rcu **slot;struct lpm_trie_node *node; -raw_spin_lock (&trie->lock); +/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, +* so the programs (can be more than one that used this map) were +* disconnected from events. Wait for outstanding programs to complete +* update/lookup/delete/get_next_key and free the trie. +*/
I think the first part of the comment is slightly misleading, e.g. map refcount could drop to zero also for various other reasons, not strictly due to prog refcnt dropping to 0, so I would just keep the second part with 'Wait for outstanding [...]' which is okay since this is eventually what is relevant in this context.
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 591
Quoted 9 lines
kernel/bpf/lpm_trie.c +1
-1+synchronize_rcu ();/* Always start at the root and walk down to a node that has no * children. Then free that node, nullify its reference in the parent @@ -588,7 +593,7 @@ static void trie_free(struct bpf_map *map) } unlock: -raw_spin_unlock (&trie->lock);
Could you do a minor change here: since we get rid of the locking as there's no user left anymore after grace period, it would be great if you could also change the 'unlock' label name above.
Other than that, good to go, thanks!
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 599
Quoted 5 lines
kernel/bpf/lpm_trie.c +1
-0+kfree (trie); }static int trie_get_next_key (struct bpf_map *map,void *_key,void *_next_key)
Thanks,
Daniel
Quoted 29 lines · Daniel Borkmann
Hi Yonghong,
Quoted 26 lines · Yonghong Song
There is a memory leak happening in
lpm_triemap_freecallback functiontrie_free. The trie structure itself does not get freed.Also,
trie_freefunction did not dosynchronize_rcubefore freeing various data structures. This is incorrect as somercu_read_lockregion(s) for lookup, update, delete orget_next_keymay not complete yet. The fix is to addsynchronize_rcuin the beginning oftrie_free. The uselessspin_lockis removed from this function as well.Fixes b95a5c4db09b ("bpf: add a longest prefix match trie map implementation")Reported-by Mathieu Malaterre <[email protected]>Reported-by Alexei Starovoitov <[email protected]>Tested-by Mathieu Malaterre <[email protected]>Signed-off-by Yonghong Song <[email protected]>--- kernel/bpf/lpm_trie.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-)kernel/bpf/lpm_trie.c +0
-0@@ -555,7 +555,12 @@ static void trie_free(struct bpf_map *map)struct lpm_trie_node __rcu **slot;struct lpm_trie_node *node;
Quoted 10 lines
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 562
Quoted 6 lines
kernel/bpf/lpm_trie.c +5
-1-raw_spin_lock (&trie->lock); +/* at this point bpf_prog->aux->refcnt == 0 and this map->refcnt == 0, +* so the programs (can be more than one that used this map) were +* disconnected from events. Wait for outstanding programs to complete +* update/lookup/delete/get_next_key and free the trie. +*/ I think the first part of the comment is slightly misleading, e.g. map refcount could drop to zero also for various other reasons, not strictly due to prog refcnt dropping to 0, so I would just keep the second part with 'Wait for
Oh, yes. Make sense. Copy-paste without thinking.
We have similar comments in virtually all other places of using synchronize_rcu under kernel/bpf. This can be cleaned up later though.
Quoted 2 lines
outstanding [...]' which is okay since this is eventually what is relevant in this context.
Will do.
Quoted 8 lines
Quoted 8 lines
kernel/bpf/lpm_trie.c +1
-0+synchronize_rcu ();/* Always start at the root and walk down to a node that has no * children. Then free that node, nullify its reference in the parent @@ -588,7 +593,7 @@ static void trie_free(struct bpf_map *map) }
Quoted 6 lines
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 591
Quoted 2 lines
kernel/bpf/lpm_trie.c +0
-1unlock: -raw_spin_unlock (&trie->lock);Could you do a minor change here: since we get rid of the locking as there's no user left anymore after grace period, it would be great if you could also change the 'unlock' label name above.
Will do.
Quoted 12 lines
Other than that, good to go, thanks!
kernel/bpf/lpm_trie.c static void trie_free(struct bpf_map *map) · line 599
Quoted 5 lines
kernel/bpf/lpm_trie.c +1
-0+kfree (trie); }static int trie_get_next_key (struct bpf_map *map,void *_key,void *_next_key)Thanks,
Daniel