Skip to contentSkip to conversation

selftests/bpf: work-around EBUSY errors from hashmap update/delete

PATCH Superseded Andrii Nakryiko 3 participants 3 messages Dec 22, 2020 – Dec 23, 2020

Subsystems (5)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Andrii Nakryiko

author maintainer
#0 lore raw

PATCH 1/1 selftests/bpf: work-around EBUSY errors from hashmap update/delete

20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked") introduced a possibility of getting EBUSY error on lock contention, which seems to happen very deterministically in test_maps when running 1024 threads on low-CPU machine. In libbpf CI case, it's a 2 CPU VM and it's hitting this 100% of the time. Work around by retrying on EBUSY (and EAGAIN, while we are at it) after a small sleep. sched_yield() is too agressive and fails even after 20 retries, so I went with usleep(1) for backoff.

Also log actual error returned to make it easier to see what's going on.

Fixes 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked")
Cc Song Liu <[email protected]>
---
 tools/testing/selftests/bpf/test_maps.c | 46 +++++++++++++++++++++----
 1 file changed, 40 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/test_maps.c +40 -6
index 0ad3e6305ff0..809004f4995f 100644
@@ -1312,22 +1312,56 @@ static void test_map_stress(void)
 #define DO_UPDATE 1
 #define DO_DELETE 0
 
+static int map_update_retriable(int map_fd, const void *key, const void *value,
+				int flags, int attempts)
+{
+	while (bpf_map_update_elem(map_fd, key, value, flags)) {
+		if (!attempts || (errno != EAGAIN && errno != EBUSY))
+			return -errno;
+
+		usleep(1);
+		attempts--;
+	}
+
+	return 0;
+}
+
+static int map_delete_retriable(int map_fd, const void *key, int attempts)
+{
+	while (bpf_map_delete_elem(map_fd, key)) {
+		if (!attempts || (errno != EAGAIN && errno != EBUSY))
+			return -errno;
+
+		usleep(1);
+		attempts--;
+	}
+
+	return 0;
+}
+
 static void test_update_delete(unsigned int fn, void *data)
 {
 	int do_update = ((int *)data)[1];
 	int fd = ((int *)data)[0];
-	int i, key, value;
+	int i, key, value, err;
 
 	for (i = fn; i < MAP_SIZE; i += TASKS) {
 		key = value = i;
 
 		if (do_update) {
-			assert(bpf_map_update_elem(fd, &key, &value,
-						   BPF_NOEXIST) == 0);
-			assert(bpf_map_update_elem(fd, &key, &value,
-						   BPF_EXIST) == 0);
+			err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, 20);
+			if (err)
+				printf("error %d %d\n", err, errno);
+			assert(err == 0);
+			err = map_update_retriable(fd, &key, &value, BPF_EXIST, 20);
+			if (err)
+				printf("error %d %d\n", err, errno);
+			assert(err == 0);
 		} else {
-			assert(bpf_map_delete_elem(fd, &key) == 0);
+			err = map_delete_retriable(fd, &key, 5);
+			if (err)
+				printf("error %d %d\n", err, errno);
+			assert(err == 0);
 		}
 	}
 }
Signature · 1 line
2.24.1
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Andrii Nakryiko

20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked") introduced a possibility of getting EBUSY error on lock contention, which seems to happen very deterministically in test_maps when running 1024 threads on low-CPU machine. In libbpf CI case, it's a 2 CPU VM and it's hitting this 100% of the time. Work around by retrying on EBUSY (and EAGAIN, while we are at it) after a small sleep. sched_yield() is too agressive and fails even after 20 retries, so I went with usleep(1) for backoff.

Quoted 5 lines

Also log actual error returned to make it easier to see what's going on.

Fixes 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked")
Cc Song Liu <[email protected]>

Thanks for the fix!

With one minor nitpick below

Quoted 11 lines

---
tools/testing/selftests/bpf/test_maps.c | 46 +++++++++++++++++++++----

1 file changed, 40 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/test_maps.c +0 -0
index 0ad3e6305ff0..809004f4995f 100644
@@ -1312,22 +1312,56 @@ static void test_map_stress(void)
 #define DO_UPDATE 1
 #define DO_DELETE 0

[...]

tools/testing/selftests/bpf/test_maps.c static void test_map_stress(void) · line 1361

Quoted 9 lines
tools/testing/selftests/bpf/test_maps.c +6 -0
+				printf("error %d %d\n", err, errno);
+			assert(err == 0);
+			err = map_update_retriable(fd, &key, &value, BPF_EXIST, 20);
+			if (err)
+				printf("error %d %d\n", err, errno);
+			assert(err == 0);
		} else {
tools/testing/selftests/bpf/test_maps.c +1 -1
-			assert(bpf_map_delete_elem(fd, &key) == 0);
+			err = map_delete_retriable(fd, &key, 5);

nit: Why 5 here vs. 20 above?

Quoted 9 lines
tools/testing/selftests/bpf/test_maps.c +3 -0
+			if (err)
+				printf("error %d %d\n", err, errno);
+			assert(err == 0);
		}
	}
}
Signature · 1 line
2.24.1
#2 lore raw
Quoted 49 lines · Song Liu
Quoted 13 lines · Andrii Nakryiko

20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked") introduced a possibility of getting EBUSY error on lock contention, which seems to happen very deterministically in test_maps when running 1024 threads on low-CPU machine. In libbpf CI case, it's a 2 CPU VM and it's hitting this 100% of the time. Work around by retrying on EBUSY (and EAGAIN, while we are at it) after a small sleep. sched_yield() is too agressive and fails even after 20 retries, so I went with usleep(1) for backoff.

Quoted 5 lines

Also log actual error returned to make it easier to see what's going on.

Fixes 20b6cc34ea74 ("bpf: Avoid hashtab deadlock with map_locked")
Cc Song Liu <[email protected]>

Thanks for the fix!

With one minor nitpick below

Quoted 11 lines

---
tools/testing/selftests/bpf/test_maps.c | 46 +++++++++++++++++++++----

1 file changed, 40 insertions(+), 6 deletions(-)
tools/testing/selftests/bpf/test_maps.c +0 -0
index 0ad3e6305ff0..809004f4995f 100644
@@ -1312,22 +1312,56 @@ static void test_map_stress(void)
 #define DO_UPDATE 1
 #define DO_DELETE 0

[...]

tools/testing/selftests/bpf/test_maps.c static void test_map_stress(void) · line 1361

Quoted 9 lines
tools/testing/selftests/bpf/test_maps.c +7 -1
+                             printf("error %d %d\n", err, errno);
+                     assert(err == 0);
+                     err = map_update_retriable(fd, &key, &value, BPF_EXIST, 20);
+                     if (err)
+                             printf("error %d %d\n", err, errno);
+                     assert(err == 0);
              } else {
-                     assert(bpf_map_delete_elem(fd, &key) == 0);
+                     err = map_delete_retriable(fd, &key, 5);
Quoted 1 line

nit: Why 5 here vs. 20 above?

Forgot to update here. I'll make all of them the same, thanks.

Quoted 11 lines
Quoted 10 lines
tools/testing/selftests/bpf/test_maps.c +3 -0
+                     if (err)
+                             printf("error %d %d\n", err, errno);
+                     assert(err == 0);
              }
      }

}
--
2.24.1