kernel/kcov.c
Source file repositories/reference/linux-study-clean/kernel/kcov.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/kcov.c- Extension
.c- Size
- 32305 bytes
- Lines
- 1157
- Domain
- Core OS
- Bucket
- Scheduler, Processes, Timers, Sync, And Syscalls
- Inferred role
- Core OS: operation-table or driver-model contract
- Status
- pattern 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.
- Defines an operation table; this is where Linux turns generic core objects into subsystem-specific behavior.
- Touches user memory; correctness depends on fault-safe copying and privilege boundary handling.
- Uses kernel synchronization; read lock ordering, sleepability, and interrupt context assumptions before translating.
- Allocates kernel memory; connect allocation flags and lifetime to context constraints.
- Defines or uses C structs; map object ownership, embedded links, reference counts, and lock ownership.
Dependency Surface
linux/atomic.hlinux/compiler.hlinux/errno.hlinux/export.hlinux/types.hlinux/file.hlinux/fs.hlinux/hashtable.hlinux/init.hlinux/jiffies.hlinux/kmsan-checks.hlinux/mm.hlinux/preempt.hlinux/printk.hlinux/sched.hlinux/slab.hlinux/spinlock.hlinux/vmalloc.hlinux/debugfs.hlinux/uaccess.hlinux/kcov.hlinux/refcount.hlinux/log2.hasm/setup.h
Detected Declarations
struct kcovstruct kcov_remote_areastruct kcov_remotestruct kcov_percpu_datafunction hash_for_each_possiblefunction list_for_eachfunction kcov_remote_area_putfunction in_serving_softirqfunction check_kcov_modefunction canonicalize_ipfunction __sanitizer_cov_trace_pcfunction write_comp_datafunction __sanitizer_cov_trace_cmp1function __sanitizer_cov_trace_cmp2function __sanitizer_cov_trace_cmp4function __sanitizer_cov_trace_cmp8function __sanitizer_cov_trace_const_cmp1function __sanitizer_cov_trace_const_cmp2function __sanitizer_cov_trace_const_cmp4function __sanitizer_cov_trace_const_cmp8function __sanitizer_cov_trace_switchfunction kcov_startfunction kcov_stopfunction kcov_task_resetfunction kcov_task_initfunction kcov_resetfunction kcov_remote_resetfunction kcov_disablefunction kcov_getfunction kcov_putfunction kcov_task_exitfunction kcov_remote_startfunction kcov_mmapfunction kcov_openfunction kcov_closefunction kcov_get_modefunction __sanitizer_cov_trace_pcfunction kcov_check_handlefunction kcov_ioctl_lockedfunction kcov_ioctlfunction kcov_remote_startfunction kcov_remote_softirq_startfunction kcov_remote_softirq_stopfunction kcov_remote_startfunction kcov_remote_startfunction kcov_move_areafunction kcov_remote_stopfunction kcov_common_handle
Annotated Snippet
static const struct file_operations kcov_fops = {
.open = kcov_open,
.unlocked_ioctl = kcov_ioctl,
.compat_ioctl = kcov_ioctl,
.mmap = kcov_mmap,
.release = kcov_close,
};
/*
* kcov_remote_start() and kcov_remote_stop() can be used to annotate a section
* of code in a kernel background thread or in a softirq to allow kcov to be
* used to collect coverage from that part of code.
*
* The handle argument of kcov_remote_start() identifies a code section that is
* used for coverage collection. A userspace process passes this handle to
* KCOV_REMOTE_ENABLE ioctl to make the used kcov device start collecting
* coverage for the code section identified by this handle.
*
* The usage of these annotations in the kernel code is different depending on
* the type of the kernel thread whose code is being annotated.
*
* For global kernel threads that are spawned in a limited number of instances
* (e.g. one USB hub_event() worker thread is spawned per USB HCD) and for
* softirqs, each instance must be assigned a unique 4-byte instance id. The
* instance id is then combined with a 1-byte subsystem id to get a handle via
* kcov_remote_handle(subsystem_id, instance_id).
*
* For local kernel threads that are spawned from system calls handler when a
* user interacts with some kernel interface (e.g. vhost workers), a handle is
* passed from a userspace process as the common_handle field of the
* kcov_remote_arg struct (note, that the user must generate a handle by using
* kcov_remote_handle() with KCOV_SUBSYSTEM_COMMON as the subsystem id and an
* arbitrary 4-byte non-zero number as the instance id). This common handle
* then gets saved into the task_struct of the process that issued the
* KCOV_REMOTE_ENABLE ioctl. When this process issues system calls that spawn
* kernel threads, the common handle must be retrieved via kcov_common_handle()
* and passed to the spawned threads via custom annotations. Those kernel
* threads must in turn be annotated with kcov_remote_start(common_handle) and
* kcov_remote_stop(). All of the threads that are spawned by the same process
* obtain the same handle, hence the name "common".
*
* See Documentation/dev-tools/kcov.rst for more details.
*
* Internally, kcov_remote_start() looks up the kcov device associated with the
* provided handle, allocates an area for coverage collection, and saves the
* pointers to kcov and area into the current task_struct to allow coverage to
* be collected via __sanitizer_cov_trace_pc().
* In turns kcov_remote_stop() clears those pointers from task_struct to stop
* collecting coverage and copies all collected coverage into the kcov area.
*/
static inline bool kcov_mode_enabled(unsigned int mode)
{
return (mode & ~KCOV_IN_CTXSW) != KCOV_MODE_DISABLED;
}
static void kcov_remote_softirq_start(struct task_struct *t)
__must_hold(&kcov_percpu_data.lock)
{
struct kcov_percpu_data *data = this_cpu_ptr(&kcov_percpu_data);
unsigned int mode;
mode = READ_ONCE(t->kcov_mode);
barrier();
if (kcov_mode_enabled(mode)) {
data->saved_mode = mode;
data->saved_size = t->kcov_size;
data->saved_area = t->kcov_area;
data->saved_sequence = t->kcov_sequence;
data->saved_kcov = t->kcov;
kcov_stop(t);
}
}
static void kcov_remote_softirq_stop(struct task_struct *t)
__must_hold(&kcov_percpu_data.lock)
{
struct kcov_percpu_data *data = this_cpu_ptr(&kcov_percpu_data);
if (data->saved_kcov) {
kcov_start(t, data->saved_kcov, data->saved_size,
data->saved_area, data->saved_mode,
data->saved_sequence);
data->saved_mode = 0;
data->saved_size = 0;
data->saved_area = NULL;
data->saved_sequence = 0;
data->saved_kcov = NULL;
}
}
Annotation
- Immediate include surface: `linux/atomic.h`, `linux/compiler.h`, `linux/errno.h`, `linux/export.h`, `linux/types.h`, `linux/file.h`, `linux/fs.h`, `linux/hashtable.h`.
- Detected declarations: `struct kcov`, `struct kcov_remote_area`, `struct kcov_remote`, `struct kcov_percpu_data`, `function hash_for_each_possible`, `function list_for_each`, `function kcov_remote_area_put`, `function in_serving_softirq`, `function check_kcov_mode`, `function canonicalize_ip`.
- Atlas domain: Core OS / Scheduler, Processes, Timers, Sync, And Syscalls.
- Implementation status: pattern implementation candidate.
- This snippet crosses the user/kernel memory boundary; validate fault handling and access checks before translating the pattern.
- 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.