* Potential memory leak in htab_map_update_elem?
@ 2019-01-11 5:08 Kris Van Hees
2019-01-11 8:12 ` Daniel Borkmann
0 siblings, 1 reply; 3+ messages in thread
From: Kris Van Hees @ 2019-01-11 5:08 UTC (permalink / raw)
To: netdev
Maybe I am missing something trivial here, but it looks to me that there is
a leak of htab elements in htab_map_update_elem when you are updating an
existing element. After the new element is linked into the bucket list, the
following code snippet is found:
if (l_old) {
hlist_nulls_del_rcu(&l_old->hash_node);
if (!htab_is_prealloc(htab))
free_htab_elem(htab, l_old);
}
Nothing is done with l_old in the remainder of the function, and to me this
looks like that element is be leaked if the htab is preallocated because we
never add it to the free list. In fact, free_htab_elem() contains the very
conditional that handles the two cases (preallocated vs non-preallocated.
Cheers,
Kris
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Potential memory leak in htab_map_update_elem?
2019-01-11 5:08 Potential memory leak in htab_map_update_elem? Kris Van Hees
@ 2019-01-11 8:12 ` Daniel Borkmann
2019-01-11 15:20 ` Kris Van Hees
0 siblings, 1 reply; 3+ messages in thread
From: Daniel Borkmann @ 2019-01-11 8:12 UTC (permalink / raw)
To: Kris Van Hees; +Cc: netdev, ast
Hi Kris,
On 01/11/2019 06:08 AM, Kris Van Hees wrote:
> Maybe I am missing something trivial here, but it looks to me that there is
> a leak of htab elements in htab_map_update_elem when you are updating an
> existing element. After the new element is linked into the bucket list, the
> following code snippet is found:
>
> if (l_old) {
> hlist_nulls_del_rcu(&l_old->hash_node);
> if (!htab_is_prealloc(htab))
> free_htab_elem(htab, l_old);
> }
>
> Nothing is done with l_old in the remainder of the function, and to me this
> looks like that element is be leaked if the htab is preallocated because we
> never add it to the free list. In fact, free_htab_elem() contains the very
> conditional that handles the two cases (preallocated vs non-preallocated.
In this case in alloc_htab_elem() we are swapping out the per-cpu extra element
with the existing one to avoid freelist_pop/push combination in order to have
a fast replace, meaning we cannot call free_htab_elem() on it like in plain
htab_map_delete_elem() case.
Thanks,
Daniel
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Potential memory leak in htab_map_update_elem?
2019-01-11 8:12 ` Daniel Borkmann
@ 2019-01-11 15:20 ` Kris Van Hees
0 siblings, 0 replies; 3+ messages in thread
From: Kris Van Hees @ 2019-01-11 15:20 UTC (permalink / raw)
To: Daniel Borkmann; +Cc: Kris Van Hees, netdev, ast
On Fri, Jan 11, 2019 at 09:12:57AM +0100, Daniel Borkmann wrote:
> Hi Kris,
>
> On 01/11/2019 06:08 AM, Kris Van Hees wrote:
> > Maybe I am missing something trivial here, but it looks to me that there is
> > a leak of htab elements in htab_map_update_elem when you are updating an
> > existing element. After the new element is linked into the bucket list, the
> > following code snippet is found:
> >
> > if (l_old) {
> > hlist_nulls_del_rcu(&l_old->hash_node);
> > if (!htab_is_prealloc(htab))
> > free_htab_elem(htab, l_old);
> > }
> >
> > Nothing is done with l_old in the remainder of the function, and to me this
> > looks like that element is be leaked if the htab is preallocated because we
> > never add it to the free list. In fact, free_htab_elem() contains the very
> > conditional that handles the two cases (preallocated vs non-preallocated.
>
> In this case in alloc_htab_elem() we are swapping out the per-cpu extra element
> with the existing one to avoid freelist_pop/push combination in order to have
> a fast replace, meaning we cannot call free_htab_elem() on it like in plain
> htab_map_delete_elem() case.
Aha, and since it is a per-cpu extra element, you never run into a problem
with two updates using this trick because only one update can ever be ongoing
per cpu. Nifty!
Thanks for explaining!
Kris
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2019-01-11 15:21 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-01-11 5:08 Potential memory leak in htab_map_update_elem? Kris Van Hees
2019-01-11 8:12 ` Daniel Borkmann
2019-01-11 15:20 ` Kris Van Hees
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).