Skip to content

Commit 769b044

Browse files
fweisbecIngo Molnar
authored andcommitted
tracing/core: drop the old trace_printk() implementation in favour of trace_bprintk()
Impact: faster and lighter tracing Now that we have trace_bprintk() which is faster and consume lesser memory than trace_printk() and has the same purpose, we can now drop the old implementation in favour of the binary one from trace_bprintk(), which means we move all the implementation of trace_bprintk() to trace_printk(), so the Api doesn't change except that we must now use trace_seq_bprintk() to print the TRACE_PRINT entries. Some changes result of this: - Previously, trace_bprintk depended of a single tracer and couldn't work without. This tracer has been dropped and the whole implementation of trace_printk() (like the module formats management) is now integrated in the tracing core (comes with CONFIG_TRACING), though we keep the file trace_printk (previously trace_bprintk.c) where we can find the module management. Thus we don't overflow trace.c - changes some parts to use trace_seq_bprintk() to print TRACE_PRINT entries. - change a bit trace_printk/trace_vprintk macros to support non-builtin formats constants, and fix 'const' qualifiers warnings. But this is all transparent for developers. - etc... V2: - Rebase against last changes - Fix mispell on the changelog V3: - Rebase against last changes (moving trace_printk() to kernel.h) Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Acked-by: Steven Rostedt <rostedt@goodmis.org> LKML-Reference: <1236356510-8381-5-git-send-email-fweisbec@gmail.com> Signed-off-by: Ingo Molnar <mingo@elte.hu>
1 parent 1ba28e0 commit 769b044

12 files changed

Lines changed: 158 additions & 309 deletions

File tree

‎include/linux/ftrace.h‎

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -223,31 +223,6 @@ extern int ftrace_make_nop(struct module *mod,
223223
*/
224224
extern int ftrace_make_call(struct dyn_ftrace *rec, unsigned long addr);
225225

226-
#ifdef CONFIG_TRACE_BPRINTK
227-
extern int trace_vbprintk(unsigned long ip, const char *fmt, va_list args);
228-
extern int __trace_bprintk(unsigned long ip, const char *fmt, ...)
229-
__attribute__ ((format (printf, 2, 3)));
230-
231-
static inline void ____trace_bprintk_check_format(const char *fmt, ...)
232-
__attribute__ ((format (printf, 1, 2)));
233-
static inline void ____trace_bprintk_check_format(const char *fmt, ...) {}
234-
#define __trace_bprintk_check_format(fmt, args...) \
235-
do { \
236-
if (0) \
237-
____trace_bprintk_check_format(fmt, ##args); \
238-
} while (0)
239-
240-
#define trace_bprintk(fmt, args...) \
241-
do { \
242-
static char *__attribute__((section("__trace_bprintk_fmt"))) \
243-
trace_bprintk_fmt = fmt; \
244-
__trace_bprintk_check_format(fmt, ##args); \
245-
__trace_bprintk(_THIS_IP_, trace_bprintk_fmt, ##args); \
246-
} while (0)
247-
#else
248-
#define trace_bprintk trace_printk
249-
#endif
250-
251226
/* May be defined in arch */
252227
extern int ftrace_arch_read_dyn_info(char *buf, int size);
253228

‎include/linux/kernel.h‎

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,16 @@ extern void ftrace_off_permanent(void);
423423
extern void
424424
ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
425425

426+
static inline void __attribute__ ((format (printf, 1, 2)))
427+
____trace_printk_check_format(const char *fmt, ...)
428+
{
429+
}
430+
#define __trace_printk_check_format(fmt, args...) \
431+
do { \
432+
if (0) \
433+
____trace_printk_check_format(fmt, ##args); \
434+
} while (0)
435+
426436
/**
427437
* trace_printk - printf formatting in the ftrace buffer
428438
* @fmt: the printf format for printing
@@ -439,13 +449,31 @@ ftrace_special(unsigned long arg1, unsigned long arg2, unsigned long arg3);
439449
* Please refrain from leaving trace_printks scattered around in
440450
* your code.
441451
*/
442-
# define trace_printk(fmt...) __trace_printk(_THIS_IP_, fmt)
452+
453+
#define trace_printk(fmt, args...) \
454+
do { \
455+
static const char *trace_printk_fmt \
456+
__attribute__((section("__trace_printk_fmt"))); \
457+
trace_printk_fmt = fmt; \
458+
__trace_printk_check_format(fmt, ##args); \
459+
__trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \
460+
} while (0)
461+
443462
extern int
444463
__trace_printk(unsigned long ip, const char *fmt, ...)
445464
__attribute__ ((format (printf, 2, 3)));
446-
# define ftrace_vprintk(fmt, ap) __trace_printk(_THIS_IP_, fmt, ap)
465+
466+
#define ftrace_vprintk(fmt, vargs) \
467+
do { \
468+
static const char *trace_printk_fmt \
469+
__attribute__((section("__trace_printk_fmt"))); \
470+
trace_printk_fmt = fmt; \
471+
__ftrace_vprintk(_THIS_IP_, trace_printk_fmt, vargs); \
472+
} while (0)
473+
447474
extern int
448475
__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
476+
449477
extern void ftrace_dump(void);
450478
#else
451479
static inline void
@@ -467,7 +495,7 @@ ftrace_vprintk(const char *fmt, va_list ap)
467495
return 0;
468496
}
469497
static inline void ftrace_dump(void) { }
470-
#endif
498+
#endif /* CONFIG_TRACING */
471499

472500
/*
473501
* Display an IP address in readable format.

‎include/linux/module.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ struct module
329329
unsigned int num_tracepoints;
330330
#endif
331331

332-
#ifdef CONFIG_TRACE_BPRINTK
332+
#ifdef CONFIG_TRACING
333333
const char **trace_bprintk_fmt_start;
334334
unsigned int num_trace_bprintk_fmt;
335335
#endif

‎kernel/trace/Kconfig‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ config TRACING
5252
select STACKTRACE if STACKTRACE_SUPPORT
5353
select TRACEPOINTS
5454
select NOP_TRACER
55+
select BINARY_PRINTF
5556

5657
#
5758
# Minimum requirements an architecture has to meet for us to
@@ -97,12 +98,6 @@ config FUNCTION_GRAPH_TRACER
9798
This is done by setting the current return address on the current
9899
task structure into a stack of calls.
99100

100-
config TRACE_BPRINTK
101-
bool "Binary printk for tracing"
102-
default y
103-
depends on TRACING
104-
select BINARY_PRINTF
105-
106101
config IRQSOFF_TRACER
107102
bool "Interrupts-off Latency Tracer"
108103
default n

‎kernel/trace/Makefile‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ obj-$(CONFIG_TRACING) += trace.o
2222
obj-$(CONFIG_TRACING) += trace_clock.o
2323
obj-$(CONFIG_TRACING) += trace_output.o
2424
obj-$(CONFIG_TRACING) += trace_stat.o
25-
obj-$(CONFIG_TRACE_BPRINTK) += trace_bprintk.o
25+
obj-$(CONFIG_TRACING) += trace_printk.o
2626
obj-$(CONFIG_CONTEXT_SWITCH_TRACER) += trace_sched_switch.o
2727
obj-$(CONFIG_SYSPROF_TRACER) += trace_sysprof.o
2828
obj-$(CONFIG_FUNCTION_TRACER) += trace_functions.o

‎kernel/trace/trace.c‎

Lines changed: 62 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1169,6 +1169,67 @@ void trace_graph_return(struct ftrace_graph_ret *trace)
11691169
}
11701170
#endif /* CONFIG_FUNCTION_GRAPH_TRACER */
11711171

1172+
1173+
/**
1174+
* trace_vprintk - write binary msg to tracing buffer
1175+
*
1176+
*/
1177+
int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1178+
{
1179+
static DEFINE_SPINLOCK(trace_buf_lock);
1180+
static u32 trace_buf[TRACE_BUF_SIZE];
1181+
1182+
struct ring_buffer_event *event;
1183+
struct trace_array *tr = &global_trace;
1184+
struct trace_array_cpu *data;
1185+
struct print_entry *entry;
1186+
unsigned long flags;
1187+
int resched;
1188+
int cpu, len = 0, size, pc;
1189+
1190+
if (unlikely(tracing_selftest_running || tracing_disabled))
1191+
return 0;
1192+
1193+
/* Don't pollute graph traces with trace_vprintk internals */
1194+
pause_graph_tracing();
1195+
1196+
pc = preempt_count();
1197+
resched = ftrace_preempt_disable();
1198+
cpu = raw_smp_processor_id();
1199+
data = tr->data[cpu];
1200+
1201+
if (unlikely(atomic_read(&data->disabled)))
1202+
goto out;
1203+
1204+
spin_lock_irqsave(&trace_buf_lock, flags);
1205+
len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1206+
1207+
if (len > TRACE_BUF_SIZE || len < 0)
1208+
goto out_unlock;
1209+
1210+
size = sizeof(*entry) + sizeof(u32) * len;
1211+
event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
1212+
if (!event)
1213+
goto out_unlock;
1214+
entry = ring_buffer_event_data(event);
1215+
entry->ip = ip;
1216+
entry->depth = depth;
1217+
entry->fmt = fmt;
1218+
1219+
memcpy(entry->buf, trace_buf, sizeof(u32) * len);
1220+
ring_buffer_unlock_commit(tr->buffer, event);
1221+
1222+
out_unlock:
1223+
spin_unlock_irqrestore(&trace_buf_lock, flags);
1224+
1225+
out:
1226+
ftrace_preempt_enable(resched);
1227+
unpause_graph_tracing();
1228+
1229+
return len;
1230+
}
1231+
EXPORT_SYMBOL_GPL(trace_vprintk);
1232+
11721233
enum trace_file_type {
11731234
TRACE_FILE_LAT_FMT = 1,
11741235
TRACE_FILE_ANNOTATE = 2,
@@ -1564,7 +1625,7 @@ static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
15641625

15651626
trace_assign_type(field, entry);
15661627

1567-
ret = trace_seq_printf(s, "%s", field->buf);
1628+
ret = trace_seq_bprintf(s, field->fmt, field->buf);
15681629
if (!ret)
15691630
return TRACE_TYPE_PARTIAL_LINE;
15701631

@@ -3714,155 +3775,6 @@ static __init int tracer_init_debugfs(void)
37143775
return 0;
37153776
}
37163777

3717-
int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
3718-
{
3719-
static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
3720-
static char trace_buf[TRACE_BUF_SIZE];
3721-
3722-
struct ring_buffer_event *event;
3723-
struct trace_array *tr = &global_trace;
3724-
struct trace_array_cpu *data;
3725-
int cpu, len = 0, size, pc;
3726-
struct print_entry *entry;
3727-
unsigned long irq_flags;
3728-
3729-
if (tracing_disabled || tracing_selftest_running)
3730-
return 0;
3731-
3732-
pc = preempt_count();
3733-
preempt_disable_notrace();
3734-
cpu = raw_smp_processor_id();
3735-
data = tr->data[cpu];
3736-
3737-
if (unlikely(atomic_read(&data->disabled)))
3738-
goto out;
3739-
3740-
pause_graph_tracing();
3741-
raw_local_irq_save(irq_flags);
3742-
__raw_spin_lock(&trace_buf_lock);
3743-
len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3744-
3745-
len = min(len, TRACE_BUF_SIZE-1);
3746-
trace_buf[len] = 0;
3747-
3748-
size = sizeof(*entry) + len + 1;
3749-
event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
3750-
if (!event)
3751-
goto out_unlock;
3752-
entry = ring_buffer_event_data(event);
3753-
entry->ip = ip;
3754-
entry->depth = depth;
3755-
3756-
memcpy(&entry->buf, trace_buf, len);
3757-
entry->buf[len] = 0;
3758-
ring_buffer_unlock_commit(tr->buffer, event);
3759-
3760-
out_unlock:
3761-
__raw_spin_unlock(&trace_buf_lock);
3762-
raw_local_irq_restore(irq_flags);
3763-
unpause_graph_tracing();
3764-
out:
3765-
preempt_enable_notrace();
3766-
3767-
return len;
3768-
}
3769-
EXPORT_SYMBOL_GPL(trace_vprintk);
3770-
3771-
int __trace_printk(unsigned long ip, const char *fmt, ...)
3772-
{
3773-
int ret;
3774-
va_list ap;
3775-
3776-
if (!(trace_flags & TRACE_ITER_PRINTK))
3777-
return 0;
3778-
3779-
va_start(ap, fmt);
3780-
ret = trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3781-
va_end(ap);
3782-
return ret;
3783-
}
3784-
EXPORT_SYMBOL_GPL(__trace_printk);
3785-
3786-
int __ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap)
3787-
{
3788-
if (!(trace_flags & TRACE_ITER_PRINTK))
3789-
return 0;
3790-
3791-
return trace_vprintk(ip, task_curr_ret_stack(current), fmt, ap);
3792-
}
3793-
EXPORT_SYMBOL_GPL(__ftrace_vprintk);
3794-
3795-
/**
3796-
* trace_vbprintk - write binary msg to tracing buffer
3797-
*
3798-
* Caller must insure @fmt are valid when msg is in tracing buffer.
3799-
*/
3800-
int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
3801-
{
3802-
static DEFINE_SPINLOCK(trace_buf_lock);
3803-
static u32 trace_buf[TRACE_BUF_SIZE];
3804-
3805-
struct ring_buffer_event *event;
3806-
struct trace_array *tr = &global_trace;
3807-
struct trace_array_cpu *data;
3808-
struct bprintk_entry *entry;
3809-
unsigned long flags;
3810-
int resched;
3811-
int cpu, len = 0, size, pc;
3812-
3813-
if (tracing_disabled || !trace_bprintk_enable)
3814-
return 0;
3815-
3816-
pc = preempt_count();
3817-
resched = ftrace_preempt_disable();
3818-
cpu = raw_smp_processor_id();
3819-
data = tr->data[cpu];
3820-
3821-
if (unlikely(atomic_read(&data->disabled)))
3822-
goto out;
3823-
3824-
spin_lock_irqsave(&trace_buf_lock, flags);
3825-
len = vbin_printf(trace_buf, TRACE_BUF_SIZE, fmt, args);
3826-
3827-
if (len > TRACE_BUF_SIZE || len < 0)
3828-
goto out_unlock;
3829-
3830-
size = sizeof(*entry) + sizeof(u32) * len;
3831-
event = trace_buffer_lock_reserve(tr, TRACE_BPRINTK, size, flags, pc);
3832-
if (!event)
3833-
goto out_unlock;
3834-
entry = ring_buffer_event_data(event);
3835-
entry->ip = ip;
3836-
entry->fmt = fmt;
3837-
3838-
memcpy(entry->buf, trace_buf, sizeof(u32) * len);
3839-
ring_buffer_unlock_commit(tr->buffer, event);
3840-
3841-
out_unlock:
3842-
spin_unlock_irqrestore(&trace_buf_lock, flags);
3843-
3844-
out:
3845-
ftrace_preempt_enable(resched);
3846-
3847-
return len;
3848-
}
3849-
EXPORT_SYMBOL_GPL(trace_vbprintk);
3850-
3851-
int __trace_bprintk(unsigned long ip, const char *fmt, ...)
3852-
{
3853-
int ret;
3854-
va_list ap;
3855-
3856-
if (!fmt)
3857-
return 0;
3858-
3859-
va_start(ap, fmt);
3860-
ret = trace_vbprintk(ip, fmt, ap);
3861-
va_end(ap);
3862-
return ret;
3863-
}
3864-
EXPORT_SYMBOL_GPL(__trace_bprintk);
3865-
38663778
static int trace_panic_handler(struct notifier_block *this,
38673779
unsigned long event, void *unused)
38683780
{

‎kernel/trace/trace.h‎

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ enum trace_type {
2020
TRACE_WAKE,
2121
TRACE_STACK,
2222
TRACE_PRINT,
23-
TRACE_BPRINTK,
2423
TRACE_SPECIAL,
2524
TRACE_MMIO_RW,
2625
TRACE_MMIO_MAP,
@@ -120,16 +119,10 @@ struct userstack_entry {
120119
*/
121120
struct print_entry {
122121
struct trace_entry ent;
123-
unsigned long ip;
122+
unsigned long ip;
124123
int depth;
125-
char buf[];
126-
};
127-
128-
struct bprintk_entry {
129-
struct trace_entry ent;
130-
unsigned long ip;
131-
const char *fmt;
132-
u32 buf[];
124+
const char *fmt;
125+
u32 buf[];
133126
};
134127
#ifdef CONFIG_TRACE_BPRINTK
135128
extern int trace_bprintk_enable;
@@ -296,7 +289,6 @@ extern void __ftrace_bad_type(void);
296289
IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
297290
IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
298291
IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
299-
IF_ASSIGN(var, ent, struct bprintk_entry, TRACE_BPRINTK);\
300292
IF_ASSIGN(var, ent, struct special_entry, 0); \
301293
IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
302294
TRACE_MMIO_RW); \

0 commit comments

Comments
 (0)