mbox series

[bpf-next,v4,0/3] bpf: introduce timeout hash map

Message ID 20210117042224.17839-1-xiyou.wangcong@gmail.com
Headers show
Series bpf: introduce timeout hash map | expand

Message

Cong Wang Jan. 17, 2021, 4:22 a.m. UTC
From: Cong Wang <cong.wang@bytedance.com>

This patchset introduces a new eBPF hash map whose elements have
timeouts. Patch 1 is the implementation of timeout map, patch 2 adds
some test cases for timeout map in test_maps, and patch 3 adds a test
case in map ptr test.

Please check each patch description for more details.

---
v4: merge gc_work into gc_idle_work to avoid a nasty race condition
    fix a potential use-after-free
    add one more test case
    improve comments and update changelog

v3: move gc list from bucket to elem
    reuse lru_node in struct htab_elem
    drop patches which are no longer necessary
    fix delete path
    add a test case for delete path
    add parallel test cases
    change timeout to ms
    drop batch ops

v2: fix hashmap ptr test
    add a test case in map ptr test
    factor out htab_timeout_map_alloc()

Cong Wang (3):
  bpf: introduce timeout hash map
  selftests/bpf: add test cases for bpf timeout map
  selftests/bpf: add timeout map check in map_ptr tests

 include/linux/bpf_types.h                     |   1 +
 include/uapi/linux/bpf.h                      |   5 +-
 kernel/bpf/hashtab.c                          | 239 +++++++++++++++++-
 kernel/bpf/syscall.c                          |   3 +-
 tools/include/uapi/linux/bpf.h                |   1 +
 .../selftests/bpf/progs/map_ptr_kern.c        |  20 ++
 tools/testing/selftests/bpf/test_maps.c       |  47 ++++
 7 files changed, 307 insertions(+), 9 deletions(-)

Comments

Cong Wang Jan. 18, 2021, 7:10 p.m. UTC | #1
On Sat, Jan 16, 2021 at 8:22 PM Cong Wang <xiyou.wangcong@gmail.com> wrote:
> +static void htab_gc(struct work_struct *work)

> +{

> +       struct htab_elem *e, *tmp;

> +       struct llist_node *lhead;

> +       struct bpf_htab *htab;

> +       int i, count;

> +

> +       htab = container_of(work, struct bpf_htab, gc_work.work);

> +       lhead = llist_del_all(&htab->gc_list);

> +

> +       llist_for_each_entry_safe(e, tmp, lhead, gc_node) {

> +               unsigned long flags;

> +               struct bucket *b;

> +               u32 hash;

> +

> +               hash = e->hash;

> +               b = __select_bucket(htab, hash);

> +               if (htab_lock_bucket(htab, b, hash, &flags))

> +                       continue;

> +               hlist_nulls_del_rcu(&e->hash_node);

> +               atomic_set(&e->pending, 0);

> +               free_htab_elem(htab, e);

> +               htab_unlock_bucket(htab, b, hash, flags);

> +

> +               cond_resched();

> +       }

> +

> +       for (count = 0, i = 0; i < htab->n_buckets; i++) {


I just realized a followup fix is not folded into this patch, I
actually added a timestamp check here to avoid scanning the whole
table more frequently than once per second. It is clearly my mistake
to miss it when formatting this patchset.

I will send v5 after waiting for other feedback.

Thanks!