public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: Yonghong Song <yhs@fb.com>
To: G <chapterk93@163.com>, <bpf@vger.kernel.org>
Subject: Re: using bpf_map_update_elem and bpf_map_get_next_key at the same time when looping through the hash map
Date: Tue, 6 Jul 2021 11:46:04 -0700	[thread overview]
Message-ID: <87a28f77-19c0-636a-3c79-a2c4eec17d81@fb.com> (raw)
In-Reply-To: <51e18157.22f7.17a79cc5306.Coremail.chapterk93@163.com>



On 7/5/21 8:11 PM, G wrote:
> Hi BPF Experts
> 
> I'm having an issue with using "bpf_map_update_elem" and  "bpf_map_get_next_key" at the same time when looping through the bpf HashMap.
> My program turns to an infinite loop and the pseudocode is as following:
> ------------------------------------------------------------------------------
>      bpf.MapCreate          // type=BPF_MAP_TYPE_HASH size=128
>      for { bpf.MapUpdate }  // add(update) 128 elements at once
> 
>      then loop through the map to update each element
>      bpf.MapGetNextKey(fd, nil, &scankey) // find first key
>      for {
>            bpf.MapUpate(fd, &scankey, &val, BPF_EXIST)
>            bpf.MapGetNextKey(fd, &scankey, &scankey)
>      }
> ------------------------------------------------------------------------------
> 
> I have tried to read the relevant kernel code, and seems like it is moving the element to the top of the has bucket when calling the “bpf_map_update_elem” even the element already exists in the hash map. See the following source code:
> ------------------------------------------------------------------------------
>      // kernel/bpf/hashtab.c
>      htab_map_update_elem {
>          ...
>         /* add new element to the head of the list, so that
>          * concurrent search will find it before old elem
>          */
>         hlist_nulls_add_head_rcu(&l_new->hash_node, head);
>          ...
>      }
> ------------------------------------------------------------------------------
> 
> Therefore, when I was trying to traversing the two elements in the same hash a bucket, it ran into an infinite loop by repeatedly getting the key of these two elements. Not sure my understanding for "bpf_map_update_elem"and "bpf_map_get_next_key" is correct or not. My question is: is that behave as the design? or is it a bug for the bpf hashmap? Please let me know, thanks.

bpf_map_get_next_key() is added after bpf_map_update_elem(). So the 
above behavior is in the kernel already for sometimes.

bpf_map_get_next_key() is not super reliable for hash table as if some 
deletion happens, the get_next_key may start from the beginning.
The recommendation is to use bpf_map_*_batch() interface.
If your kernel does not implement bpf_map_*_batch() interface, I think
it would be best you call bpf_map_get_next_key() for ALL elements before
doing any update/delete.

> 
> Best regards
> W.Gao
> 

      reply	other threads:[~2021-07-06 18:46 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-06  3:11 using bpf_map_update_elem and bpf_map_get_next_key at the same time when looping through the hash map G
2021-07-06 18:46 ` Yonghong Song [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87a28f77-19c0-636a-3c79-a2c4eec17d81@fb.com \
    --to=yhs@fb.com \
    --cc=bpf@vger.kernel.org \
    --cc=chapterk93@163.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox