Skip to content

Commit 48ead02

Browse files
fweisbecSteven Rostedt
authored andcommitted
tracing/core: bring back raw trace_printk for dynamic formats strings
Impact: fix callsites with dynamic format strings Since its new binary implementation, trace_printk() internally uses static containers for the format strings on each callsites. But the value is assigned once at build time, which means that it can't take dynamic formats. So this patch unearthes the raw trace_printk implementation for the callers that will need trace_printk to be able to carry these dynamic format strings. The trace_printk() macro will use the appropriate implementation for each callsite. Most of the time however, the binary implementation will still be used. The other impact of this patch is that mmiotrace_printk() will use the old implementation because it calls the low level trace_vprintk and we can't guess here whether the format passed in it is dynamic or not. Some parts of this patch have been written by Steven Rostedt (most notably the part that chooses the appropriate implementation for each callsites). Signed-off-by: Frederic Weisbecker <fweisbec@gmail.com> Signed-off-by: Steven Rostedt <srostedt@redhat.com>
1 parent db526ca commit 48ead02

8 files changed

Lines changed: 213 additions & 39 deletions

File tree

‎include/linux/kernel.h‎

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -452,31 +452,45 @@ do { \
452452

453453
#define trace_printk(fmt, args...) \
454454
do { \
455-
static const char *trace_printk_fmt \
456-
__attribute__((section("__trace_printk_fmt"))); \
457-
\
458-
if (!trace_printk_fmt) \
459-
trace_printk_fmt = fmt; \
460-
\
461455
__trace_printk_check_format(fmt, ##args); \
462-
__trace_printk(_THIS_IP_, trace_printk_fmt, ##args); \
456+
if (__builtin_constant_p(fmt)) { \
457+
static const char *trace_printk_fmt \
458+
__attribute__((section("__trace_printk_fmt"))) = \
459+
__builtin_constant_p(fmt) ? fmt : NULL; \
460+
\
461+
__trace_bprintk(_THIS_IP_, trace_printk_fmt, ##args); \
462+
} else \
463+
__trace_printk(_THIS_IP_, fmt, ##args); \
463464
} while (0)
464465

466+
extern int
467+
__trace_bprintk(unsigned long ip, const char *fmt, ...)
468+
__attribute__ ((format (printf, 2, 3)));
469+
465470
extern int
466471
__trace_printk(unsigned long ip, const char *fmt, ...)
467472
__attribute__ ((format (printf, 2, 3)));
468473

474+
/*
475+
* The double __builtin_constant_p is because gcc will give us an error
476+
* if we try to allocate the static variable to fmt if it is not a
477+
* constant. Even with the outer if statement.
478+
*/
469479
#define ftrace_vprintk(fmt, vargs) \
470480
do { \
471-
static const char *trace_printk_fmt \
472-
__attribute__((section("__trace_printk_fmt"))); \
473-
\
474-
if (!trace_printk_fmt) \
475-
trace_printk_fmt = fmt; \
481+
if (__builtin_constant_p(fmt)) { \
482+
static const char *trace_printk_fmt \
483+
__attribute__((section("__trace_printk_fmt"))) = \
484+
__builtin_constant_p(fmt) ? fmt : NULL; \
476485
\
477-
__ftrace_vprintk(_THIS_IP_, trace_printk_fmt, vargs); \
486+
__ftrace_vbprintk(_THIS_IP_, trace_printk_fmt, vargs); \
487+
} else \
488+
__ftrace_vprintk(_THIS_IP_, fmt, vargs); \
478489
} while (0)
479490

491+
extern int
492+
__ftrace_vbprintk(unsigned long ip, const char *fmt, va_list ap);
493+
480494
extern int
481495
__ftrace_vprintk(unsigned long ip, const char *fmt, va_list ap);
482496

‎kernel/trace/trace.c‎

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1179,10 +1179,10 @@ void trace_graph_return(struct ftrace_graph_ret *trace)
11791179

11801180

11811181
/**
1182-
* trace_vprintk - write binary msg to tracing buffer
1182+
* trace_vbprintk - write binary msg to tracing buffer
11831183
*
11841184
*/
1185-
int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1185+
int trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args)
11861186
{
11871187
static raw_spinlock_t trace_buf_lock =
11881188
(raw_spinlock_t)__RAW_SPIN_LOCK_UNLOCKED;
@@ -1191,7 +1191,7 @@ int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
11911191
struct ring_buffer_event *event;
11921192
struct trace_array *tr = &global_trace;
11931193
struct trace_array_cpu *data;
1194-
struct print_entry *entry;
1194+
struct bprint_entry *entry;
11951195
unsigned long flags;
11961196
int resched;
11971197
int cpu, len = 0, size, pc;
@@ -1219,7 +1219,7 @@ int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
12191219
goto out_unlock;
12201220

12211221
size = sizeof(*entry) + sizeof(u32) * len;
1222-
event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, flags, pc);
1222+
event = trace_buffer_lock_reserve(tr, TRACE_BPRINT, size, flags, pc);
12231223
if (!event)
12241224
goto out_unlock;
12251225
entry = ring_buffer_event_data(event);
@@ -1240,6 +1240,60 @@ int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
12401240

12411241
return len;
12421242
}
1243+
EXPORT_SYMBOL_GPL(trace_vbprintk);
1244+
1245+
int trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args)
1246+
{
1247+
static raw_spinlock_t trace_buf_lock = __RAW_SPIN_LOCK_UNLOCKED;
1248+
static char trace_buf[TRACE_BUF_SIZE];
1249+
1250+
struct ring_buffer_event *event;
1251+
struct trace_array *tr = &global_trace;
1252+
struct trace_array_cpu *data;
1253+
int cpu, len = 0, size, pc;
1254+
struct print_entry *entry;
1255+
unsigned long irq_flags;
1256+
1257+
if (tracing_disabled || tracing_selftest_running)
1258+
return 0;
1259+
1260+
pc = preempt_count();
1261+
preempt_disable_notrace();
1262+
cpu = raw_smp_processor_id();
1263+
data = tr->data[cpu];
1264+
1265+
if (unlikely(atomic_read(&data->disabled)))
1266+
goto out;
1267+
1268+
pause_graph_tracing();
1269+
raw_local_irq_save(irq_flags);
1270+
__raw_spin_lock(&trace_buf_lock);
1271+
len = vsnprintf(trace_buf, TRACE_BUF_SIZE, fmt, args);
1272+
1273+
len = min(len, TRACE_BUF_SIZE-1);
1274+
trace_buf[len] = 0;
1275+
1276+
size = sizeof(*entry) + len + 1;
1277+
event = trace_buffer_lock_reserve(tr, TRACE_PRINT, size, irq_flags, pc);
1278+
if (!event)
1279+
goto out_unlock;
1280+
entry = ring_buffer_event_data(event);
1281+
entry->ip = ip;
1282+
entry->depth = depth;
1283+
1284+
memcpy(&entry->buf, trace_buf, len);
1285+
entry->buf[len] = 0;
1286+
ring_buffer_unlock_commit(tr->buffer, event);
1287+
1288+
out_unlock:
1289+
__raw_spin_unlock(&trace_buf_lock);
1290+
raw_local_irq_restore(irq_flags);
1291+
unpause_graph_tracing();
1292+
out:
1293+
preempt_enable_notrace();
1294+
1295+
return len;
1296+
}
12431297
EXPORT_SYMBOL_GPL(trace_vprintk);
12441298

12451299
enum trace_file_type {
@@ -1628,6 +1682,22 @@ static enum print_line_t print_hex_fmt(struct trace_iterator *iter)
16281682
return TRACE_TYPE_HANDLED;
16291683
}
16301684

1685+
static enum print_line_t print_bprintk_msg_only(struct trace_iterator *iter)
1686+
{
1687+
struct trace_seq *s = &iter->seq;
1688+
struct trace_entry *entry = iter->ent;
1689+
struct bprint_entry *field;
1690+
int ret;
1691+
1692+
trace_assign_type(field, entry);
1693+
1694+
ret = trace_seq_bprintf(s, field->fmt, field->buf);
1695+
if (!ret)
1696+
return TRACE_TYPE_PARTIAL_LINE;
1697+
1698+
return TRACE_TYPE_HANDLED;
1699+
}
1700+
16311701
static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
16321702
{
16331703
struct trace_seq *s = &iter->seq;
@@ -1637,7 +1707,7 @@ static enum print_line_t print_printk_msg_only(struct trace_iterator *iter)
16371707

16381708
trace_assign_type(field, entry);
16391709

1640-
ret = trace_seq_bprintf(s, field->fmt, field->buf);
1710+
ret = trace_seq_printf(s, "%s", field->buf);
16411711
if (!ret)
16421712
return TRACE_TYPE_PARTIAL_LINE;
16431713

@@ -1702,6 +1772,11 @@ static enum print_line_t print_trace_line(struct trace_iterator *iter)
17021772
return ret;
17031773
}
17041774

1775+
if (iter->ent->type == TRACE_BPRINT &&
1776+
trace_flags & TRACE_ITER_PRINTK &&
1777+
trace_flags & TRACE_ITER_PRINTK_MSGONLY)
1778+
return print_bprintk_msg_only(iter);
1779+
17051780
if (iter->ent->type == TRACE_PRINT &&
17061781
trace_flags & TRACE_ITER_PRINTK &&
17071782
trace_flags & TRACE_ITER_PRINTK_MSGONLY)

‎kernel/trace/trace.h‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ enum trace_type {
2020
TRACE_WAKE,
2121
TRACE_STACK,
2222
TRACE_PRINT,
23+
TRACE_BPRINT,
2324
TRACE_SPECIAL,
2425
TRACE_MMIO_RW,
2526
TRACE_MMIO_MAP,
@@ -117,14 +118,21 @@ struct userstack_entry {
117118
/*
118119
* trace_printk entry:
119120
*/
120-
struct print_entry {
121+
struct bprint_entry {
121122
struct trace_entry ent;
122123
unsigned long ip;
123124
int depth;
124125
const char *fmt;
125126
u32 buf[];
126127
};
127128

129+
struct print_entry {
130+
struct trace_entry ent;
131+
unsigned long ip;
132+
int depth;
133+
char buf[];
134+
};
135+
128136
#define TRACE_OLD_SIZE 88
129137

130138
struct trace_field_cont {
@@ -286,6 +294,7 @@ extern void __ftrace_bad_type(void);
286294
IF_ASSIGN(var, ent, struct stack_entry, TRACE_STACK); \
287295
IF_ASSIGN(var, ent, struct userstack_entry, TRACE_USER_STACK);\
288296
IF_ASSIGN(var, ent, struct print_entry, TRACE_PRINT); \
297+
IF_ASSIGN(var, ent, struct bprint_entry, TRACE_BPRINT); \
289298
IF_ASSIGN(var, ent, struct special_entry, 0); \
290299
IF_ASSIGN(var, ent, struct trace_mmiotrace_rw, \
291300
TRACE_MMIO_RW); \
@@ -570,6 +579,8 @@ extern int trace_selftest_startup_branch(struct tracer *trace,
570579
extern void *head_page(struct trace_array_cpu *data);
571580
extern long ns2usecs(cycle_t nsec);
572581
extern int
582+
trace_vbprintk(unsigned long ip, int depth, const char *fmt, va_list args);
583+
extern int
573584
trace_vprintk(unsigned long ip, int depth, const char *fmt, va_list args);
574585

575586
extern unsigned long trace_flags;

‎kernel/trace/trace_event_types.h‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ TRACE_EVENT_FORMAT(user_stack, TRACE_USER_STACK, userstack_entry, ignore,
102102
"\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n\t=> (%08lx)\n")
103103
);
104104

105-
TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore,
105+
TRACE_EVENT_FORMAT(bprint, TRACE_PRINT, bprint_entry, ignore,
106106
TRACE_STRUCT(
107107
TRACE_FIELD(unsigned long, ip, ip)
108108
TRACE_FIELD(unsigned int, depth, depth)
@@ -112,6 +112,15 @@ TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore,
112112
TP_RAW_FMT("%08lx (%d) fmt:%p %s")
113113
);
114114

115+
TRACE_EVENT_FORMAT(print, TRACE_PRINT, print_entry, ignore,
116+
TRACE_STRUCT(
117+
TRACE_FIELD(unsigned long, ip, ip)
118+
TRACE_FIELD(unsigned int, depth, depth)
119+
TRACE_FIELD_ZERO_CHAR(buf)
120+
),
121+
TP_RAW_FMT("%08lx (%d) fmt:%p %s")
122+
);
123+
115124
TRACE_EVENT_FORMAT(branch, TRACE_BRANCH, trace_branch, ignore,
116125
TRACE_STRUCT(
117126
TRACE_FIELD(unsigned int, line, line)

‎kernel/trace/trace_functions_graph.c‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ print_graph_return(struct ftrace_graph_ret *trace, struct trace_seq *s,
684684
}
685685

686686
static enum print_line_t
687-
print_graph_comment(struct print_entry *trace, struct trace_seq *s,
687+
print_graph_comment(struct bprint_entry *trace, struct trace_seq *s,
688688
struct trace_entry *ent, struct trace_iterator *iter)
689689
{
690690
int i;
@@ -781,8 +781,8 @@ print_graph_function(struct trace_iterator *iter)
781781
trace_assign_type(field, entry);
782782
return print_graph_return(&field->ret, s, entry, iter);
783783
}
784-
case TRACE_PRINT: {
785-
struct print_entry *field;
784+
case TRACE_BPRINT: {
785+
struct bprint_entry *field;
786786
trace_assign_type(field, entry);
787787
return print_graph_comment(field, s, entry, iter);
788788
}

‎kernel/trace/trace_mmiotrace.c‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,18 +254,15 @@ static enum print_line_t mmio_print_mark(struct trace_iterator *iter)
254254
{
255255
struct trace_entry *entry = iter->ent;
256256
struct print_entry *print = (struct print_entry *)entry;
257+
const char *msg = print->buf;
257258
struct trace_seq *s = &iter->seq;
258259
unsigned long long t = ns2usecs(iter->ts);
259260
unsigned long usec_rem = do_div(t, USEC_PER_SEC);
260261
unsigned secs = (unsigned long)t;
261262
int ret;
262263

263264
/* The trailing newline must be in the message. */
264-
ret = trace_seq_printf(s, "MARK %u.%06lu ", secs, usec_rem);
265-
if (!ret)
266-
return TRACE_TYPE_PARTIAL_LINE;
267-
268-
ret = trace_seq_bprintf(s, print->fmt, print->buf);
265+
ret = trace_seq_printf(s, "MARK %u.%06lu %s", secs, usec_rem, msg);
269266
if (!ret)
270267
return TRACE_TYPE_PARTIAL_LINE;
271268

0 commit comments

Comments
 (0)