bpf: add bpf_for_each_map_elem() helper
Get series with b4
b4 am [email protected]Versions newest first · linked by author and title
- v5 Latest12 patches 1 replyIn review
- v412 patches 6 repliesSuperseded
- v311 patches 20 repliesSuperseded
Earlier versions · 2
| Version | Posted | Patches | Replies | Status |
|---|---|---|---|---|
| v2 | 11 patches | 17 replies | Superseded | |
| v1 | 8 patches | 19 replies | Superseded |
Changes in v5 from the cover letter
- rebase on top of bpf-next.
Patches in v5 No applies recorded yet
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/12 | bpf: factor out visit_func_call_insn() in check_cfg()kernel/ | +23 | 0 replies | |
| 2/12 | bpf: factor out verbose_invalid_scalar()kernel/ | +19 | 0 replies | |
| 3/12 | bpf: refactor check_func_call() to allow callback functionkernel/ | +43 | 0 replies | |
| 4/12 | bpf: change return value of verifier function add_subprog()kernel/ | +2 | 0 replies | |
| 5/12 | bpf: add bpf_for_each_map_elem() helperinclude/ include/ include/ +5 more | +307 | 1 reply | |
| 6/12 | bpf: add hashtab support for bpf_for_each_map_elem() helperinclude/ kernel/ kernel/ | +96 | 0 replies | |
| 7/12 | bpf: add arraymap support for bpf_for_each_map_elem() helperkernel/ | +40 | 0 replies | |
| 8/12 | libbpf: move function is_ldimm64() earlier in libbpf.ctools/ | +6 | 0 replies | |
| 9/12 | libbpf: support subprog address relocationtools/ | +61 | 0 replies | |
| 10/12 | bpftool: print subprog address properlytools/ | +3 | 0 replies | |
| 11/12 | selftests/bpf: add hashmap test for bpf_for_each_map_elem() helpertools/ tools/ tools/ | +179 | 0 replies | |
| 12/12 | selftests/bpf: add arraymap test for bpf_for_each_map_elem() helpertools/ tools/ | +118 | 0 replies |
Reviews trailers per patch
All 12 patches have a Reviewed-by or Acked-by.
| Patch | Andrii Nakryiko |
|---|---|
| 1/12 | A-b |
| 2/12 | A-b |
| 3/12 | A-b |
| 4/12 | A-b |
| 5/12 | A-b |
| 6/12 | A-b |
| 7/12 | A-b |
| 8/12 | A-b |
| 9/12 | A-b |
| 10/12 | A-b |
| 11/12 | A-b |
| 12/12 | A-b |
Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.
Cover letter [PATCH bpf-next v5 00/12] bpf: add bpf_for_each_map_elem() helper
This patch set introduced bpf_for_each_map_elem() helper. The helper permits bpf program iterates through all elements for a particular map.
The work originally inspired by an internal discussion where firewall rules are kept in a map and bpf prog wants to check packet 5 tuples against all rules in the map. A bounded loop can be used but it has a few drawbacks. As the loop iteration goes up, verification time goes up too. For really large maps, verification may fail.
A helper which abstracts out the loop itself will not have verification time issue.
A recent discussion in [1] involves to iterate all hash map elements in bpf program. Currently iterating all hashmap elements in bpf program is not easy if key space is really big. Having a helper to abstract out the loop itself is even more meaningful.
The proposed helper signature looks like:
long bpf_for_each_map_elem(map, callback_fn, callback_ctx, flags)
where callback_fn is a static function and callback_ctx is a piece of data allocated on the caller stack which can be accessed by the callback_fn. The callback_fn signature might be different for different maps. For example, for hash/array maps, the signature is
long callback_fn(map, key, val, callback_ctx)
In the rest of series, Patches 1/2/3/4 did some refactoring. Patch 5 implemented core kernel support for the helper. Patches 6 and 7 added hashmap and arraymap support. Patches 8/9 added libbpf support. Patch 10 added bpftool support. Patches 11 and 12 added selftests for hashmap and arraymap.
[1]: https://lore.kernel.org/bpf/[email protected]/
Changelogs:
v4 -> v5:
- rebase on top of bpf-next.
v3 -> v4:
- better refactoring of check_func_call(), calculate subprogno outside of __check_func_call() helper. (Andrii)
- better documentation (like the list of supported maps and their callback signatures) in uapi header. (Andrii)
- implement and use ASSERT_LT in selftests. (Andrii)
- a few other minor changes.
v2 -> v3:
- add comments in retrieve_ptr_limit(), which is in sanitize_ptr_alu(), to clarify the code is not executed for PTR_TO_MAP_KEY handling, but code is manually tested. (Alexei)
- require BTF for callback function. (Alexei)
- simplify hashmap/arraymap callback return handling as return value [0, 1] has been enforced by the verifier. (Alexei)
- also mark global subprog (if used in ld_imm64) as RELO_SUBPROG_ADDR. (Andrii)
- handle the condition to mark RELO_SUBPROG_ADDR properly. (Andrii)
- make bpftool subprog insn offset dumping consist with pcrel calls. (Andrii)
v1 -> v2:
- setup callee frame in check_helper_call() and then proceed to verify helper return value as normal (Alexei)
- use meta data to keep track of map/func pointer to avoid hard coding the register number (Alexei)
- verify callback_fn return value range [0, 1]. (Alexei)
- add migrate_{disable, enable} to ensure percpu value is the one bpf program expects to see. (Alexei)
- change bpf_for_each_map_elem() return value to the number of iterated elements. (Andrii)
- Change libbpf pseudo_func relo name to RELO_SUBPROG_ADDR and use more rigid checking for the relocation. (Andrii)
- Better format to print out subprog address with bpftool. (Andrii)
- Use bpf_prog_test_run to trigger bpf run, instead of bpf_iter. (Andrii)
- Other misc changes.
Yonghong Song (12):
bpf: factor out visit_func_call_insn() in check_cfg()
bpf: factor out verbose_invalid_scalar()
bpf: refactor check_func_call() to allow callback function
bpf: change return value of verifier function add_subprog()
bpf: add bpf_for_each_map_elem() helper
bpf: add hashtab support for bpf_for_each_map_elem() helper
bpf: add arraymap support for bpf_for_each_map_elem() helper
libbpf: move function is_ldimm64() earlier in libbpf.c
libbpf: support subprog address relocation
bpftool: print subprog address properly
selftests/bpf: add hashmap test for bpf_for_each_map_elem() helper selftests/bpf: add arraymap test for bpf_for_each_map_elem() helper
include/linux/bpf.h | 17 + include/linux/bpf_verifier.h | 3 + include/uapi/linux/bpf.h | 38 ++ kernel/bpf/arraymap.c | 40 ++ kernel/bpf/bpf_iter.c | 16 + kernel/bpf/hashtab.c | 65 ++++ kernel/bpf/helpers.c | 2 + kernel/bpf/verifier.c | 364 +++++++++++++++--- kernel/trace/bpf_trace.c | 2 + tools/bpf/bpftool/xlated_dumper.c | 3 + tools/include/uapi/linux/bpf.h | 38 ++ tools/lib/bpf/libbpf.c | 76 +++- .../selftests/bpf/prog_tests/for_each.c | 130 +++++++ .../bpf/progs/for_each_array_map_elem.c | 61 +++ .../bpf/progs/for_each_hash_map_elem.c | 95 +++++ tools/testing/selftests/bpf/test_progs.h | 11 + 16 files changed, 897 insertions(+), 64 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/for_each.c create mode 100644 tools/testing/selftests/bpf/progs/for_each_array_map_elem.c create mode 100644 tools/testing/selftests/bpf/progs/for_each_hash_map_elem.c
Signature · 1 line
2.24.1