selftests/bpf: Export map_update_retriable()

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>
This commit is contained in:
Hou Tao 2023-11-01 11:24:54 +08:00 committed by Alexei Starovoitov
parent d79924ca57
commit b9b7955316
2 changed files with 17 additions and 5 deletions

View 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);

View 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