Skip to contentSkip to conversation

selftests/bpf: fix incorrect users of create_and_get_cgroup

PATCH Applied Stanislav Fomichev 2 participants 2 messages Jan 7, 2019

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 selftests/bpf: fix incorrect users of create_and_get_cgroup

We have some tests that assume create_and_get_cgroup returns -1 on error which is incorrect (it returns 0 on error). Since fd might be zero in general case, change create_and_get_cgroup to return -1 on error and fix the users that assume 0 on error.

Fixes f269099a7e7a ("tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper")
Fixes 7d2c6cfc5411 ("bpf: use --cgroup in test_suite if supplied")

v2:
- instead of fixing the uses that assume -1 on error, convert the users that assume 0 on error (fd might be zero in general case)

---
 samples/bpf/test_cgrp2_attach2.c                   | 14 +++++++-------
 samples/bpf/test_current_task_under_cgroup_user.c  |  2 +-
 tools/testing/selftests/bpf/cgroup_helpers.c       |  6 +++---
 tools/testing/selftests/bpf/test_cgroup_storage.c  |  2 +-
 tools/testing/selftests/bpf/test_dev_cgroup.c      |  2 +-
 tools/testing/selftests/bpf/test_netcnt.c          |  2 +-
 .../selftests/bpf/test_skb_cgroup_id_user.c        |  2 +-
 tools/testing/selftests/bpf/test_sock.c            |  2 +-
 tools/testing/selftests/bpf/test_sock_addr.c       |  2 +-
 tools/testing/selftests/bpf/test_socket_cookie.c   |  2 +-
 tools/testing/selftests/bpf/test_tcpbpf_user.c     |  2 +-
 tools/testing/selftests/bpf/test_tcpnotify_user.c  |  2 +-
 12 files changed, 20 insertions(+), 20 deletions(-)
samples/bpf/test_cgrp2_attach2.c +7 -7
index d7b68ef5ba79..0bb6507256b7 100644
@@ -77,7 +77,7 @@ static int test_foo_bar(void)
 
 	/* Create cgroup /foo, get fd, and join it */
 	foo = create_and_get_cgroup(FOO);
-	if (!foo)
+	if (foo < 0)
 		goto err;
 
 	if (join_cgroup(FOO))
@@ -94,7 +94,7 @@ static int test_foo_bar(void)
 
 	/* Create cgroup /foo/bar, get fd, and join it */
 	bar = create_and_get_cgroup(BAR);
-	if (!bar)
+	if (bar < 0)
 		goto err;
 
 	if (join_cgroup(BAR))
@@ -298,19 +298,19 @@ static int test_multiprog(void)
 		goto err;
 
 	cg1 = create_and_get_cgroup("/cg1");
-	if (!cg1)
+	if (cg1 < 0)
 		goto err;
 	cg2 = create_and_get_cgroup("/cg1/cg2");
-	if (!cg2)
+	if (cg2 < 0)
 		goto err;
 	cg3 = create_and_get_cgroup("/cg1/cg2/cg3");
-	if (!cg3)
+	if (cg3 < 0)
 		goto err;
 	cg4 = create_and_get_cgroup("/cg1/cg2/cg3/cg4");
-	if (!cg4)
+	if (cg4 < 0)
 		goto err;
 	cg5 = create_and_get_cgroup("/cg1/cg2/cg3/cg4/cg5");
-	if (!cg5)
+	if (cg5 < 0)
 		goto err;
 
 	if (join_cgroup("/cg1/cg2/cg3/cg4/cg5"))
samples/bpf/test_current_task_under_cgroup_user.c +1 -1
index 2259f997a26c..f082d6ac59f0 100644
@@ -32,7 +32,7 @@ int main(int argc, char **argv)
 
 	cg2 = create_and_get_cgroup(CGROUP_PATH);
 
-	if (!cg2)
+	if (cg2 < 0)
 		goto err;
 
 	if (bpf_map_update_elem(map_fd[0], &idx, &cg2, BPF_ANY)) {
tools/testing/selftests/bpf/cgroup_helpers.c +3 -3
index cf16948aad4a..6692a40a6979 100644
@@ -155,7 +155,7 @@ void cleanup_cgroup_environment(void)
  * This function creates a cgroup under the top level workdir and returns the
  * file descriptor. It is idempotent.
  *
- * On success, it returns the file descriptor. On failure it returns 0.
+ * On success, it returns the file descriptor. On failure it returns -1.
  * If there is a failure, it prints the error to stderr.
  */
 int create_and_get_cgroup(const char *path)
@@ -166,13 +166,13 @@ int create_and_get_cgroup(const char *path)
 	format_cgroup_path(cgroup_path, path);
 	if (mkdir(cgroup_path, 0777) && errno != EEXIST) {
 		log_err("mkdiring cgroup %s .. %s", path, cgroup_path);
-		return 0;
+		return -1;
 	}
 
 	fd = open(cgroup_path, O_RDONLY);
 	if (fd < 0) {
 		log_err("Opening Cgroup");
-		return 0;
+		return -1;
 	}
 
 	return fd;
tools/testing/selftests/bpf/test_cgroup_storage.c +1 -1
index f44834155f25..2fc4625c1a15 100644
@@ -81,7 +81,7 @@ int main(int argc, char **argv)
 
 	/* Create a cgroup, get fd, and join it */
 	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-	if (!cgroup_fd) {
+	if (cgroup_fd < 0) {
 		printf("Failed to create test cgroup\n");
 		goto err;
 	}
tools/testing/selftests/bpf/test_dev_cgroup.c +1 -1
index 9c8b50bac7e0..76e4993b7c16 100644
@@ -43,7 +43,7 @@ int main(int argc, char **argv)
 
 	/* Create a cgroup, get fd, and join it */
 	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-	if (!cgroup_fd) {
+	if (cgroup_fd < 0) {
 		printf("Failed to create test cgroup\n");
 		goto err;
 	}
tools/testing/selftests/bpf/test_netcnt.c +1 -1
index 44ed7f29f8ab..c1da5404454a 100644
@@ -65,7 +65,7 @@ int main(int argc, char **argv)
 
 	/* Create a cgroup, get fd, and join it */
 	cgroup_fd = create_and_get_cgroup(TEST_CGROUP);
-	if (!cgroup_fd) {
+	if (cgroup_fd < 0) {
 		printf("Failed to create test cgroup\n");
 		goto err;
 	}
tools/testing/selftests/bpf/test_skb_cgroup_id_user.c +1 -1
index c121cc59f314..9220747c069d 100644
@@ -164,7 +164,7 @@ int main(int argc, char **argv)
 		goto err;
 
 	cgfd = create_and_get_cgroup(CGROUP_PATH);
-	if (!cgfd)
+	if (cgfd < 0)
 		goto err;
 
 	if (join_cgroup(CGROUP_PATH))
tools/testing/selftests/bpf/test_sock.c +1 -1
index b8ebe2f58074..561ffb6d6433 100644
@@ -458,7 +458,7 @@ int main(int argc, char **argv)
 		goto err;
 
 	cgfd = create_and_get_cgroup(CG_PATH);
-	if (!cgfd)
+	if (cgfd < 0)
 		goto err;
 
 	if (join_cgroup(CG_PATH))
tools/testing/selftests/bpf/test_sock_addr.c +1 -1
index d94336cbd8bd..3f110eaaf29c 100644
@@ -1442,7 +1442,7 @@ int main(int argc, char **argv)
 		goto err;
 
 	cgfd = create_and_get_cgroup(CG_PATH);
-	if (!cgfd)
+	if (cgfd < 0)
 		goto err;
 
 	if (join_cgroup(CG_PATH))
tools/testing/selftests/bpf/test_socket_cookie.c +1 -1
index b6c2c605d8c0..fc7832ee566b 100644
@@ -202,7 +202,7 @@ int main(int argc, char **argv)
 		goto err;
 
 	cgfd = create_and_get_cgroup(CG_PATH);
-	if (!cgfd)
+	if (cgfd < 0)
 		goto err;
 
 	if (join_cgroup(CG_PATH))
tools/testing/selftests/bpf/test_tcpbpf_user.c +1 -1
index e6eebda7d112..716b4e3be581 100644
@@ -103,7 +103,7 @@ int main(int argc, char **argv)
 		goto err;
 
 	cg_fd = create_and_get_cgroup(cg_path);
-	if (!cg_fd)
+	if (cg_fd < 0)
 		goto err;
 
 	if (join_cgroup(cg_path))
tools/testing/selftests/bpf/test_tcpnotify_user.c +1 -1
index ff3c4522aed6..4e4353711a86 100644
@@ -115,7 +115,7 @@ int main(int argc, char **argv)
 		goto err;
 
 	cg_fd = create_and_get_cgroup(cg_path);
-	if (!cg_fd)
+	if (cg_fd < 0)
 		goto err;
 
 	if (join_cgroup(cg_path))
Signature · 1 line
2.20.1.97.g81188d93c3-goog
#1 lore raw
Quoted 5 lines · Stanislav Fomichev

We have some tests that assume create_and_get_cgroup returns -1 on error which is incorrect (it returns 0 on error). Since fd might be zero in general case, change create_and_get_cgroup to return -1 on error and fix the users that assume 0 on error.

Quoted 8 lines
Fixes f269099a7e7a ("tools/bpf: add a selftest for bpf_get_current_cgroup_id() helper")
Fixes 7d2c6cfc5411 ("bpf: use --cgroup in test_suite if supplied")

v2:
- instead of fixing the uses that assume -1 on error, convert the users that assume 0 on error (fd might be zero in general case)

Applied, Thanks