Skip to content

Commit aef59b5

Browse files
rostedtgregkh
authored andcommitted
ring-buffer: Fix recursion protection transitions between interrupt context
commit b02414c upstream. The recursion protection of the ring buffer depends on preempt_count() to be correct. But it is possible that the ring buffer gets called after an interrupt comes in but before it updates the preempt_count(). This will trigger a false positive in the recursion code. Use the same trick from the ftrace function callback recursion code which uses a "transition" bit that gets set, to allow for a single recursion for to handle transitions between contexts. Cc: stable@vger.kernel.org Fixes: 567cd4d ("ring-buffer: User context bit recursion checking") Signed-off-by: Steven Rostedt (VMware) <rostedt@goodmis.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 2cd7174 commit aef59b5

1 file changed

Lines changed: 46 additions & 12 deletions

File tree

‎kernel/trace/ring_buffer.c‎

Lines changed: 46 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -422,14 +422,16 @@ struct rb_event_info {
422422

423423
/*
424424
* Used for which event context the event is in.
425-
* NMI = 0
426-
* IRQ = 1
427-
* SOFTIRQ = 2
428-
* NORMAL = 3
425+
* TRANSITION = 0
426+
* NMI = 1
427+
* IRQ = 2
428+
* SOFTIRQ = 3
429+
* NORMAL = 4
429430
*
430431
* See trace_recursive_lock() comment below for more details.
431432
*/
432433
enum {
434+
RB_CTX_TRANSITION,
433435
RB_CTX_NMI,
434436
RB_CTX_IRQ,
435437
RB_CTX_SOFTIRQ,
@@ -2660,10 +2662,10 @@ rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
26602662
* a bit of overhead in something as critical as function tracing,
26612663
* we use a bitmask trick.
26622664
*
2663-
* bit 0 = NMI context
2664-
* bit 1 = IRQ context
2665-
* bit 2 = SoftIRQ context
2666-
* bit 3 = normal context.
2665+
* bit 1 = NMI context
2666+
* bit 2 = IRQ context
2667+
* bit 3 = SoftIRQ context
2668+
* bit 4 = normal context.
26672669
*
26682670
* This works because this is the order of contexts that can
26692671
* preempt other contexts. A SoftIRQ never preempts an IRQ
@@ -2686,6 +2688,30 @@ rb_wakeups(struct ring_buffer *buffer, struct ring_buffer_per_cpu *cpu_buffer)
26862688
* The least significant bit can be cleared this way, and it
26872689
* just so happens that it is the same bit corresponding to
26882690
* the current context.
2691+
*
2692+
* Now the TRANSITION bit breaks the above slightly. The TRANSITION bit
2693+
* is set when a recursion is detected at the current context, and if
2694+
* the TRANSITION bit is already set, it will fail the recursion.
2695+
* This is needed because there's a lag between the changing of
2696+
* interrupt context and updating the preempt count. In this case,
2697+
* a false positive will be found. To handle this, one extra recursion
2698+
* is allowed, and this is done by the TRANSITION bit. If the TRANSITION
2699+
* bit is already set, then it is considered a recursion and the function
2700+
* ends. Otherwise, the TRANSITION bit is set, and that bit is returned.
2701+
*
2702+
* On the trace_recursive_unlock(), the TRANSITION bit will be the first
2703+
* to be cleared. Even if it wasn't the context that set it. That is,
2704+
* if an interrupt comes in while NORMAL bit is set and the ring buffer
2705+
* is called before preempt_count() is updated, since the check will
2706+
* be on the NORMAL bit, the TRANSITION bit will then be set. If an
2707+
* NMI then comes in, it will set the NMI bit, but when the NMI code
2708+
* does the trace_recursive_unlock() it will clear the TRANSTION bit
2709+
* and leave the NMI bit set. But this is fine, because the interrupt
2710+
* code that set the TRANSITION bit will then clear the NMI bit when it
2711+
* calls trace_recursive_unlock(). If another NMI comes in, it will
2712+
* set the TRANSITION bit and continue.
2713+
*
2714+
* Note: The TRANSITION bit only handles a single transition between context.
26892715
*/
26902716

26912717
static __always_inline int
@@ -2701,8 +2727,16 @@ trace_recursive_lock(struct ring_buffer_per_cpu *cpu_buffer)
27012727
bit = pc & NMI_MASK ? RB_CTX_NMI :
27022728
pc & HARDIRQ_MASK ? RB_CTX_IRQ : RB_CTX_SOFTIRQ;
27032729

2704-
if (unlikely(val & (1 << (bit + cpu_buffer->nest))))
2705-
return 1;
2730+
if (unlikely(val & (1 << (bit + cpu_buffer->nest)))) {
2731+
/*
2732+
* It is possible that this was called by transitioning
2733+
* between interrupt context, and preempt_count() has not
2734+
* been updated yet. In this case, use the TRANSITION bit.
2735+
*/
2736+
bit = RB_CTX_TRANSITION;
2737+
if (val & (1 << (bit + cpu_buffer->nest)))
2738+
return 1;
2739+
}
27062740

27072741
val |= (1 << (bit + cpu_buffer->nest));
27082742
cpu_buffer->current_context = val;
@@ -2717,8 +2751,8 @@ trace_recursive_unlock(struct ring_buffer_per_cpu *cpu_buffer)
27172751
cpu_buffer->current_context - (1 << cpu_buffer->nest);
27182752
}
27192753

2720-
/* The recursive locking above uses 4 bits */
2721-
#define NESTED_BITS 4
2754+
/* The recursive locking above uses 5 bits */
2755+
#define NESTED_BITS 5
27222756

27232757
/**
27242758
* ring_buffer_nest_start - Allow to trace while nested

0 commit comments

Comments
 (0)