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

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

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/bpf/progs/test_map_lookup_percpu_elem.c
Extension
.c
Size
1719 bytes
Lines
77
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 read_percpu_elem_ctx {
	void *map;
	__u64 sum;
};

static int read_percpu_elem_callback(__u32 index, struct read_percpu_elem_ctx *ctx)
{
	__u64 key = 0;
	__u64 *value;

	value = bpf_map_lookup_percpu_elem(ctx->map, &key, index);
	if (value)
		ctx->sum += *value;
	return 0;
}

SEC("tp/syscalls/sys_enter_getuid")
int sysenter_getuid(const void *ctx)
{
	struct read_percpu_elem_ctx map_ctx;

	if (my_pid != (bpf_get_current_pid_tgid() >> 32))
		return 0;

	map_ctx.map = &percpu_array_map;
	map_ctx.sum = 0;
	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
	percpu_array_elem_sum = map_ctx.sum;

	map_ctx.map = &percpu_hash_map;
	map_ctx.sum = 0;
	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
	percpu_hash_elem_sum = map_ctx.sum;

	map_ctx.map = &percpu_lru_hash_map;
	map_ctx.sum = 0;
	bpf_loop(nr_cpus, read_percpu_elem_callback, &map_ctx, 0);
	percpu_lru_hash_elem_sum = map_ctx.sum;

	return 0;
}

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

Annotation

Implementation Notes