Skip to content

selftests: add shared Makefile for BPF selftests and a new memcg test

Ziyang Men bpf cgroups linux-input linux-kselftest linux-mm lkml llvm sched-ext 4 versions – New

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v5 Latest4 patches 0 repliesNew
  2. v44 patches 1 replySuperseded
  3. v34 patches 2 repliesSuperseded
Earlier versions · 1
VersionPostedPatchesRepliesStatus
v24 patches7 repliesSuperseded

Changes in v5 from the cover letter

- Apply Tejun's suggestion: make the vmlinux.h stamp a normal prerequisite so dependent BPF objects rebuild in the same make invocation when the header changes.
- Fix AI bot suggestion: assert in the BPF selftest that the memcg event enum argument types remain unsigned. This assert might not be necessary since these enum values are always uint32 on BTF.

Patches in v5 No applies recorded yet

#Subject and filesSizeRepliesApplied
1/4selftests: add shared lib.bpf.mk to build BPF progs and skeletonstools/testing/selftests/lib.bpf.mk+321 -00 replies
2/4selftests/cgroup: add memcg_stat_cross_cpu correctness test for flushtools/testing/selftests/cgroup/.gitignore tools/testing/selftests/cgroup/Makefile tools/testing/selftests/cgroup/config +6 more+1022 -250 replies
3/4selftests/hid: build the BPF program via the shared lib.bpf.mktools/testing/selftests/hid/.gitignore tools/testing/selftests/hid/Makefile tools/testing/selftests/hid/progs/hid.c+19 -1680 replies
4/4selftests/sched_ext: build BPF schedulers via the shared lib.bpf.mktools/testing/selftests/sched_ext/Makefile+26 -1230 replies

Reviews trailers per patch

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

Read the v5 thread

Cover letter [PATCH v5 0/4] selftests: add shared Makefile for BPF selftests and a new memcg test

This series refactors the BPF-related build machinery that is currently duplicated across selftests/{bpf,sched_ext,hid}/. It adds the shared tools/testing/selftests/lib.bpf.mk fragment and wires up three consumers, simplifying both existing BPF selftests and future additions.

The series also adds a cgroup memory-controller selftest that complements the existing coverage.

Patch 1 adds lib.bpf.mk.

Patch 2 adds selftests/cgroup/test_memcg_stat_cross_cpu, which verifies a cgroup flush by comparing values read through BPF memcg kfuncs with the corresponding cgroupfs values. The existing bpf/cgroup_iter_memcg test only checks whether selected values are non-zero.

Patches 3 and 4 replace the duplicated BPF build machinery in selftests/{hid,sched_ext} with lib.bpf.mk.

===
Changes since v4:
- Apply Tejun's suggestion: make the vmlinux.h stamp a normal prerequisite so dependent BPF objects rebuild in the same make invocation when the header changes.
- Fix AI bot suggestion: assert in the BPF selftest that the memcg event enum argument types remain unsigned. This assert might not be necessary since these enum values are always uint32 on BTF.

===
Changes since v3:
For shared Makefile:
- Track in-tree libbpf headers directly so a single make invocation rebuilds BPF objects and skeletons after a header change.
- Restore the target definitions needed by LoongArch and MIPS cross builds.
- Validate output directories and skeleton suffixes, and reject dotted or hyphenated source stems before they reach build and cleanup rules.
- Add stamp tracking for vmlinux.h generation while preserving content-based header updates.
- Make skeletons and subskeletons explicit outputs, so a missing subskeleton is regenerated and both sets participate in the all target.
- Namespace private variables, expand the caller-visible contract, track a supplied VMLINUX_H, and remove unnecessary include search paths.

For cgroup selftest:
- Add CONFIG_BPF_EVENTS and probe every required kfunc plus the sleepable cgroup iterator before running.
- Bound CPU fanout, cgroup/process count, and total memory use; skip a case when the flush threshold would require more than 256 MiB.
- Check CPU discovery, polling, iterator creation, and BPF errors, with clearer diagnostics.
- Compare each BPF statistic against two following memory.stat samples, keeping exact equality on a quiet subtree while tolerating values that move between the two samples.

For hid and sched_ext:
- Remove unused HID locals instead of suppressing warnings, drop obsolete variables and comments, tighten cleanup, and correct the generated-output description.

===
Changes since v2:
For shared Makefile:
- Use USERCFLAGS and USERLDFLAGS to extend CFLAGS and LDFLAGS.
- Pass OPT_FLAGS/RELEASE and EXTRA_CFLAGS to the libbpf and bpftool sub-makes.
- Use one rule per BPF source instead of a global vpath that also affected the caller's normal %.c rules.
- Take HOSTCC/HOSTLD from lib.mk instead of including Makefile.include, which also rewrote the caller's AR, LD, and CFLAGS.
- Report a missing vmlinux from the recipe rather than at parse time, so "make clean" still works without BTF.

For cgroup selftest:
- Create one cgroup tree, perform one flush, then read through BPF and cgroupfs.
- Use common cgroup helpers such as cg_read_key_long() and values_close().
- Use cpu_set_t rather than manually allocated CPU lists.
- Move test_memcontrol.c's anonymous-memory allocator into cgroup_util.
- Address the remaining review feedback.

===
Changes since v1:
- Generalize lib.bpf.mk for source layouts and suffixes, extra headers and flags, skeleton suffixes, subskeletons, and configurable output directories.
- Add patch 3 (hid) and patch 4 (sched_ext), converting those directories to the shared fragment.

Ziyang Men (4):
selftests: add shared lib.bpf.mk to build BPF progs and skeletons selftests/cgroup: add memcg_stat_cross_cpu correctness test for flush selftests/hid: build the BPF program via the shared lib.bpf.mk selftests/sched_ext: build BPF schedulers via the shared lib.bpf.mk

 tools/testing/selftests/cgroup/.gitignore     |   8 +
 tools/testing/selftests/cgroup/Makefile       |  46 ++
 tools/testing/selftests/cgroup/config         |   5 +
 .../selftests/cgroup/lib/cgroup_util.c        |  82 ++
 .../cgroup/lib/include/cgroup_util.h          |   3 +
 .../cgroup/memcg_stat_cross_cpu.bpf.c         | 101 +++
 .../selftests/cgroup/memcg_stat_cross_cpu.h   |  18 +
 .../cgroup/test_memcg_stat_cross_cpu.c        | 755 ++++++++++++++++++
 .../selftests/cgroup/test_memcontrol.c        |  29 +-
 tools/testing/selftests/hid/.gitignore        |   1 +
 tools/testing/selftests/hid/Makefile          | 179 +----
 tools/testing/selftests/hid/progs/hid.c       |   7 +-
 tools/testing/selftests/lib.bpf.mk            | 321 ++++++++
 tools/testing/selftests/sched_ext/Makefile    | 149 +---
 14 files changed, 1388 insertions(+), 316 deletions(-)
 create mode 100644 tools/testing/selftests/cgroup/memcg_stat_cross_cpu.bpf.c
 create mode 100644 tools/testing/selftests/cgroup/memcg_stat_cross_cpu.h
 create mode 100644 tools/testing/selftests/cgroup/test_memcg_stat_cross_cpu.c
 create mode 100644 tools/testing/selftests/lib.bpf.mk
Signature · 1 line
2.53.0-Meta