Skip to content

Commit da916e9

Browse files
author
Peter Zijlstra
committed
perf: Make perf_pmu_unregister() useable
Previously it was only safe to call perf_pmu_unregister() if there were no active events of that pmu around -- which was impossible to guarantee since it races all sorts against perf_init_event(). Rework the whole thing by: - keeping track of all events for a given pmu - 'hiding' the pmu from perf_init_event() - waiting for the appropriate (s)rcu grace periods such that all prior references to the PMU will be completed - detaching all still existing events of that pmu (see first point) and moving them to a new REVOKED state. - actually freeing the pmu data. Where notably the new REVOKED state must inhibit all event actions from reaching code that wants to use event->pmu. Signed-off-by: Peter Zijlstra (Intel) <peterz@infradead.org> Reviewed-by: Ravi Bangoria <ravi.bangoria@amd.com> Link: https://lkml.kernel.org/r/20250307193723.525402029@infradead.org
1 parent 4da0600 commit da916e9

File tree

2 files changed

+280
-55
lines changed

2 files changed

+280
-55
lines changed

include/linux/perf_event.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,9 @@ struct perf_output_handle;
325325
struct pmu {
326326
struct list_head entry;
327327

328+
spinlock_t events_lock;
329+
struct list_head events;
330+
328331
struct module *module;
329332
struct device *dev;
330333
struct device *parent;
@@ -622,9 +625,10 @@ struct perf_addr_filter_range {
622625
* enum perf_event_state - the states of an event:
623626
*/
624627
enum perf_event_state {
625-
PERF_EVENT_STATE_DEAD = -4,
626-
PERF_EVENT_STATE_EXIT = -3,
627-
PERF_EVENT_STATE_ERROR = -2,
628+
PERF_EVENT_STATE_DEAD = -5,
629+
PERF_EVENT_STATE_REVOKED = -4, /* pmu gone, must not touch */
630+
PERF_EVENT_STATE_EXIT = -3, /* task died, still inherit */
631+
PERF_EVENT_STATE_ERROR = -2, /* scheduling error, can enable */
628632
PERF_EVENT_STATE_OFF = -1,
629633
PERF_EVENT_STATE_INACTIVE = 0,
630634
PERF_EVENT_STATE_ACTIVE = 1,
@@ -865,6 +869,7 @@ struct perf_event {
865869
void *security;
866870
#endif
867871
struct list_head sb_list;
872+
struct list_head pmu_list;
868873

869874
/*
870875
* Certain events gets forwarded to another pmu internally by over-
@@ -1155,7 +1160,7 @@ extern void perf_aux_output_flag(struct perf_output_handle *handle, u64 flags);
11551160
extern void perf_event_itrace_started(struct perf_event *event);
11561161

11571162
extern int perf_pmu_register(struct pmu *pmu, const char *name, int type);
1158-
extern void perf_pmu_unregister(struct pmu *pmu);
1163+
extern int perf_pmu_unregister(struct pmu *pmu);
11591164

11601165
extern void __perf_event_task_sched_in(struct task_struct *prev,
11611166
struct task_struct *task);
@@ -1760,7 +1765,7 @@ static inline bool needs_branch_stack(struct perf_event *event)
17601765

17611766
static inline bool has_aux(struct perf_event *event)
17621767
{
1763-
return event->pmu->setup_aux;
1768+
return event->pmu && event->pmu->setup_aux;
17641769
}
17651770

17661771
static inline bool has_aux_action(struct perf_event *event)

0 commit comments

Comments
 (0)