Skip to content

Commit

Permalink
selftests/bpf: Export map_update_retriable()
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Hou Tao authored and Alexei Starovoitov committed Nov 10, 2023
1 parent d79924c commit b9b7955
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 5 deletions.
17 changes: 12 additions & 5 deletions tools/testing/selftests/bpf/test_maps.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions tools/testing/selftests/bpf/test_maps.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>

#define CHECK(condition, tag, format...) ({ \
int __ret = !!(condition); \
Expand All @@ -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

0 comments on commit b9b7955

Please sign in to comment.