Skip to content

Commit 23316a3

Browse files
yonghong-songborkmann
authored andcommitted
tools/bpf: fix selftest get_cgroup_id_user
Commit f269099 ("tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper") added a test for bpf_get_current_cgroup_id() helper. The bpf program is attached to tracepoint syscalls/sys_enter_nanosleep and will record the cgroup id if the tracepoint is hit. The test program creates a cgroup and attachs itself to this cgroup and expects that the test program process cgroup id is the same as the cgroup_id retrieved by the bpf program. In a light system where no other processes called nanosleep syscall, the test case can pass. In a busy system where many different processes can hit syscalls/sys_enter_nanosleep tracepoint, the cgroup id recorded by bpf program may not match the test program process cgroup_id. This patch fixed an issue by communicating the test program pid to bpf program. The bpf program only records cgroup id if the current task pid is the same as passed-in pid. This ensures that the recorded cgroup_id is for the cgroup within which the test program resides. Fixes: f269099 ("tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper") Signed-off-by: Yonghong Song <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 58990d1 commit 23316a3

File tree

2 files changed

+23
-3
lines changed

2 files changed

+23
-3
lines changed

tools/testing/selftests/bpf/get_cgroup_id_kern.c

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,24 @@ struct bpf_map_def SEC("maps") cg_ids = {
1111
.max_entries = 1,
1212
};
1313

14+
struct bpf_map_def SEC("maps") pidmap = {
15+
.type = BPF_MAP_TYPE_ARRAY,
16+
.key_size = sizeof(__u32),
17+
.value_size = sizeof(__u32),
18+
.max_entries = 1,
19+
};
20+
1421
SEC("tracepoint/syscalls/sys_enter_nanosleep")
1522
int trace(void *ctx)
1623
{
17-
__u32 key = 0;
24+
__u32 pid = bpf_get_current_pid_tgid();
25+
__u32 key = 0, *expected_pid;
1826
__u64 *val;
1927

28+
expected_pid = bpf_map_lookup_elem(&pidmap, &key);
29+
if (!expected_pid || *expected_pid != pid)
30+
return 0;
31+
2032
val = bpf_map_lookup_elem(&cg_ids, &key);
2133
if (val)
2234
*val = bpf_get_current_cgroup_id();

tools/testing/selftests/bpf/get_cgroup_id_user.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,13 @@ int main(int argc, char **argv)
5050
const char *probe_name = "syscalls/sys_enter_nanosleep";
5151
const char *file = "get_cgroup_id_kern.o";
5252
int err, bytes, efd, prog_fd, pmu_fd;
53+
int cgroup_fd, cgidmap_fd, pidmap_fd;
5354
struct perf_event_attr attr = {};
54-
int cgroup_fd, cgidmap_fd;
5555
struct bpf_object *obj;
5656
__u64 kcgid = 0, ucgid;
57+
__u32 key = 0, pid;
5758
int exit_code = 1;
5859
char buf[256];
59-
__u32 key = 0;
6060

6161
err = setup_cgroup_environment();
6262
if (CHECK(err, "setup_cgroup_environment", "err %d errno %d\n", err,
@@ -81,6 +81,14 @@ int main(int argc, char **argv)
8181
cgidmap_fd, errno))
8282
goto close_prog;
8383

84+
pidmap_fd = bpf_find_map(__func__, obj, "pidmap");
85+
if (CHECK(pidmap_fd < 0, "bpf_find_map", "err %d errno %d\n",
86+
pidmap_fd, errno))
87+
goto close_prog;
88+
89+
pid = getpid();
90+
bpf_map_update_elem(pidmap_fd, &key, &pid, 0);
91+
8492
snprintf(buf, sizeof(buf),
8593
"/sys/kernel/debug/tracing/events/%s/id", probe_name);
8694
efd = open(buf, O_RDONLY, 0);

0 commit comments

Comments
 (0)