Skip to content

Commit 0d40a6d

Browse files
Sebastian Andrzej SiewiorPeter Zijlstra
authored andcommitted
perf: Move swevent_htable::recursion into task_struct.
The swevent_htable::recursion counter is used to avoid creating an swevent while an event is processed to avoid recursion. The counter is per-CPU and preemption must be disabled to have a stable counter. perf_pending_task() disables preemption to access the counter and then signal. This is problematic on PREEMPT_RT because sending a signal uses a spinlock_t which must not be acquired in atomic on PREEMPT_RT because it becomes a sleeping lock. The atomic context can be avoided by moving the counter into the task_struct. There is a 4 byte hole between futex_state (usually always on) and the following perf pointer (perf_event_ctxp). After the recursion lost some weight it fits perfectly. Move swevent_htable::recursion into task_struct. Signed-off-by: Sebastian Andrzej Siewior <[email protected]> Signed-off-by: Peter Zijlstra (Intel) <[email protected]> Tested-by: Marco Elver <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 5af42f9 commit 0d40a6d

File tree

4 files changed

+11
-17
lines changed

4 files changed

+11
-17
lines changed

include/linux/perf_event.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -970,12 +970,6 @@ struct perf_event_context {
970970
local_t nr_pending;
971971
};
972972

973-
/*
974-
* Number of contexts where an event can trigger:
975-
* task, softirq, hardirq, nmi.
976-
*/
977-
#define PERF_NR_CONTEXTS 4
978-
979973
struct perf_cpu_pmu_context {
980974
struct perf_event_pmu_context epc;
981975
struct perf_event_pmu_context *task_epc;

include/linux/sched.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -734,6 +734,12 @@ enum perf_event_task_context {
734734
perf_nr_task_contexts,
735735
};
736736

737+
/*
738+
* Number of contexts where an event can trigger:
739+
* task, softirq, hardirq, nmi.
740+
*/
741+
#define PERF_NR_CONTEXTS 4
742+
737743
struct wake_q_node {
738744
struct wake_q_node *next;
739745
};
@@ -1256,6 +1262,7 @@ struct task_struct {
12561262
unsigned int futex_state;
12571263
#endif
12581264
#ifdef CONFIG_PERF_EVENTS
1265+
u8 perf_recursion[PERF_NR_CONTEXTS];
12591266
struct perf_event_context *perf_event_ctxp;
12601267
struct mutex perf_event_mutex;
12611268
struct list_head perf_event_list;

kernel/events/core.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9763,11 +9763,7 @@ struct swevent_htable {
97639763
struct swevent_hlist *swevent_hlist;
97649764
struct mutex hlist_mutex;
97659765
int hlist_refcount;
9766-
9767-
/* Recursion avoidance in each contexts */
9768-
u8 recursion[PERF_NR_CONTEXTS];
97699766
};
9770-
97719767
static DEFINE_PER_CPU(struct swevent_htable, swevent_htable);
97729768

97739769
/*
@@ -9965,17 +9961,13 @@ DEFINE_PER_CPU(struct pt_regs, __perf_regs[4]);
99659961

99669962
int perf_swevent_get_recursion_context(void)
99679963
{
9968-
struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
9969-
9970-
return get_recursion_context(swhash->recursion);
9964+
return get_recursion_context(current->perf_recursion);
99719965
}
99729966
EXPORT_SYMBOL_GPL(perf_swevent_get_recursion_context);
99739967

99749968
void perf_swevent_put_recursion_context(int rctx)
99759969
{
9976-
struct swevent_htable *swhash = this_cpu_ptr(&swevent_htable);
9977-
9978-
put_recursion_context(swhash->recursion, rctx);
9970+
put_recursion_context(current->perf_recursion, rctx);
99799971
}
99809972

99819973
void ___perf_sw_event(u32 event_id, u64 nr, struct pt_regs *regs, u64 addr)
@@ -13642,6 +13634,7 @@ int perf_event_init_task(struct task_struct *child, u64 clone_flags)
1364213634
{
1364313635
int ret;
1364413636

13637+
memset(child->perf_recursion, 0, sizeof(child->perf_recursion));
1364513638
child->perf_event_ctxp = NULL;
1364613639
mutex_init(&child->perf_event_mutex);
1364713640
INIT_LIST_HEAD(&child->perf_event_list);

kernel/events/internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ static inline int get_recursion_context(u8 *recursion)
221221
return rctx;
222222
}
223223

224-
static inline void put_recursion_context(u8 *recursion, int rctx)
224+
static inline void put_recursion_context(u8 *recursion, unsigned char rctx)
225225
{
226226
barrier();
227227
recursion[rctx]--;

0 commit comments

Comments
 (0)