Commit ca6c213
Peter Zijlstra
perf: Fix missing SIGTRAPs
Marco reported:
Due to the implementation of how SIGTRAP are delivered if
perf_event_attr::sigtrap is set, we've noticed 3 issues:
1. Missing SIGTRAP due to a race with event_sched_out() (more
details below).
2. Hardware PMU events being disabled due to returning 1 from
perf_event_overflow(). The only way to re-enable the event is
for user space to first "properly" disable the event and then
re-enable it.
3. The inability to automatically disable an event after a
specified number of overflows via PERF_EVENT_IOC_REFRESH.
The worst of the 3 issues is problem (1), which occurs when a
pending_disable is "consumed" by a racing event_sched_out(), observed
as follows:
CPU0 | CPU1
--------------------------------+---------------------------
__perf_event_overflow() |
perf_event_disable_inatomic() |
pending_disable = CPU0 | ...
| _perf_event_enable()
| event_function_call()
| task_function_call()
| /* sends IPI to CPU0 */
<IPI> | ...
__perf_event_enable() +---------------------------
ctx_resched()
task_ctx_sched_out()
ctx_sched_out()
group_sched_out()
event_sched_out()
pending_disable = -1
</IPI>
<IRQ-work>
perf_pending_event()
perf_pending_event_disable()
/* Fails to send SIGTRAP because no pending_disable! */
</IRQ-work>
In the above case, not only is that particular SIGTRAP missed, but also
all future SIGTRAPs because 'event_limit' is not reset back to 1.
To fix, rework pending delivery of SIGTRAP via IRQ-work by introduction
of a separate 'pending_sigtrap', no longer using 'event_limit' and
'pending_disable' for its delivery.
Additionally; and different to Marco's proposed patch:
- recognise that pending_disable effectively duplicates oncpu for
the case where it is set. As such, change the irq_work handler to
use ->oncpu to target the event and use pending_* as boolean toggles.
- observe that SIGTRAP targets the ctx->task, so the context switch
optimization that carries contexts between tasks is invalid. If
the irq_work were delayed enough to hit after a context switch the
SIGTRAP would be delivered to the wrong task.
- observe that if the event gets scheduled out
(rotation/migration/context-switch/...) the irq-work would be
insufficient to deliver the SIGTRAP when the event gets scheduled
back in (the irq-work might still be pending on the old CPU).
Therefore have event_sched_out() convert the pending sigtrap into a
task_work which will deliver the signal at return_to_user.
Fixes: 97ba62b ("perf: Add support for SIGTRAP on perf events")
Reported-by: Dmitry Vyukov <[email protected]>
Debugged-by: Dmitry Vyukov <[email protected]>
Reported-by: Marco Elver <[email protected]>
Debugged-by: Marco Elver <[email protected]>
Signed-off-by: Peter Zijlstra (Intel) <[email protected]>
Reviewed-by: Marco Elver <[email protected]>
Tested-by: Marco Elver <[email protected]>1 parent 9abf231 commit ca6c213
File tree
3 files changed
+129
-43
lines changed- include/linux
- kernel/events
3 files changed
+129
-43
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
756 | 756 | | |
757 | 757 | | |
758 | 758 | | |
759 | | - | |
760 | | - | |
761 | | - | |
| 759 | + | |
| 760 | + | |
| 761 | + | |
| 762 | + | |
762 | 763 | | |
763 | | - | |
| 764 | + | |
| 765 | + | |
| 766 | + | |
764 | 767 | | |
765 | 768 | | |
766 | 769 | | |
| |||
877 | 880 | | |
878 | 881 | | |
879 | 882 | | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
880 | 891 | | |
881 | 892 | | |
882 | 893 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
54 | 54 | | |
55 | 55 | | |
56 | 56 | | |
| 57 | + | |
57 | 58 | | |
58 | 59 | | |
59 | 60 | | |
| |||
2276 | 2277 | | |
2277 | 2278 | | |
2278 | 2279 | | |
2279 | | - | |
2280 | | - | |
| 2280 | + | |
| 2281 | + | |
2281 | 2282 | | |
2282 | 2283 | | |
2283 | 2284 | | |
| 2285 | + | |
| 2286 | + | |
| 2287 | + | |
| 2288 | + | |
| 2289 | + | |
| 2290 | + | |
| 2291 | + | |
| 2292 | + | |
| 2293 | + | |
| 2294 | + | |
| 2295 | + | |
| 2296 | + | |
| 2297 | + | |
| 2298 | + | |
| 2299 | + | |
2284 | 2300 | | |
2285 | 2301 | | |
2286 | 2302 | | |
| |||
2432 | 2448 | | |
2433 | 2449 | | |
2434 | 2450 | | |
2435 | | - | |
| 2451 | + | |
2436 | 2452 | | |
2437 | 2453 | | |
2438 | 2454 | | |
| |||
2471 | 2487 | | |
2472 | 2488 | | |
2473 | 2489 | | |
2474 | | - | |
2475 | | - | |
2476 | | - | |
| 2490 | + | |
| 2491 | + | |
2477 | 2492 | | |
2478 | 2493 | | |
2479 | 2494 | | |
| |||
3428 | 3443 | | |
3429 | 3444 | | |
3430 | 3445 | | |
| 3446 | + | |
| 3447 | + | |
| 3448 | + | |
| 3449 | + | |
| 3450 | + | |
| 3451 | + | |
| 3452 | + | |
| 3453 | + | |
| 3454 | + | |
| 3455 | + | |
| 3456 | + | |
| 3457 | + | |
| 3458 | + | |
| 3459 | + | |
3431 | 3460 | | |
3432 | 3461 | | |
3433 | 3462 | | |
3434 | | - | |
3435 | | - | |
3436 | 3463 | | |
3437 | 3464 | | |
3438 | 3465 | | |
| |||
3473 | 3500 | | |
3474 | 3501 | | |
3475 | 3502 | | |
| 3503 | + | |
3476 | 3504 | | |
3477 | 3505 | | |
3478 | 3506 | | |
| |||
4939 | 4967 | | |
4940 | 4968 | | |
4941 | 4969 | | |
4942 | | - | |
| 4970 | + | |
4943 | 4971 | | |
4944 | 4972 | | |
4945 | 4973 | | |
| |||
6439 | 6467 | | |
6440 | 6468 | | |
6441 | 6469 | | |
6442 | | - | |
| 6470 | + | |
| 6471 | + | |
6443 | 6472 | | |
6444 | 6473 | | |
6445 | 6474 | | |
| |||
6448 | 6477 | | |
6449 | 6478 | | |
6450 | 6479 | | |
6451 | | - | |
| 6480 | + | |
| 6481 | + | |
| 6482 | + | |
| 6483 | + | |
6452 | 6484 | | |
6453 | | - | |
| 6485 | + | |
6454 | 6486 | | |
| 6487 | + | |
| 6488 | + | |
| 6489 | + | |
| 6490 | + | |
6455 | 6491 | | |
6456 | 6492 | | |
6457 | 6493 | | |
| 6494 | + | |
| 6495 | + | |
| 6496 | + | |
6458 | 6497 | | |
6459 | | - | |
6460 | | - | |
6461 | | - | |
| 6498 | + | |
| 6499 | + | |
6462 | 6500 | | |
6463 | | - | |
6464 | | - | |
| 6501 | + | |
| 6502 | + | |
| 6503 | + | |
| 6504 | + | |
| 6505 | + | |
6465 | 6506 | | |
6466 | | - | |
6467 | | - | |
6468 | 6507 | | |
6469 | 6508 | | |
6470 | 6509 | | |
| |||
6484 | 6523 | | |
6485 | 6524 | | |
6486 | 6525 | | |
6487 | | - | |
| 6526 | + | |
6488 | 6527 | | |
6489 | 6528 | | |
6490 | 6529 | | |
6491 | | - | |
| 6530 | + | |
6492 | 6531 | | |
6493 | 6532 | | |
6494 | | - | |
| 6533 | + | |
6495 | 6534 | | |
6496 | | - | |
| 6535 | + | |
6497 | 6536 | | |
6498 | 6537 | | |
6499 | | - | |
6500 | 6538 | | |
6501 | 6539 | | |
6502 | 6540 | | |
6503 | 6541 | | |
| 6542 | + | |
6504 | 6543 | | |
6505 | | - | |
6506 | | - | |
| 6544 | + | |
| 6545 | + | |
| 6546 | + | |
| 6547 | + | |
6507 | 6548 | | |
6508 | 6549 | | |
6509 | 6550 | | |
6510 | 6551 | | |
6511 | 6552 | | |
| 6553 | + | |
| 6554 | + | |
6512 | 6555 | | |
6513 | 6556 | | |
6514 | 6557 | | |
6515 | 6558 | | |
| 6559 | + | |
| 6560 | + | |
| 6561 | + | |
| 6562 | + | |
| 6563 | + | |
| 6564 | + | |
| 6565 | + | |
| 6566 | + | |
| 6567 | + | |
| 6568 | + | |
| 6569 | + | |
| 6570 | + | |
| 6571 | + | |
| 6572 | + | |
| 6573 | + | |
| 6574 | + | |
| 6575 | + | |
| 6576 | + | |
| 6577 | + | |
| 6578 | + | |
| 6579 | + | |
| 6580 | + | |
| 6581 | + | |
6516 | 6582 | | |
6517 | 6583 | | |
6518 | 6584 | | |
| |||
9212 | 9278 | | |
9213 | 9279 | | |
9214 | 9280 | | |
9215 | | - | |
9216 | | - | |
| 9281 | + | |
| 9282 | + | |
9217 | 9283 | | |
9218 | 9284 | | |
9219 | 9285 | | |
| |||
9236 | 9302 | | |
9237 | 9303 | | |
9238 | 9304 | | |
9239 | | - | |
9240 | | - | |
9241 | 9305 | | |
9242 | 9306 | | |
9243 | 9307 | | |
| 9308 | + | |
| 9309 | + | |
| 9310 | + | |
| 9311 | + | |
| 9312 | + | |
| 9313 | + | |
| 9314 | + | |
| 9315 | + | |
| 9316 | + | |
| 9317 | + | |
| 9318 | + | |
| 9319 | + | |
| 9320 | + | |
| 9321 | + | |
9244 | 9322 | | |
9245 | 9323 | | |
9246 | 9324 | | |
9247 | 9325 | | |
9248 | | - | |
| 9326 | + | |
9249 | 9327 | | |
9250 | 9328 | | |
9251 | 9329 | | |
9252 | 9330 | | |
9253 | 9331 | | |
9254 | 9332 | | |
9255 | | - | |
9256 | | - | |
| 9333 | + | |
| 9334 | + | |
9257 | 9335 | | |
9258 | 9336 | | |
9259 | 9337 | | |
| |||
11570 | 11648 | | |
11571 | 11649 | | |
11572 | 11650 | | |
11573 | | - | |
11574 | | - | |
| 11651 | + | |
| 11652 | + | |
11575 | 11653 | | |
11576 | 11654 | | |
11577 | 11655 | | |
| |||
11593 | 11671 | | |
11594 | 11672 | | |
11595 | 11673 | | |
11596 | | - | |
11597 | | - | |
11598 | | - | |
11599 | 11674 | | |
11600 | 11675 | | |
11601 | 11676 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
22 | 22 | | |
23 | 23 | | |
24 | 24 | | |
25 | | - | |
| 25 | + | |
26 | 26 | | |
27 | 27 | | |
28 | 28 | | |
| |||
0 commit comments