@@ -207,6 +207,19 @@ static void perf_ctx_unlock(struct perf_cpu_context *cpuctx,
207
207
__perf_ctx_unlock (& cpuctx -> ctx );
208
208
}
209
209
210
+ typedef struct {
211
+ struct perf_cpu_context * cpuctx ;
212
+ struct perf_event_context * ctx ;
213
+ } class_perf_ctx_lock_t ;
214
+
215
+ static inline void class_perf_ctx_lock_destructor (class_perf_ctx_lock_t * _T )
216
+ { perf_ctx_unlock (_T -> cpuctx , _T -> ctx ); }
217
+
218
+ static inline class_perf_ctx_lock_t
219
+ class_perf_ctx_lock_constructor (struct perf_cpu_context * cpuctx ,
220
+ struct perf_event_context * ctx )
221
+ { perf_ctx_lock (cpuctx , ctx ); return (class_perf_ctx_lock_t ){ cpuctx , ctx }; }
222
+
210
223
#define TASK_TOMBSTONE ((void *)-1L)
211
224
212
225
static bool is_kernel_event (struct perf_event * event )
@@ -944,7 +957,13 @@ static void perf_cgroup_switch(struct task_struct *task)
944
957
if (READ_ONCE (cpuctx -> cgrp ) == cgrp )
945
958
return ;
946
959
947
- perf_ctx_lock (cpuctx , cpuctx -> task_ctx );
960
+ guard (perf_ctx_lock )(cpuctx , cpuctx -> task_ctx );
961
+ /*
962
+ * Re-check, could've raced vs perf_remove_from_context().
963
+ */
964
+ if (READ_ONCE (cpuctx -> cgrp ) == NULL )
965
+ return ;
966
+
948
967
perf_ctx_disable (& cpuctx -> ctx , true);
949
968
950
969
ctx_sched_out (& cpuctx -> ctx , NULL , EVENT_ALL |EVENT_CGROUP );
@@ -962,7 +981,6 @@ static void perf_cgroup_switch(struct task_struct *task)
962
981
ctx_sched_in (& cpuctx -> ctx , NULL , EVENT_ALL |EVENT_CGROUP );
963
982
964
983
perf_ctx_enable (& cpuctx -> ctx , true);
965
- perf_ctx_unlock (cpuctx , cpuctx -> task_ctx );
966
984
}
967
985
968
986
static int perf_cgroup_ensure_storage (struct perf_event * event ,
@@ -2120,18 +2138,6 @@ list_del_event(struct perf_event *event, struct perf_event_context *ctx)
2120
2138
if (event -> group_leader == event )
2121
2139
del_event_from_groups (event , ctx );
2122
2140
2123
- /*
2124
- * If event was in error state, then keep it
2125
- * that way, otherwise bogus counts will be
2126
- * returned on read(). The only way to get out
2127
- * of error state is by explicit re-enabling
2128
- * of the event
2129
- */
2130
- if (event -> state > PERF_EVENT_STATE_OFF ) {
2131
- perf_cgroup_event_disable (event , ctx );
2132
- perf_event_set_state (event , PERF_EVENT_STATE_OFF );
2133
- }
2134
-
2135
2141
ctx -> generation ++ ;
2136
2142
event -> pmu_ctx -> nr_events -- ;
2137
2143
}
@@ -2149,8 +2155,9 @@ perf_aux_output_match(struct perf_event *event, struct perf_event *aux_event)
2149
2155
}
2150
2156
2151
2157
static void put_event (struct perf_event * event );
2152
- static void event_sched_out (struct perf_event * event ,
2153
- struct perf_event_context * ctx );
2158
+ static void __event_disable (struct perf_event * event ,
2159
+ struct perf_event_context * ctx ,
2160
+ enum perf_event_state state );
2154
2161
2155
2162
static void perf_put_aux_event (struct perf_event * event )
2156
2163
{
@@ -2183,8 +2190,7 @@ static void perf_put_aux_event(struct perf_event *event)
2183
2190
* state so that we don't try to schedule it again. Note
2184
2191
* that perf_event_enable() will clear the ERROR status.
2185
2192
*/
2186
- event_sched_out (iter , ctx );
2187
- perf_event_set_state (event , PERF_EVENT_STATE_ERROR );
2193
+ __event_disable (iter , ctx , PERF_EVENT_STATE_ERROR );
2188
2194
}
2189
2195
}
2190
2196
@@ -2242,18 +2248,6 @@ static inline struct list_head *get_event_list(struct perf_event *event)
2242
2248
& event -> pmu_ctx -> flexible_active ;
2243
2249
}
2244
2250
2245
- /*
2246
- * Events that have PERF_EV_CAP_SIBLING require being part of a group and
2247
- * cannot exist on their own, schedule them out and move them into the ERROR
2248
- * state. Also see _perf_event_enable(), it will not be able to recover
2249
- * this ERROR state.
2250
- */
2251
- static inline void perf_remove_sibling_event (struct perf_event * event )
2252
- {
2253
- event_sched_out (event , event -> ctx );
2254
- perf_event_set_state (event , PERF_EVENT_STATE_ERROR );
2255
- }
2256
-
2257
2251
static void perf_group_detach (struct perf_event * event )
2258
2252
{
2259
2253
struct perf_event * leader = event -> group_leader ;
@@ -2289,8 +2283,15 @@ static void perf_group_detach(struct perf_event *event)
2289
2283
*/
2290
2284
list_for_each_entry_safe (sibling , tmp , & event -> sibling_list , sibling_list ) {
2291
2285
2286
+ /*
2287
+ * Events that have PERF_EV_CAP_SIBLING require being part of
2288
+ * a group and cannot exist on their own, schedule them out
2289
+ * and move them into the ERROR state. Also see
2290
+ * _perf_event_enable(), it will not be able to recover this
2291
+ * ERROR state.
2292
+ */
2292
2293
if (sibling -> event_caps & PERF_EV_CAP_SIBLING )
2293
- perf_remove_sibling_event (sibling );
2294
+ __event_disable (sibling , ctx , PERF_EVENT_STATE_ERROR );
2294
2295
2295
2296
sibling -> group_leader = sibling ;
2296
2297
list_del_init (& sibling -> sibling_list );
@@ -2493,11 +2494,14 @@ __perf_remove_from_context(struct perf_event *event,
2493
2494
state = PERF_EVENT_STATE_EXIT ;
2494
2495
if (flags & DETACH_REVOKE )
2495
2496
state = PERF_EVENT_STATE_REVOKED ;
2496
- if (flags & DETACH_DEAD ) {
2497
- event -> pending_disable = 1 ;
2497
+ if (flags & DETACH_DEAD )
2498
2498
state = PERF_EVENT_STATE_DEAD ;
2499
- }
2499
+
2500
2500
event_sched_out (event , ctx );
2501
+
2502
+ if (event -> state > PERF_EVENT_STATE_OFF )
2503
+ perf_cgroup_event_disable (event , ctx );
2504
+
2501
2505
perf_event_set_state (event , min (event -> state , state ));
2502
2506
2503
2507
if (flags & DETACH_GROUP )
@@ -2562,6 +2566,15 @@ static void perf_remove_from_context(struct perf_event *event, unsigned long fla
2562
2566
event_function_call (event , __perf_remove_from_context , (void * )flags );
2563
2567
}
2564
2568
2569
+ static void __event_disable (struct perf_event * event ,
2570
+ struct perf_event_context * ctx ,
2571
+ enum perf_event_state state )
2572
+ {
2573
+ event_sched_out (event , ctx );
2574
+ perf_cgroup_event_disable (event , ctx );
2575
+ perf_event_set_state (event , state );
2576
+ }
2577
+
2565
2578
/*
2566
2579
* Cross CPU call to disable a performance event
2567
2580
*/
@@ -2576,13 +2589,18 @@ static void __perf_event_disable(struct perf_event *event,
2576
2589
perf_pmu_disable (event -> pmu_ctx -> pmu );
2577
2590
ctx_time_update_event (ctx , event );
2578
2591
2592
+ /*
2593
+ * When disabling a group leader, the whole group becomes ineligible
2594
+ * to run, so schedule out the full group.
2595
+ */
2579
2596
if (event == event -> group_leader )
2580
2597
group_sched_out (event , ctx );
2581
- else
2582
- event_sched_out (event , ctx );
2583
2598
2584
- perf_event_set_state (event , PERF_EVENT_STATE_OFF );
2585
- perf_cgroup_event_disable (event , ctx );
2599
+ /*
2600
+ * But only mark the leader OFF; the siblings will remain
2601
+ * INACTIVE.
2602
+ */
2603
+ __event_disable (event , ctx , PERF_EVENT_STATE_OFF );
2586
2604
2587
2605
perf_pmu_enable (event -> pmu_ctx -> pmu );
2588
2606
}
@@ -2656,8 +2674,8 @@ static void perf_event_unthrottle(struct perf_event *event, bool start)
2656
2674
2657
2675
static void perf_event_throttle (struct perf_event * event )
2658
2676
{
2659
- event -> pmu -> stop (event , 0 );
2660
2677
event -> hw .interrupts = MAX_INTERRUPTS ;
2678
+ event -> pmu -> stop (event , 0 );
2661
2679
if (event == event -> group_leader )
2662
2680
perf_log_throttle (event , 0 );
2663
2681
}
@@ -7439,6 +7457,10 @@ perf_sample_ustack_size(u16 stack_size, u16 header_size,
7439
7457
if (!regs )
7440
7458
return 0 ;
7441
7459
7460
+ /* No mm, no stack, no dump. */
7461
+ if (!current -> mm )
7462
+ return 0 ;
7463
+
7442
7464
/*
7443
7465
* Check if we fit in with the requested stack size into the:
7444
7466
* - TASK_SIZE
@@ -8150,6 +8172,9 @@ perf_callchain(struct perf_event *event, struct pt_regs *regs)
8150
8172
const u32 max_stack = event -> attr .sample_max_stack ;
8151
8173
struct perf_callchain_entry * callchain ;
8152
8174
8175
+ if (!current -> mm )
8176
+ user = false;
8177
+
8153
8178
if (!kernel && !user )
8154
8179
return & __empty_callchain ;
8155
8180
@@ -11749,7 +11774,12 @@ static void perf_swevent_cancel_hrtimer(struct perf_event *event)
11749
11774
{
11750
11775
struct hw_perf_event * hwc = & event -> hw ;
11751
11776
11752
- if (is_sampling_event (event )) {
11777
+ /*
11778
+ * The throttle can be triggered in the hrtimer handler.
11779
+ * The HRTIMER_NORESTART should be used to stop the timer,
11780
+ * rather than hrtimer_cancel(). See perf_swevent_hrtimer()
11781
+ */
11782
+ if (is_sampling_event (event ) && (hwc -> interrupts != MAX_INTERRUPTS )) {
11753
11783
ktime_t remaining = hrtimer_get_remaining (& hwc -> hrtimer );
11754
11784
local64_set (& hwc -> period_left , ktime_to_ns (remaining ));
11755
11785
@@ -11804,7 +11834,8 @@ static void cpu_clock_event_start(struct perf_event *event, int flags)
11804
11834
static void cpu_clock_event_stop (struct perf_event * event , int flags )
11805
11835
{
11806
11836
perf_swevent_cancel_hrtimer (event );
11807
- cpu_clock_event_update (event );
11837
+ if (flags & PERF_EF_UPDATE )
11838
+ cpu_clock_event_update (event );
11808
11839
}
11809
11840
11810
11841
static int cpu_clock_event_add (struct perf_event * event , int flags )
@@ -11882,7 +11913,8 @@ static void task_clock_event_start(struct perf_event *event, int flags)
11882
11913
static void task_clock_event_stop (struct perf_event * event , int flags )
11883
11914
{
11884
11915
perf_swevent_cancel_hrtimer (event );
11885
- task_clock_event_update (event , event -> ctx -> time );
11916
+ if (flags & PERF_EF_UPDATE )
11917
+ task_clock_event_update (event , event -> ctx -> time );
11886
11918
}
11887
11919
11888
11920
static int task_clock_event_add (struct perf_event * event , int flags )
0 commit comments