kernel/livepatch/core.c
Source file repositories/reference/linux-study-clean/kernel/livepatch/core.c
File Facts
- System
- Linux kernel
- Corpus path
kernel/livepatch/core.c- Extension
.c- Size
- 34235 bytes
- Lines
- 1388
- 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.
- 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/module.hlinux/kernel.hlinux/mutex.hlinux/slab.hlinux/list.hlinux/kallsyms.hlinux/livepatch.hlinux/elf.hlinux/moduleloader.hlinux/completion.hlinux/memory.hlinux/rcupdate.hasm/cacheflush.hcore.hpatch.hstate.htransition.h
Detected Declarations
struct klp_find_argfunction klp_is_modulefunction klp_find_object_modulefunction klp_initializedfunction klp_for_each_funcfunction klp_for_each_objectfunction klp_match_callbackfunction klp_find_callbackfunction klp_find_object_symbolfunction klp_resolve_symbolsfunction clear_relocate_addfunction codefunction klp_apply_section_relocsfunction enabled_storefunction enabled_showfunction transition_showfunction force_storefunction replace_showfunction stack_order_showfunction klp_for_each_patchfunction patched_showfunction klp_free_object_dynamicfunction klp_free_func_nopfunction klp_add_object_nopsfunction klp_for_each_funcfunction klp_add_nopsfunction klp_for_each_patchfunction klp_kobj_release_patchfunction klp_kobj_release_objectfunction klp_kobj_release_funcfunction __klp_free_funcsfunction klp_for_each_func_safefunction klp_free_object_loadedfunction klp_for_each_funcfunction __klp_free_objectsfunction klp_for_each_object_safefunction klp_free_objectsfunction klp_free_objects_dynamicfunction klp_free_patch_finishfunction klp_free_patch_startfunction klp_free_patch_work_fnfunction klp_free_patch_asyncfunction klp_free_replaced_patches_asyncfunction klp_for_each_patch_safefunction klp_init_funcfunction klp_write_object_relocsfunction klp_apply_object_relocsfunction klp_clear_object_relocs
Annotated Snippet
* This function is supposed to be called from the livepatch module_init()
* callback.
*
* Return: 0 on success, otherwise error
*/
int klp_enable_patch(struct klp_patch *patch)
{
int ret;
struct klp_object *obj;
if (!patch || !patch->mod || !patch->objs)
return -EINVAL;
klp_for_each_object_static(patch, obj) {
if (!obj->funcs)
return -EINVAL;
}
if (!is_livepatch_module(patch->mod)) {
pr_err("module %s is not marked as a livepatch module\n",
patch->mod->name);
return -EINVAL;
}
if (!klp_initialized())
return -ENODEV;
if (!klp_have_reliable_stack()) {
pr_warn("This architecture doesn't have support for the livepatch consistency model.\n");
pr_warn("The livepatch transition may never complete.\n");
}
mutex_lock(&klp_mutex);
if (!klp_is_patch_compatible(patch)) {
pr_err("Livepatch patch (%s) is not compatible with the already installed livepatches.\n",
patch->mod->name);
mutex_unlock(&klp_mutex);
return -EINVAL;
}
if (!try_module_get(patch->mod)) {
mutex_unlock(&klp_mutex);
return -ENODEV;
}
klp_init_patch_early(patch);
ret = klp_init_patch(patch);
if (ret)
goto err;
ret = __klp_enable_patch(patch);
if (ret)
goto err;
mutex_unlock(&klp_mutex);
return 0;
err:
klp_free_patch_start(patch);
mutex_unlock(&klp_mutex);
klp_free_patch_finish(patch);
return ret;
}
EXPORT_SYMBOL_GPL(klp_enable_patch);
/*
* This function unpatches objects from the replaced livepatches.
*
* We could be pretty aggressive here. It is called in the situation where
* these structures are no longer accessed from the ftrace handler.
* All functions are redirected by the klp_transition_patch. They
* use either a new code or they are in the original code because
* of the special nop function patches.
*
* The only exception is when the transition was forced. In this case,
* klp_ftrace_handler() might still see the replaced patch on the stack.
* Fortunately, it is carefully designed to work with removed functions
* thanks to RCU. We only have to keep the patches on the system. Also
* this is handled transparently by patch->module_put.
*/
void klp_unpatch_replaced_patches(struct klp_patch *new_patch)
{
struct klp_patch *old_patch;
Annotation
- Immediate include surface: `linux/module.h`, `linux/kernel.h`, `linux/mutex.h`, `linux/slab.h`, `linux/list.h`, `linux/kallsyms.h`, `linux/livepatch.h`, `linux/elf.h`.
- Detected declarations: `struct klp_find_arg`, `function klp_is_module`, `function klp_find_object_module`, `function klp_initialized`, `function klp_for_each_func`, `function klp_for_each_object`, `function klp_match_callback`, `function klp_find_callback`, `function klp_find_object_symbol`, `function klp_resolve_symbols`.
- 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.