Skip to content

Commit 5322ea5

Browse files
Peter Zijlstraingomolnar
authored andcommitted
perf/ring-buffer: Use regular variables for nesting
While the IRQ/NMI will nest, the nest-count will be invariant over the actual exception, since it will decrement equal to increment. This means we can -- carefully -- use a regular variable since the typical LOAD-STORE race doesn't exist (similar to preempt_count). This optimizes the ring-buffer for all LOAD-STORE architectures, since they need to use atomic ops to implement local_t. Suggested-by: Alexander Shishkin <alexander.shishkin@linux.intel.com> Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Cc: Arnaldo Carvalho de Melo <acme@redhat.com> Cc: Jiri Olsa <jolsa@redhat.com> Cc: Linus Torvalds <torvalds@linux-foundation.org> Cc: Peter Zijlstra <peterz@infradead.org> Cc: Stephane Eranian <eranian@google.com> Cc: Thomas Gleixner <tglx@linutronix.de> Cc: Vince Weaver <vincent.weaver@maine.edu> Cc: acme@kernel.org Cc: mark.rutland@arm.com Cc: namhyung@kernel.org Cc: yabinc@google.com Link: http://lkml.kernel.org/r/20190517115418.481392777@infradead.org Signed-off-by: Ingo Molnar <mingo@kernel.org>
1 parent 4d839dd commit 5322ea5

2 files changed

Lines changed: 28 additions & 17 deletions

File tree

‎kernel/events/internal.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ struct ring_buffer {
2424
atomic_t poll; /* POLL_ for wakeups */
2525

2626
local_t head; /* write position */
27-
local_t nest; /* nested writers */
27+
unsigned int nest; /* nested writers */
2828
local_t events; /* event limit */
2929
local_t wakeup; /* wakeup stamp */
3030
local_t lost; /* nr records lost */
@@ -41,7 +41,7 @@ struct ring_buffer {
4141

4242
/* AUX area */
4343
long aux_head;
44-
local_t aux_nest;
44+
unsigned int aux_nest;
4545
long aux_wakeup; /* last aux_watermark boundary crossed by aux_head */
4646
unsigned long aux_pgoff;
4747
int aux_nr_pages;

‎kernel/events/ring_buffer.c‎

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,30 @@ static void perf_output_get_handle(struct perf_output_handle *handle)
3838
struct ring_buffer *rb = handle->rb;
3939

4040
preempt_disable();
41-
local_inc(&rb->nest);
41+
42+
/*
43+
* Avoid an explicit LOAD/STORE such that architectures with memops
44+
* can use them.
45+
*/
46+
(*(volatile unsigned int *)&rb->nest)++;
4247
handle->wakeup = local_read(&rb->wakeup);
4348
}
4449

4550
static void perf_output_put_handle(struct perf_output_handle *handle)
4651
{
4752
struct ring_buffer *rb = handle->rb;
4853
unsigned long head;
54+
unsigned int nest;
55+
56+
/*
57+
* If this isn't the outermost nesting, we don't have to update
58+
* @rb->user_page->data_head.
59+
*/
60+
nest = READ_ONCE(rb->nest);
61+
if (nest > 1) {
62+
WRITE_ONCE(rb->nest, nest - 1);
63+
goto out;
64+
}
4965

5066
again:
5167
/*
@@ -64,15 +80,6 @@ static void perf_output_put_handle(struct perf_output_handle *handle)
6480
* load above to be stale.
6581
*/
6682

67-
/*
68-
* If this isn't the outermost nesting, we don't have to update
69-
* @rb->user_page->data_head.
70-
*/
71-
if (local_read(&rb->nest) > 1) {
72-
local_dec(&rb->nest);
73-
goto out;
74-
}
75-
7683
/*
7784
* Since the mmap() consumer (userspace) can run on a different CPU:
7885
*
@@ -108,15 +115,15 @@ static void perf_output_put_handle(struct perf_output_handle *handle)
108115
* write will (temporarily) publish a stale value.
109116
*/
110117
barrier();
111-
local_set(&rb->nest, 0);
118+
WRITE_ONCE(rb->nest, 0);
112119

113120
/*
114121
* Ensure we decrement @rb->nest before we validate the @rb->head.
115122
* Otherwise we cannot be sure we caught the 'last' nested update.
116123
*/
117124
barrier();
118125
if (unlikely(head != local_read(&rb->head))) {
119-
local_inc(&rb->nest);
126+
WRITE_ONCE(rb->nest, 1);
120127
goto again;
121128
}
122129

@@ -355,6 +362,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
355362
struct perf_event *output_event = event;
356363
unsigned long aux_head, aux_tail;
357364
struct ring_buffer *rb;
365+
unsigned int nest;
358366

359367
if (output_event->parent)
360368
output_event = output_event->parent;
@@ -385,13 +393,16 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
385393
if (!refcount_inc_not_zero(&rb->aux_refcount))
386394
goto err;
387395

396+
nest = READ_ONCE(rb->aux_nest);
388397
/*
389398
* Nesting is not supported for AUX area, make sure nested
390399
* writers are caught early
391400
*/
392-
if (WARN_ON_ONCE(local_xchg(&rb->aux_nest, 1)))
401+
if (WARN_ON_ONCE(nest))
393402
goto err_put;
394403

404+
WRITE_ONCE(rb->aux_nest, nest + 1);
405+
395406
aux_head = rb->aux_head;
396407

397408
handle->rb = rb;
@@ -419,7 +430,7 @@ void *perf_aux_output_begin(struct perf_output_handle *handle,
419430
if (!handle->size) { /* A, matches D */
420431
event->pending_disable = smp_processor_id();
421432
perf_output_wakeup(handle);
422-
local_set(&rb->aux_nest, 0);
433+
WRITE_ONCE(rb->aux_nest, 0);
423434
goto err_put;
424435
}
425436
}
@@ -508,7 +519,7 @@ void perf_aux_output_end(struct perf_output_handle *handle, unsigned long size)
508519

509520
handle->event = NULL;
510521

511-
local_set(&rb->aux_nest, 0);
522+
WRITE_ONCE(rb->aux_nest, 0);
512523
/* can't be last */
513524
rb_free_aux(rb);
514525
ring_buffer_put(rb);

0 commit comments

Comments
 (0)