tools/testing/selftests/rseq/basic_percpu_ops_test.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/rseq/basic_percpu_ops_test.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/rseq/basic_percpu_ops_test.c- Extension
.c- Size
- 7981 bytes
- Lines
- 354
- 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.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
assert.hpthread.hsched.hstdint.hstdio.hstdlib.hstring.hstddef.hkselftest.hrseq.h
Detected Declarations
struct percpu_lock_entrystruct percpu_lockstruct test_data_entrystruct spinlock_test_datastruct percpu_list_nodestruct percpu_list_entrystruct percpu_listfunction get_current_cpu_idfunction rseq_validate_cpu_idfunction rseq_use_cpu_indexfunction get_current_cpu_idfunction rseq_validate_cpu_idfunction rseq_use_cpu_indexfunction rseq_this_cpu_lockfunction rseq_percpu_unlockfunction test_percpu_spinlockfunction this_cpu_list_pushfunction test_percpu_listfunction main
Annotated Snippet
struct percpu_lock_entry {
intptr_t v;
} __attribute__((aligned(128)));
struct percpu_lock {
struct percpu_lock_entry c[CPU_SETSIZE];
};
struct test_data_entry {
intptr_t count;
} __attribute__((aligned(128)));
struct spinlock_test_data {
struct percpu_lock lock;
struct test_data_entry c[CPU_SETSIZE];
int reps;
};
struct percpu_list_node {
intptr_t data;
struct percpu_list_node *next;
};
struct percpu_list_entry {
struct percpu_list_node *head;
} __attribute__((aligned(128)));
struct percpu_list {
struct percpu_list_entry c[CPU_SETSIZE];
};
/* A simple percpu spinlock. Returns the cpu lock was acquired on. */
int rseq_this_cpu_lock(struct percpu_lock *lock)
{
int cpu;
for (;;) {
int ret;
cpu = get_current_cpu_id();
ret = rseq_cmpeqv_storev(RSEQ_MO_RELAXED, RSEQ_PERCPU,
&lock->c[cpu].v, 0, 1, cpu);
if (rseq_likely(!ret))
break;
/* Retry if comparison fails or rseq aborts. */
}
/*
* Acquire semantic when taking lock after control dependency.
* Matches rseq_smp_store_release().
*/
rseq_smp_acquire__after_ctrl_dep();
return cpu;
}
void rseq_percpu_unlock(struct percpu_lock *lock, int cpu)
{
assert(lock->c[cpu].v == 1);
/*
* Release lock, with release semantic. Matches
* rseq_smp_acquire__after_ctrl_dep().
*/
rseq_smp_store_release(&lock->c[cpu].v, 0);
}
void *test_percpu_spinlock_thread(void *arg)
{
struct spinlock_test_data *data = arg;
int i, cpu;
if (rseq_register_current_thread()) {
fprintf(stderr, "Error: rseq_register_current_thread(...) failed(%d): %s\n",
errno, strerror(errno));
abort();
}
for (i = 0; i < data->reps; i++) {
cpu = rseq_this_cpu_lock(&data->lock);
data->c[cpu].count++;
rseq_percpu_unlock(&data->lock, cpu);
}
if (rseq_unregister_current_thread()) {
fprintf(stderr, "Error: rseq_unregister_current_thread(...) failed(%d): %s\n",
errno, strerror(errno));
abort();
}
return NULL;
}
/*
* A simple test which implements a sharded counter using a per-cpu
Annotation
- Immediate include surface: `assert.h`, `pthread.h`, `sched.h`, `stdint.h`, `stdio.h`, `stdlib.h`, `string.h`, `stddef.h`.
- Detected declarations: `struct percpu_lock_entry`, `struct percpu_lock`, `struct test_data_entry`, `struct spinlock_test_data`, `struct percpu_list_node`, `struct percpu_list_entry`, `struct percpu_list`, `function get_current_cpu_id`, `function rseq_validate_cpu_id`, `function rseq_use_cpu_index`.
- Atlas domain: Support Tooling And Documentation / tools.
- Implementation status: source implementation candidate.
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.