[PATCH bpf-next v1 00/25] Local kptrs, BPF linked lists

Kumar Kartikeya Dwivedi
2022-10-11T01:23:31Z

Hi, this is the non-RFC v1. This is mostly a major rewrite of the previous set,
hence complete review of some of these patches is needed again. I have cut down
the series to only the specific pieces needed to enable other work (like Dave's
RB-Tree). The rest will come as a follow up to this.

--

This series introduces user defined BPF objects, by introducing the idea of
local kptrs. These are kptrs (strongly typed pointers) that refer to objects of
a user defined type, hence called "local" kptrs. This allows BPF programs to
allocate their own objects, build their own object hierarchies, and use the
basic building blocks provided by BPF runtime to build their own data structures
flexibly.

Then, we introduce the support for single ownership BPF linked lists, which can
be put inside BPF maps, or local kptrs, and hold such allocated local kptrs as
elements. It works as an instrusive collection, which is done to allow making
local kptrs part of multiple data structures at the same time in the future.

The eventual goal of this and future patches is to allow one to do some limited
form of kernel style programming in BPF C, and allow programmers to build their
own complex data structures flexibly out of basic building blocks.

The key difference will be that such programs are verified to be safe, preserve
runtime integrity of the system, and are proven to be bug free as far as the
invariants of BPF specific APIs are concerned.

One immediate use case that will be using the entire infrastructure this series
is introducing will be managing percpu NMI safe linked lists inside BPF
programs.

The other use case this will serve in the near future will be linking kernel
structures like XDP frame and sk_buff directly into user data structures
(rbtree, pifomap, etc.) for packet queueing. This will follow single ownership
concept included in this series.

The user has complete control of the internal locking, and hence also the
batching of operations for each critical section.

The features are:
- Local kptrs - User defined kernel objects.
- bpf_kptr_new, bpf_kptr_drop to allocate and free them.
- Single ownership BPF linked lists.
- Support for them in BPF maps.
- Support for them in local kptrs.
- Global spin locks.
- Spin locks inside local kptrs.
- Allow storing local kptrs in all BPF maps with support for kernel kptrs.

Some other notable things:
- Completely static verification of locking.
- Kfunc argument handling has been completely reworked.
- Argument rewriting support for kfuncs.
- Search pruning now understands non-size precise registers.
- A new bpf_experimental.h header as a dumping ground for these APIs.

Any functionality exposed in this series is NOT part of UAPI. It is only
available through use of kfuncs, and structs that can be added to map value may
also change their size or name in the future. Hence, every feature in this
series must be considered experimental.

Follow-ups:
-----------
* Support for kptrs (local and kernel) in local storage and percpu maps + kptr tests
* Fixes that rebase on top of refactorings in this series for dynptrs
and helper access checks (not included since unrelated, and this is too big already)

Next steps:
-----------
* NMI safe percpu single ownership linked lists (using local_t protection).
* Lockless linked lists.
* Allow RCU protected local kptrs. This then allows RCU protected list lookups,
since spinlock protection for readers does not scale.
* Introduce explicit RCU read sections (using kfuncs).
* Introduce bpf_refcount for local kptrs, shared ownership.
* Introduce shared ownership linked lists.
* Documentation.

Notes:
------

* Dave's patch for libbpf sections is still needed for this to move forward.

Changelog:
----------
RFC v1 -> v1
RFC v1: https://lore.kernel.org/bpf/20220904204145.3089-1-memxor@gmail.com

* Mostly a complete rewrite of BTF parsing, refactor existing code (Kartikeya)
* Rebase kfunc rewrite for bpf-next, add support for more changes
* Cache type metadata in BTF to avoid recomputation inside verifier (Kartikeya)
* Remove __kernel tag, make things similar to map values, reserve bpf_ prefix
* bpf_kptr_new, bpf_kptr_drop (Alexei)
* Rename precision state enum values (Alexei)
* Drop explicit constructor/destructor support (Alexei)
* Rewrite code for constructing/destructing objects and offload to runtime
* Minimize duplication in bpf_map_value_off_desc handling (Alexei)
* Expose global memory allocator (Alexei)
* Address other nits from Alexei
* Split out local kptrs in maps, more kptrs in maps support into a follow up

Links:
------
* Dave's BPF RB-Tree RFC series
v1 (Discussion thread)
https://lore.kernel.org/bpf/20220722183438.3319790-1-davemarchevsky@fb.com
v2 (With support for static locks)
https://lore.kernel.org/bpf/20220830172759.4069786-1-davemarchevsky@fb.com
* BPF Linked Lists Discussion
https://lore.kernel.org/bpf/CAP01T74U30+yeBHEgmgzTJ-XYxZ0zj71kqCDJtTH9YQNfTK+Xw@mail.gmail.com
* BPF Memory Allocator from Alexei
https://lore.kernel.org/bpf/20220902211058.60789-1-alexei.starovoitov@gmail.com
* BPF Memory Allocator UAPI Discussion
https://lore.kernel.org/bpf/d3f76b27f4e55ec9e400ae8dcaecbb702a4932e8.camel@fb.com

Dave Marchevsky (1):
libbpf: Add support for private BSS map section

Kumar Kartikeya Dwivedi (24):
bpf: Document UAPI details for special BPF types
bpf: Allow specifying volatile type modifier for kptrs
bpf: Clobber stack slot when writing over spilled PTR_TO_BTF_ID
bpf: Fix slot type check in check_stack_write_var_off
bpf: Drop reg_type_may_be_refcounted_or_null
bpf: Refactor kptr_off_tab into fields_tab
bpf: Consolidate spin_lock, timer management into fields_tab
bpf: Refactor map->off_arr handling
bpf: Support bpf_list_head in map values
bpf: Introduce local kptrs
bpf: Recognize bpf_{spin_lock,list_head,list_node} in local kptrs
bpf: Verify ownership relationships for owning types
bpf: Support locking bpf_spin_lock in local kptr
bpf: Allow locking bpf_spin_lock global variables
bpf: Rewrite kfunc argument handling
bpf: Drop kfunc bits from btf_check_func_arg_match
bpf: Support constant scalar arguments for kfuncs
bpf: Teach verifier about non-size constant arguments
bpf: Introduce bpf_kptr_new
bpf: Introduce bpf_kptr_drop
bpf: Permit NULL checking pointer with non-zero fixed offset
bpf: Introduce single ownership BPF linked list API
selftests/bpf: Add __contains macro to bpf_experimental.h
selftests/bpf: Add BPF linked list API tests

Documentation/bpf/bpf_design_QA.rst | 44 +
Documentation/bpf/kfuncs.rst | 30 +
include/linux/bpf.h | 238 ++-
include/linux/bpf_verifier.h | 22 +-
include/linux/btf.h | 76 +-
include/linux/filter.h | 4 +-
kernel/bpf/arraymap.c | 30 +-
kernel/bpf/bpf_local_storage.c | 2 +-
kernel/bpf/btf.c | 1204 +++++++-------
kernel/bpf/core.c | 14 +
kernel/bpf/hashtab.c | 40 +-
kernel/bpf/helpers.c | 138 +-
kernel/bpf/local_storage.c | 2 +-
kernel/bpf/map_in_map.c | 18 +-
kernel/bpf/syscall.c | 381 +++--
kernel/bpf/verifier.c | 1449 ++++++++++++++---
net/bpf/bpf_dummy_struct_ops.c | 3 +-
net/core/bpf_sk_storage.c | 4 +-
net/core/filter.c | 13 +-
net/ipv4/bpf_tcp_ca.c | 3 +-
net/netfilter/nf_conntrack_bpf.c | 1 +
tools/lib/bpf/libbpf.c | 65 +-
tools/testing/selftests/bpf/DENYLIST.s390x | 1 +
.../testing/selftests/bpf/bpf_experimental.h | 85 +
.../bpf/prog_tests/kfunc_dynptr_param.c | 2 +-
.../selftests/bpf/prog_tests/linked_list.c | 88 +
.../testing/selftests/bpf/progs/linked_list.c | 325 ++++
tools/testing/selftests/bpf/verifier/calls.c | 4 +-
.../selftests/bpf/verifier/ref_tracking.c | 4 +-
29 files changed, 3145 insertions(+), 1145 deletions(-)
create mode 100644 tools/testing/selftests/bpf/bpf_experimental.h
create mode 100644 tools/testing/selftests/bpf/prog_tests/linked_list.c
create mode 100644 tools/testing/selftests/bpf/progs/linked_list.c

--
2.34.1