tools/testing/selftests/futex/functional/robust_list.c
Source file repositories/reference/linux-study-clean/tools/testing/selftests/futex/functional/robust_list.c
File Facts
- System
- Linux kernel
- Corpus path
tools/testing/selftests/futex/functional/robust_list.c- Extension
.c- Size
- 17944 bytes
- Lines
- 792
- 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
futextest.h../../kselftest_harness.hdlfcn.herrno.hpthread.hsignal.hstdint.hstdatomic.hstdbool.hstddef.hsys/auxv.hsys/mman.hsys/wait.h
Detected Declarations
struct lock_structfunction set_robust_listfunction get_robust_listfunction sys_futex_robust_unlockfunction create_childfunction set_listfunction basicfunction child_fn_lockfunction child_listfunction child_fn_lock_with_errorfunction child_lock_holderfunction child_wait_lockfunction child_circular_listfunction __vdso_futex_robust_list64_try_unlockfunction robust_listXX_try_unlockfunction wordfunction waken
Annotated Snippet
struct lock_struct {
_Atomic(unsigned int) futex;
struct robust_list list;
};
/*
* Helper function to spawn a child thread. Returns -1 on error, pid on success
*/
static int create_child(int (*fn)(void *arg), void *arg)
{
char *stack;
pid_t pid;
stack = mmap(NULL, STACK_SIZE, PROT_READ | PROT_WRITE,
MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, -1, 0);
if (stack == MAP_FAILED)
return -1;
stack += STACK_SIZE;
pid = clone(fn, stack, CLONE_VM | SIGCHLD, arg);
if (pid == -1)
return -1;
return pid;
}
/*
* Helper function to prepare and register a robust list
*/
static int set_list(struct robust_list_head *head)
{
int ret;
ret = set_robust_list(head, sizeof(*head));
if (ret)
return ret;
head->futex_offset = (size_t) offsetof(struct lock_struct, futex) -
(size_t) offsetof(struct lock_struct, list);
head->list.next = &head->list;
head->list_op_pending = NULL;
return 0;
}
/*
* A basic (and incomplete) mutex lock function with robustness
*/
static int mutex_lock(struct lock_struct *lock, struct robust_list_head *head, bool error_inject)
{
_Atomic(unsigned int) *futex = &lock->futex;
unsigned int zero = 0;
pid_t tid = gettid();
int ret = -1;
/*
* Set list_op_pending before starting the lock, so the kernel can catch
* the case where the thread died during the lock operation
*/
head->list_op_pending = &lock->list;
if (atomic_compare_exchange_strong(futex, &zero, tid)) {
/*
* We took the lock, insert it in the robust list
*/
struct robust_list *list = &head->list;
/* Error injection to test list_op_pending */
if (error_inject)
return 0;
while (list->next != &head->list)
list = list->next;
list->next = &lock->list;
lock->list.next = &head->list;
ret = 0;
} else {
/*
* We didn't take the lock, wait until the owner wakes (or dies)
*/
struct timespec to;
to.tv_sec = FUTEX_TIMEOUT;
to.tv_nsec = 0;
tid = atomic_load(futex);
Annotation
- Immediate include surface: `futextest.h`, `../../kselftest_harness.h`, `dlfcn.h`, `errno.h`, `pthread.h`, `signal.h`, `stdint.h`, `stdatomic.h`.
- Detected declarations: `struct lock_struct`, `function set_robust_list`, `function get_robust_list`, `function sys_futex_robust_unlock`, `function create_child`, `function set_list`, `function basic`, `function child_fn_lock`, `function child_list`, `function child_fn_lock_with_error`.
- 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.