diff --git a/tools/testing/selftests/bpf/prog_tests/cgroup_hierarchical_stats.c b/tools/testing/selftests/bpf/prog_tests/cgroup_hierarchical_stats.c index 5d0a8bb110a4..e01fac401ec5 100644 --- a/tools/testing/selftests/bpf/prog_tests/cgroup_hierarchical_stats.c +++ b/tools/testing/selftests/bpf/prog_tests/cgroup_hierarchical_stats.c @@ -155,28 +155,30 @@ static void reclaimer(const char *cgroup_path, size_t size) int err; /* Join cgroup in the parent process workdir */ - join_parent_cgroup(cgroup_path); + if (join_parent_cgroup(cgroup_path)) + exit(EACCES); /* Allocate memory */ buf = malloc(size); + if(!buf) + exit(ENOMEM); + + /* Write to memory to make sure it's actually allocated */ for (ptr = buf; ptr < buf + size; ptr += PAGE_SIZE) *ptr = 1; - /* - * Try to reclaim memory. - * memory.reclaim can return EAGAIN if the amount is not - * fully reclaimed. - */ + /* Try to reclaim memory */ snprintf(size_buf, 128, "%lu", size); err = write_cgroup_file_parent(cgroup_path, "memory.reclaim", size_buf); free(buf); - exit(err && errno != EAGAIN); + /* memory.reclaim returns EAGAIN if the amount is not fully reclaimed */ + exit(err && errno != EAGAIN ? errno : 0); } static int induce_vmscan(void) { - int i, status, err = 0; + int i, status; /* * In every leaf cgroup, run a child process that allocates some memory @@ -189,13 +191,13 @@ static int induce_vmscan(void) pid = fork(); if (pid == 0) reclaimer(cgroups[i].path, MB(5)); - if (!ASSERT_GT(pid, 0, "fork reclaimer child")) + if (!ASSERT_GT(pid, 0, "fork reclaimer")) return pid; /* Cleanup reclaimer child */ waitpid(pid, &status, 0); - err = !WIFEXITED(status) || WEXITSTATUS(status); - ASSERT_OK(err, "reclaimer child exit status"); + ASSERT_TRUE(WIFEXITED(status), "reclaimer exited"); + ASSERT_EQ(WEXITSTATUS(status), 0, "reclaim exit code"); } return 0; } @@ -203,7 +205,7 @@ static int induce_vmscan(void) static unsigned long long get_cgroup_vmscan_delay(unsigned long long cgroup_id, const char *file_name) { - char buf[128], path[128]; + static char buf[128], path[128]; unsigned long long vmscan = 0, id = 0; int err; diff --git a/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c b/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c index 0a1a3bebdf4c..85a65a72482e 100644 --- a/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c +++ b/tools/testing/selftests/bpf/progs/cgroup_hierarchical_stats.c @@ -37,14 +37,14 @@ struct vmscan { struct { __uint(type, BPF_MAP_TYPE_PERCPU_HASH); - __uint(max_entries, 10); + __uint(max_entries, 100); __type(key, __u64); __type(value, struct vmscan_percpu); } pcpu_cgroup_vmscan_elapsed SEC(".maps"); struct { __uint(type, BPF_MAP_TYPE_HASH); - __uint(max_entries, 10); + __uint(max_entries, 100); __type(key, __u64); __type(value, struct vmscan); } cgroup_vmscan_elapsed SEC(".maps"); @@ -65,11 +65,13 @@ static inline uint64_t cgroup_id(struct cgroup *cgrp) static inline int create_vmscan_percpu_elem(__u64 cg_id, __u64 state) { struct vmscan_percpu pcpu_init = {.state = state, .prev = 0}; + int err; - if (bpf_map_update_elem(&pcpu_cgroup_vmscan_elapsed, &cg_id, - &pcpu_init, BPF_NOEXIST)) { - bpf_printk("failed to create pcpu entry for cgroup %llu\n" - , cg_id); + err = bpf_map_update_elem(&pcpu_cgroup_vmscan_elapsed, &cg_id, + &pcpu_init, BPF_NOEXIST); + if (err) { + bpf_printk("failed to create pcpu entry for cgroup %llu: %d\n" + , cg_id, err); return 1; } return 0; @@ -78,11 +80,13 @@ static inline int create_vmscan_percpu_elem(__u64 cg_id, __u64 state) static inline int create_vmscan_elem(__u64 cg_id, __u64 state, __u64 pending) { struct vmscan init = {.state = state, .pending = pending}; + int err; - if (bpf_map_update_elem(&cgroup_vmscan_elapsed, &cg_id, - &init, BPF_NOEXIST)) { - bpf_printk("failed to create entry for cgroup %llu\n" - , cg_id); + err = bpf_map_update_elem(&cgroup_vmscan_elapsed, &cg_id, + &init, BPF_NOEXIST); + if (err) { + bpf_printk("failed to create entry for cgroup %llu: %d\n" + , cg_id, err); return 1; } return 0; @@ -220,7 +224,7 @@ int BPF_PROG(dump_vmscan, struct bpf_iter_meta *meta, struct cgroup *cgrp) total_stat = bpf_map_lookup_elem(&cgroup_vmscan_elapsed, &cg_id); if (!total_stat) { bpf_printk("error finding stats for cgroup %llu\n", cg_id); - BPF_SEQ_PRINTF(seq, "cg_id: %llu, total_vmscan_delay: -1\n", + BPF_SEQ_PRINTF(seq, "cg_id: %llu, total_vmscan_delay: 0\n", cg_id); return 1; }