|
22 | 22 | #include <linux/slab.h> |
23 | 23 | #include <linux/delay.h> |
24 | 24 |
|
| 25 | +#include <trace/events/sched.h> |
| 26 | + |
25 | 27 | #include <asm/setup.h> |
26 | 28 |
|
27 | 29 | #include "trace_output.h" |
@@ -212,12 +214,32 @@ int trace_event_raw_init(struct trace_event_call *call) |
212 | 214 | } |
213 | 215 | EXPORT_SYMBOL_GPL(trace_event_raw_init); |
214 | 216 |
|
| 217 | +bool trace_event_ignore_this_pid(struct trace_event_file *trace_file) |
| 218 | +{ |
| 219 | + struct trace_array *tr = trace_file->tr; |
| 220 | + struct trace_array_cpu *data; |
| 221 | + struct trace_pid_list *pid_list; |
| 222 | + |
| 223 | + pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 224 | + if (!pid_list) |
| 225 | + return false; |
| 226 | + |
| 227 | + data = this_cpu_ptr(tr->trace_buffer.data); |
| 228 | + |
| 229 | + return data->ignore_pid; |
| 230 | +} |
| 231 | +EXPORT_SYMBOL_GPL(trace_event_ignore_this_pid); |
| 232 | + |
215 | 233 | void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer, |
216 | 234 | struct trace_event_file *trace_file, |
217 | 235 | unsigned long len) |
218 | 236 | { |
219 | 237 | struct trace_event_call *event_call = trace_file->event_call; |
220 | 238 |
|
| 239 | + if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) && |
| 240 | + trace_event_ignore_this_pid(trace_file)) |
| 241 | + return NULL; |
| 242 | + |
221 | 243 | local_save_flags(fbuffer->flags); |
222 | 244 | fbuffer->pc = preempt_count(); |
223 | 245 | fbuffer->trace_file = trace_file; |
@@ -459,15 +481,114 @@ static int cmp_pid(const void *key, const void *elt) |
459 | 481 | return 1; |
460 | 482 | } |
461 | 483 |
|
| 484 | +static bool |
| 485 | +check_ignore_pid(struct trace_pid_list *filtered_pids, struct task_struct *task) |
| 486 | +{ |
| 487 | + pid_t search_pid; |
| 488 | + pid_t *pid; |
| 489 | + |
| 490 | + /* |
| 491 | + * Return false, because if filtered_pids does not exist, |
| 492 | + * all pids are good to trace. |
| 493 | + */ |
| 494 | + if (!filtered_pids) |
| 495 | + return false; |
| 496 | + |
| 497 | + search_pid = task->pid; |
| 498 | + |
| 499 | + pid = bsearch(&search_pid, filtered_pids->pids, |
| 500 | + filtered_pids->nr_pids, sizeof(pid_t), |
| 501 | + cmp_pid); |
| 502 | + if (!pid) |
| 503 | + return true; |
| 504 | + |
| 505 | + return false; |
| 506 | +} |
| 507 | + |
| 508 | +static void |
| 509 | +event_filter_pid_sched_switch_probe_pre(void *data, |
| 510 | + struct task_struct *prev, struct task_struct *next) |
| 511 | +{ |
| 512 | + struct trace_array *tr = data; |
| 513 | + struct trace_pid_list *pid_list; |
| 514 | + |
| 515 | + pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 516 | + |
| 517 | + this_cpu_write(tr->trace_buffer.data->ignore_pid, |
| 518 | + check_ignore_pid(pid_list, prev) && |
| 519 | + check_ignore_pid(pid_list, next)); |
| 520 | +} |
| 521 | + |
| 522 | +static void |
| 523 | +event_filter_pid_sched_switch_probe_post(void *data, |
| 524 | + struct task_struct *prev, struct task_struct *next) |
| 525 | +{ |
| 526 | + struct trace_array *tr = data; |
| 527 | + struct trace_pid_list *pid_list; |
| 528 | + |
| 529 | + pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 530 | + |
| 531 | + this_cpu_write(tr->trace_buffer.data->ignore_pid, |
| 532 | + check_ignore_pid(pid_list, next)); |
| 533 | +} |
| 534 | + |
| 535 | +static void |
| 536 | +event_filter_pid_sched_wakeup_probe_pre(void *data, struct task_struct *task) |
| 537 | +{ |
| 538 | + struct trace_array *tr = data; |
| 539 | + struct trace_pid_list *pid_list; |
| 540 | + |
| 541 | + /* Nothing to do if we are already tracing */ |
| 542 | + if (!this_cpu_read(tr->trace_buffer.data->ignore_pid)) |
| 543 | + return; |
| 544 | + |
| 545 | + pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 546 | + |
| 547 | + this_cpu_write(tr->trace_buffer.data->ignore_pid, |
| 548 | + check_ignore_pid(pid_list, task)); |
| 549 | +} |
| 550 | + |
| 551 | +static void |
| 552 | +event_filter_pid_sched_wakeup_probe_post(void *data, struct task_struct *task) |
| 553 | +{ |
| 554 | + struct trace_array *tr = data; |
| 555 | + struct trace_pid_list *pid_list; |
| 556 | + |
| 557 | + /* Nothing to do if we are not tracing */ |
| 558 | + if (this_cpu_read(tr->trace_buffer.data->ignore_pid)) |
| 559 | + return; |
| 560 | + |
| 561 | + pid_list = rcu_dereference_sched(tr->filtered_pids); |
| 562 | + |
| 563 | + /* Set tracing if current is enabled */ |
| 564 | + this_cpu_write(tr->trace_buffer.data->ignore_pid, |
| 565 | + check_ignore_pid(pid_list, current)); |
| 566 | +} |
| 567 | + |
462 | 568 | static void __ftrace_clear_event_pids(struct trace_array *tr) |
463 | 569 | { |
464 | 570 | struct trace_pid_list *pid_list; |
| 571 | + struct trace_event_file *file; |
| 572 | + int cpu; |
465 | 573 |
|
466 | 574 | pid_list = rcu_dereference_protected(tr->filtered_pids, |
467 | 575 | lockdep_is_held(&event_mutex)); |
468 | 576 | if (!pid_list) |
469 | 577 | return; |
470 | 578 |
|
| 579 | + unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_pre, tr); |
| 580 | + unregister_trace_sched_switch(event_filter_pid_sched_switch_probe_post, tr); |
| 581 | + |
| 582 | + unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, tr); |
| 583 | + unregister_trace_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, tr); |
| 584 | + |
| 585 | + list_for_each_entry(file, &tr->events, list) { |
| 586 | + clear_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); |
| 587 | + } |
| 588 | + |
| 589 | + for_each_possible_cpu(cpu) |
| 590 | + per_cpu_ptr(tr->trace_buffer.data, cpu)->ignore_pid = false; |
| 591 | + |
471 | 592 | rcu_assign_pointer(tr->filtered_pids, NULL); |
472 | 593 |
|
473 | 594 | /* Wait till all users are no longer using pid filtering */ |
@@ -1429,13 +1550,14 @@ static int max_pids(struct trace_pid_list *pid_list) |
1429 | 1550 | } |
1430 | 1551 |
|
1431 | 1552 | static ssize_t |
1432 | | -ftrace_event_pid_write(struct file *file, const char __user *ubuf, |
| 1553 | +ftrace_event_pid_write(struct file *filp, const char __user *ubuf, |
1433 | 1554 | size_t cnt, loff_t *ppos) |
1434 | 1555 | { |
1435 | | - struct seq_file *m = file->private_data; |
| 1556 | + struct seq_file *m = filp->private_data; |
1436 | 1557 | struct trace_array *tr = m->private; |
1437 | 1558 | struct trace_pid_list *filtered_pids = NULL; |
1438 | 1559 | struct trace_pid_list *pid_list = NULL; |
| 1560 | + struct trace_event_file *file; |
1439 | 1561 | struct trace_parser parser; |
1440 | 1562 | unsigned long val; |
1441 | 1563 | loff_t this_pos; |
@@ -1564,15 +1686,35 @@ ftrace_event_pid_write(struct file *file, const char __user *ubuf, |
1564 | 1686 |
|
1565 | 1687 | rcu_assign_pointer(tr->filtered_pids, pid_list); |
1566 | 1688 |
|
1567 | | - mutex_unlock(&event_mutex); |
| 1689 | + list_for_each_entry(file, &tr->events, list) { |
| 1690 | + set_bit(EVENT_FILE_FL_PID_FILTER_BIT, &file->flags); |
| 1691 | + } |
1568 | 1692 |
|
1569 | 1693 | if (filtered_pids) { |
1570 | 1694 | synchronize_sched(); |
1571 | 1695 |
|
1572 | 1696 | free_pages((unsigned long)filtered_pids->pids, filtered_pids->order); |
1573 | 1697 | kfree(filtered_pids); |
| 1698 | + } else { |
| 1699 | + /* |
| 1700 | + * Register a probe that is called before all other probes |
| 1701 | + * to set ignore_pid if next or prev do not match. |
| 1702 | + * Register a probe this is called after all other probes |
| 1703 | + * to only keep ignore_pid set if next pid matches. |
| 1704 | + */ |
| 1705 | + register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_pre, |
| 1706 | + tr, INT_MAX); |
| 1707 | + register_trace_prio_sched_switch(event_filter_pid_sched_switch_probe_post, |
| 1708 | + tr, 0); |
| 1709 | + |
| 1710 | + register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_pre, |
| 1711 | + tr, INT_MAX); |
| 1712 | + register_trace_prio_sched_wakeup(event_filter_pid_sched_wakeup_probe_post, |
| 1713 | + tr, 0); |
1574 | 1714 | } |
1575 | 1715 |
|
| 1716 | + mutex_unlock(&event_mutex); |
| 1717 | + |
1576 | 1718 | ret = read; |
1577 | 1719 | *ppos += read; |
1578 | 1720 |
|
|
0 commit comments