Current perf assumes that events in a group are independent. Close an event doesn't impact the value of the other events in the same group. If the closed event is a member, after the event closure, other events are still running like a group. If the closed event is a leader, other events are running as singleton events. Add PERF_EV_CAP_SIBLING to allow events to indicate they require being part of a group, and when the leader dies they cannot exist independently.
Unified · Split
| @@ -576,9 +576,13 @@ typedef void (*perf_overflow_handler_t)(struct perf_event *, · 575 unmodified lines | ||
| 576 | 576 | * PERF_EV_CAP_SOFTWARE: Is a software event. |
| 577 | 577 | * PERF_EV_CAP_READ_ACTIVE_PKG: A CPU event (or cgroup event) that can be read |
| 578 | 578 | * from any CPU in the package where it is active. |
| 579 | + * PERF_EV_CAP_SIBLING: An event with this flag must be a group sibling and | |
| 580 | + * cannot be a group leader. If an event with this flag is detached from the | |
| 581 | + * group it is scheduled out and moved into an unrecoverable ERROR state. | |
| 579 | 582 | */ |
| 580 | 583 | #define PERF_EV_CAP_SOFTWARE BIT(0) |
| 581 | 584 | #define PERF_EV_CAP_READ_ACTIVE_PKG BIT(1) |
| 585 | +#define PERF_EV_CAP_SIBLING BIT(2) | |
| 582 | 586 | |
| 583 | 587 | #define SWEVENT_HLIST_BITS 8 |
| 584 | 588 | #define SWEVENT_HLIST_SIZE (1 << SWEVENT_HLIST_BITS) |
| @@ -2133,8 +2133,24 @@ static inline struct list_head *get_event_list(struct perf_event *event) · 2132 unmodified lines | ||
| 2133 | 2133 | return event->attr.pinned ? &ctx->pinned_active : &ctx->flexible_active; |
| 2134 | 2134 | } |
| 2135 | 2135 | |
| 2136 | +/* | |
| 2137 | + * Events that have PERF_EV_CAP_SIBLING require being part of a group and | |
| 2138 | + * cannot exist on their own, schedule them out and move them into the ERROR | |
| 2139 | + * state. Also see _perf_event_enable(), it will not be able to recover | |
| 2140 | + * this ERROR state. | |
| 2141 | + */ | |
| 2142 | +static inline void perf_remove_sibling_event(struct perf_event *event) | |
| 2143 | +{ | |
| 2144 | + struct perf_event_context *ctx = event->ctx; | |
| 2145 | + struct perf_cpu_context *cpuctx = __get_cpu_context(ctx); | |
| 2146 | + | |
| 2147 | + event_sched_out(event, cpuctx, ctx); | |
| 2148 | + perf_event_set_state(event, PERF_EVENT_STATE_ERROR); | |
| 2149 | +} | |
| 2150 | + | |
| 2136 | 2151 | static void perf_group_detach(struct perf_event *event) |
| 2137 | 2152 | { |
| 2153 | + struct perf_event *leader = event->group_leader; | |
| 2138 | 2154 | struct perf_event *sibling, *tmp; |
| 2139 | 2155 | struct perf_event_context *ctx = event->ctx; |
| 2140 | 2156 | |
| @@ -2153,7 +2169,7 @@ static void perf_group_detach(struct perf_event *event) · 12 unmodified lines | ||
| 2153 | 2169 | /* |
| 2154 | 2170 | * If this is a sibling, remove it from its group. |
| 2155 | 2171 | */ |
| 2156 | − if (event->group_leader != event) { | |
| 2172 | + if (leader != event) { | |
| 2157 | 2173 | list_del_init(&event->sibling_list); |
| 2158 | 2174 | event->group_leader->nr_siblings--; |
| 2159 | 2175 | goto out; |
| @@ -2166,6 +2182,9 @@ static void perf_group_detach(struct perf_event *event) · 6 unmodified lines | ||
| 2166 | 2182 | */ |
| 2167 | 2183 | list_for_each_entry_safe(sibling, tmp, &event->sibling_list, sibling_list) { |
| 2168 | 2184 | |
| 2185 | + if (sibling->event_caps & PERF_EV_CAP_SIBLING) | |
| 2186 | + perf_remove_sibling_event(sibling); | |
| 2187 | + | |
| 2169 | 2188 | sibling->group_leader = sibling; |
| 2170 | 2189 | list_del_init(&sibling->sibling_list); |
| 2171 | 2190 | |
| @@ -2183,10 +2202,10 @@ static void perf_group_detach(struct perf_event *event) · 11 unmodified lines | ||
| 2183 | 2202 | } |
| 2184 | 2203 | |
| 2185 | 2204 | out: |
| 2186 | − perf_event__header_size(event->group_leader); | |
| 2187 | − | |
| 2188 | − for_each_sibling_event(tmp, event->group_leader) | |
| 2205 | + for_each_sibling_event(tmp, leader) | |
| 2189 | 2206 | perf_event__header_size(tmp); |
| 2207 | + | |
| 2208 | + perf_event__header_size(leader); | |
| 2190 | 2209 | } |
| 2191 | 2210 | |
| 2192 | 2211 | static bool is_orphaned_event(struct perf_event *event) |
| @@ -2979,6 +2998,7 @@ static void _perf_event_enable(struct perf_event *event) · 786 unmodified lines | ||
| 2979 | 2998 | raw_spin_lock_irq(&ctx->lock); |
| 2980 | 2999 | if (event->state >= PERF_EVENT_STATE_INACTIVE || |
| 2981 | 3000 | event->state < PERF_EVENT_STATE_ERROR) { |
| 3001 | +out: | |
| 2982 | 3002 | raw_spin_unlock_irq(&ctx->lock); |
| 2983 | 3003 | return; |
| 2984 | 3004 | } |
| @@ -2990,8 +3010,16 @@ static void _perf_event_enable(struct perf_event *event) · 5 unmodified lines | ||
| 2990 | 3010 | * has gone back into error state, as distinct from the task having |
| 2991 | 3011 | * been scheduled away before the cross-call arrived. |
| 2992 | 3012 | */ |
| 2993 | − if (event->state == PERF_EVENT_STATE_ERROR) | |
| 3013 | + if (event->state == PERF_EVENT_STATE_ERROR) { | |
| 3014 | + /* | |
| 3015 | + * Detached SIBLING events cannot leave ERROR state. | |
| 3016 | + */ | |
| 3017 | + if (event->event_caps & PERF_EV_CAP_SIBLING && | |
| 3018 | + event->group_leader == event) | |
| 3019 | + goto out; | |
| 3020 | + | |
| 2994 | 3021 | event->state = PERF_EVENT_STATE_OFF; |
| 3022 | + } | |
| 2995 | 3023 | raw_spin_unlock_irq(&ctx->lock); |
| 2996 | 3024 | |
| 2997 | 3025 | event_function_call(event, __perf_event_enable, NULL); |