Skip to content

Commit e4a9ea5

Browse files
Steven Rostedtrostedt
authored andcommitted
tracing: Replace trace_event struct array with pointer array
Currently the trace_event structures are placed in the _ftrace_events section, and at link time, the linker makes one large array of all the trace_event structures. On boot up, this array is read (much like the initcall sections) and the events are processed. The problem is that there is no guarantee that gcc will place complex structures nicely together in an array format. Two structures in the same file may be placed awkwardly, because gcc has no clue that they are suppose to be in an array. A hack was used previous to force the alignment to 4, to pack the structures together. But this caused alignment issues with other architectures (sparc). Instead of packing the structures into an array, the structures' addresses are now put into the _ftrace_event section. As pointers are always the natural alignment, gcc should always pack them tightly together (otherwise initcall, extable, etc would also fail). By having the pointers to the structures in the section, we can still iterate the trace_events without causing unnecessary alignment problems with other architectures, or depending on the current behaviour of gcc that will likely change in the future just to tick us kernel developers off a little more. The _ftrace_event section is also moved into the .init.data section as it is now only needed at boot up. Suggested-by: David Miller <davem@davemloft.net> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Acked-by: David S. Miller <davem@davemloft.net> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 9ffdc6c commit e4a9ea5

6 files changed

Lines changed: 32 additions & 29 deletions

File tree

‎include/asm-generic/vmlinux.lds.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@
124124
#endif
125125

126126
#ifdef CONFIG_EVENT_TRACING
127-
#define FTRACE_EVENTS() VMLINUX_SYMBOL(__start_ftrace_events) = .; \
127+
#define FTRACE_EVENTS() . = ALIGN(8); \
128+
VMLINUX_SYMBOL(__start_ftrace_events) = .; \
128129
*(_ftrace_events) \
129130
VMLINUX_SYMBOL(__stop_ftrace_events) = .;
130131
#else
@@ -179,9 +180,6 @@
179180
TRACE_PRINTKS() \
180181
\
181182
STRUCT_ALIGN(); \
182-
FTRACE_EVENTS() \
183-
\
184-
STRUCT_ALIGN(); \
185183
TRACE_SYSCALLS()
186184

187185
/*
@@ -482,6 +480,7 @@
482480
KERNEL_CTORS() \
483481
*(.init.rodata) \
484482
MCOUNT_REC() \
483+
FTRACE_EVENTS() \
485484
DEV_DISCARD(init.rodata) \
486485
CPU_DISCARD(init.rodata) \
487486
MEM_DISCARD(init.rodata) \

‎include/linux/module.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ struct module
389389
unsigned int num_trace_bprintk_fmt;
390390
#endif
391391
#ifdef CONFIG_EVENT_TRACING
392-
struct ftrace_event_call *trace_events;
392+
struct ftrace_event_call **trace_events;
393393
unsigned int num_trace_events;
394394
#endif
395395
#ifdef CONFIG_FTRACE_MCOUNT_RECORD

‎include/linux/syscalls.h‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,28 +128,30 @@ extern struct trace_event_functions exit_syscall_print_funcs;
128128
static struct syscall_metadata \
129129
__attribute__((__aligned__(4))) __syscall_meta_##sname; \
130130
static struct ftrace_event_call __used \
131-
__attribute__((__aligned__(4))) \
132-
__attribute__((section("_ftrace_events"))) \
133131
event_enter_##sname = { \
134132
.name = "sys_enter"#sname, \
135133
.class = &event_class_syscall_enter, \
136134
.event.funcs = &enter_syscall_print_funcs, \
137135
.data = (void *)&__syscall_meta_##sname,\
138136
}; \
137+
static struct ftrace_event_call __used \
138+
__attribute__((section("_ftrace_events"))) \
139+
*__event_enter_##sname = &event_enter_##sname; \
139140
__TRACE_EVENT_FLAGS(enter_##sname, TRACE_EVENT_FL_CAP_ANY)
140141

141142
#define SYSCALL_TRACE_EXIT_EVENT(sname) \
142143
static struct syscall_metadata \
143144
__attribute__((__aligned__(4))) __syscall_meta_##sname; \
144145
static struct ftrace_event_call __used \
145-
__attribute__((__aligned__(4))) \
146-
__attribute__((section("_ftrace_events"))) \
147146
event_exit_##sname = { \
148147
.name = "sys_exit"#sname, \
149148
.class = &event_class_syscall_exit, \
150149
.event.funcs = &exit_syscall_print_funcs, \
151150
.data = (void *)&__syscall_meta_##sname,\
152151
}; \
152+
static struct ftrace_event_call __used \
153+
__attribute__((section("_ftrace_events"))) \
154+
*__event_exit_##sname = &event_exit_##sname; \
153155
__TRACE_EVENT_FLAGS(exit_##sname, TRACE_EVENT_FL_CAP_ANY)
154156

155157
#define SYSCALL_METADATA(sname, nb) \

‎include/trace/ftrace.h‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -446,14 +446,16 @@ static inline notrace int ftrace_get_offsets_##call( \
446446
* .reg = ftrace_event_reg,
447447
* };
448448
*
449-
* static struct ftrace_event_call __used
450-
* __attribute__((__aligned__(4)))
451-
* __attribute__((section("_ftrace_events"))) event_<call> = {
449+
* static struct ftrace_event_call event_<call> = {
452450
* .name = "<call>",
453451
* .class = event_class_<template>,
454452
* .event = &ftrace_event_type_<call>,
455453
* .print_fmt = print_fmt_<call>,
456454
* };
455+
* // its only safe to use pointers when doing linker tricks to
456+
* // create an array.
457+
* static struct ftrace_event_call __used
458+
* __attribute__((section("_ftrace_events"))) *__event_<call> = &event_<call>;
457459
*
458460
*/
459461

@@ -579,28 +581,28 @@ static struct ftrace_event_class __used event_class_##call = { \
579581
#undef DEFINE_EVENT
580582
#define DEFINE_EVENT(template, call, proto, args) \
581583
\
582-
static struct ftrace_event_call __used \
583-
__attribute__((__aligned__(4))) \
584-
__attribute__((section("_ftrace_events"))) event_##call = { \
584+
static struct ftrace_event_call __used event_##call = { \
585585
.name = #call, \
586586
.class = &event_class_##template, \
587587
.event.funcs = &ftrace_event_type_funcs_##template, \
588588
.print_fmt = print_fmt_##template, \
589-
};
589+
}; \
590+
static struct ftrace_event_call __used \
591+
__attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
590592

591593
#undef DEFINE_EVENT_PRINT
592594
#define DEFINE_EVENT_PRINT(template, call, proto, args, print) \
593595
\
594596
static const char print_fmt_##call[] = print; \
595597
\
596-
static struct ftrace_event_call __used \
597-
__attribute__((__aligned__(4))) \
598-
__attribute__((section("_ftrace_events"))) event_##call = { \
598+
static struct ftrace_event_call __used event_##call = { \
599599
.name = #call, \
600600
.class = &event_class_##template, \
601601
.event.funcs = &ftrace_event_type_funcs_##call, \
602602
.print_fmt = print_fmt_##call, \
603-
}
603+
}; \
604+
static struct ftrace_event_call __used \
605+
__attribute__((section("_ftrace_events"))) *__event_##call = &event_##call
604606

605607
#include TRACE_INCLUDE(TRACE_INCLUDE_FILE)
606608

‎kernel/trace/trace_events.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1284,7 +1284,7 @@ trace_create_file_ops(struct module *mod)
12841284
static void trace_module_add_events(struct module *mod)
12851285
{
12861286
struct ftrace_module_file_ops *file_ops = NULL;
1287-
struct ftrace_event_call *call, *start, *end;
1287+
struct ftrace_event_call **call, **start, **end;
12881288

12891289
start = mod->trace_events;
12901290
end = mod->trace_events + mod->num_trace_events;
@@ -1297,7 +1297,7 @@ static void trace_module_add_events(struct module *mod)
12971297
return;
12981298

12991299
for_each_event(call, start, end) {
1300-
__trace_add_event_call(call, mod,
1300+
__trace_add_event_call(*call, mod,
13011301
&file_ops->id, &file_ops->enable,
13021302
&file_ops->filter, &file_ops->format);
13031303
}
@@ -1367,8 +1367,8 @@ static struct notifier_block trace_module_nb = {
13671367
.priority = 0,
13681368
};
13691369

1370-
extern struct ftrace_event_call __start_ftrace_events[];
1371-
extern struct ftrace_event_call __stop_ftrace_events[];
1370+
extern struct ftrace_event_call *__start_ftrace_events[];
1371+
extern struct ftrace_event_call *__stop_ftrace_events[];
13721372

13731373
static char bootup_event_buf[COMMAND_LINE_SIZE] __initdata;
13741374

@@ -1384,7 +1384,7 @@ __setup("trace_event=", setup_trace_event);
13841384

13851385
static __init int event_trace_init(void)
13861386
{
1387-
struct ftrace_event_call *call;
1387+
struct ftrace_event_call **call;
13881388
struct dentry *d_tracer;
13891389
struct dentry *entry;
13901390
struct dentry *d_events;
@@ -1430,7 +1430,7 @@ static __init int event_trace_init(void)
14301430
pr_warning("tracing: Failed to allocate common fields");
14311431

14321432
for_each_event(call, __start_ftrace_events, __stop_ftrace_events) {
1433-
__trace_add_event_call(call, NULL, &ftrace_event_id_fops,
1433+
__trace_add_event_call(*call, NULL, &ftrace_event_id_fops,
14341434
&ftrace_enable_fops,
14351435
&ftrace_event_filter_fops,
14361436
&ftrace_event_format_fops);

‎kernel/trace/trace_export.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,13 @@ struct ftrace_event_class event_class_ftrace_##call = { \
161161
.fields = LIST_HEAD_INIT(event_class_ftrace_##call.fields),\
162162
}; \
163163
\
164-
struct ftrace_event_call __used \
165-
__attribute__((__aligned__(4))) \
166-
__attribute__((section("_ftrace_events"))) event_##call = { \
164+
struct ftrace_event_call __used event_##call = { \
167165
.name = #call, \
168166
.event.type = etype, \
169167
.class = &event_class_ftrace_##call, \
170168
.print_fmt = print, \
171169
}; \
170+
struct ftrace_event_call __used \
171+
__attribute__((section("_ftrace_events"))) *__event_##call = &event_##call;
172172

173173
#include "trace_entries.h"

0 commit comments

Comments
 (0)