lib/error-inject.c
Source file repositories/reference/linux-study-clean/lib/error-inject.c
File Facts
- System
- Linux kernel
- Corpus path
lib/error-inject.c- Extension
.c- Size
- 5434 bytes
- Lines
- 240
- Domain
- Kernel Services
- Bucket
- lib
- Inferred role
- Kernel Services: implementation source
- Status
- source implementation candidate
Why This File Exists
Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- Shared kernel service surface used by multiple subsystems, including helpers, cryptography, virtualization support, and async I/O infrastructure.
- 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/error-injection.hlinux/debugfs.hlinux/kallsyms.hlinux/kprobes.hlinux/module.hlinux/mutex.hlinux/list.hlinux/slab.hasm/sections.h
Detected Declarations
struct ei_entryfunction within_error_injection_listfunction get_injectable_error_typefunction populate_error_injection_listfunction populate_kernel_ei_listfunction module_load_ei_listfunction module_unload_ei_listfunction ei_module_callbackfunction module_ei_initfunction ei_seq_stopfunction ei_seq_showfunction ei_debugfs_initfunction init_error_injection
Annotated Snippet
struct ei_entry {
struct list_head list;
unsigned long start_addr;
unsigned long end_addr;
int etype;
void *priv;
};
bool within_error_injection_list(unsigned long addr)
{
struct ei_entry *ent;
bool ret = false;
mutex_lock(&ei_mutex);
list_for_each_entry(ent, &error_injection_list, list) {
if (addr >= ent->start_addr && addr < ent->end_addr) {
ret = true;
break;
}
}
mutex_unlock(&ei_mutex);
return ret;
}
int get_injectable_error_type(unsigned long addr)
{
struct ei_entry *ent;
int ei_type = -EINVAL;
mutex_lock(&ei_mutex);
list_for_each_entry(ent, &error_injection_list, list) {
if (addr >= ent->start_addr && addr < ent->end_addr) {
ei_type = ent->etype;
break;
}
}
mutex_unlock(&ei_mutex);
return ei_type;
}
/*
* Lookup and populate the error_injection_list.
*
* For safety reasons we only allow certain functions to be overridden with
* bpf_error_injection, so we need to populate the list of the symbols that have
* been marked as safe for overriding.
*/
static void populate_error_injection_list(struct error_injection_entry *start,
struct error_injection_entry *end,
void *priv)
{
struct error_injection_entry *iter;
struct ei_entry *ent;
unsigned long entry, offset = 0, size = 0;
mutex_lock(&ei_mutex);
for (iter = start; iter < end; iter++) {
entry = (unsigned long)dereference_symbol_descriptor((void *)iter->addr);
if (!kernel_text_address(entry) ||
!kallsyms_lookup_size_offset(entry, &size, &offset)) {
pr_err("Failed to find error inject entry at %p\n",
(void *)entry);
continue;
}
ent = kmalloc_obj(*ent);
if (!ent)
break;
ent->start_addr = entry;
ent->end_addr = entry + size;
ent->etype = iter->etype;
ent->priv = priv;
INIT_LIST_HEAD(&ent->list);
list_add_tail(&ent->list, &error_injection_list);
}
mutex_unlock(&ei_mutex);
}
/* Markers of the _error_inject_whitelist section */
extern struct error_injection_entry __start_error_injection_whitelist[];
extern struct error_injection_entry __stop_error_injection_whitelist[];
static void __init populate_kernel_ei_list(void)
{
populate_error_injection_list(__start_error_injection_whitelist,
__stop_error_injection_whitelist,
NULL);
}
Annotation
- Immediate include surface: `linux/error-injection.h`, `linux/debugfs.h`, `linux/kallsyms.h`, `linux/kprobes.h`, `linux/module.h`, `linux/mutex.h`, `linux/list.h`, `linux/slab.h`.
- Detected declarations: `struct ei_entry`, `function within_error_injection_list`, `function get_injectable_error_type`, `function populate_error_injection_list`, `function populate_kernel_ei_list`, `function module_load_ei_list`, `function module_unload_ei_list`, `function ei_module_callback`, `function module_ei_init`, `function ei_seq_stop`.
- Atlas domain: Kernel Services / lib.
- 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.