arch/x86/kernel/ftrace.c
Source file repositories/reference/linux-study-clean/arch/x86/kernel/ftrace.c
File Facts
- System
- Linux kernel
- Corpus path
arch/x86/kernel/ftrace.c- Extension
.c- Size
- 18220 bytes
- Lines
- 677
- Domain
- Architecture Layer
- Bucket
- arch/x86
- Inferred role
- Architecture Layer: implementation source
- Status
- source implementation candidate
Why This File Exists
CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- CPU and platform-specific kernel glue: boot entry, traps, syscall entry, interrupts, page tables, context switch, and low-level barriers.
- 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/spinlock.hlinux/hardirq.hlinux/uaccess.hlinux/ftrace.hlinux/percpu.hlinux/sched.hlinux/slab.hlinux/init.hlinux/list.hlinux/module.hlinux/memory.hlinux/vmalloc.hlinux/set_memory.hlinux/execmem.htrace/syscall.hasm/kprobes.hasm/ftrace.hasm/nops.hasm/text-patching.h
Detected Declarations
function ftrace_arch_code_modify_preparefunction ftrace_arch_code_modify_post_processfunction ftrace_verify_codefunction text_poke_earlyfunction ftrace_make_nopfunction ftrace_make_callfunction __ftrace_replace_codefunction ftrace_update_ftrace_funcfunction ftrace_replace_codefunction for_ftrace_rec_iterfunction for_ftrace_rec_iterfunction arch_ftrace_update_codefunction tramp_freefunction create_trampolinefunction set_ftrace_ops_rofunction do_for_each_ftrace_opfunction calc_trampoline_call_offsetfunction arch_ftrace_update_trampolinefunction arch_ftrace_trampoline_freefunction ftrace_mod_jmpfunction ftrace_enable_ftrace_graph_callerfunction ftrace_disable_ftrace_graph_callerfunction skip_ftrace_returnfunction prepare_ftrace_returnfunction ftrace_graph_func
Annotated Snippet
switch (ftrace_test_record(rec, enable)) {
case FTRACE_UPDATE_IGNORE:
default:
continue;
case FTRACE_UPDATE_MAKE_CALL:
old = ftrace_nop_replace();
break;
case FTRACE_UPDATE_MODIFY_CALL:
case FTRACE_UPDATE_MAKE_NOP:
old = ftrace_call_replace(rec->ip, ftrace_get_addr_curr(rec));
break;
}
ret = ftrace_verify_code(rec->ip, old);
if (ret) {
ftrace_expected = old;
ftrace_bug(ret, rec);
ftrace_expected = NULL;
return;
}
}
for_ftrace_rec_iter(iter) {
rec = ftrace_rec_iter_record(iter);
switch (ftrace_test_record(rec, enable)) {
case FTRACE_UPDATE_IGNORE:
default:
continue;
case FTRACE_UPDATE_MAKE_CALL:
case FTRACE_UPDATE_MODIFY_CALL:
new = ftrace_call_replace(rec->ip, ftrace_get_addr_new(rec));
break;
case FTRACE_UPDATE_MAKE_NOP:
new = ftrace_nop_replace();
break;
}
smp_text_poke_batch_add((void *)rec->ip, new, MCOUNT_INSN_SIZE, NULL);
ftrace_update_record(rec, enable);
}
smp_text_poke_batch_finish();
}
void arch_ftrace_update_code(int command)
{
ftrace_modify_all_code(command);
}
/* Currently only x86_64 supports dynamic trampolines */
#ifdef CONFIG_X86_64
static inline void *alloc_tramp(unsigned long size)
{
return execmem_alloc_rw(EXECMEM_FTRACE, size);
}
static inline void tramp_free(void *tramp)
{
execmem_free(tramp);
}
/* Defined as markers to the end of the ftrace default trampolines */
extern void ftrace_regs_caller_end(void);
extern void ftrace_caller_end(void);
extern void ftrace_caller_op_ptr(void);
extern void ftrace_regs_caller_op_ptr(void);
extern void ftrace_regs_caller_jmp(void);
/* movq function_trace_op(%rip), %rdx */
/* 0x48 0x8b 0x15 <offset-to-ftrace_trace_op (4 bytes)> */
#define OP_REF_SIZE 7
/*
* The ftrace_ops is passed to the function callback. Since the
* trampoline only services a single ftrace_ops, we can pass in
* that ops directly.
*
* The ftrace_op_code_union is used to create a pointer to the
* ftrace_ops that will be passed to the callback function.
*/
union ftrace_op_code_union {
char code[OP_REF_SIZE];
struct {
char op[3];
int offset;
} __attribute__((packed));
Annotation
- Immediate include surface: `linux/spinlock.h`, `linux/hardirq.h`, `linux/uaccess.h`, `linux/ftrace.h`, `linux/percpu.h`, `linux/sched.h`, `linux/slab.h`, `linux/init.h`.
- Detected declarations: `function ftrace_arch_code_modify_prepare`, `function ftrace_arch_code_modify_post_process`, `function ftrace_verify_code`, `function text_poke_early`, `function ftrace_make_nop`, `function ftrace_make_call`, `function __ftrace_replace_code`, `function ftrace_update_ftrace_func`, `function ftrace_replace_code`, `function for_ftrace_rec_iter`.
- Atlas domain: Architecture Layer / arch/x86.
- 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.