kernel/kcsan/core.c
Source file repositories/reference/linux-study-clean/kernel/kcsan/core.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/kcsan/core.c- Extension
.c- Size
- 48788 bytes
- Lines
- 1372
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: exported/initcall integration point
- Status
- integration implementation candidate
Why This File Exists
Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Core operating-system implementation surface: boot, tasks, memory, VFS, syscall-facing interfaces, synchronization, credentials, and isolation.
- Exports symbols or registers init work; inspect boot/module ordering and who consumes the exported contract.
- 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
linux/atomic.hlinux/bug.hlinux/delay.hlinux/export.hlinux/init.hlinux/kernel.hlinux/list.hlinux/minmax.hlinux/moduleparam.hlinux/percpu.hlinux/preempt.hlinux/sched.hlinux/string.hlinux/uaccess.hencoding.hkcsan.hpermissive.h
Detected Declarations
function insert_watchpointfunction try_consume_watchpointfunction consume_watchpointfunction remove_watchpointfunction kcsan_check_scoped_accessesfunction is_atomicfunction should_watchfunction kcsan_prandom_u32_maxfunction reset_kcsan_skipfunction kcsan_is_enabledfunction delay_accessfunction read_instrumented_memoryfunction kcsan_save_irqtracefunction kcsan_restore_irqtracefunction get_kcsan_stack_depthfunction add_kcsan_stack_depthfunction find_reorder_accessfunction set_reorder_accessfunction kcsan_found_watchpointfunction kcsan_setup_watchpointfunction TRUEfunction check_accessfunction kcsan_initfunction kcsan_disable_currentfunction kcsan_enable_currentfunction kcsan_enable_current_nowarnfunction kcsan_nestable_atomic_beginfunction kcsan_nestable_atomic_endfunction kcsan_flat_atomic_beginfunction kcsan_flat_atomic_endfunction kcsan_atomic_nextfunction kcsan_set_access_maskfunction kcsan_begin_scoped_accessfunction kcsan_end_scoped_accessfunction __kcsan_check_accessfunction __tsan_read_rangefunction __tsan_write_rangefunction __tsan_func_entryfunction __tsan_func_exitfunction __tsan_initfunction builtinsfunction __tsan_atomic_thread_fencefunction __tsan_atomic_signal_fenceexport kcsan_disable_currentexport kcsan_enable_currentexport kcsan_enable_current_nowarnexport kcsan_nestable_atomic_beginexport kcsan_nestable_atomic_end
Annotated Snippet
if (value_change == KCSAN_VALUE_CHANGE_MAYBE) {
if (access_mask != 0) {
/*
* For access with access_mask, we require a
* value-change, as it is likely that races on
* ~access_mask bits are expected.
*/
value_change = KCSAN_VALUE_CHANGE_FALSE;
} else if (size > 8 || is_assert) {
/* Always assume a value-change. */
value_change = KCSAN_VALUE_CHANGE_TRUE;
}
}
/*
* No need to increment 'data_races' counter, as the racing
* thread already did.
*
* Count 'assert_failures' for each failed ASSERT access,
* therefore both this thread and the racing thread may
* increment this counter.
*/
if (is_assert && value_change == KCSAN_VALUE_CHANGE_TRUE)
atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
kcsan_report_known_origin(ptr, size, type, ip,
value_change, watchpoint - watchpoints,
old, new, access_mask);
} else if (value_change == KCSAN_VALUE_CHANGE_TRUE) {
/* Inferring a race, since the value should not have changed. */
atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_RACES_UNKNOWN_ORIGIN]);
if (is_assert)
atomic_long_inc(&kcsan_counters[KCSAN_COUNTER_ASSERT_FAILURES]);
if (IS_ENABLED(CONFIG_KCSAN_REPORT_RACE_UNKNOWN_ORIGIN) || is_assert) {
kcsan_report_unknown_origin(ptr, size, type, ip,
old, new, access_mask);
}
}
/*
* Remove watchpoint; must be after reporting, since the slot may be
* reused after this point.
*/
remove_watchpoint(watchpoint);
atomic_long_dec(&kcsan_counters[KCSAN_COUNTER_USED_WATCHPOINTS]);
out_unlock:
if (!interrupt_watcher)
local_irq_restore(irq_flags);
kcsan_restore_irqtrace(current);
ctx->disable_scoped--;
/*
* Reordered accesses cannot be used for value change detection,
* therefore never consider for reordering if access_mask is set.
* ASSERT_EXCLUSIVE are not real accesses, ignore them as well.
*/
if (!access_mask && !is_assert)
set_reorder_access(ctx, ptr, size, type, ip);
out:
user_access_restore(ua_flags);
}
static __always_inline void
check_access(const volatile void *ptr, size_t size, int type, unsigned long ip)
{
atomic_long_t *watchpoint;
long encoded_watchpoint;
/*
* Do nothing for 0 sized check; this comparison will be optimized out
* for constant sized instrumentation (__tsan_{read,write}N).
*/
if (unlikely(size == 0))
return;
again:
/*
* Avoid user_access_save in fast-path: find_watchpoint is safe without
* user_access_save, as the address that ptr points to is only used to
* check if a watchpoint exists; ptr is never dereferenced.
*/
watchpoint = find_watchpoint((unsigned long)ptr, size,
!(type & KCSAN_ACCESS_WRITE),
&encoded_watchpoint);
/*
* It is safe to check kcsan_is_enabled() after find_watchpoint in the
* slow-path, as long as no state changes that cause a race to be
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/bug.h`, `linux/delay.h`, `linux/export.h`, `linux/init.h`, `linux/kernel.h`, `linux/list.h`, `linux/minmax.h`.
- Detected declarations: `function insert_watchpoint`, `function try_consume_watchpoint`, `function consume_watchpoint`, `function remove_watchpoint`, `function kcsan_check_scoped_accesses`, `function is_atomic`, `function should_watch`, `function kcsan_prandom_u32_max`, `function reset_kcsan_skip`, `function kcsan_is_enabled`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: integration 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.