Skip to content

bpf: Add bpf_call_rcu() and bpf_call_rcu_tasks_trace()

Puranjay Mohan bpf rcu 6 versions – Applied

Status from this message: This series was applied to bpf/bpf-next.git (master)

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v6 Latest4 patches 10 repliesApplied 4/4
  2. v54 patches 6 repliesSuperseded
  3. v44 patches 6 repliesSuperseded
Earlier versions · 3
VersionPostedPatchesRepliesStatus
v34 patches1 replySuperseded
v24 patches2 repliesSuperseded
v14 patches3 repliesSuperseded

Changes in v6 from the cover letter

- Size struct bpf_rcu_head at 48 bytes rather than 64, matching what the inline state uses (Alexei)
- Trim patch 1's changelog: drop the reasoning about possible future layouts and about what the other async kfuncs return
- Rebase on bpf-next/master

Patches in v6 All 4 applied

#Subject and filesSizeRepliesApplied
1/4bpf: Add bpf_call_rcu() kfuncinclude/linux/bpf.h include/uapi/linux/bpf.h kernel/bpf/btf.c +6 more+199 -30 repliesApplied
2/4selftests/bpf: Add tests for bpf_call_rcu()tools/testing/selftests/bpf/prog_tests/call_rcu.c tools/testing/selftests/bpf/progs/call_rcu.c tools/testing/selftests/bpf/progs/call_rcu_fail.c+478 -00 repliesApplied
3/4bpf: Add bpf_call_rcu_tasks_trace() kfunckernel/bpf/helpers.c kernel/bpf/verifier.c+47 -170 repliesApplied
4/4selftests/bpf: Add a test for bpf_call_rcu_tasks_trace()tools/testing/selftests/bpf/prog_tests/call_rcu.c tools/testing/selftests/bpf/progs/call_rcu.c+25 -40 repliesApplied

Reviews trailers per patch

No Reviewed-by, Acked-by or Tested-by trailers for v6 yet.

Read the v6 thread

Cover letter [PATCH bpf-next v6 0/4] bpf: Add bpf_call_rcu() and bpf_call_rcu_tasks_trace()

Changelog:
v5: https://lore.kernel.org/all/[email protected]/
Changes in v6:
- Size struct bpf_rcu_head at 48 bytes rather than 64, matching what the inline state uses (Alexei)
- Trim patch 1's changelog: drop the reasoning about possible future layouts and about what the other async kfuncs return
- Rebase on bpf-next/master

v4: https://lore.kernel.org/all/[email protected]/
Changes in v5:
- Rename the "hash_map" subtest to "bad_map" so that it matches its helper test_call_rcu_bad_map() (bpf-ci)
- Bump the callback counter after chain_err in the selftest callback. Userspace polls that counter and then reads chain_err, so it could still see the initial value before the re-arm had stored one, which let the chain subtest's assertion pass without checking anything
- Say in patch 1 why embedding struct rcu_head in a uapi struct is acceptable here (Mykyta, Paul, Alexei)
- Rebase on bpf-next/master
v3: https://lore.kernel.org/all/[email protected]/
Changes in v4:
- Drop an unrelated hunk in bpf_async_update_prog_callback() that turned PTR_ERR(prog) into -EBADF. That is the shared bpf_timer/bpf_wq path and bpf_prog_inc_not_zero() returns -ENOENT, so it would have changed the errno bpf_timer_set_callback() and bpf_wq_set_callback() report to userspace (Sashiko). No other changes from v3
v2: https://lore.kernel.org/all/[email protected]/
Changes in v3:
- Move the bpf_call_rcu_tasks_trace() verifier bits from patch 1 to patch 3; patch 1 alone emitted "resolve_btfids: unresolved symbol bpf_call_rcu_tasks_trace" (Sashiko)
- Poll the callback counter with an acquire load (Sashiko)
- teardown: v2 only checked that the program was eventually freed, which passes even if nothing was ever armed. Also assert that the chain ran, and read the -EPERM back through an independent .bss fd
- Use kern_sync_rcu() instead of open coding the grace-period wait
- Return -EBADF rather than -ENOENT when the calling program is going away, matching bpf_task_work_schedule()
- mismatch_map now pins the bpf_rcu_head label the new code emits; it passed with that branch removed. Add a two_heads test, and wait for each grace period separately in the chain test
- Commit messages: correct the -EPERM parity claim, explain the inline callback state and the struct size, motivate the tasks trace flavour
v1: https://lore.kernel.org/all/[email protected]/
Changes in v2:
- Rebase on bpf-next/master
- Use rcu_read_lock_dont_migrate() over open coding (Alexei)
- Improve re-arming selftest to detect failure (Sashiko)

BPF programs that manage their own objects have no way to run their own logic once an RCU grace period has elapsed. bpf_obj_drop() defers a free, but returning an index to an allocator or unpinning a resource once readers are done has no equivalent. sched_ext's BPF library works around this today by pushing freed nodes onto a list and having a userspace thread call membarrier(MEMBARRIER_CMD_GLOBAL) and then run a BPF program to reclaim them; it is the first intended user.

Add:

int bpf_call_rcu(struct bpf_rcu_head *rh, void *map,
int (*callback)(struct bpf_map *map, void *key,
void *value));

and bpf_call_rcu_tasks_trace(), same signature, which also waits for sleepable programs.

@rh is a struct bpf_rcu_head embedded in a value of @map, so the callback runs as callback(map, key, value) for the element it lives in and needs no cookie. The field is only accepted in BPF_MAP_TYPE_ARRAY, and arming holds a reference on the calling program until the callback has run. Patch 1 covers the lifetime rules.

This needs

https://lore.kernel.org/all/[email protected]/

for call_rcu() and call_srcu() to be safe from the contexts a BPF program can be called in.

Puranjay Mohan (4):
bpf: Add bpf_call_rcu() kfunc
selftests/bpf: Add tests for bpf_call_rcu()
bpf: Add bpf_call_rcu_tasks_trace() kfunc selftests/bpf: Add a test for bpf_call_rcu_tasks_trace()

 include/linux/bpf.h                           |  10 +
 include/uapi/linux/bpf.h                      |   4 +
 kernel/bpf/btf.c                              |   7 +
 kernel/bpf/helpers.c                          | 102 +++++++
 kernel/bpf/map_in_map.c                       |   4 +
 kernel/bpf/map_iter.c                         |   6 +
 kernel/bpf/syscall.c                          |  11 +-
 kernel/bpf/verifier.c                         |  84 +++++-
 tools/include/uapi/linux/bpf.h                |   4 +
 .../selftests/bpf/prog_tests/call_rcu.c       | 275 ++++++++++++++++++
 tools/testing/selftests/bpf/progs/call_rcu.c  | 110 +++++++
 .../selftests/bpf/progs/call_rcu_fail.c       | 114 ++++++++
 12 files changed, 728 insertions(+), 3 deletions(-)
 create mode 100644 tools/testing/selftests/bpf/prog_tests/call_rcu.c
 create mode 100644 tools/testing/selftests/bpf/progs/call_rcu.c
 create mode 100644 tools/testing/selftests/bpf/progs/call_rcu_fail.c

base-commit: 79dc258c9392051420a26f1504c647bd3d27c66a

Signature · 1 line
2.53.0-Meta