Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
+11
View File
@@ -0,0 +1,11 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace-direct.o
obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace-direct-too.o
obj-$(CONFIG_SAMPLE_FTRACE_DIRECT) += ftrace-direct-modify.o
obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace-direct-multi.o
obj-$(CONFIG_SAMPLE_FTRACE_DIRECT_MULTI) += ftrace-direct-multi-modify.o
obj-$(CONFIG_SAMPLE_FTRACE_OPS) += ftrace-ops.o
CFLAGS_sample-trace-array.o := -I$(src)
obj-$(CONFIG_SAMPLE_TRACE_ARRAY) += sample-trace-array.o
@@ -0,0 +1,339 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/ftrace.h>
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
#include <asm/asm-offsets.h>
#endif
extern void my_direct_func1(void);
extern void my_direct_func2(void);
void my_direct_func1(void)
{
trace_printk("my direct func1\n");
}
void my_direct_func2(void)
{
trace_printk("my direct func2\n");
}
extern void my_tramp1(void *);
extern void my_tramp2(void *);
static unsigned long my_ip = (unsigned long)schedule;
#ifdef CONFIG_RISCV
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:\n"
" addi sp,sp,-2*"SZREG"\n"
" "REG_S" t0,0*"SZREG"(sp)\n"
" "REG_S" ra,1*"SZREG"(sp)\n"
" call my_direct_func1\n"
" "REG_L" t0,0*"SZREG"(sp)\n"
" "REG_L" ra,1*"SZREG"(sp)\n"
" addi sp,sp,2*"SZREG"\n"
" jr t0\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:\n"
" addi sp,sp,-2*"SZREG"\n"
" "REG_S" t0,0*"SZREG"(sp)\n"
" "REG_S" ra,1*"SZREG"(sp)\n"
" call my_direct_func2\n"
" "REG_L" t0,0*"SZREG"(sp)\n"
" "REG_L" ra,1*"SZREG"(sp)\n"
" addi sp,sp,2*"SZREG"\n"
" jr t0\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_RISCV */
#ifdef CONFIG_X86_64
#include <asm/ibt.h>
#include <asm/nospec-branch.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" call my_direct_func1\n"
" leave\n"
ASM_RET
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" call my_direct_func2\n"
" leave\n"
ASM_RET
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_X86_64 */
#ifdef CONFIG_S390
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" brasl %r14,my_direct_func1\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" brasl %r14,my_direct_func2\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_S390 */
#ifdef CONFIG_ARM64
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:"
" hint 34\n" // bti c
" sub sp, sp, #16\n"
" stp x9, x30, [sp]\n"
" bl my_direct_func1\n"
" ldp x30, x9, [sp]\n"
" add sp, sp, #16\n"
" ret x9\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:"
" hint 34\n" // bti c
" sub sp, sp, #16\n"
" stp x9, x30, [sp]\n"
" bl my_direct_func2\n"
" ldp x30, x9, [sp]\n"
" add sp, sp, #16\n"
" ret x9\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_ARM64 */
#ifdef CONFIG_LOONGARCH
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:\n"
" addi.d $sp, $sp, -16\n"
" st.d $t0, $sp, 0\n"
" st.d $ra, $sp, 8\n"
" bl my_direct_func1\n"
" ld.d $ra, $sp, 0\n"
" ld.d $t0, $sp, 8\n"
" addi.d $sp, $sp, 16\n"
" jr $t0\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:\n"
" addi.d $sp, $sp, -16\n"
" st.d $t0, $sp, 0\n"
" st.d $ra, $sp, 8\n"
" bl my_direct_func2\n"
" ld.d $ra, $sp, 0\n"
" ld.d $t0, $sp, 8\n"
" addi.d $sp, $sp, 16\n"
" jr $t0\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_LOONGARCH */
#ifdef CONFIG_PPC
#include <asm/ppc_asm.h>
#ifdef CONFIG_PPC64
#define STACK_FRAME_SIZE 48
#else
#define STACK_FRAME_SIZE 24
#endif
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
#define PPC64_TOC_SAVE_AND_UPDATE \
" std 2, 24(1)\n" \
" bcl 20, 31, 1f\n" \
" 1: mflr 12\n" \
" ld 2, (99f - 1b)(12)\n"
#define PPC64_TOC_RESTORE \
" ld 2, 24(1)\n"
#define PPC64_TOC \
" 99: .quad .TOC.@tocbase\n"
#else
#define PPC64_TOC_SAVE_AND_UPDATE ""
#define PPC64_TOC_RESTORE ""
#define PPC64_TOC ""
#endif
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtlr 0\n"
#define PPC_FTRACE_RET \
" blr\n"
#else
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtctr 0\n"
#define PPC_FTRACE_RET \
" mtlr 0\n" \
" bctr\n"
#endif
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
" bl my_direct_func1\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
" bl my_direct_func2\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
PPC64_TOC
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_PPC */
static struct ftrace_ops direct;
static unsigned long my_tramp = (unsigned long)my_tramp1;
static unsigned long tramps[2] = {
(unsigned long)my_tramp1,
(unsigned long)my_tramp2,
};
static int simple_thread(void *arg)
{
static int t;
int ret = 0;
while (!kthread_should_stop()) {
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(2 * HZ);
if (ret)
continue;
t ^= 1;
ret = modify_ftrace_direct(&direct, tramps[t]);
if (!ret)
my_tramp = tramps[t];
WARN_ON_ONCE(ret);
}
return 0;
}
static struct task_struct *simple_tsk;
static int __init ftrace_direct_init(void)
{
int ret;
ftrace_set_filter_ip(&direct, (unsigned long) my_ip, 0, 0);
ret = register_ftrace_direct(&direct, my_tramp);
if (!ret)
simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
return ret;
}
static void __exit ftrace_direct_exit(void)
{
kthread_stop(simple_tsk);
unregister_ftrace_direct(&direct, my_tramp, true);
}
module_init(ftrace_direct_init);
module_exit(ftrace_direct_exit);
MODULE_AUTHOR("Steven Rostedt");
MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
MODULE_LICENSE("GPL");
@@ -0,0 +1,383 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/ftrace.h>
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
#include <asm/asm-offsets.h>
#endif
extern void my_direct_func1(unsigned long ip);
extern void my_direct_func2(unsigned long ip);
void my_direct_func1(unsigned long ip)
{
trace_printk("my direct func1 ip %lx\n", ip);
}
void my_direct_func2(unsigned long ip)
{
trace_printk("my direct func2 ip %lx\n", ip);
}
extern void my_tramp1(void *);
extern void my_tramp2(void *);
#ifdef CONFIG_RISCV
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:\n"
" addi sp,sp,-3*"SZREG"\n"
" "REG_S" a0,0*"SZREG"(sp)\n"
" "REG_S" t0,1*"SZREG"(sp)\n"
" "REG_S" ra,2*"SZREG"(sp)\n"
" mv a0,t0\n"
" call my_direct_func1\n"
" "REG_L" a0,0*"SZREG"(sp)\n"
" "REG_L" t0,1*"SZREG"(sp)\n"
" "REG_L" ra,2*"SZREG"(sp)\n"
" addi sp,sp,3*"SZREG"\n"
" jr t0\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:\n"
" addi sp,sp,-3*"SZREG"\n"
" "REG_S" a0,0*"SZREG"(sp)\n"
" "REG_S" t0,1*"SZREG"(sp)\n"
" "REG_S" ra,2*"SZREG"(sp)\n"
" mv a0,t0\n"
" call my_direct_func2\n"
" "REG_L" a0,0*"SZREG"(sp)\n"
" "REG_L" t0,1*"SZREG"(sp)\n"
" "REG_L" ra,2*"SZREG"(sp)\n"
" addi sp,sp,3*"SZREG"\n"
" jr t0\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_RISCV */
#ifdef CONFIG_X86_64
#include <asm/ibt.h>
#include <asm/nospec-branch.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" pushq %rdi\n"
" movq 8(%rbp), %rdi\n"
" call my_direct_func1\n"
" popq %rdi\n"
" leave\n"
ASM_RET
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" pushq %rdi\n"
" movq 8(%rbp), %rdi\n"
" call my_direct_func2\n"
" popq %rdi\n"
" leave\n"
ASM_RET
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_X86_64 */
#ifdef CONFIG_S390
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" lgr %r2,%r0\n"
" brasl %r14,my_direct_func1\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp1, .-my_tramp1\n"
"\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" lgr %r2,%r0\n"
" brasl %r14,my_direct_func2\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_S390 */
#ifdef CONFIG_ARM64
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:"
" hint 34\n" // bti c
" sub sp, sp, #32\n"
" stp x9, x30, [sp]\n"
" str x0, [sp, #16]\n"
" mov x0, x30\n"
" bl my_direct_func1\n"
" ldp x30, x9, [sp]\n"
" ldr x0, [sp, #16]\n"
" add sp, sp, #32\n"
" ret x9\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:"
" hint 34\n" // bti c
" sub sp, sp, #32\n"
" stp x9, x30, [sp]\n"
" str x0, [sp, #16]\n"
" mov x0, x30\n"
" bl my_direct_func2\n"
" ldp x30, x9, [sp]\n"
" ldr x0, [sp, #16]\n"
" add sp, sp, #32\n"
" ret x9\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_ARM64 */
#ifdef CONFIG_LOONGARCH
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:\n"
" addi.d $sp, $sp, -32\n"
" st.d $a0, $sp, 0\n"
" st.d $t0, $sp, 8\n"
" st.d $ra, $sp, 16\n"
" move $a0, $t0\n"
" bl my_direct_func1\n"
" ld.d $a0, $sp, 0\n"
" ld.d $ra, $sp, 8\n"
" ld.d $t0, $sp, 16\n"
" addi.d $sp, $sp, 32\n"
" jr $t0\n"
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:\n"
" addi.d $sp, $sp, -32\n"
" st.d $a0, $sp, 0\n"
" st.d $t0, $sp, 8\n"
" st.d $ra, $sp, 16\n"
" move $a0, $t0\n"
" bl my_direct_func2\n"
" ld.d $a0, $sp, 0\n"
" ld.d $ra, $sp, 8\n"
" ld.d $t0, $sp, 16\n"
" addi.d $sp, $sp, 32\n"
" jr $t0\n"
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_LOONGARCH */
#ifdef CONFIG_PPC
#include <asm/ppc_asm.h>
#ifdef CONFIG_PPC64
#define STACK_FRAME_SIZE 48
#else
#define STACK_FRAME_SIZE 24
#endif
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
#define PPC64_TOC_SAVE_AND_UPDATE \
" std 2, 24(1)\n" \
" bcl 20, 31, 1f\n" \
" 1: mflr 12\n" \
" ld 2, (99f - 1b)(12)\n"
#define PPC64_TOC_RESTORE \
" ld 2, 24(1)\n"
#define PPC64_TOC \
" 99: .quad .TOC.@tocbase\n"
#else
#define PPC64_TOC_SAVE_AND_UPDATE ""
#define PPC64_TOC_RESTORE ""
#define PPC64_TOC ""
#endif
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtlr 0\n"
#define PPC_FTRACE_RET \
" blr\n"
#define PPC_FTRACE_RECOVER_IP \
" lwz 8, 4(3)\n" \
" li 9, 6\n" \
" slw 8, 8, 9\n" \
" sraw 8, 8, 9\n" \
" add 3, 3, 8\n" \
" addi 3, 3, 4\n"
#else
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtctr 0\n"
#define PPC_FTRACE_RET \
" mtlr 0\n" \
" bctr\n"
#define PPC_FTRACE_RECOVER_IP ""
#endif
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp1, @function\n"
" .globl my_tramp1\n"
" my_tramp1:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mr 3, 0\n"
PPC_FTRACE_RECOVER_IP
" bl my_direct_func1\n"
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
" .size my_tramp1, .-my_tramp1\n"
" .type my_tramp2, @function\n"
" .globl my_tramp2\n"
" my_tramp2:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mr 3, 0\n"
PPC_FTRACE_RECOVER_IP
" bl my_direct_func2\n"
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
PPC64_TOC
" .size my_tramp2, .-my_tramp2\n"
" .popsection\n"
);
#endif /* CONFIG_PPC */
static unsigned long my_tramp = (unsigned long)my_tramp1;
static unsigned long tramps[2] = {
(unsigned long)my_tramp1,
(unsigned long)my_tramp2,
};
static struct ftrace_ops direct;
static int simple_thread(void *arg)
{
static int t;
int ret = 0;
while (!kthread_should_stop()) {
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(2 * HZ);
if (ret)
continue;
t ^= 1;
ret = modify_ftrace_direct(&direct, tramps[t]);
if (!ret)
my_tramp = tramps[t];
WARN_ON_ONCE(ret);
}
return 0;
}
static struct task_struct *simple_tsk;
static int __init ftrace_direct_multi_init(void)
{
int ret;
ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
ret = register_ftrace_direct(&direct, my_tramp);
if (!ret)
simple_tsk = kthread_run(simple_thread, NULL, "event-sample-fn");
return ret;
}
static void __exit ftrace_direct_multi_exit(void)
{
kthread_stop(simple_tsk);
unregister_ftrace_direct(&direct, my_tramp, true);
}
module_init(ftrace_direct_multi_init);
module_exit(ftrace_direct_multi_exit);
MODULE_AUTHOR("Jiri Olsa");
MODULE_DESCRIPTION("Example use case of using modify_ftrace_direct()");
MODULE_LICENSE("GPL");
@@ -0,0 +1,241 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/module.h>
#include <linux/mm.h> /* for handle_mm_fault() */
#include <linux/ftrace.h>
#include <linux/sched/stat.h>
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
#include <asm/asm-offsets.h>
#endif
extern void my_direct_func(unsigned long ip);
void my_direct_func(unsigned long ip)
{
trace_printk("ip %lx\n", ip);
}
extern void my_tramp(void *);
#ifdef CONFIG_RISCV
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
" addi sp,sp,-3*"SZREG"\n"
" "REG_S" a0,0*"SZREG"(sp)\n"
" "REG_S" t0,1*"SZREG"(sp)\n"
" "REG_S" ra,2*"SZREG"(sp)\n"
" mv a0,t0\n"
" call my_direct_func\n"
" "REG_L" a0,0*"SZREG"(sp)\n"
" "REG_L" t0,1*"SZREG"(sp)\n"
" "REG_L" ra,2*"SZREG"(sp)\n"
" addi sp,sp,3*"SZREG"\n"
" jr t0\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_RISCV */
#ifdef CONFIG_X86_64
#include <asm/ibt.h>
#include <asm/nospec-branch.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" pushq %rdi\n"
" movq 8(%rbp), %rdi\n"
" call my_direct_func\n"
" popq %rdi\n"
" leave\n"
ASM_RET
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_X86_64 */
#ifdef CONFIG_S390
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" lgr %r2,%r0\n"
" brasl %r14,my_direct_func\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_S390 */
#ifdef CONFIG_ARM64
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
" hint 34\n" // bti c
" sub sp, sp, #32\n"
" stp x9, x30, [sp]\n"
" str x0, [sp, #16]\n"
" mov x0, x30\n"
" bl my_direct_func\n"
" ldp x30, x9, [sp]\n"
" ldr x0, [sp, #16]\n"
" add sp, sp, #32\n"
" ret x9\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_ARM64 */
#ifdef CONFIG_LOONGARCH
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
" addi.d $sp, $sp, -32\n"
" st.d $a0, $sp, 0\n"
" st.d $t0, $sp, 8\n"
" st.d $ra, $sp, 16\n"
" move $a0, $t0\n"
" bl my_direct_func\n"
" ld.d $a0, $sp, 0\n"
" ld.d $ra, $sp, 8\n"
" ld.d $t0, $sp, 16\n"
" addi.d $sp, $sp, 32\n"
" jr $t0\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_LOONGARCH */
#ifdef CONFIG_PPC
#include <asm/ppc_asm.h>
#ifdef CONFIG_PPC64
#define STACK_FRAME_SIZE 48
#else
#define STACK_FRAME_SIZE 24
#endif
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
#define PPC64_TOC_SAVE_AND_UPDATE \
" std 2, 24(1)\n" \
" bcl 20, 31, 1f\n" \
" 1: mflr 12\n" \
" ld 2, (99f - 1b)(12)\n"
#define PPC64_TOC_RESTORE \
" ld 2, 24(1)\n"
#define PPC64_TOC \
" 99: .quad .TOC.@tocbase\n"
#else
#define PPC64_TOC_SAVE_AND_UPDATE ""
#define PPC64_TOC_RESTORE ""
#define PPC64_TOC ""
#endif
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtlr 0\n"
#define PPC_FTRACE_RET \
" blr\n"
#define PPC_FTRACE_RECOVER_IP \
" lwz 8, 4(3)\n" \
" li 9, 6\n" \
" slw 8, 8, 9\n" \
" sraw 8, 8, 9\n" \
" add 3, 3, 8\n" \
" addi 3, 3, 4\n"
#else
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtctr 0\n"
#define PPC_FTRACE_RET \
" mtlr 0\n" \
" bctr\n"
#define PPC_FTRACE_RECOVER_IP ""
#endif
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mr 3, 0\n"
PPC_FTRACE_RECOVER_IP
" bl my_direct_func\n"
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
PPC64_TOC
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_PPC */
static struct ftrace_ops direct;
static int __init ftrace_direct_multi_init(void)
{
ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
ftrace_set_filter_ip(&direct, (unsigned long) schedule, 0, 0);
return register_ftrace_direct(&direct, (unsigned long) my_tramp);
}
static void __exit ftrace_direct_multi_exit(void)
{
unregister_ftrace_direct(&direct, (unsigned long) my_tramp, true);
}
module_init(ftrace_direct_multi_init);
module_exit(ftrace_direct_multi_exit);
MODULE_AUTHOR("Jiri Olsa");
MODULE_DESCRIPTION("Example use case of using register_ftrace_direct_multi()");
MODULE_LICENSE("GPL");
@@ -0,0 +1,256 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/module.h>
#include <linux/mm.h> /* for handle_mm_fault() */
#include <linux/ftrace.h>
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
#include <asm/asm-offsets.h>
#endif
extern void my_direct_func(struct vm_area_struct *vma, unsigned long address,
unsigned int flags, struct pt_regs *regs);
void my_direct_func(struct vm_area_struct *vma, unsigned long address,
unsigned int flags, struct pt_regs *regs)
{
trace_printk("handle mm fault vma=%p address=%lx flags=%x regs=%p\n",
vma, address, flags, regs);
}
extern void my_tramp(void *);
#ifdef CONFIG_RISCV
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
" addi sp,sp,-5*"SZREG"\n"
" "REG_S" a0,0*"SZREG"(sp)\n"
" "REG_S" a1,1*"SZREG"(sp)\n"
" "REG_S" a2,2*"SZREG"(sp)\n"
" "REG_S" t0,3*"SZREG"(sp)\n"
" "REG_S" ra,4*"SZREG"(sp)\n"
" call my_direct_func\n"
" "REG_L" a0,0*"SZREG"(sp)\n"
" "REG_L" a1,1*"SZREG"(sp)\n"
" "REG_L" a2,2*"SZREG"(sp)\n"
" "REG_L" t0,3*"SZREG"(sp)\n"
" "REG_L" ra,4*"SZREG"(sp)\n"
" addi sp,sp,5*"SZREG"\n"
" jr t0\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_RISCV */
#ifdef CONFIG_X86_64
#include <asm/ibt.h>
#include <asm/nospec-branch.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" pushq %rdi\n"
" pushq %rsi\n"
" pushq %rdx\n"
" pushq %rcx\n"
" call my_direct_func\n"
" popq %rcx\n"
" popq %rdx\n"
" popq %rsi\n"
" popq %rdi\n"
" leave\n"
ASM_RET
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_X86_64 */
#ifdef CONFIG_S390
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" brasl %r14,my_direct_func\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_S390 */
#ifdef CONFIG_ARM64
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
" hint 34\n" // bti c
" sub sp, sp, #48\n"
" stp x9, x30, [sp]\n"
" stp x0, x1, [sp, #16]\n"
" stp x2, x3, [sp, #32]\n"
" bl my_direct_func\n"
" ldp x30, x9, [sp]\n"
" ldp x0, x1, [sp, #16]\n"
" ldp x2, x3, [sp, #32]\n"
" add sp, sp, #48\n"
" ret x9\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_ARM64 */
#ifdef CONFIG_LOONGARCH
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
" addi.d $sp, $sp, -48\n"
" st.d $a0, $sp, 0\n"
" st.d $a1, $sp, 8\n"
" st.d $a2, $sp, 16\n"
" st.d $t0, $sp, 24\n"
" st.d $ra, $sp, 32\n"
" bl my_direct_func\n"
" ld.d $a0, $sp, 0\n"
" ld.d $a1, $sp, 8\n"
" ld.d $a2, $sp, 16\n"
" ld.d $ra, $sp, 24\n"
" ld.d $t0, $sp, 32\n"
" addi.d $sp, $sp, 48\n"
" jr $t0\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_LOONGARCH */
#ifdef CONFIG_PPC
#include <asm/ppc_asm.h>
#ifdef CONFIG_PPC64
#define STACK_FRAME_SIZE 64
#define STACK_FRAME_ARG1 32
#define STACK_FRAME_ARG2 40
#define STACK_FRAME_ARG3 48
#define STACK_FRAME_ARG4 56
#else
#define STACK_FRAME_SIZE 32
#define STACK_FRAME_ARG1 16
#define STACK_FRAME_ARG2 20
#define STACK_FRAME_ARG3 24
#define STACK_FRAME_ARG4 28
#endif
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
#define PPC64_TOC_SAVE_AND_UPDATE \
" std 2, 24(1)\n" \
" bcl 20, 31, 1f\n" \
" 1: mflr 12\n" \
" ld 2, (99f - 1b)(12)\n"
#define PPC64_TOC_RESTORE \
" ld 2, 24(1)\n"
#define PPC64_TOC \
" 99: .quad .TOC.@tocbase\n"
#else
#define PPC64_TOC_SAVE_AND_UPDATE ""
#define PPC64_TOC_RESTORE ""
#define PPC64_TOC ""
#endif
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtlr 0\n"
#define PPC_FTRACE_RET \
" blr\n"
#else
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtctr 0\n"
#define PPC_FTRACE_RET \
" mtlr 0\n" \
" bctr\n"
#endif
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
PPC_STL" 3, "__stringify(STACK_FRAME_ARG1)"(1)\n"
PPC_STL" 4, "__stringify(STACK_FRAME_ARG2)"(1)\n"
PPC_STL" 5, "__stringify(STACK_FRAME_ARG3)"(1)\n"
PPC_STL" 6, "__stringify(STACK_FRAME_ARG4)"(1)\n"
" bl my_direct_func\n"
PPC_LL" 6, "__stringify(STACK_FRAME_ARG4)"(1)\n"
PPC_LL" 5, "__stringify(STACK_FRAME_ARG3)"(1)\n"
PPC_LL" 4, "__stringify(STACK_FRAME_ARG2)"(1)\n"
PPC_LL" 3, "__stringify(STACK_FRAME_ARG1)"(1)\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
PPC64_TOC
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_PPC */
static struct ftrace_ops direct;
static int __init ftrace_direct_init(void)
{
ftrace_set_filter_ip(&direct, (unsigned long) handle_mm_fault, 0, 0);
return register_ftrace_direct(&direct, (unsigned long) my_tramp);
}
static void __exit ftrace_direct_exit(void)
{
unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
}
module_init(ftrace_direct_init);
module_exit(ftrace_direct_exit);
MODULE_AUTHOR("Steven Rostedt");
MODULE_DESCRIPTION("Another example use case of using register_ftrace_direct()");
MODULE_LICENSE("GPL");
+223
View File
@@ -0,0 +1,223 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/module.h>
#include <linux/sched.h> /* for wake_up_process() */
#include <linux/ftrace.h>
#if !defined(CONFIG_ARM64) && !defined(CONFIG_PPC32)
#include <asm/asm-offsets.h>
#endif
extern void my_direct_func(struct task_struct *p);
void my_direct_func(struct task_struct *p)
{
trace_printk("waking up %s-%d\n", p->comm, p->pid);
}
extern void my_tramp(void *);
#ifdef CONFIG_RISCV
#include <asm/asm.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
" addi sp,sp,-3*"SZREG"\n"
" "REG_S" a0,0*"SZREG"(sp)\n"
" "REG_S" t0,1*"SZREG"(sp)\n"
" "REG_S" ra,2*"SZREG"(sp)\n"
" call my_direct_func\n"
" "REG_L" a0,0*"SZREG"(sp)\n"
" "REG_L" t0,1*"SZREG"(sp)\n"
" "REG_L" ra,2*"SZREG"(sp)\n"
" addi sp,sp,3*"SZREG"\n"
" jr t0\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_RISCV */
#ifdef CONFIG_X86_64
#include <asm/ibt.h>
#include <asm/nospec-branch.h>
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
ASM_ENDBR
" pushq %rbp\n"
" movq %rsp, %rbp\n"
CALL_DEPTH_ACCOUNT
" pushq %rdi\n"
" call my_direct_func\n"
" popq %rdi\n"
" leave\n"
ASM_RET
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_X86_64 */
#ifdef CONFIG_S390
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
" lgr %r1,%r15\n"
" stmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" stg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" aghi %r15,"__stringify(-STACK_FRAME_OVERHEAD)"\n"
" stg %r1,"__stringify(__SF_BACKCHAIN)"(%r15)\n"
" brasl %r14,my_direct_func\n"
" aghi %r15,"__stringify(STACK_FRAME_OVERHEAD)"\n"
" lmg %r0,%r5,"__stringify(__SF_GPRS)"(%r15)\n"
" lg %r14,"__stringify(__SF_GPRS+8*8)"(%r15)\n"
" lgr %r1,%r0\n"
" br %r1\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_S390 */
#ifdef CONFIG_ARM64
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:"
" hint 34\n" // bti c
" sub sp, sp, #32\n"
" stp x9, x30, [sp]\n"
" str x0, [sp, #16]\n"
" bl my_direct_func\n"
" ldp x30, x9, [sp]\n"
" ldr x0, [sp, #16]\n"
" add sp, sp, #32\n"
" ret x9\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_ARM64 */
#ifdef CONFIG_LOONGARCH
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
" addi.d $sp, $sp, -32\n"
" st.d $a0, $sp, 0\n"
" st.d $t0, $sp, 8\n"
" st.d $ra, $sp, 16\n"
" bl my_direct_func\n"
" ld.d $a0, $sp, 0\n"
" ld.d $ra, $sp, 8\n"
" ld.d $t0, $sp, 16\n"
" addi.d $sp, $sp, 32\n"
" jr $t0\n"
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_LOONGARCH */
#ifdef CONFIG_PPC
#include <asm/ppc_asm.h>
#ifdef CONFIG_PPC64
#define STACK_FRAME_SIZE 48
#else
#define STACK_FRAME_SIZE 24
#endif
#if defined(CONFIG_PPC64_ELF_ABI_V2) && !defined(CONFIG_PPC_KERNEL_PCREL)
#define PPC64_TOC_SAVE_AND_UPDATE \
" std 2, 24(1)\n" \
" bcl 20, 31, 1f\n" \
" 1: mflr 12\n" \
" ld 2, (99f - 1b)(12)\n"
#define PPC64_TOC_RESTORE \
" ld 2, 24(1)\n"
#define PPC64_TOC \
" 99: .quad .TOC.@tocbase\n"
#else
#define PPC64_TOC_SAVE_AND_UPDATE ""
#define PPC64_TOC_RESTORE ""
#define PPC64_TOC ""
#endif
#ifdef CONFIG_PPC_FTRACE_OUT_OF_LINE
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtlr 0\n"
#define PPC_FTRACE_RET \
" blr\n"
#else
#define PPC_FTRACE_RESTORE_LR \
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n" \
" mtctr 0\n"
#define PPC_FTRACE_RET \
" mtlr 0\n" \
" bctr\n"
#endif
asm (
" .pushsection .text, \"ax\", @progbits\n"
" .type my_tramp, @function\n"
" .globl my_tramp\n"
" my_tramp:\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" mflr 0\n"
PPC_STL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_STLU" 1, -"__stringify(STACK_FRAME_SIZE)"(1)\n"
PPC64_TOC_SAVE_AND_UPDATE
PPC_STL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
" bl my_direct_func\n"
PPC_LL" 3, "__stringify(STACK_FRAME_MIN_SIZE)"(1)\n"
PPC64_TOC_RESTORE
" addi 1, 1, "__stringify(STACK_FRAME_SIZE)"\n"
PPC_FTRACE_RESTORE_LR
" addi 1, 1, "__stringify(STACK_FRAME_MIN_SIZE)"\n"
PPC_LL" 0, "__stringify(PPC_LR_STKOFF)"(1)\n"
PPC_FTRACE_RET
PPC64_TOC
" .size my_tramp, .-my_tramp\n"
" .popsection\n"
);
#endif /* CONFIG_PPC */
static struct ftrace_ops direct;
static int __init ftrace_direct_init(void)
{
ftrace_set_filter_ip(&direct, (unsigned long) wake_up_process, 0, 0);
return register_ftrace_direct(&direct, (unsigned long) my_tramp);
}
static void __exit ftrace_direct_exit(void)
{
unregister_ftrace_direct(&direct, (unsigned long)my_tramp, true);
}
module_init(ftrace_direct_init);
module_exit(ftrace_direct_exit);
MODULE_AUTHOR("Steven Rostedt");
MODULE_DESCRIPTION("Example use case of using register_ftrace_direct()");
MODULE_LICENSE("GPL");
+252
View File
@@ -0,0 +1,252 @@
// SPDX-License-Identifier: GPL-2.0-only
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#include <linux/ftrace.h>
#include <linux/ktime.h>
#include <linux/module.h>
#include <asm/barrier.h>
/*
* Arbitrary large value chosen to be sufficiently large to minimize noise but
* sufficiently small to complete quickly.
*/
static unsigned int nr_function_calls = 100000;
module_param(nr_function_calls, uint, 0);
MODULE_PARM_DESC(nr_function_calls, "How many times to call the relevant tracee");
/*
* The number of ops associated with a call site affects whether a tracer can
* be called directly or whether it's necessary to go via the list func, which
* can be significantly more expensive.
*/
static unsigned int nr_ops_relevant = 1;
module_param(nr_ops_relevant, uint, 0);
MODULE_PARM_DESC(nr_ops_relevant, "How many ftrace_ops to associate with the relevant tracee");
/*
* On architectures where all call sites share the same trampoline, having
* tracers enabled for distinct functions can force the use of the list func
* and incur overhead for all call sites.
*/
static unsigned int nr_ops_irrelevant;
module_param(nr_ops_irrelevant, uint, 0);
MODULE_PARM_DESC(nr_ops_irrelevant, "How many ftrace_ops to associate with the irrelevant tracee");
/*
* On architectures with DYNAMIC_FTRACE_WITH_REGS, saving the full pt_regs can
* be more expensive than only saving the minimal necessary regs.
*/
static bool save_regs;
module_param(save_regs, bool, 0);
MODULE_PARM_DESC(save_regs, "Register ops with FTRACE_OPS_FL_SAVE_REGS (save all registers in the trampoline)");
static bool assist_recursion;
module_param(assist_recursion, bool, 0);
MODULE_PARM_DESC(assist_reursion, "Register ops with FTRACE_OPS_FL_RECURSION");
static bool assist_rcu;
module_param(assist_rcu, bool, 0);
MODULE_PARM_DESC(assist_reursion, "Register ops with FTRACE_OPS_FL_RCU");
/*
* By default, a trivial tracer is used which immediately returns to mimimize
* overhead. Sometimes a consistency check using a more expensive tracer is
* desireable.
*/
static bool check_count;
module_param(check_count, bool, 0);
MODULE_PARM_DESC(check_count, "Check that tracers are called the expected number of times\n");
/*
* Usually it's not interesting to leave the ops registered after the test
* runs, but sometimes it can be useful to leave them registered so that they
* can be inspected through the tracefs 'enabled_functions' file.
*/
static bool persist;
module_param(persist, bool, 0);
MODULE_PARM_DESC(persist, "Successfully load module and leave ftrace ops registered after test completes\n");
/*
* Marked as noinline to ensure that an out-of-line traceable copy is
* generated by the compiler.
*
* The barrier() ensures the compiler won't elide calls by determining there
* are no side-effects.
*/
static noinline void tracee_relevant(void)
{
barrier();
}
/*
* Marked as noinline to ensure that an out-of-line traceable copy is
* generated by the compiler.
*
* The barrier() ensures the compiler won't elide calls by determining there
* are no side-effects.
*/
static noinline void tracee_irrelevant(void)
{
barrier();
}
struct sample_ops {
struct ftrace_ops ops;
unsigned int count;
};
static void ops_func_nop(unsigned long ip, unsigned long parent_ip,
struct ftrace_ops *op,
struct ftrace_regs *fregs)
{
/* do nothing */
}
static void ops_func_count(unsigned long ip, unsigned long parent_ip,
struct ftrace_ops *op,
struct ftrace_regs *fregs)
{
struct sample_ops *self;
self = container_of(op, struct sample_ops, ops);
self->count++;
}
static struct sample_ops *ops_relevant;
static struct sample_ops *ops_irrelevant;
static struct sample_ops *ops_alloc_init(void *tracee, ftrace_func_t func,
unsigned long flags, int nr)
{
struct sample_ops *ops;
ops = kcalloc(nr, sizeof(*ops), GFP_KERNEL);
if (WARN_ON_ONCE(!ops))
return NULL;
for (unsigned int i = 0; i < nr; i++) {
ops[i].ops.func = func;
ops[i].ops.flags = flags;
WARN_ON_ONCE(ftrace_set_filter_ip(&ops[i].ops, (unsigned long)tracee, 0, 0));
WARN_ON_ONCE(register_ftrace_function(&ops[i].ops));
}
return ops;
}
static void ops_destroy(struct sample_ops *ops, int nr)
{
if (!ops)
return;
for (unsigned int i = 0; i < nr; i++) {
WARN_ON_ONCE(unregister_ftrace_function(&ops[i].ops));
ftrace_free_filter(&ops[i].ops);
}
kfree(ops);
}
static void ops_check(struct sample_ops *ops, int nr,
unsigned int expected_count)
{
if (!ops || !check_count)
return;
for (unsigned int i = 0; i < nr; i++) {
if (ops->count == expected_count)
continue;
pr_warn("Counter called %u times (expected %u)\n",
ops->count, expected_count);
}
}
static ftrace_func_t tracer_relevant = ops_func_nop;
static ftrace_func_t tracer_irrelevant = ops_func_nop;
static int __init ftrace_ops_sample_init(void)
{
unsigned long flags = 0;
ktime_t start, end;
u64 period;
if (!IS_ENABLED(CONFIG_DYNAMIC_FTRACE_WITH_REGS) && save_regs) {
pr_info("this kernel does not support saving registers\n");
save_regs = false;
} else if (save_regs) {
flags |= FTRACE_OPS_FL_SAVE_REGS;
}
if (assist_recursion)
flags |= FTRACE_OPS_FL_RECURSION;
if (assist_rcu)
flags |= FTRACE_OPS_FL_RCU;
if (check_count) {
tracer_relevant = ops_func_count;
tracer_irrelevant = ops_func_count;
}
pr_info("registering:\n"
" relevant ops: %u\n"
" tracee: %ps\n"
" tracer: %ps\n"
" irrelevant ops: %u\n"
" tracee: %ps\n"
" tracer: %ps\n"
" saving registers: %s\n"
" assist recursion: %s\n"
" assist RCU: %s\n",
nr_ops_relevant, tracee_relevant, tracer_relevant,
nr_ops_irrelevant, tracee_irrelevant, tracer_irrelevant,
save_regs ? "YES" : "NO",
assist_recursion ? "YES" : "NO",
assist_rcu ? "YES" : "NO");
ops_relevant = ops_alloc_init(tracee_relevant, tracer_relevant,
flags, nr_ops_relevant);
ops_irrelevant = ops_alloc_init(tracee_irrelevant, tracer_irrelevant,
flags, nr_ops_irrelevant);
start = ktime_get();
for (unsigned int i = 0; i < nr_function_calls; i++)
tracee_relevant();
end = ktime_get();
ops_check(ops_relevant, nr_ops_relevant, nr_function_calls);
ops_check(ops_irrelevant, nr_ops_irrelevant, 0);
period = ktime_to_ns(ktime_sub(end, start));
pr_info("Attempted %u calls to %ps in %lluns (%lluns / call)\n",
nr_function_calls, tracee_relevant,
period, div_u64(period, nr_function_calls));
if (persist)
return 0;
ops_destroy(ops_relevant, nr_ops_relevant);
ops_destroy(ops_irrelevant, nr_ops_irrelevant);
/*
* The benchmark completed successfully, but there's no reason to keep
* the module around. Return an error so the user doesn't have to
* manually unload the module.
*/
return -EINVAL;
}
module_init(ftrace_ops_sample_init);
static void __exit ftrace_ops_sample_exit(void)
{
ops_destroy(ops_relevant, nr_ops_relevant);
ops_destroy(ops_irrelevant, nr_ops_irrelevant);
}
module_exit(ftrace_ops_sample_exit);
MODULE_AUTHOR("Mark Rutland");
MODULE_DESCRIPTION("Example of using custom ftrace_ops");
MODULE_LICENSE("GPL");
@@ -0,0 +1,143 @@
// SPDX-License-Identifier: GPL-2.0-only
#include <linux/module.h>
#include <linux/kthread.h>
#include <linux/trace.h>
#include <linux/trace_events.h>
#include <linux/timer.h>
#include <linux/err.h>
#include <linux/jiffies.h>
#include <linux/workqueue.h>
/*
* Any file that uses trace points, must include the header.
* But only one file, must include the header by defining
* CREATE_TRACE_POINTS first. This will make the C code that
* creates the handles for the trace points.
*/
#define CREATE_TRACE_POINTS
#include "sample-trace-array.h"
struct trace_array *tr;
static void mytimer_handler(struct timer_list *unused);
static struct task_struct *simple_tsk;
static void trace_work_fn(struct work_struct *work)
{
/*
* Disable tracing for event "sample_event".
*/
trace_array_set_clr_event(tr, "sample-subsystem", "sample_event",
false);
}
static DECLARE_WORK(trace_work, trace_work_fn);
/*
* mytimer: Timer setup to disable tracing for event "sample_event". This
* timer is only for the purposes of the sample module to demonstrate access of
* Ftrace instances from within kernel.
*/
static DEFINE_TIMER(mytimer, mytimer_handler);
static void mytimer_handler(struct timer_list *unused)
{
schedule_work(&trace_work);
}
static void simple_thread_func(int count)
{
set_current_state(TASK_INTERRUPTIBLE);
schedule_timeout(HZ);
/*
* Printing count value using trace_array_printk() - trace_printk()
* equivalent for the instance buffers.
*/
trace_array_printk(tr, _THIS_IP_, "trace_array_printk: count=%d\n",
count);
/*
* Tracepoint for event "sample_event". This will print the
* current value of count and current jiffies.
*/
trace_sample_event(count, jiffies);
}
static int simple_thread(void *arg)
{
int count = 0;
unsigned long delay = msecs_to_jiffies(5000);
/*
* Enable tracing for "sample_event".
*/
trace_array_set_clr_event(tr, "sample-subsystem", "sample_event", true);
/*
* Adding timer - mytimer. This timer will disable tracing after
* delay seconds.
*
*/
add_timer(&mytimer);
mod_timer(&mytimer, jiffies+delay);
while (!kthread_should_stop())
simple_thread_func(count++);
timer_delete(&mytimer);
cancel_work_sync(&trace_work);
/*
* trace_array_put() decrements the reference counter associated with
* the trace array - "tr". We are done using the trace array, hence
* decrement the reference counter so that it can be destroyed using
* trace_array_destroy().
*/
trace_array_put(tr);
return 0;
}
static int __init sample_trace_array_init(void)
{
/*
* Return a pointer to the trace array with name "sample-instance" if it
* exists, else create a new trace array.
*
* NOTE: This function increments the reference counter
* associated with the trace array - "tr".
*/
tr = trace_array_get_by_name("sample-instance", "sched,timer,kprobes");
if (!tr)
return -1;
/*
* If context specific per-cpu buffers havent already been allocated.
*/
trace_array_init_printk(tr);
simple_tsk = kthread_run(simple_thread, NULL, "sample-instance");
if (IS_ERR(simple_tsk)) {
trace_array_put(tr);
trace_array_destroy(tr);
return -1;
}
return 0;
}
static void __exit sample_trace_array_exit(void)
{
kthread_stop(simple_tsk);
/*
* We are unloading our module and no longer require the trace array.
* Remove/destroy "tr" using trace_array_destroy()
*/
trace_array_destroy(tr);
}
module_init(sample_trace_array_init);
module_exit(sample_trace_array_exit);
MODULE_AUTHOR("Divya Indi");
MODULE_DESCRIPTION("Sample module for kernel access to Ftrace instances");
MODULE_LICENSE("GPL");
@@ -0,0 +1,84 @@
/* SPDX-License-Identifier: GPL-2.0 */
/*
* If TRACE_SYSTEM is defined, that will be the directory created
* in the ftrace directory under /sys/kernel/tracing/events/<system>
*
* The define_trace.h below will also look for a file name of
* TRACE_SYSTEM.h where TRACE_SYSTEM is what is defined here.
* In this case, it would look for sample-trace.h
*
* If the header name will be different than the system name
* (as in this case), then you can override the header name that
* define_trace.h will look up by defining TRACE_INCLUDE_FILE
*
* This file is called sample-trace-array.h but we want the system
* to be called "sample-subsystem". Therefore we must define the name of this
* file:
*
* #define TRACE_INCLUDE_FILE sample-trace-array
*
* As we do in the bottom of this file.
*
* Notice that TRACE_SYSTEM should be defined outside of #if
* protection, just like TRACE_INCLUDE_FILE.
*/
#undef TRACE_SYSTEM
#define TRACE_SYSTEM sample-subsystem
/*
* TRACE_SYSTEM is expected to be a C valid variable (alpha-numeric
* and underscore), although it may start with numbers. If for some
* reason it is not, you need to add the following lines:
*/
#undef TRACE_SYSTEM_VAR
#define TRACE_SYSTEM_VAR sample_subsystem
/*
* But the above is only needed if TRACE_SYSTEM is not alpha-numeric
* and underscored. By default, TRACE_SYSTEM_VAR will be equal to
* TRACE_SYSTEM. As TRACE_SYSTEM_VAR must be alpha-numeric, if
* TRACE_SYSTEM is not, then TRACE_SYSTEM_VAR must be defined with
* only alpha-numeric and underscores.
*
* The TRACE_SYSTEM_VAR is only used internally and not visible to
* user space.
*/
/*
* Notice that this file is not protected like a normal header.
* We also must allow for rereading of this file. The
*
* || defined(TRACE_HEADER_MULTI_READ)
*
* serves this purpose.
*/
#if !defined(_SAMPLE_TRACE_ARRAY_H) || defined(TRACE_HEADER_MULTI_READ)
#define _SAMPLE_TRACE_ARRAY_H
#include <linux/tracepoint.h>
TRACE_EVENT(sample_event,
TP_PROTO(int count, unsigned long time),
TP_ARGS(count, time),
TP_STRUCT__entry(
__field(int, count)
__field(unsigned long, time)
),
TP_fast_assign(
__entry->count = count;
__entry->time = time;
),
TP_printk("count value=%d at jiffies=%lu", __entry->count,
__entry->time)
);
#endif
#undef TRACE_INCLUDE_PATH
#define TRACE_INCLUDE_PATH .
#define TRACE_INCLUDE_FILE sample-trace-array
#include <trace/define_trace.h>