Skip to content

bpf: Fix incorrect handling of user flags by percpu map updates

Masoud Aghasi bpf 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 Latest3 patches 4 repliesApplied 3/3
  2. v53 patches 4 repliesSuperseded
  3. v43 patches 5 repliesSuperseded
Earlier versions · 3
VersionPostedPatchesRepliesStatus
v33 patches1 replySuperseded
v22 patches6 repliesSuperseded
v12 patches1 replySuperseded

Changes in v6 from the cover letter

- Improve the style and structure of the selftest

Patches in v6 All 3 applied

#Subject and filesSizeRepliesApplied
1/3bpf: Fix incorrect handling of user flags in bpf_percpu_array_updateinclude/linux/bpf.h kernel/bpf/arraymap.c+6 -31 replyApplied
2/3bpf: Fix incorrect handling of user flags by percpu hash map updateskernel/bpf/hashtab.c+6 -41 replyApplied
3/3selftests/bpf: add tests for percpu map flags combinationtools/testing/selftests/bpf/prog_tests/percpu_alloc.c+254 -02 repliesApplied

Reviews trailers per patch

All 3 patches have a Reviewed-by or Acked-by.

PatchLeon Hwang
1/3A-b
2/3A-b
3/3A-b

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

Read the v6 thread

Cover letter [PATCH bpf v6 0/3] bpf: Fix incorrect handling of user flags by percpu map updates

For BPF_MAP_TYPE_PERCPU_ARRAY map, bpf_percpu_array_update() is not considering the possibility of a combination of (BPF_NOEXIST, BPF_EXIST) flags with (BPF_F_CPU, BPF_F_ALL_CPUS) flags. This causes the (BPF_NOEXIST, BPF_EXIST) flags to lose their effect in some cases.

For BPF_MAP_TYPE_PERCPU_HASH and BPF_MAP_TYPE_LRU_PERCPU_HASH maps, htab_map_check_update_flags() and check_flags() are not considering the possibility of a combination of (BPF_NOEXIST, BPF_EXIST) flags with (BPF_F_CPU, BPF_F_ALL_CPUS) flags. This causes the (BPF_NOEXIST, BPF_EXIST) flags to lose their effect in some cases.

For example, when using (BPF_F_CPU | BPF_EXIST) flag combination with bpf_map_update_elem() on a BPF_MAP_TYPE_PERCPU_HASH map, the BPF_EXIST flag does not prevent new insertions as expected.

This series fixes the bug by adding proper flag validations and checks and adds regression tests.

V6 changes:
- Improve the style and structure of the selftest

V5 changes: https://lore.kernel.org/bpf/[email protected]/T/
- Simplify flag validations by introducing new macros in bpf.h
- Add helpers to verify the values after updates in the new tests
- Improve the style and structure of the new tests

V4 changes: https://lore.kernel.org/bpf/[email protected]/T/
- Edit selftest to use proper value_sz in the new cgroup map test
- Edit selftest to pin the current pid to the current cpu for LRU map
- Edit commit messages to include user-visible changes

V3 changes: https://lore.kernel.org/bpf/[email protected]/T/
- Split the fix into two separate patches for array map and hash maps
- Extend the selftest to cover all percpu map types
- Extend the selftest to cover all applicable flag combinations
- Extend the selftest to cover the unnecessary delete issue of LRU map

V2 changes: https://lore.kernel.org/bpf/[email protected]/T/
- Fix a BPF_EXIST flag check in __htab_lru_percpu_map_update_elem()
- Add additional test for BPF_MAP_TYPE_LRU_PERCPU_HASH map
- Add check for BPF_EXIST, BPF_NOEXIST combination in percpu array map

V1: https://lore.kernel.org/bpf/[email protected]/T/

Masoud Aghasi (3):
bpf: Fix incorrect handling of user flags in bpf_percpu_array_update
bpf: Fix incorrect handling of user flags by percpu hash map updates selftests/bpf: add tests for percpu map flags combination

 include/linux/bpf.h                           |   3 +
 kernel/bpf/arraymap.c                         |   6 +-
 kernel/bpf/hashtab.c                          |  10 +-
 .../selftests/bpf/prog_tests/percpu_alloc.c   | 254 ++++++++++++++++++
 4 files changed, 266 insertions(+), 7 deletions(-)
Signature · 1 line
2.47.3