tracing: Some more fixes for 6.5
Subsystems (6)
Steven Rostedt
maintainercover tracing: Some more fixes for 6.5
Tracing fixes for 6.5:
- Update to fix of prototypes (my merge of Arnd's patch caused another warning).
- Add selftest to struct sizes for user events
- Quiet false positive of FORTIFY_SOURCE
Due to backward compatibilty, the structure used to save stack traces in the kernel had a fixed size of 8. This structure is exported to user space via the tracing format file. A change was made to allow more than 8 functions to be recorded, and user space now uses the size field to know how many functions are actually in the stack. But the structure still has size of 8 (even though it points into the ring buffer that has the required amount allocated to hold a full stack. This was fine until the fortifier noticed that the memcpy(&entry->caller, stack, size) was greater than the 8 functions and would complain at runtime about it. Hide this by using a pointer to the stack location on the ring buffer instead of using the address of the entry structure caller field.
- Fix a deadloop in reading trace_pipe that was caused by a mismatch between ring_buffer_empty() returning false which then asked to read the data, but the read code uses rb_num_of_entries() that returned zero, and causing a infinite "retry".
- Fix a warning caused by not using all pages allocated to store ftrace functions, where this can happen if the linker inserts a bunch of "NULL" entries, causing the accounting of how many pages needed to be off.
git://git.kernel.org/pub/scm/linux/kernel/git/trace/linux-trace.git
trace/urgent
Head SHA1: bec3c25c247c4f88a33d79675a09e1644c9a3114
Arnd Bergmann (1):
tracing: arm64: Avoid missing-prototype warnings
Beau Belgrave (1):
selftests/user_events: Test struct size match cases
Steven Rostedt (Google) (1):
tracing: Stop FORTIFY_SOURCE complaining about stack trace caller
Zheng Yejian (2):
ring-buffer: Fix deadloop issue on reading trace_pipe
ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()
----
arch/arm64/include/asm/ftrace.h | 4 +++ arch/arm64/include/asm/syscall.h | 3 ++ arch/arm64/kernel/syscall.c | 3 -- include/linux/ftrace.h | 9 ++++++ kernel/trace/fgraph.c | 1 + kernel/trace/ftrace.c | 45 ++++++++++++++++++-------- kernel/trace/ftrace_internal.h | 5 +-- kernel/trace/ring_buffer.c | 24 ++++++++------ kernel/trace/trace.c | 21 ++++++++++-- kernel/trace/trace_kprobe_selftest.c | 3 ++ tools/testing/selftests/user_events/dyn_test.c | 12 +++++++ 11 files changed, 100 insertions(+), 30 deletions(-)
Steven Rostedt
maintainerPATCH 4/5 ftrace: Fix possible warning on checking all pages used in ftrace_process_locs()
From: Zheng Yejian <[email protected]>
As comments in ftrace_process_locs(), there may be NULL pointers in mcount_loc section:
Quoted 4 lines
Some architecture linkers will pad between the different
mcount_locsections of different object files to satisfy alignments. Skip any NULL pointers.
After commit 20e5227e9f55 ("ftrace: allow NULL pointers in mcount_loc"), NULL pointers will be accounted when allocating ftrace pages but skipped before adding into ftrace pages, this may result in some pages not being used. Then after commit 706c81f87f84 ("ftrace: Remove extra helper functions"), warning may occur at:
WARN_ON(pg->next);
To fix it, only warn for case that no pointers skipped but pages not used up, then free those unused pages after releasing ftrace_lock.
--- kernel/trace/ftrace.c | 45 +++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-)
kernel/trace/ftrace.c +31 -14
@@ -3305,6 +3305,22 @@ static int ftrace_allocate_records(struct ftrace_page *pg, int count)return cnt; } +static void ftrace_free_pages (struct ftrace_page *pages) +{ +struct ftrace_page *pg = pages; + +while (pg) { +if (pg->records) { +free_pages ((unsigned long )pg->records, pg->order); + ftrace_number_of_pages -=1 << pg->order; + } + pages = pg->next; +kfree (pg); + pg = pages; + ftrace_number_of_groups--; + } +} +static struct ftrace_page *ftrace_allocate_pages (unsigned long num_to_init) {
@@ -3343,17 +3359,7 @@ ftrace_allocate_pages(unsigned long num_to_init)return start_pg; free_pages: - pg = start_pg; -while (pg) { -if (pg->records) { -free_pages ((unsigned long )pg->records, pg->order); - ftrace_number_of_pages -=1 << pg->order; - } - start_pg = pg->next; -kfree (pg); - pg = start_pg; - ftrace_number_of_groups--; - } +ftrace_free_pages (start_pg);pr_info ("ftrace: FAILED to allocate memory for functions\n" );return NULL ; }
@@ -6471,9 +6477,11 @@ static int ftrace_process_locs(struct module *mod,unsigned long *start, unsigned long *end) { + struct ftrace_page *pg_unuse = NULL;struct ftrace_page *start_pg;struct ftrace_page *pg;struct dyn_ftrace *rec; +unsigned long skipped =0 ;unsigned long count;unsigned long *p;unsigned long addr;
@@ -6536,8 +6544,10 @@ static int ftrace_process_locs(struct module *mod,* object files to satisfy alignments. * Skip any NULL pointers. */ -if (!addr) +if (!addr) { + skipped++;continue ; + } end_offset = (pg->index+1 ) *sizeof (pg->records[0 ]);if (end_offset >PAGE_SIZE << pg->order) {
@@ -6551,8 +6561,10 @@ static int ftrace_process_locs(struct module *mod, rec->ip = addr; } -/* We should have used all pages */ -WARN_ON (pg->next); +if (pg->next) { + pg_unuse = pg->next; + pg->next =NULL ; + }/* Assign the last page to ftrace_pages */ ftrace_pages = pg;
@@ -6574,6 +6586,11 @@ static int ftrace_process_locs(struct module *mod, out:mutex_unlock (&ftrace_lock); +/* We should have used all pages unless we skipped some */ +if (pg_unuse) { +WARN_ON (!skipped); +ftrace_free_pages (pg_unuse); + }return ret; }
Signature · 1 line
2.40.1
Steven Rostedt
maintainerPATCH 1/5 selftests/user_events: Test struct size match cases
In reply to Steven RostedtFrom: Beau Belgrave <[email protected]>
The self tests for user_events currently does not ensure that the edge case for struct types work properly with size differences.
Add cases for mis-matching struct names and sizes to ensure they work properly.
--- tools/testing/selftests/user_events/dyn_test.c | 12 ++++++++++++ 1 file changed, 12 insertions(+)
tools/testing/selftests/user_events/dyn_test.c +12 -0
@@ -217,6 +217,18 @@ TEST_F(user, matching) {/* Types don't match */ TEST_NMATCH ("__test_event u64 a; u64 b" ,"__test_event u32 a; u32 b" ); + +/* Struct name and size matches */ +TEST_MATCH ("__test_event struct my_struct a 20" , +"__test_event struct my_struct a 20" ); + +/* Struct name don't match */ +TEST_NMATCH ("__test_event struct my_struct a 20" , +"__test_event struct my_struct b 20" ); + +/* Struct size don't match */ +TEST_NMATCH ("__test_event struct my_struct a 20" , +"__test_event struct my_struct a 21" ); }int main (int argc,char **argv)
Signature · 1 line
2.40.1
Steven Rostedt
maintainerPATCH 3/5 ring-buffer: Fix deadloop issue on reading trace_pipe
In reply to Steven RostedtFrom: Zheng Yejian <[email protected]>
Soft lockup occurs when reading file 'trace_pipe':
watchdog: BUG: soft lockup - CPU#6 stuck for 22s! [cat:4488] [...] RIP: 0010:ring_buffer_empty_cpu+0xed/0x170 RSP: 0018:ffff88810dd6fc48 EFLAGS: 00000246 RAX: 0000000000000000 RBX: 0000000000000246 RCX: ffffffff93d1aaeb RDX: ffff88810a280040 RSI: 0000000000000008 RDI: ffff88811164b218 RBP: ffff88811164b218 R08: 0000000000000000 R09: ffff88815156600f R10: ffffed102a2acc01 R11: 0000000000000001 R12: 0000000051651901 R13: 0000000000000000 R14: ffff888115e49500 R15: 0000000000000000 [...] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 CR2: 00007f8d853c2000 CR3: 000000010dcd8000 CR4: 00000000000006e0 DR0: 0000000000000000 DR1: 0000000000000000 DR2: 0000000000000000 DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 0000000000000400 Call Trace: __find_next_entry+0x1a8/0x4b0 ? peek_next_entry+0x250/0x250 ? down_write+0xa5/0x120 ? down_write_killable+0x130/0x130 trace_find_next_entry_inc+0x3b/0x1d0 tracing_read_pipe+0x423/0xae0 ? tracing_splice_read_pipe+0xcb0/0xcb0 vfs_read+0x16b/0x490 ksys_read+0x105/0x210 ? __ia32_sys_pwrite64+0x200/0x200 ? switch_fpu_return+0x108/0x220 do_syscall_64+0x33/0x40 entry_SYSCALL_64_after_hwframe+0x61/0xc6
Through the vmcore, I found it's because in tracing_read_pipe(), ring_buffer_empty_cpu() found some buffer is not empty but then it cannot read anything due to "rb_num_of_entries() == 0" always true, Then it infinitely loop the procedure due to user buffer not been filled, see following code path:
tracing_read_pipe() {
... ...
waitagain:
tracing_wait_pipe() // 1. find non-empty buffer here
trace_find_next_entry_inc() // 2. loop here try to find an entry
__find_next_entry()
ring_buffer_empty_cpu(); // 3. find non-empty buffer
peek_next_entry() // 4. but peek always return NULL
ring_buffer_peek()
rb_buffer_peek()
rb_get_reader_page()
// 5. because rb_num_of_entries() == 0 always true here
// then return NULL
// 6. user buffer not been filled so goto 'waitgain'
// and eventually leads to an deadloop in kernel!!!}
By some analyzing, I found that when resetting ringbuffer, the 'entries' of its pages are not all cleared (see rb_reset_cpu()). Then when reducing the ringbuffer, and if some reduced pages exist dirty 'entries' data, they will be added into 'cpu_buffer->overrun' (see rb_remove_pages()), which cause wrong 'overrun' count and eventually cause the deadloop issue.
To fix it, we need to clear every pages in rb_reset_cpu().
--- kernel/trace/ring_buffer.c | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-)
kernel/trace/ring_buffer.c +15 -9
@@ -5242,28 +5242,34 @@ unsigned long ring_buffer_size(struct trace_buffer *buffer, int cpu) }EXPORT_SYMBOL_GPL (ring_buffer_size); +static void rb_clear_buffer_page (struct buffer_page *page) +{ +local_set (&page->write,0 ); +local_set (&page->entries,0 ); +rb_init_page (page->page); + page->read =0 ; +} +static void rb_reset_cpu (struct ring_buffer_per_cpu *cpu_buffer) { +struct buffer_page *page; +rb_head_page_deactivate (cpu_buffer); cpu_buffer->head_page = list_entry(cpu_buffer->pages, struct buffer_page, list); -local_set (&cpu_buffer->head_page->write,0 ); -local_set (&cpu_buffer->head_page->entries,0 ); -local_set (&cpu_buffer->head_page->page->commit,0 ); - - cpu_buffer->head_page->read =0 ; +rb_clear_buffer_page (cpu_buffer->head_page); +list_for_each_entry (page, cpu_buffer->pages, list) { +rb_clear_buffer_page (page); + } cpu_buffer->tail_page = cpu_buffer->head_page; cpu_buffer->commit_page = cpu_buffer->head_page;INIT_LIST_HEAD (&cpu_buffer->reader_page->list);INIT_LIST_HEAD (&cpu_buffer->new_pages); -local_set (&cpu_buffer->reader_page->write,0 ); -local_set (&cpu_buffer->reader_page->entries,0 ); -local_set (&cpu_buffer->reader_page->page->commit,0 ); - cpu_buffer->reader_page->read =0 ; +rb_clear_buffer_page (cpu_buffer->reader_page);local_set (&cpu_buffer->entries_bytes,0 );local_set (&cpu_buffer->overrun,0 );
Signature · 1 line
2.40.1
Steven Rostedt
maintainerPATCH 5/5 tracing: Stop FORTIFY_SOURCE complaining about stack trace caller
In reply to Steven RostedtFrom: "Steven Rostedt (Google)" <[email protected]>
The stack_trace event is an event created by the tracing subsystem to store stack traces. It originally just contained a hard coded array of 8 words to hold the stack, and a "size" to know how many entries are there. This is exported to user space as:
name: kernel_stack
ID: 4
format:
field:unsigned short common_type; offset:0; size:2; signed:0; field:unsigned char common_flags; offset:2; size:1; signed:0; field:unsigned char common_preempt_count; offset:3; size:1; signed:0; field:int common_pid; offset:4; size:4; signed:1; field:int size; offset:8; size:4; signed:1; field:unsigned long caller[8]; offset:16; size:64; signed:0;
print fmt: "\t=> %ps\n\t=> %ps\n\t=> %ps\n" "\t=> %ps\n\t=> %ps\n\t=> %ps\n" "\t=> %ps\n\t=> %ps\n",i
(void *)REC->caller[0], (void *)REC->caller[1], (void *)REC->caller[2],
(void *)REC->caller[3], (void *)REC->caller[4], (void *)REC->caller[5],
(void *)REC->caller[6], (void *)REC->caller[7]
Where the user space tracers could parse the stack. The library was updated for this specific event to only look at the size, and not the array. But some older users still look at the array (note, the older code still checks to make sure the array fits inside the event that it read. That is, if only 4 words were saved, the parser would not read the fifth word because it will see that it was outside of the event size).
This event was changed a while ago to be more dynamic, and would save a full stack even if it was greater than 8 words. It does this by simply allocating more ring buffer to hold the extra words. Then it copies in the stack via:
memcpy(&entry->caller, fstack->calls, size);
As the entry is struct stack_entry, that is created by a macro to both create the structure and export this to user space, it still had the caller field of entry defined as: unsigned long caller[8].
When the stack is greater than 8, the FORTIFY_SOURCE code notices that the amount being copied is greater than the source array and complains about it. It has no idea that the source is pointing to the ring buffer with the required allocation.
To hide this from the FORTIFY_SOURCE logic, pointer arithmetic is used:
ptr = ring_buffer_event_data(event); entry = ptr; ptr += offsetof(typeof(*entry), caller); memcpy(ptr, fstack->calls, size);
--- kernel/trace/trace.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-)
kernel/trace/trace.c +19 -2
@@ -3118,6 +3118,7 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer,struct ftrace_stack *fstack;struct stack_entry *entry;int stackidx; +void *ptr;/* * Add one, for this function and the call to save_stack_trace()
@@ -3161,9 +3162,25 @@ static void __ftrace_trace_stack(struct trace_buffer *buffer, trace_ctx);if (!event) goto out; - entry =ring_buffer_event_data (event); + ptr =ring_buffer_event_data (event); + entry = ptr; + +/* +* For backward compatibility reasons, the entry->caller is an +* array of 8 slots to store the stack. This is also exported +* to user space. The amount allocated on the ring buffer actually +* holds enough for the stack specified by nr_entries. This will +* go into the location of entry->caller. Due to string fortifiers +* checking the size of the destination of memcpy() it triggers +* when it detects that size is greater than 8. To hide this from +* the fortifiers, we use "ptr" and pointer arithmetic to assign caller. +* +* The below is really just: +* memcpy(&entry->caller, fstack->calls, size); +*/ + ptr += offsetof(typeof(*entry), caller); +memcpy (ptr, fstack->calls, size); -memcpy (&entry->caller, fstack->calls, size); entry->size = nr_entries;if (!call_filter_check_discard (call, entry, buffer, event))
Signature · 1 line
2.40.1
Steven Rostedt
maintainerPATCH 2/5 tracing: arm64: Avoid missing-prototype warnings
In reply to Steven RostedtFrom: Arnd Bergmann <[email protected]>
These are all tracing W=1 warnings in arm64 allmodconfig about missing prototypes:
kernel/trace/trace_kprobe_selftest.c:7:5: error: no previous prototype for 'kprobe_trace_selftest_target' [-Werror=missing-pro totypes]kernel/trace/ftrace.c:329:5: error: no previous prototype for '__register_ftrace_function' [-Werror=missing-prototypes] kernel/trace/ftrace.c:372:5: error: no previous prototype for '__unregister_ftrace_function' [-Werror=missing-prototypes] kernel/trace/ftrace.c:4130:15: error: no previous prototype for 'arch_ftrace_match_adjust' [-Werror=missing-prototypes] kernel/trace/fgraph.c:243:15: error: no previous prototype for 'ftrace_return_to_handler' [-Werror=missing-prototypes] kernel/trace/fgraph.c:358:6: error: no previous prototype for 'ftrace_graph_sleep_time_control' [-Werror=missing-prototypes] arch/arm64/kernel/ftrace.c:460:6: error: no previous prototype for 'prepare_ftrace_return' [-Werror=missing-prototypes] arch/arm64/kernel/ptrace.c:2172:5: error: no previous prototype for 'syscall_trace_enter' [-Werror=missing-prototypes] arch/arm64/kernel/ptrace.c:2195:6: error: no previous prototype for 'syscall_trace_exit' [-Werror=missing-prototypes]
Move the declarations to an appropriate header where they can be seen by the caller and callee, and make sure the headers are included where needed.
--- arch/arm64/include/asm/ftrace.h | 4 ++++ arch/arm64/include/asm/syscall.h | 3 +++ arch/arm64/kernel/syscall.c | 3 --- include/linux/ftrace.h | 9 +++++++++ kernel/trace/fgraph.c | 1 + kernel/trace/ftrace_internal.h | 5 +++-- kernel/trace/trace_kprobe_selftest.c | 3 +++ 7 files changed, 23 insertions(+), 5 deletions(-)
arch/arm64/include/asm/ftrace.h +4 -0
@@ -211,6 +211,10 @@ static inline unsigned long fgraph_ret_regs_frame_pointer(struct fgraph_ret_regs {return ret_regs->fp; } + +void prepare_ftrace_return (unsigned long self_addr,unsigned long *parent, +unsigned long frame_pointer); +#endif /* ifdef CONFIG_FUNCTION_GRAPH_TRACER */ #endif
arch/arm64/include/asm/syscall.h +3 -0
@@ -85,4 +85,7 @@ static inline int syscall_get_arch(struct task_struct *task) return AUDIT_ARCH_AARCH64; } +int syscall_trace_enter (struct pt_regs *regs); +void syscall_trace_exit (struct pt_regs *regs); +#endif /* __ASM_SYSCALL_H */
arch/arm64/kernel/syscall.c +0 -3
@@ -75,9 +75,6 @@ static inline bool has_syscall_work(unsigned long flags)return unlikely (flags & _TIF_SYSCALL_WORK); } -int syscall_trace_enter (struct pt_regs *regs); -void syscall_trace_exit (struct pt_regs *regs); -static void el0_svc_common (struct pt_regs *regs,int scno,int sc_nr,const syscall_fn_t syscall_table[]) {
include/linux/ftrace.h +9 -0
@@ -41,6 +41,15 @@ struct ftrace_ops;struct ftrace_regs ;struct dyn_ftrace ; +char *arch_ftrace_match_adjust (char *str,const char *search); + +#ifdef CONFIG_HAVE_FUNCTION_GRAPH_RETVAL +struct fgraph_ret_regs ; +unsigned long ftrace_return_to_handler (struct fgraph_ret_regs *ret_regs); +#else +unsigned long ftrace_return_to_handler (unsigned long frame_pointer); +#endif +#ifdef CONFIG_FUNCTION_TRACER /* * If the arch's mcount caller does not support all of ftrace's
kernel/trace/fgraph.c +1 -0
@@ -15,6 +15,7 @@#include <trace/events/sched.h> #include "ftrace_internal.h" +#include "trace.h" #ifdef CONFIG_DYNAMIC_FTRACE #define ASSIGN_OPS_HASH (opsname, val) \
kernel/trace/ftrace_internal.h +3 -2
@@ -2,6 +2,9 @@#ifndef _LINUX_KERNEL_FTRACE_INTERNAL_H#define _LINUX_KERNEL_FTRACE_INTERNAL_H +int __register_ftrace_function (struct ftrace_ops *ops); +int __unregister_ftrace_function (struct ftrace_ops *ops); +#ifdef CONFIG_FUNCTION_TRACER extern struct mutex ftrace_lock;
@@ -15,8 +18,6 @@ int ftrace_ops_test(struct ftrace_ops *ops, unsigned long ip, void *regs);#else /* !CONFIG_DYNAMIC_FTRACE */ -int __register_ftrace_function (struct ftrace_ops *ops); -int __unregister_ftrace_function (struct ftrace_ops *ops);/* Keep as macros so we do not need to define the commands */ # define ftrace_startup (ops, command) \ ({ \
kernel/trace/trace_kprobe_selftest.c +3 -0
@@ -1,4 +1,7 @@// SPDX-License-Identifier: GPL-2.0 + +#include "trace_kprobe_selftest.h" +/* * Function used during the kprobe self test. This function is in a separate * compile unit so it can be compile with CC_FLAGS_FTRACE to ensure that it
Signature · 1 line
2.40.1