tools/testing/selftests/bpf/prog_tests/test_task_local_data.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/bpf/prog_tests/test_task_local_data.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/bpf/prog_tests/test_task_local_data.c- Extension
.c- Size
- 11183 bytes
- Lines
- 376
- 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.
- Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
pthread.hbpf/btf.htest_progs.htask_local_data.htest_task_local_data.skel.h
Detected Declarations
struct test_tld_structfunction reset_tldfunction test_task_local_data_basicfunction test_task_local_data_racefunction test_task_local_data_dyn_sizefunction test_task_local_data
Annotated Snippet
struct test_tld_struct {
__u64 a;
__u64 b;
__u64 c;
__u64 d;
};
#include "test_task_local_data.skel.h"
TLD_DEFINE_KEY(value0_key, "value0", sizeof(int));
/*
* Reset task local data between subtests by clearing metadata other
* than the statically defined value0. This is safe as subtests run
* sequentially. Users of task local data library should not touch
* library internal.
*/
static void reset_tld(__u16 dyn_data_size)
{
if (tld_meta_p) {
/* Remove TLDs created by tld_create_key() */
tld_meta_p->cnt = 1;
tld_meta_p->size = dyn_data_size + 8;
memset(&tld_meta_p->metadata[1], 0,
(TLD_MAX_DATA_CNT - 1) * sizeof(struct tld_metadata));
}
}
/* Serialize access to bpf program's global variables */
static pthread_mutex_t global_mutex;
static tld_key_t *tld_keys;
#define TEST_BASIC_THREAD_NUM 32
void *test_task_local_data_basic_thread(void *arg)
{
LIBBPF_OPTS(bpf_test_run_opts, opts);
struct test_task_local_data *skel = (struct test_task_local_data *)arg;
int fd, err, tid, *value0, *value1;
struct test_tld_struct *value2;
fd = bpf_map__fd(skel->maps.tld_data_map);
value0 = tld_get_data(fd, value0_key);
if (!ASSERT_OK_PTR(value0, "tld_get_data"))
goto out;
value1 = tld_get_data(fd, tld_keys[1]);
if (!ASSERT_OK_PTR(value1, "tld_get_data"))
goto out;
value2 = tld_get_data(fd, tld_keys[2]);
if (!ASSERT_OK_PTR(value2, "tld_get_data"))
goto out;
tid = sys_gettid();
*value0 = tid + 0;
*value1 = tid + 1;
value2->a = tid + 2;
value2->b = tid + 3;
value2->c = tid + 4;
value2->d = tid + 5;
pthread_mutex_lock(&global_mutex);
/* Run task_main that read task local data and save to global variables */
err = bpf_prog_test_run_opts(bpf_program__fd(skel->progs.task_main), &opts);
ASSERT_OK(err, "run task_main");
ASSERT_OK(opts.retval, "task_main retval");
ASSERT_EQ(skel->bss->test_value0, tid + 0, "tld_get_data value0");
ASSERT_EQ(skel->bss->test_value1, tid + 1, "tld_get_data value1");
ASSERT_EQ(skel->bss->test_value2.a, tid + 2, "tld_get_data value2.a");
ASSERT_EQ(skel->bss->test_value2.b, tid + 3, "tld_get_data value2.b");
ASSERT_EQ(skel->bss->test_value2.c, tid + 4, "tld_get_data value2.c");
ASSERT_EQ(skel->bss->test_value2.d, tid + 5, "tld_get_data value2.d");
pthread_mutex_unlock(&global_mutex);
/* Make sure valueX are indeed local to threads */
ASSERT_EQ(*value0, tid + 0, "value0");
ASSERT_EQ(*value1, tid + 1, "value1");
ASSERT_EQ(value2->a, tid + 2, "value2.a");
ASSERT_EQ(value2->b, tid + 3, "value2.b");
ASSERT_EQ(value2->c, tid + 4, "value2.c");
ASSERT_EQ(value2->d, tid + 5, "value2.d");
*value0 = tid + 5;
*value1 = tid + 4;
value2->a = tid + 3;
Annotation
- Immediate include surface: `pthread.h`, `bpf/btf.h`, `test_progs.h`, `task_local_data.h`, `test_task_local_data.skel.h`.
- Detected declarations: `struct test_tld_struct`, `function reset_tld`, `function test_task_local_data_basic`, `function test_task_local_data_race`, `function test_task_local_data_dyn_size`, `function test_task_local_data`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
- Synchronization appears in or near this file; preserve lock ordering, sleepability, and interrupt-context constraints.
Implementation Notes
- This generated page is the file-by-file coverage layer; curated subsystem chapters should link here when they synthesize a multi-file control flow.
- Core OS pages should be promoted from atlas-only to deep-reviewed when they explain data structures, invariants, locking, lifecycle, and C implementation snippets.
- Driver-family pages are intentionally pattern-oriented unless they are part of the selected PCIe/NVMe representative device path.