Commit 0ef6740e authored by Kumar Kartikeya Dwivedi's avatar Kumar Kartikeya Dwivedi Committed by Alexei Starovoitov
Browse files

selftests/bpf: Add tests for kptr_ref refcounting



Check at runtime how various operations for kptr_ref affect its refcount
and verify against the actual count.

Signed-off-by: default avatarKumar Kartikeya Dwivedi <memxor@gmail.com>
Link: https://lore.kernel.org/r/20220511194654.765705-5-memxor@gmail.com


Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
parent 04accf79
Loading
Loading
Loading
Loading
+24 −3
Original line number Diff line number Diff line
// SPDX-License-Identifier: GPL-2.0
#include <test_progs.h>
#include <network_helpers.h>

#include "map_kptr.skel.h"
#include "map_kptr_fail.skel.h"
@@ -81,8 +82,13 @@ static void test_map_kptr_fail(void)
	}
}

static void test_map_kptr_success(void)
static void test_map_kptr_success(bool test_run)
{
	LIBBPF_OPTS(bpf_test_run_opts, opts,
		.data_in = &pkt_v4,
		.data_size_in = sizeof(pkt_v4),
		.repeat = 1,
	);
	struct map_kptr *skel;
	int key = 0, ret;
	char buf[24];
@@ -91,6 +97,16 @@ static void test_map_kptr_success(void)
	if (!ASSERT_OK_PTR(skel, "map_kptr__open_and_load"))
		return;

	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_map_kptr_ref), &opts);
	ASSERT_OK(ret, "test_map_kptr_ref refcount");
	ASSERT_OK(opts.retval, "test_map_kptr_ref retval");
	ret = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.test_map_kptr_ref2), &opts);
	ASSERT_OK(ret, "test_map_kptr_ref2 refcount");
	ASSERT_OK(opts.retval, "test_map_kptr_ref2 retval");

	if (test_run)
		return;

	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.array_map), &key, buf, 0);
	ASSERT_OK(ret, "array_map update");
	ret = bpf_map_update_elem(bpf_map__fd(skel->maps.array_map), &key, buf, 0);
@@ -116,7 +132,12 @@ static void test_map_kptr_success(void)

void test_map_kptr(void)
{
	if (test__start_subtest("success"))
		test_map_kptr_success();
	if (test__start_subtest("success")) {
		test_map_kptr_success(false);
		/* Do test_run twice, so that we see refcount going back to 1
		 * after we leave it in map from first iteration.
		 */
		test_map_kptr_success(true);
	}
	test_map_kptr_fail();
}
+104 −2
Original line number Diff line number Diff line
@@ -141,7 +141,7 @@ SEC("tc")
int test_map_kptr(struct __sk_buff *ctx)
{
	struct map_value *v;
	int i, key = 0;
	int key = 0;

#define TEST(map)					\
	v = bpf_map_lookup_elem(&map, &key);		\
@@ -162,7 +162,7 @@ SEC("tc")
int test_map_in_map_kptr(struct __sk_buff *ctx)
{
	struct map_value *v;
	int i, key = 0;
	int key = 0;
	void *map;

#define TEST(map_in_map)                                \
@@ -187,4 +187,106 @@ int test_map_in_map_kptr(struct __sk_buff *ctx)
	return 0;
}

SEC("tc")
int test_map_kptr_ref(struct __sk_buff *ctx)
{
	struct prog_test_ref_kfunc *p, *p_st;
	unsigned long arg = 0;
	struct map_value *v;
	int key = 0, ret;

	p = bpf_kfunc_call_test_acquire(&arg);
	if (!p)
		return 1;

	p_st = p->next;
	if (p_st->cnt.refs.counter != 2) {
		ret = 2;
		goto end;
	}

	v = bpf_map_lookup_elem(&array_map, &key);
	if (!v) {
		ret = 3;
		goto end;
	}

	p = bpf_kptr_xchg(&v->ref_ptr, p);
	if (p) {
		ret = 4;
		goto end;
	}
	if (p_st->cnt.refs.counter != 2)
		return 5;

	p = bpf_kfunc_call_test_kptr_get(&v->ref_ptr, 0, 0);
	if (!p)
		return 6;
	if (p_st->cnt.refs.counter != 3) {
		ret = 7;
		goto end;
	}
	bpf_kfunc_call_test_release(p);
	if (p_st->cnt.refs.counter != 2)
		return 8;

	p = bpf_kptr_xchg(&v->ref_ptr, NULL);
	if (!p)
		return 9;
	bpf_kfunc_call_test_release(p);
	if (p_st->cnt.refs.counter != 1)
		return 10;

	p = bpf_kfunc_call_test_acquire(&arg);
	if (!p)
		return 11;
	p = bpf_kptr_xchg(&v->ref_ptr, p);
	if (p) {
		ret = 12;
		goto end;
	}
	if (p_st->cnt.refs.counter != 2)
		return 13;
	/* Leave in map */

	return 0;
end:
	bpf_kfunc_call_test_release(p);
	return ret;
}

SEC("tc")
int test_map_kptr_ref2(struct __sk_buff *ctx)
{
	struct prog_test_ref_kfunc *p, *p_st;
	struct map_value *v;
	int key = 0;

	v = bpf_map_lookup_elem(&array_map, &key);
	if (!v)
		return 1;

	p_st = v->ref_ptr;
	if (!p_st || p_st->cnt.refs.counter != 2)
		return 2;

	p = bpf_kptr_xchg(&v->ref_ptr, NULL);
	if (!p)
		return 3;
	if (p_st->cnt.refs.counter != 2) {
		bpf_kfunc_call_test_release(p);
		return 4;
	}

	p = bpf_kptr_xchg(&v->ref_ptr, p);
	if (p) {
		bpf_kfunc_call_test_release(p);
		return 5;
	}
	if (p_st->cnt.refs.counter != 2)
		return 6;

	return 0;
}

char _license[] SEC("license") = "GPL";