kprobe_multi.c (3064B)
1// SPDX-License-Identifier: GPL-2.0 2#include <linux/bpf.h> 3#include <bpf/bpf_helpers.h> 4#include <bpf/bpf_tracing.h> 5#include <stdbool.h> 6 7char _license[] SEC("license") = "GPL"; 8 9extern const void bpf_fentry_test1 __ksym; 10extern const void bpf_fentry_test2 __ksym; 11extern const void bpf_fentry_test3 __ksym; 12extern const void bpf_fentry_test4 __ksym; 13extern const void bpf_fentry_test5 __ksym; 14extern const void bpf_fentry_test6 __ksym; 15extern const void bpf_fentry_test7 __ksym; 16extern const void bpf_fentry_test8 __ksym; 17 18int pid = 0; 19bool test_cookie = false; 20 21__u64 kprobe_test1_result = 0; 22__u64 kprobe_test2_result = 0; 23__u64 kprobe_test3_result = 0; 24__u64 kprobe_test4_result = 0; 25__u64 kprobe_test5_result = 0; 26__u64 kprobe_test6_result = 0; 27__u64 kprobe_test7_result = 0; 28__u64 kprobe_test8_result = 0; 29 30__u64 kretprobe_test1_result = 0; 31__u64 kretprobe_test2_result = 0; 32__u64 kretprobe_test3_result = 0; 33__u64 kretprobe_test4_result = 0; 34__u64 kretprobe_test5_result = 0; 35__u64 kretprobe_test6_result = 0; 36__u64 kretprobe_test7_result = 0; 37__u64 kretprobe_test8_result = 0; 38 39extern bool CONFIG_X86_KERNEL_IBT __kconfig __weak; 40 41static void kprobe_multi_check(void *ctx, bool is_return) 42{ 43 if (bpf_get_current_pid_tgid() >> 32 != pid) 44 return; 45 46 __u64 cookie = test_cookie ? bpf_get_attach_cookie(ctx) : 0; 47 __u64 addr = bpf_get_func_ip(ctx) - (CONFIG_X86_KERNEL_IBT ? 4 : 0); 48 49#define SET(__var, __addr, __cookie) ({ \ 50 if (((const void *) addr == __addr) && \ 51 (!test_cookie || (cookie == __cookie))) \ 52 __var = 1; \ 53}) 54 55 if (is_return) { 56 SET(kretprobe_test1_result, &bpf_fentry_test1, 8); 57 SET(kretprobe_test2_result, &bpf_fentry_test2, 2); 58 SET(kretprobe_test3_result, &bpf_fentry_test3, 7); 59 SET(kretprobe_test4_result, &bpf_fentry_test4, 6); 60 SET(kretprobe_test5_result, &bpf_fentry_test5, 5); 61 SET(kretprobe_test6_result, &bpf_fentry_test6, 4); 62 SET(kretprobe_test7_result, &bpf_fentry_test7, 3); 63 SET(kretprobe_test8_result, &bpf_fentry_test8, 1); 64 } else { 65 SET(kprobe_test1_result, &bpf_fentry_test1, 1); 66 SET(kprobe_test2_result, &bpf_fentry_test2, 7); 67 SET(kprobe_test3_result, &bpf_fentry_test3, 2); 68 SET(kprobe_test4_result, &bpf_fentry_test4, 3); 69 SET(kprobe_test5_result, &bpf_fentry_test5, 4); 70 SET(kprobe_test6_result, &bpf_fentry_test6, 5); 71 SET(kprobe_test7_result, &bpf_fentry_test7, 6); 72 SET(kprobe_test8_result, &bpf_fentry_test8, 8); 73 } 74 75#undef SET 76} 77 78/* 79 * No tests in here, just to trigger 'bpf_fentry_test*' 80 * through tracing test_run 81 */ 82SEC("fentry/bpf_modify_return_test") 83int BPF_PROG(trigger) 84{ 85 return 0; 86} 87 88SEC("kprobe.multi/bpf_fentry_tes??") 89int test_kprobe(struct pt_regs *ctx) 90{ 91 kprobe_multi_check(ctx, false); 92 return 0; 93} 94 95SEC("kretprobe.multi/bpf_fentry_test*") 96int test_kretprobe(struct pt_regs *ctx) 97{ 98 kprobe_multi_check(ctx, true); 99 return 0; 100} 101 102SEC("kprobe.multi") 103int test_kprobe_manual(struct pt_regs *ctx) 104{ 105 kprobe_multi_check(ctx, false); 106 return 0; 107} 108 109SEC("kretprobe.multi") 110int test_kretprobe_manual(struct pt_regs *ctx) 111{ 112 kprobe_multi_check(ctx, true); 113 return 0; 114}