selftests/bpf: Export map_update_retriable()
authorHou Tao <houtao1@huawei.com>
Wed, 1 Nov 2023 03:24:54 +0000 (11:24 +0800)
committerAlexei Starovoitov <ast@kernel.org>
Fri, 10 Nov 2023 02:58:40 +0000 (18:58 -0800)
Export map_update_retriable() to make it usable for other map_test
cases. These cases may only need retry for specific errno, so add
a new callback parameter to let map_update_retriable() decide whether or
not the errno is retriable.

Signed-off-by: Hou Tao <houtao1@huawei.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20231101032455.3808547-3-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>
tools/testing/selftests/bpf/test_maps.c
tools/testing/selftests/bpf/test_maps.h

index 7fc00e423e4ddaa09f24455ba4195cc85d80b0e1..767e0693df1063239531974bbfb8ea8d848560e4 100644 (file)
@@ -1396,13 +1396,18 @@ static void test_map_stress(void)
 #define MAX_DELAY_US 50000
 #define MIN_DELAY_RANGE_US 5000
 
-static int map_update_retriable(int map_fd, const void *key, const void *value,
-                               int flags, int attempts)
+static bool retry_for_again_or_busy(int err)
+{
+       return (err == EAGAIN || err == EBUSY);
+}
+
+int map_update_retriable(int map_fd, const void *key, const void *value, int flags, int attempts,
+                        retry_for_error_fn need_retry)
 {
        int delay = rand() % MIN_DELAY_RANGE_US;
 
        while (bpf_map_update_elem(map_fd, key, value, flags)) {
-               if (!attempts || (errno != EAGAIN && errno != EBUSY))
+               if (!attempts || !need_retry(errno))
                        return -errno;
 
                if (delay <= MAX_DELAY_US / 2)
@@ -1445,11 +1450,13 @@ static void test_update_delete(unsigned int fn, void *data)
                key = value = i;
 
                if (do_update) {
-                       err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES);
+                       err = map_update_retriable(fd, &key, &value, BPF_NOEXIST, MAP_RETRIES,
+                                                  retry_for_again_or_busy);
                        if (err)
                                printf("error %d %d\n", err, errno);
                        assert(err == 0);
-                       err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES);
+                       err = map_update_retriable(fd, &key, &value, BPF_EXIST, MAP_RETRIES,
+                                                  retry_for_again_or_busy);
                        if (err)
                                printf("error %d %d\n", err, errno);
                        assert(err == 0);
index f6fbca761732f9db137964fb5cefdd8308fb1bee..e4ac704a536c1a28bcdc4ddaaf0156eccfceabc7 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 
 #define CHECK(condition, tag, format...) ({                            \
        int __ret = !!(condition);                                      \
@@ -16,4 +17,8 @@
 
 extern int skips;
 
+typedef bool (*retry_for_error_fn)(int err);
+int map_update_retriable(int map_fd, const void *key, const void *value, int flags, int attempts,
+                        retry_for_error_fn need_retry);
+
 #endif