Skip to content

bpf: map pre-alloc

Alexei Starovoitov lkml netdev 2 versions – In review

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v2 Latest9 of 12 patches 4 repliesIn review
  2. v19 patches 5 repliesSuperseded

Changes in v2 from the cover letter

. fix few issues spotted by Daniel
. converted stackmap into pre-allocation as well
. added a workaround for lockdep false positive
. added pcpu_freelist_populate to be used by hashmap and stackmap

9 of 12 patches are in this archive. The cover letter announces 12.

Patches in v2 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/12bpf: prevent kprobe+bpf deadlocksinclude/linux/bpf.h kernel/bpf/syscall.c kernel/trace/bpf_trace.c+16 -20 replies
2/12bpf: introduce percpu_freelistkernel/bpf/Makefile kernel/bpf/percpu_freelist.c kernel/bpf/percpu_freelist.h+132 -10 replies
3/12bpf: pre-allocate hash map elementsinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/hashtab.c +1 more+186 -740 replies
4/12bpf: check for reserved flag bits in array and stack mapskernel/bpf/arraymap.c kernel/bpf/stackmap.c+4 -10 replies
5/12bpf: convert stackmap to pre-allocationinclude/linux/bpf.h kernel/bpf/stackmap.c kernel/bpf/syscall.c+71 -180 replies
6/12samples/bpf: make map creation more verbosesamples/bpf/bpf_load.c+4 -10 replies
7/12samples/bpf: move ksym_search() into librarysamples/bpf/bpf_load.c samples/bpf/bpf_load.h samples/bpf/offwaketime_user.c+69 -660 replies
8/12samples/bpf: add map_flags to bpf loadersamples/bpf/bpf_helpers.h samples/bpf/bpf_load.c samples/bpf/fds_example.c +5 more+23 -150 replies
9/12samples/bpf: test both pre-alloc and normal mapssamples/bpf/test_maps.c+9 -10 replies

Reviews trailers per patch

1 of 9 patches has a Reviewed-by or Acked-by.

PatchDaniel Borkmann
1/12A-b
2/12
3/12
4/12
5/12
6/12
7/12
8/12
9/12

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v2 thread

Cover letter [PATCH v2 net-next 0/12] bpf: map pre-alloc

v1->v2:
. fix few issues spotted by Daniel
. converted stackmap into pre-allocation as well
. added a workaround for lockdep false positive
. added pcpu_freelist_populate to be used by hashmap and stackmap

this path set switches bpf hash map to use pre-allocation by default and introduces BPF_F_NO_PREALLOC flag to keep old behavior for cases where full map pre-allocation is too memory expensive.

Some time back Daniel Wagner reported crashes when bpf hash map is used to compute time intervals between preempt_disable->preempt_enable and recently Tom Zanussi reported a dead lock in iovisor/bcc/funccount tool if it's used to count the number of invocations of kernel '*spin*' functions. Both problems are due to the recursive use of slub and can only be solved by pre-allocating all map elements.

A lot of different solutions were considered. Many implemented, but at the end pre-allocation seems to be the only feasible answer. As far as pre-allocation goes it also was implemented 4 different ways:
- simple free-list with single lock
- percpu_ida with optimizations
- blk-mq-tag variant customized for bpf use case
- percpu_freelist
For bpf style of alloc/free patterns percpu_freelist is the best and implemented in this patch set.
Detailed performance numbers in patch 3.
Patch 2 introduces percpu_freelist
Patch 1 fixes simple deadlocks due to missing recursion checks Patch 5: converts stackmap to pre-allocation
Patches 6-9: prepare test infra
Patch 10: stress test for hash map infra. It attaches to spin_lock functions and bpf_map_update/delete are called from different contexts Patch 11: stress for bpf_get_stackid
Patch 12: map performance test

Alexei Starovoitov (12):
bpf: prevent kprobe+bpf deadlocks
bpf: introduce percpu_freelist
bpf: pre-allocate hash map elements
bpf: check for reserved flag bits in array and stack maps
bpf: convert stackmap to pre-allocation samples/bpf: make map creation more verbose samples/bpf: move ksym_search() into library samples/bpf: add map_flags to bpf loader samples/bpf: test both pre-alloc and normal maps samples/bpf: add bpf map stress test samples/bpf: stress test bpf_get_stackid samples/bpf: add map performance test

 include/linux/bpf.h              |   6 +
 include/uapi/linux/bpf.h         |   3 +
 kernel/bpf/Makefile              |   2 +-
 kernel/bpf/arraymap.c            |   2 +-
 kernel/bpf/hashtab.c             | 240 +++++++++++++++++++++++++++------------
 kernel/bpf/percpu_freelist.c     | 100 ++++++++++++++++
 kernel/bpf/percpu_freelist.h     |  31 +++++
 kernel/bpf/stackmap.c            |  89 ++++++++++++---
 kernel/bpf/syscall.c             |  30 ++++-
 kernel/trace/bpf_trace.c         |   2 -
 samples/bpf/Makefile             |   8 ++
 samples/bpf/bpf_helpers.h        |   1 +
 samples/bpf/bpf_load.c           |  70 +++++++++++-
 samples/bpf/bpf_load.h           |   6 +
 samples/bpf/fds_example.c        |   2 +-
 samples/bpf/libbpf.c             |   5 +-
 samples/bpf/libbpf.h             |   2 +-
 samples/bpf/map_perf_test_kern.c | 100 ++++++++++++++++
 samples/bpf/map_perf_test_user.c | 155 +++++++++++++++++++++++++
 samples/bpf/offwaketime_user.c   |  67 +----------
 samples/bpf/sock_example.c       |   2 +-
 samples/bpf/spintest_kern.c      |  68 +++++++++++
 samples/bpf/spintest_user.c      |  50 ++++++++
 samples/bpf/test_maps.c          |  29 +++--
 samples/bpf/test_verifier.c      |   4 +-
 25 files changed, 895 insertions(+), 179 deletions(-)
 create mode 100644 kernel/bpf/percpu_freelist.c
 create mode 100644 kernel/bpf/percpu_freelist.h
 create mode 100644 samples/bpf/map_perf_test_kern.c
 create mode 100644 samples/bpf/map_perf_test_user.c
 create mode 100644 samples/bpf/spintest_kern.c
 create mode 100644 samples/bpf/spintest_user.c
Signature · 1 line
2.8.0.rc1