public inbox for bpf@vger.kernel.org
 help / color / mirror / Atom feed
From: G <chapterk93@163.com>
To: bpf@vger.kernel.org
Subject: 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:11:11 +0800 (CST)	[thread overview]
Message-ID: <51e18157.22f7.17a79cc5306.Coremail.chapterk93@163.com> (raw)

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.

Best regards
W.Gao

             reply	other threads:[~2021-07-06  3:11 UTC|newest]

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

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=51e18157.22f7.17a79cc5306.Coremail.chapterk93@163.com \
    --to=chapterk93@163.com \
    --cc=bpf@vger.kernel.org \
    /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