tools/testing/selftests/bpf/progs/rbtree.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/progs/rbtree.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/rbtree.c
Extension
.c
Size
6830 bytes
Lines
324
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct node_data {
	long key;
	long data;
	struct bpf_rb_node node;
};

struct root_nested_inner {
	struct bpf_spin_lock glock;
	struct bpf_rb_root root __contains(node_data, node);
};

struct root_nested {
	struct root_nested_inner inner;
};

long less_callback_ran = -1;
long removed_key = -1;
long first_data[2] = {-1, -1};

#define private(name) SEC(".data." #name) __hidden __attribute__((aligned(8)))
private(A) struct bpf_spin_lock glock;
private(A) struct bpf_rb_root groot __contains(node_data, node);
private(A) struct bpf_rb_root groot_array[2] __contains(node_data, node);
private(A) struct bpf_rb_root groot_array_one[1] __contains(node_data, node);
private(B) struct root_nested groot_nested;

static bool less(struct bpf_rb_node *a, const struct bpf_rb_node *b)
{
	struct node_data *node_a;
	struct node_data *node_b;

	node_a = container_of(a, struct node_data, node);
	node_b = container_of(b, struct node_data, node);
	less_callback_ran = 1;

	return node_a->key < node_b->key;
}

static long __add_three(struct bpf_rb_root *root, struct bpf_spin_lock *lock)
{
	struct node_data *n, *m;

	n = bpf_obj_new(typeof(*n));
	if (!n)
		return 1;
	n->key = 5;

	m = bpf_obj_new(typeof(*m));
	if (!m) {
		bpf_obj_drop(n);
		return 2;
	}
	m->key = 1;

	bpf_spin_lock(lock);
	bpf_rbtree_add(root, &n->node, less);
	bpf_rbtree_add(root, &m->node, less);
	bpf_spin_unlock(lock);

	n = bpf_obj_new(typeof(*n));
	if (!n)
		return 3;
	n->key = 3;

	bpf_spin_lock(lock);
	bpf_rbtree_add(root, &n->node, less);
	bpf_spin_unlock(lock);
	return 0;
}

SEC("tc")
long rbtree_add_nodes(void *ctx)
{
	return __add_three(&groot, &glock);
}

SEC("tc")
long rbtree_add_nodes_nested(void *ctx)
{
	return __add_three(&groot_nested.inner.root, &groot_nested.inner.glock);
}

SEC("tc")
long rbtree_add_and_remove(void *ctx)
{
	struct bpf_rb_node *res = NULL;
	struct node_data *n, *m = NULL;

	n = bpf_obj_new(typeof(*n));
	if (!n)

Annotation

Implementation Notes