Skip to content

Commit e2ace00

Browse files
amlutorostedt
authored andcommitted
tracing: Choose static tp_printk buffer by explicit nesting count
Currently, the trace_printk code chooses which static buffer to use based on what type of atomic context (NMI, IRQ, etc) it's in. Simplify the code and make it more robust: simply count the nesting depth and choose a buffer based on the current nesting depth. The new code will only drop an event if we nest more than 4 deep, and the old code was guaranteed to malfunction if that happened. Link: http://lkml.kernel.org/r/07ab03aecfba25fcce8f9a211b14c9c5e2865c58.1464289095.git.luto@kernel.org Acked-by: Namhyung Kim <namhyung@kernel.org> Signed-off-by: Andy Lutomirski <luto@kernel.org> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 35abb67 commit e2ace00

1 file changed

Lines changed: 24 additions & 59 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 24 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2339,83 +2339,41 @@ static void __trace_userstack(struct trace_array *tr, unsigned long flags)
23392339

23402340
/* created for use with alloc_percpu */
23412341
struct trace_buffer_struct {
2342-
char buffer[TRACE_BUF_SIZE];
2342+
int nesting;
2343+
char buffer[4][TRACE_BUF_SIZE];
23432344
};
23442345

23452346
static struct trace_buffer_struct *trace_percpu_buffer;
2346-
static struct trace_buffer_struct *trace_percpu_sirq_buffer;
2347-
static struct trace_buffer_struct *trace_percpu_irq_buffer;
2348-
static struct trace_buffer_struct *trace_percpu_nmi_buffer;
23492347

23502348
/*
2351-
* The buffer used is dependent on the context. There is a per cpu
2352-
* buffer for normal context, softirq contex, hard irq context and
2353-
* for NMI context. Thise allows for lockless recording.
2354-
*
2355-
* Note, if the buffers failed to be allocated, then this returns NULL
2349+
* Thise allows for lockless recording. If we're nested too deeply, then
2350+
* this returns NULL.
23562351
*/
23572352
static char *get_trace_buf(void)
23582353
{
2359-
struct trace_buffer_struct *percpu_buffer;
2360-
2361-
/*
2362-
* If we have allocated per cpu buffers, then we do not
2363-
* need to do any locking.
2364-
*/
2365-
if (in_nmi())
2366-
percpu_buffer = trace_percpu_nmi_buffer;
2367-
else if (in_irq())
2368-
percpu_buffer = trace_percpu_irq_buffer;
2369-
else if (in_softirq())
2370-
percpu_buffer = trace_percpu_sirq_buffer;
2371-
else
2372-
percpu_buffer = trace_percpu_buffer;
2354+
struct trace_buffer_struct *buffer = this_cpu_ptr(trace_percpu_buffer);
23732355

2374-
if (!percpu_buffer)
2356+
if (!buffer || buffer->nesting >= 4)
23752357
return NULL;
23762358

2377-
return this_cpu_ptr(&percpu_buffer->buffer[0]);
2359+
return &buffer->buffer[buffer->nesting++][0];
2360+
}
2361+
2362+
static void put_trace_buf(void)
2363+
{
2364+
this_cpu_dec(trace_percpu_buffer->nesting);
23782365
}
23792366

23802367
static int alloc_percpu_trace_buffer(void)
23812368
{
23822369
struct trace_buffer_struct *buffers;
2383-
struct trace_buffer_struct *sirq_buffers;
2384-
struct trace_buffer_struct *irq_buffers;
2385-
struct trace_buffer_struct *nmi_buffers;
23862370

23872371
buffers = alloc_percpu(struct trace_buffer_struct);
2388-
if (!buffers)
2389-
goto err_warn;
2390-
2391-
sirq_buffers = alloc_percpu(struct trace_buffer_struct);
2392-
if (!sirq_buffers)
2393-
goto err_sirq;
2394-
2395-
irq_buffers = alloc_percpu(struct trace_buffer_struct);
2396-
if (!irq_buffers)
2397-
goto err_irq;
2398-
2399-
nmi_buffers = alloc_percpu(struct trace_buffer_struct);
2400-
if (!nmi_buffers)
2401-
goto err_nmi;
2372+
if (WARN(!buffers, "Could not allocate percpu trace_printk buffer"))
2373+
return -ENOMEM;
24022374

24032375
trace_percpu_buffer = buffers;
2404-
trace_percpu_sirq_buffer = sirq_buffers;
2405-
trace_percpu_irq_buffer = irq_buffers;
2406-
trace_percpu_nmi_buffer = nmi_buffers;
2407-
24082376
return 0;
2409-
2410-
err_nmi:
2411-
free_percpu(irq_buffers);
2412-
err_irq:
2413-
free_percpu(sirq_buffers);
2414-
err_sirq:
2415-
free_percpu(buffers);
2416-
err_warn:
2417-
WARN(1, "Could not allocate percpu trace_printk buffer");
2418-
return -ENOMEM;
24192377
}
24202378

24212379
static int buffers_allocated;
@@ -2506,7 +2464,7 @@ int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
25062464
tbuffer = get_trace_buf();
25072465
if (!tbuffer) {
25082466
len = 0;
2509-
goto out;
2467+
goto out_nobuffer;
25102468
}
25112469

25122470
len = vbin_printf((u32 *)tbuffer, TRACE_BUF_SIZE/sizeof(int), fmt, args);
@@ -2532,6 +2490,9 @@ int trace_vbprintk(unsigned long ip, const char *fmt, va_list args)
25322490
}
25332491

25342492
out:
2493+
put_trace_buf();
2494+
2495+
out_nobuffer:
25352496
preempt_enable_notrace();
25362497
unpause_graph_tracing();
25372498

@@ -2563,7 +2524,7 @@ __trace_array_vprintk(struct ring_buffer *buffer,
25632524
tbuffer = get_trace_buf();
25642525
if (!tbuffer) {
25652526
len = 0;
2566-
goto out;
2527+
goto out_nobuffer;
25672528
}
25682529

25692530
len = vscnprintf(tbuffer, TRACE_BUF_SIZE, fmt, args);
@@ -2582,7 +2543,11 @@ __trace_array_vprintk(struct ring_buffer *buffer,
25822543
__buffer_unlock_commit(buffer, event);
25832544
ftrace_trace_stack(&global_trace, buffer, flags, 6, pc, NULL);
25842545
}
2585-
out:
2546+
2547+
out:
2548+
put_trace_buf();
2549+
2550+
out_nobuffer:
25862551
preempt_enable_notrace();
25872552
unpause_graph_tracing();
25882553

0 commit comments

Comments
 (0)