Slide 1

Slide 1 text

©2019 VMware, Inc. ftrace Where modifying a running kernel all started! Steven Rostedt Open Source Engineer [email protected] / [email protected]

Slide 2

Slide 2 text

2 ©2019 VMware, Inc. Ftrace Function hooks ● Allows attaching to a function in the kernel – Function Tracer – Function Graph Tracer – Perf – Stack Tracer – Kprobes – SystemTap – Pstore

Slide 3

Slide 3 text

3 ©2019 VMware, Inc. Function Tracing # cd /sys/kernel/tracing # echo function > current_tracer # cat trace # tracer: function # # entries-in-buffer/entries-written: 159693/4101675 #P:4 # # _-----=> irqs-off # / _----=> need-resched # | / _----=> need-resched # || / _---=> hardirq/softirq # ||| / _--=> preempt-depth # ||||/ delay # TASK-PID CPU# ||||| TIMESTAMP FUNCTION # | | | ||||| | | cat-3432 [002] d...... 60071.538270: __rcu_read_unlock <-__is_insn_slot_addr cat-3432 [002] d...... 60071.538270: is_bpf_text_address <-kernel_text_address cat-3432 [002] d...... 60071.538270: __rcu_read_lock <-is_bpf_text_address cat-3432 [002] d...... 60071.538271: bpf_prog_kallsyms_find <-is_bpf_text_address cat-3432 [002] d...... 60071.538271: __rcu_read_unlock <-is_bpf_text_address cat-3432 [002] d...... 60071.538271: init_object <-alloc_debug_processing cat-3432 [002] d...... 60071.538271: deactivate_slab.isra.74 <-___slab_alloc cat-3432 [002] d...... 60071.538272: preempt_count_add <-deactivate_slab.isra.74 cat-3432 [002] d...1.. 60071.538272: preempt_count_sub <-deactivate_slab.isra.74 cat-3432 [002] d...... 60071.538272: preempt_count_add <-deactivate_slab.isra.74 cat-3432 [002] d...1.. 60071.538272: preempt_count_sub <-deactivate_slab.isra.74 cat-3432 [002] d...... 60071.538273: preempt_count_add <-deactivate_slab.isra.74 cat-3432 [002] d...1.. 60071.538273: preempt_count_sub <-deactivate_slab.isra.74 cat-3432 [002] d...... 60071.538273: _raw_spin_lock <-deactivate_slab.isra.74 cat-3432 [002] d...... 60071.538273: preempt_count_add <-_raw_spin_lock cat-3432 [002] d...1.. 60071.538273: do_raw_spin_trylock <-_raw_spin_lock

Slide 4

Slide 4 text

4 ©2019 VMware, Inc. Function Graph Tracing # cd /sys/kernel/tracing # echo function_graph > current_tracer # cat trace # tracer: function_graph # # CPU DURATION FUNCTION CALLS # | | | | | | | 3) 0.868 us | } /* rt_spin_lock_slowlock_locked */ 3) | _raw_spin_unlock_irqrestore() { 3) 0.294 us | do_raw_spin_unlock(); 3) 0.374 us | preempt_count_sub(); 3) 1.542 us | } 3) 0.198 us | put_pid(); 3) 5.727 us | } /* rt_spin_lock_slowlock */ 3) + 18.867 us | } /* rt_spin_lock */ 3) | rt_spin_unlock() { 3) | rt_mutex_futex_unlock() { 3) | _raw_spin_lock_irqsave() { 3) 0.224 us | preempt_count_add(); 3) 0.376 us | do_raw_spin_trylock(); 3) 1.767 us | } 3) 0.264 us | __rt_mutex_unlock_common(); 3) | _raw_spin_unlock_irqrestore() { 3) 0.278 us | do_raw_spin_unlock(); 3) 0.249 us | preempt_count_sub(); 3) 1.421 us | } 3) 4.565 us | } 3) | migrate_enable() { 3) 0.275 us | preempt_count_add();

Slide 5

Slide 5 text

5 ©2019 VMware, Inc. Dynamic Function Tracing # cd /sys/kernel/tracing # echo ‘*sched*’ > set_ftrace_filter # echo function > current_tracer # cat trace # tracer: function # # entries-in-buffer/entries-written: 35104/35104 #P:4 # # _-----=> irqs-off # / _----=> need-resched # | / _----=> need-resched # || / _---=> hardirq/softirq # ||| / _--=> preempt-depth # ||||/ delay # TASK-PID CPU# ||||| TIMESTAMP FUNCTION # | | | ||||| | | bash-1294 [000] d..h... 60276.948739: tick_sched_timer <-__hrtimer_run_queues bash-1294 [000] d..h... 60276.948741: tick_sched_do_timer <-tick_sched_timer bash-1294 [000] d..h... 60276.948743: tick_sched_handle <-tick_sched_timer bash-1294 [000] d..h... 60276.948745: rcu_sched_clock_irq <-update_process_times bash-1294 [000] d..h... 60276.948745: scheduler_tick <-update_process_times bash-1294 [000] d...2.. 60276.948754: resched_curr_lazy <-check_preempt_wakeup bash-1294 [000] d.L.... 60276.948756: preempt_schedule_irq <- restore_regs_and_return_to_kernel ksoftirqd/0-9 [000] ....... 60276.948769: schedule <-smpboot_thread_fn bash-1294 [000] d...311 60276.948908: resched_curr <-check_preempt_curr bash-1294 [000] d...311 60276.948908: native_smp_send_reschedule <-check_preempt_curr -0 [003] dn..1.. 60276.948922: smp_reschedule_interrupt <-reschedule_interrupt -0 [003] dn..1.. 60276.948923: scheduler_ipi <-reschedule_interrupt

Slide 6

Slide 6 text

6 ©2019 VMware, Inc. How does it work? ● gcc’s profiler option: -pg – Adds a special “mcount” call to all non-inlined functions – mcount is a trampoline to jump to C code – All non-inlined functions call mcount near the beginning (after frame setup) – Requires frame pointers

Slide 7

Slide 7 text

7 ©2019 VMware, Inc. How does it work? ● gcc’s profiler option: -pg – Adds a special “mcount” call to all non-inlined functions – mcount is a trampoline to jump to C code – All non-inlined functions call mcount near the beginning (after frame setup) – Requires frame pointers ● x86 now only uses: -pg -mfentry – Adds a special “__fentry__” call to all non-inlined functions – __fentry__ is also a trampoline to jump to C code – All non-inlined function call __fentry__ at the beginning of the function – No need to have frame pointers

Slide 8

Slide 8 text

8 ©2019 VMware, Inc. A Function Call asmlinkage __visible void __sched schedule(void) { struct task_struct *tsk = current; sched_submit_work(tsk); do { preempt_disable(); __schedule(false); sched_preempt_enable_no_resched(); } while (need_resched()); sched_update_worker(tsk); }

Slide 9

Slide 9 text

9 ©2019 VMware, Inc. WARNING! The following slides may not be suitable for some audiences

Slide 10

Slide 10 text

10 ©2019 VMware, Inc. WARNING! The next slide contains ASSEMBLY!

Slide 11

Slide 11 text

11 ©2019 VMware, Inc. Disassembled Function Call : 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff819dbce6: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff819dbd03 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff819dbd42 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff819dbd22 31 ff xor %edi,%edi e8 a6 f8 ff ff callq ffffffff819db5b0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 12

Slide 12 text

12 ©2019 VMware, Inc. Disassembled Function Call : e8 1b d0 1e 00 callq ffffffff81c01930 <__fentry__> ffffffff81a14911: R_X86_64_PLT32 __fentry__-0x4 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00 With -pg -mfentry options

Slide 13

Slide 13 text

13 ©2019 VMware, Inc. At Kernel Boot Up : callq <__fentry__> push %rbx <__fentry__>: retq

Slide 14

Slide 14 text

14 ©2019 VMware, Inc. Where are all the __fentry__ callers? Can’t just leave them there  Too much overhead  Just calling and doing a return adds 13% overhead! Need to convert them to nops at boot up Need to know where they are Best to find them at compile time!

Slide 15

Slide 15 text

15 ©2019 VMware, Inc. recordmcount scripts/recordmcount.c (and there’s a perl version too!) Reads the object files one at a time Reads the relocation tables  Finds all the calls to __fentry__  Creates a table (array)  Links them back into the object file  New section called __mcount_loc – Even for __fentry__ locations

Slide 16

Slide 16 text

16 ©2019 VMware, Inc. recordmcount scripts/recordmcount.c (and there’s a perl version too!) Reads the object files one at a time Reads the relocation tables  Finds all the calls to __fentry__  Creates a table (array)  Links them back into the object file  New section called __mcount_loc – Even for __fentry__ locations  gcc 5 added -mrecord-mcount (to do this for us)

Slide 17

Slide 17 text

17 ©2019 VMware, Inc. recordmcount (kernel/sched/core.o) <__mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle : callq <__fentry__> [..] : callq <__fentry__> [..] : callq <__fentry__> [..] <_cond_resched>: callq <__fentry__> [..] : callq <__fentry__> [..]

Slide 18

Slide 18 text

18 ©2019 VMware, Inc. recordmcount (kernel/sched/core.o) <__mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle : callq <__fentry__> [..] : callq <__fentry__> [..] : callq <__fentry__> [..] <_cond_resched>: callq <__fentry__> [..] : callq <__fentry__> [..] <__mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle

Slide 19

Slide 19 text

19 ©2019 VMware, Inc. Linker Magic! vmlinux.lds  include/asm-generic/vmlinux.lds.h Magic Variables  __start_mcount_loc  __stop_mcount_loc #ifdef CONFIG_FTRACE_MCOUNT_RECORD #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY #define MCOUNT_REC() . = ALIGN(8); \ __start_mcount_loc = .; \ KEEP(*(__patchable_function_entries)) \ __stop_mcount_loc = .; #else #define MCOUNT_REC() . = ALIGN(8); \ __start_mcount_loc = .; \ KEEP(*(__mcount_loc)) \ __stop_mcount_loc = .; #endif #else #define MCOUNT_REC() #endif

Slide 20

Slide 20 text

20 ©2019 VMware, Inc. Linker Magic! vmlinux.lds  include/asm-generic/vmlinux.lds.h Magic Variables  __start_mcount_loc  __stop_mcount_loc parisc architecture #ifdef CONFIG_FTRACE_MCOUNT_RECORD #ifdef CC_USING_PATCHABLE_FUNCTION_ENTRY #define MCOUNT_REC() . = ALIGN(8); \ __start_mcount_loc = .; \ KEEP(*(__patchable_function_entries)) \ __stop_mcount_loc = .; #else #define MCOUNT_REC() . = ALIGN(8); \ __start_mcount_loc = .; \ KEEP(*(__mcount_loc)) \ __stop_mcount_loc = .; #endif #else #define MCOUNT_REC() #endif

Slide 21

Slide 21 text

21 ©2019 VMware, Inc. Linker Magic vmlinux: <__mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle <__mcount_loc>: &__put_page &put_pages_list &__activate_page &activate_page &lru_cache_add <__mcount_loc>: &vfs_llseek &default_llseek &new_sync_read &new_sync_write &__vfs_write kernel/sched/core.o: mm/swap.o: fs/read_write.o:

Slide 22

Slide 22 text

22 ©2019 VMware, Inc. Linker Magic <__start_mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle &__put_page &put_pages_list &__activate_page &activate_page &lru_cache_add &vfs_llseek &default_llseek &new_sync_read &new_sync_write &__vfs_write [...] <__stop_mcount_loc>: vmlinux: <__mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle <__mcount_loc>: &__put_page &put_pages_list &__activate_page &activate_page &lru_cache_add <__mcount_loc>: &vfs_llseek &default_llseek &new_sync_read &new_sync_write &__vfs_write kernel/sched/core.o: mm/swap.o: fs/read_write.o:

Slide 23

Slide 23 text

23 ©2019 VMware, Inc. Linker Magic <__start_mcount_loc>: 0xffffffff81a14910 0xffffffff81a149b0 0xffffffff81a14c00 0xffffffff81a14c20 0xffffffff81a14c50 0xffffffff8126f7b0 0xffffffff8126f8f0 0xffffffff8126fcc0 0xffffffff81270440 0xffffffff81270690 0xffffffff8131f0f0 0xffffffff8131f120 0xffffffff8131fb40 0xffffffff8131fd00 0xffffffff8131fed0 [...] <__stop_mcount_loc>: vmlinux: <__mcount_loc>: &schedule &yield &preempt_schedule_common &_cond_resched &schedule_idle <__mcount_loc>: &__put_page &put_pages_list &__activate_page &activate_page &lru_cache_add <__mcount_loc>: &vfs_llseek &default_llseek &new_sync_read &new_sync_write &__vfs_write kernel/sched/core.o: mm/swap.o: fs/read_write.o:

Slide 24

Slide 24 text

24 ©2019 VMware, Inc. Finding __fentry__ : callq <__fentry__> [..] : callq <__fentry__> [..] : callq <__fentry__> [..] <_cond_resched>: callq <__fentry__> [..] : callq <__fentry__> [..] <__start_mcount_loc>: [...] <__stop_mcount_loc>: vmlinux:

Slide 25

Slide 25 text

25 ©2019 VMware, Inc. : callq <__fentry__> [..] : callq <__fentry__> [..] : callq <__fentry__> [..] <_cond_resched>: callq <__fentry__> [..] : callq <__fentry__> [..] <__start_mcount_loc>: [...] <__stop_mcount_loc>: Finding __fentry__ vmlinux:

Slide 26

Slide 26 text

26 ©2019 VMware, Inc. Finding __fentry__ : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] <__start_mcount_loc>: [...] <__stop_mcount_loc>: vmlinux:

Slide 27

Slide 27 text

27 ©2019 VMware, Inc. Finding __fentry__ : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] <__start_mcount_loc>: [...] <__stop_mcount_loc>: vmlinux: gcc 5 also added -mnop-mcount

Slide 28

Slide 28 text

28 ©2019 VMware, Inc. Finding __fentry__ vmlinux: : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] <__start_mcount_loc>: [...] <__stop_mcount_loc>:

Slide 29

Slide 29 text

29 ©2019 VMware, Inc. Finding __fentry__ vmlinux: : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..]

Slide 30

Slide 30 text

30 ©2019 VMware, Inc. What about Tracing? Need to know where to enable tracing We threw away the __mcount_loc section

Slide 31

Slide 31 text

31 ©2019 VMware, Inc. What about Tracing? Need to know where to enable tracing We threw away the __mcount_loc section  The __mcount_loc section isn’t enough for us  Tracing requires saving state

Slide 32

Slide 32 text

32 ©2019 VMware, Inc. struct dyn_ftrace struct dyn_ftrace { unsigned long ip; /* address of mcount call-site */ unsigned long flags; struct dyn_arch_ftrace arch; };

Slide 33

Slide 33 text

33 ©2019 VMware, Inc. struct dyn_ftrace struct dyn_ftrace { unsigned long ip; /* address of mcount call-site */ unsigned long flags; struct dyn_arch_ftrace arch; }; arch/x86/include/asm/ftrace.h: struct dyn_arch_ftrace { /* No extra data needed for x86 */ };

Slide 34

Slide 34 text

34 ©2019 VMware, Inc. struct dyn_ftrace struct dyn_ftrace { unsigned long ip; /* address of mcount call-site */ unsigned long flags; struct dyn_arch_ftrace arch; }; arch/powerpc/include/asm/ftrace.h: struct dyn_arch_ftrace { struct module *mod; };

Slide 35

Slide 35 text

35 ©2019 VMware, Inc. Tracing data Copy from __mcount_loc before deleting that section Sorted for quick lookup Allocated in groups of pages  details out of scope for this talk Data reported at boot up – Allocated 39,317 dyn_ftrace structures – Used up 154 (4K) pages – Total of 630,784 bytes of memory $ dmesg |grep ftrace [ 0.528844] ftrace: allocating 39317 entries in 154 pages $ uname -r 5.1.11-200.fc29.x86_64

Slide 36

Slide 36 text

36 ©2019 VMware, Inc. Finding __fentry__ : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] <__start_mcount_loc>: [...] <__stop_mcount_loc>: vmlinux: ip = 0xffffffff81a14910 flags = 0 ip = 0xffffffff81a149b0 flags = 0 ip = 0xffffffff81a14c00 flags = 0 ip = 0xffffffff81a14c20 flags = 0 ip = 0xffffffff81a14c50 flags = 0 ip = 0xffffffff8126f7b0 flags = 0 ip = 0xffffffff8126f8f0 flags = 0 ip = 0xffffffff8126fcc0 flags = 0 ip = 0xffffffff81270440 flags = 0 ip = 0xffffffff81270690 flags = 0 ip = 0xffffffff8131f0f0 flags = 0 ip = 0xffffffff8131f120 flags = 0 ip = 0xffffffff8131fb40 flags = 0 ip = 0xffffffff8131fd00 flags = 0 ip = 0xffffffff8131fed0 flags = 0

Slide 37

Slide 37 text

37 ©2019 VMware, Inc. Finding __fentry__ ip = 0xffffffff81a14910 flags = 0 ip = 0xffffffff81a149b0 flags = 0 ip = 0xffffffff81a14c00 flags = 0 ip = 0xffffffff81a14c20 flags = 0 ip = 0xffffffff81a14c50 flags = 0 ip = 0xffffffff8126f7b0 flags = 0 ip = 0xffffffff8126f8f0 flags = 0 ip = 0xffffffff8126fcc0 flags = 0 ip = 0xffffffff81270440 flags = 0 ip = 0xffffffff81270690 flags = 0 ip = 0xffffffff8131f0f0 flags = 0 ip = 0xffffffff8131f120 flags = 0 ip = 0xffffffff8131fb40 flags = 0 ip = 0xffffffff8131fd00 flags = 0 # cat available_filter_functions schedule yield preempt_schedule_common _cond_resched schedule_idle __put_page put_pages_list __activate_page activate_page lru_cache_add vfs_llseek default_llseek new_sync_read new_sync_write

Slide 38

Slide 38 text

38 ©2019 VMware, Inc. Finding __fentry__ ip = 0xffffffff81a14910 flags = 0 ip = 0xffffffff81a149b0 flags = 0 ip = 0xffffffff81a14c00 flags = 0 ip = 0xffffffff81a14c20 flags = 0 ip = 0xffffffff81a14c50 flags = 0 ip = 0xffffffff8126f7b0 flags = 0 ip = 0xffffffff8126f8f0 flags = 0 ip = 0xffffffff8126fcc0 flags = 0 ip = 0xffffffff81270440 flags = 0 ip = 0xffffffff81270690 flags = 0 ip = 0xffffffff8131f0f0 flags = 0 ip = 0xffffffff8131f120 flags = 0 ip = 0xffffffff8131fb40 flags = 0 ip = 0xffffffff8131fd00 flags = 0 # echo default_llseek > set_ftrace_filter # echo sched_idle >> set_ftrace_filtre # cat set_ftrace_filter schedule_idle default_llseek

Slide 39

Slide 39 text

39 ©2019 VMware, Inc. dyn_ftrace.flags Bits 0-24: Counter for number of callbacks registered to function Bit 25: Function is being initialized and not ready to touch  module init Bit 26: Return from callback may modify IP address  kprobe or live patching Bit 27: Has unique trampoline and its enabled Bit 28: Has unique trampoline Bit 29: Saves regs is enabled (see bit 30) Bit 30: Needs to call ftrace_regs_caller (to save all regs like int3 does) Bit 31: The function is being traced

Slide 40

Slide 40 text

40 ©2019 VMware, Inc. dyn_ftrace.flags Bits 0-24: Counter for number of callbacks registered to function Bit 25: Function is being initialized and not ready to touch  module init Bit 26: Return from callback may modify IP address  kprobe or live patching Bit 27: Has unique trampoline and its enabled Bit 28: Has unique trampoline Bit 29: Saves regs is enabled (see bit 30) Bit 30: Needs to call ftrace_regs_caller (to save all regs like int3 does) Bit 31: The function is being traced

Slide 41

Slide 41 text

41 ©2019 VMware, Inc. Finding __fentry__ : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] vmlinux: ip = 0xffffffff81a14910 flags = 0 ip = 0xffffffff81a149b0 flags = 0 ip = 0xffffffff81a14c00 flags = 0 ip = 0xffffffff81a14c20 flags = 0 ip = 0xffffffff81a14c50 flags = 0 ip = 0xffffffff8126f7b0 flags = 0 ip = 0xffffffff8126f8f0 flags = 0 ip = 0xffffffff8126fcc0 flags = 0 ip = 0xffffffff81270440 flags = 0 ip = 0xffffffff81270690 flags = 0 ip = 0xffffffff8131f0f0 flags = 0 ip = 0xffffffff8131f120 flags = 0 ip = 0xffffffff8131fb40 flags = 0 ip = 0xffffffff8131fd00 flags = 0 ip = 0xffffffff8131fed0 flags = 0

Slide 42

Slide 42 text

42 ©2019 VMware, Inc. Finding __fentry__ vmlinux: ip = 0xffffffff81a14910 flags = 0x40000001 ip = 0xffffffff81a149b0 flags = 0 ip = 0xffffffff81a14c00 flags = 0 ip = 0xffffffff81a14c20 flags = 0 ip = 0xffffffff81a14c50 flags = 0 ip = 0xffffffff8126f7b0 flags = 0x00000001 ip = 0xffffffff8126f8f0 flags = 0 ip = 0xffffffff8126fcc0 flags = 0 ip = 0xffffffff81270440 flags = 0 ip = 0xffffffff81270690 flags = 0 ip = 0xffffffff8131f0f0 flags = 0 ip = 0xffffffff8131f120 flags = 0 ip = 0xffffffff8131fb40 flags = 0 ip = 0xffffffff8131fd00 flags = 0 ip = 0xffffffff8131fed0 flags = 0 : nop [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] bit 30 count = 1 count = 1

Slide 43

Slide 43 text

43 ©2019 VMware, Inc. Finding __fentry__ : call ftrace_regs_caller [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : call ftrace_caller [..] vmlinux: ip = 0xffffffff81a14910 flags = 0xe0000001 ip = 0xffffffff81a149b0 flags = 0 ip = 0xffffffff81a14c00 flags = 0 ip = 0xffffffff81a14c20 flags = 0 ip = 0xffffffff81a14c50 flags = 0 ip = 0xffffffff8126f7b0 flags = 0x80000001 ip = 0xffffffff8126f8f0 flags = 0 ip = 0xffffffff8126fcc0 flags = 0 ip = 0xffffffff81270440 flags = 0 ip = 0xffffffff81270690 flags = 0 ip = 0xffffffff8131f0f0 flags = 0 ip = 0xffffffff8131f120 flags = 0 ip = 0xffffffff8131fb40 flags = 0 ip = 0xffffffff8131fd00 flags = 0 ip = 0xffffffff8131fed0 flags = 0 bit 29,30,31 count = 1 bit 31 count = 1

Slide 44

Slide 44 text

44 ©2019 VMware, Inc. Modifying code at runtime! Not the same as at boot up SMP boxes need to take extra care Other CPUs may be executing the code you change x86 has non uniform instruction (different sizes) Instructions may cross cache and page boundaries

Slide 45

Slide 45 text

45 ©2019 VMware, Inc. Modifying code at runtime! : 0f 1f 44 00 00 nop 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 46

Slide 46 text

46 ©2019 VMware, Inc. Modifying code at runtime! : e8 1b d0 1e 00 callq ffffffff81c01930 <__fentry__> 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 47

Slide 47 text

47 ©2019 VMware, Inc. Modifying code at runtime! : 0f 1f 44 00 00 53 65 48 8b 1c 25 00 61 01 00 48 8b 43 10 48 85 c0 : 0f 1f 44 00 00 53 65 48 8b 1c 25 00 61 01 00 48 8b 43 10 48 85 c0 CPU 0 CPU 1

Slide 48

Slide 48 text

48 ©2019 VMware, Inc. Modifying code at runtime! : 0f 1f 44 00 00 53 65 48 8b 1c 25 00 61 01 00 48 8b 43 10 48 85 c0 : e8 1b d0 1e 00 53 65 48 8b 1c 25 00 61 01 00 48 8b 43 10 48 85 c0 CPU 0 CPU 1

Slide 49

Slide 49 text

49 ©2019 VMware, Inc. Modifying code at runtime! : 0f 1f d0 1e 00 53 65 48 8b 1c 25 00 61 01 00 48 8b 43 10 48 85 c0 : e8 1b d0 1e 00 53 65 48 8b 1c 25 00 61 01 00 48 8b 43 10 48 85 c0 CPU 0 CPU 1

Slide 50

Slide 50 text

50 ©2019 VMware, Inc. 0f 1f d0 1e 00 ??? 0f 1f d0 1e 00

Slide 51

Slide 51 text

51 ©2019 VMware, Inc. 0f 1f d0 1e 00 ??? BOOM! CRASH! General Protection Fault! REBOOT!

Slide 52

Slide 52 text

52 ©2019 VMware, Inc. How to go from this! : 0f 1f 44 00 00 nop 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 53

Slide 53 text

53 ©2019 VMware, Inc. To this? : e8 1b d0 1e 00 callq ffffffff81c01930 <__fentry__> 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 54

Slide 54 text

54 ©2019 VMware, Inc. Breakpoints!

Slide 55

Slide 55 text

55 ©2019 VMware, Inc. Breakpoints! : 0f 1f 44 00 00 nop 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 56

Slide 56 text

56 ©2019 VMware, Inc. Breakpoints! : 1f 44 00 00 nop 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 57

Slide 57 text

57 ©2019 VMware, Inc. How this works : nop push %rbx mov %gs:0x16100,%rbx mov 0x10(%rbx),%rax test %rax,%rax

Slide 58

Slide 58 text

58 ©2019 VMware, Inc. How this works : nop push %rbx mov %gs:0x16100,%rbx mov 0x10(%rbx),%rax test %rax,%rax do_int3(struct pt_regs *regs) { regs->ip += 5; return }

Slide 59

Slide 59 text

59 ©2019 VMware, Inc. How this works : nop push %rbx mov %gs:0x16100,%rbx mov 0x10(%rbx),%rax test %rax,%rax do_int3(struct pt_regs *regs) { regs->ip += 5; return }

Slide 60

Slide 60 text

60 ©2019 VMware, Inc. How this works : nop push %rbx mov %gs:0x16100,%rbx mov 0x10(%rbx),%rax test %rax,%rax do_int3(struct pt_regs *regs) { regs->ip += 5; return }

Slide 61

Slide 61 text

61 ©2019 VMware, Inc. How this works : nop push %rbx mov %gs:0x16100,%rbx mov 0x10(%rbx),%rax test %rax,%rax do_int3(struct pt_regs *regs) { regs->ip += 5; return }

Slide 62

Slide 62 text

62 ©2019 VMware, Inc. Breakpoints! : 1f 44 00 00 nop 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 63

Slide 63 text

63 ©2019 VMware, Inc. Breakpoints! : 1b d0 1e 00 callq ffffffff81c01930 <__fentry__> 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 64

Slide 64 text

64 ©2019 VMware, Inc. Breakpoints! : e8 1b d0 1e 00 callq ffffffff81c01930 <__fentry__> 53 push %rbx 65 48 8b 1c 25 00 61 mov %gs:0x16100,%rbx 01 00 ffffffff81a1491b: R_X86_64_32S current_task 48 8b 43 10 mov 0x10(%rbx),%rax 48 85 c0 test %rax,%rax 74 10 je ffffffff81a14938 f6 43 24 20 testb $0x20,0x24(%rbx) 75 49 jne ffffffff81a14977 48 83 bb 20 0c 00 00 cmpq $0x0,0xc20(%rbx) 00 74 1f je ffffffff81a14957 31 ff xor %edi,%edi e8 a1 f8 ff ff callq ffffffff81a141e0 <__schedule> 65 48 8b 04 25 00 61 mov %gs:0x16100,%rax 01 00

Slide 65

Slide 65 text

65 ©2019 VMware, Inc. Registering a callback with ftrace Call register_ftrace_function() Takes a ftrace_ops descriptor Static ftrace_ops (allocated at build time)  Top level ftrace tracers – function – function_graph – stack tracer – latency tracers Dynamic ftrace_ops (allocated via kmalloc() )  perf  kprobes  ftrace instances (sub buffers)

Slide 66

Slide 66 text

66 ©2019 VMware, Inc. ftrace_ops structure struct ftrace_ops { ftrace_func_t func; struct ftrace_ops __rcu *next; unsigned long flags; void *private; ftrace_func_t saved_func; #ifdef CONFIG_DYNAMIC_FTRACE struct ftrace_ops_hash local_hash; struct ftrace_ops_hash *func_hash; struct ftrace_ops_hash old_hash; unsigned long trampoline; unsigned long trampoline_size; #endif };

Slide 67

Slide 67 text

67 ©2019 VMware, Inc. ftrace_caller trampoline : callq ftrace_caller [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] vmlinux: : save_regs load_regs ftrace_call: call ftrace_stub restore_regs ftrace_stub: retq

Slide 68

Slide 68 text

68 ©2019 VMware, Inc. ftrace_caller trampoline : callq ftrace_caller [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] vmlinux: : save_regs load_regs ftrace_call: call func_trace restore_regs ftrace_stub: retq void func_trace() { /* trace */ }

Slide 69

Slide 69 text

69 ©2019 VMware, Inc. ftrace_caller trampoline : callq ftrace_caller [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] vmlinux: : save_regs load_regs ftrace_call: call func_trace restore_regs ftrace_stub: retq void func_trace() { /* trace */ } ftrace_ops.func

Slide 70

Slide 70 text

70 ©2019 VMware, Inc. Calling more that one callback on a function? Direct calls to a single function are easy Handling more than one, requires a list operation But then all functions being traced will go through a list!

Slide 71

Slide 71 text

71 ©2019 VMware, Inc. ftrace_caller trampoline : callq ftrace_caller [..] : nop [..] : nop [..] <_cond_resched>: nop [..] : nop [..] vmlinux: : save_regs load_regs ftrace_call: call list_func restore_regs ftrace_stub: retq void list_func() { /* iterate */ } void func1_func() { /* trace */ } void func2_func() { /* trace */ }

Slide 72

Slide 72 text

72 ©2019 VMware, Inc. Multiple function callback example Run function tracer on all functions Run perf on just the scheduler

Slide 73

Slide 73 text

73 ©2019 VMware, Inc. Multiple function callback example Want to trace schedule_idle()? NO list_func() perf Yes! function tracer

Slide 74

Slide 74 text

74 ©2019 VMware, Inc. Multiple function callback example Want to trace __cond_resched()? NO list_func() perf Yes! function tracer

Slide 75

Slide 75 text

75 ©2019 VMware, Inc. Multiple function callback example Want to trace yield()? NO list_func() perf Yes! function tracer

Slide 76

Slide 76 text

76 ©2019 VMware, Inc. Multiple function callback example Want to trace schedule()? Yes! list_func() perf Yes! function tracer

Slide 77

Slide 77 text

77 ©2019 VMware, Inc. ftrace_caller trampoline : callq ftrace_caller [..] : callq ftrace_caller [..] : callq ftrace_caller [..] <_cond_resched>: callq ftrace_caller [..] : callq ftrace_caller [..] vmlinux: : save_regs load_regs ftrace_call: call list_func restore_regs ftrace_stub: retq void list_func() { /* iterate */ } void function_trace() { /* function tracing */ } void perf_func() { /* function profiling */ }

Slide 78

Slide 78 text

78 ©2019 VMware, Inc. ftrace_caller trampoline : callq ftrace_caller [..] : callq dynamic_trampoline [..] : callq dynamic_trampoline [..] <_cond_resched>: callq dynamic_trampoline [..] : callq dynamic_trampoline [..] vmlinux: : save_regs load_regs ftrace_call: call list_func restore_regs ftrace_stub: retq void list_func() { /* iterate */ } void function_trace() { /* function tracing */ } void perf_func() { /* function profiling */ } : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq

Slide 79

Slide 79 text

79 ©2019 VMware, Inc. Problems with dynamic trampolines When can you free them? How do you know they are still not in use?

Slide 80

Slide 80 text

80 ©2019 VMware, Inc. Dynamic Trampoline Problem : callq dynamic_trampoline push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq

Slide 81

Slide 81 text

81 ©2019 VMware, Inc. Dynamic Trampoline Problem : callq dynamic_trampoline push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq

Slide 82

Slide 82 text

82 ©2019 VMware, Inc. Dynamic Trampoline Problem : callq dynamic_trampoline push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq

Slide 83

Slide 83 text

83 ©2019 VMware, Inc. Dynamic Trampoline Problem : callq dynamic_trampoline push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Preempted!

Slide 84

Slide 84 text

84 ©2019 VMware, Inc. Dynamic Trampoline Problem : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Preempted!

Slide 85

Slide 85 text

85 ©2019 VMware, Inc. Dynamic Trampoline Problem : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Preempted! kfree(dynamic_trampoline)

Slide 86

Slide 86 text

86 ©2019 VMware, Inc. Dynamic Trampoline Problem : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Scheduled

Slide 87

Slide 87 text

87 ©2019 VMware, Inc. Dynamic Trampoline Problem : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq

Slide 88

Slide 88 text

88 ©2019 VMware, Inc. Dynamic Trampoline Problem : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq CRASH!

Slide 89

Slide 89 text

89 ©2019 VMware, Inc. Problems with dynamic trampolines When can you free them? How do you know they are still not in use?

Slide 90

Slide 90 text

90 ©2019 VMware, Inc. Problems with dynamic trampolines When can you free them? How do you know they are still not in use? Use RCU!

Slide 91

Slide 91 text

91 ©2019 VMware, Inc. call_rcu_tasks() Added in Linux v3.18  Commit 8315f42295d2667 by Paul E. McKenney synchronize_rcu_tasks()  Waits for all tasks to voluntary schedule  We do not allow ftrace callbacks to schedule  The trampoline will not schedule

Slide 92

Slide 92 text

92 ©2019 VMware, Inc. call_rcu_tasks() Added in Linux v3.18  Commit 8315f42295d2667 by Paul E. McKenney synchronize_rcu_tasks()  Waits for all tasks to voluntary schedule  We do not allow ftrace callbacks to schedule  The trampoline will not schedule Used by ftrace in v4.12

Slide 93

Slide 93 text

93 ©2019 VMware, Inc. call_rcu_tasks() Added in Linux v3.18  Commit 8315f42295d2667 by Paul E. McKenney synchronize_rcu_tasks()  Waits for all tasks to voluntary schedule  We do not allow ftrace callbacks to schedule  The trampoline will not schedule Used by ftrace in v4.12  Yes Steven was lazy  Added with the threat that Paul was going to remove it

Slide 94

Slide 94 text

94 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Preempted!

Slide 95

Slide 95 text

95 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Preempted! call_rcu_tasks(dynamic_trampoline)

Slide 96

Slide 96 text

96 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Preempted! call_rcu_tasks(dynamic_trampoline) Wait’s for all tasks to voluntarily schedule

Slide 97

Slide 97 text

97 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq Scheduled call_rcu_tasks(dynamic_trampoline) Wait’s for all tasks to voluntarily schedule

Slide 98

Slide 98 text

98 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq call_rcu_tasks(dynamic_trampoline) Wait’s for all tasks to voluntarily schedule

Slide 99

Slide 99 text

99 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq call_rcu_tasks(dynamic_trampoline) Wait’s for all tasks to voluntarily schedule

Slide 100

Slide 100 text

100 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq call_rcu_tasks(dynamic_trampoline) All tasks have scheduled

Slide 101

Slide 101 text

101 ©2019 VMware, Inc. Dynamic Trampoline Solution : nop push %rbx mov %gs:0x16100,%rbx vmlinux: : save_regs load_regs ftrace_call: call function_trace restore_regs ftrace_stub: retq kfree(dynamic_trampoline)

Slide 102

Slide 102 text

102 ©2019 VMware, Inc. More uses of the function callback code ftrace_regs_caller() gives all registers A callback can modify any register  Needs a flag in ftrace_ops to modify the instruction pointer (ip)

Slide 103

Slide 103 text

103 ©2019 VMware, Inc. Live Kernel Patching! : callq ftrace_caller [..] Buggy schedule() function : save_regs load_regs call kernel_patch restore_regs retq void kernel_patch() { regs.ip = schedule_fix; } : nop [..] Fixed schedule() function

Slide 104

Slide 104 text

Thank You