setup_bpffs() mounts a bpffs on /sys/fs/bpf even when one is already
there, while test_progs runs other tests that pin objects in /sys/fs/bpf
in parallel.

The new, empty bpffs hides the old one until the test ends, so those
tests lose what they pinned or the directories they pin into.
bpftool_maps_access then fails to pin an iterator because
/sys/fs/bpf/test_bpftool_map doesn't exist anymore, and bpftool_metadata
can't find the program it pinned in /sys/fs/bpf/test_metadata.

Mount a bpffs only if /sys/fs/bpf isn't one already.

Fixes: 88886309d2e8 ("selftests/bpf: add a selftest for cgroup hierarchical 
stats collection")
Signed-off-by: Qiliang Yuan <[email protected]>
---
 .../bpf/prog_tests/cgroup_hierarchical_stats.c        | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

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 a93b16ed629e9..8e0b67df700bf 100644
--- a/tools/testing/selftests/bpf/prog_tests/cgroup_hierarchical_stats.c
+++ b/tools/testing/selftests/bpf/prog_tests/cgroup_hierarchical_stats.c
@@ -23,8 +23,10 @@
 #include <asm-generic/errno.h>
 #include <errno.h>
 #include <sys/types.h>
+#include <linux/magic.h>
 #include <sys/mount.h>
 #include <sys/stat.h>
+#include <sys/vfs.h>
 #include <unistd.h>
 
 #include <test_progs.h>
@@ -88,13 +90,22 @@ static int read_from_file(const char *path, char *buf, 
size_t size)
 /* mounts bpffs and mkdir for reading stats, returns 0 on success. */
 static int setup_bpffs(void)
 {
+       struct statfs st;
        int err;
 
-       /* Mount bpffs */
-       err = mount("bpf", BPFFS_ROOT, "bpf", 0, NULL);
-       mounted_bpffs = !err;
-       if (ASSERT_FALSE(err && errno != EBUSY, "mount"))
+       /*
+        * Mount bpffs unless it is already there. Mounting another one on top
+        * would hide what other tests running in parallel pin in it.
+        */
+       err = statfs(BPFFS_ROOT, &st);
+       if (!ASSERT_OK(err, "statfs " BPFFS_ROOT))
                return err;
+       if (st.f_type != BPF_FS_MAGIC) {
+               err = mount("bpf", BPFFS_ROOT, "bpf", 0, NULL);
+               if (!ASSERT_OK(err, "mount"))
+                       return err;
+               mounted_bpffs = true;
+       }
 
        /* Create a directory to contain stat files in bpffs */
        err = mkdir(BPFFS_ATTACH_COUNTERS, 0755);

-- 
2.43.0


Reply via email to