Skip to content

Commit 3fdaf80

Browse files
committed
tracing: Implement event pid filtering
Add the necessary hooks to use the pids loaded in set_event_pid to filter all the events enabled in the tracing instance that match the pids listed. Two probes are added to both sched_switch and sched_wakeup tracepoints to be called before other probes are called and after the other probes are called. The first is used to set the necessary flags to let the probes know to test if they should be traced or not. The sched_switch pre probe will set the "ignore_pid" flag if neither the previous or next task has a matching pid. The sched_switch probe will set the "ignore_pid" flag if the next task does not match the matching pid. The pre probe allows for probes tracing sched_switch to be traced if necessary. The sched_wakeup pre probe will set the "ignore_pid" flag if neither the current task nor the wakee task has a matching pid. The sched_wakeup post probe will set the "ignore_pid" flag if the current task does not have a matching pid. Cc: "Paul E. McKenney" <paulmck@linux.vnet.ibm.com> Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 4909010 commit 3fdaf80

3 files changed

Lines changed: 154 additions & 3 deletions

File tree

‎include/linux/trace_events.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ enum {
328328
EVENT_FILE_FL_SOFT_DISABLED_BIT,
329329
EVENT_FILE_FL_TRIGGER_MODE_BIT,
330330
EVENT_FILE_FL_TRIGGER_COND_BIT,
331+
EVENT_FILE_FL_PID_FILTER_BIT,
331332
};
332333

333334
/*
@@ -341,6 +342,7 @@ enum {
341342
* tracepoint may be enabled)
342343
* TRIGGER_MODE - When set, invoke the triggers associated with the event
343344
* TRIGGER_COND - When set, one or more triggers has an associated filter
345+
* PID_FILTER - When set, the event is filtered based on pid
344346
*/
345347
enum {
346348
EVENT_FILE_FL_ENABLED = (1 << EVENT_FILE_FL_ENABLED_BIT),
@@ -351,6 +353,7 @@ enum {
351353
EVENT_FILE_FL_SOFT_DISABLED = (1 << EVENT_FILE_FL_SOFT_DISABLED_BIT),
352354
EVENT_FILE_FL_TRIGGER_MODE = (1 << EVENT_FILE_FL_TRIGGER_MODE_BIT),
353355
EVENT_FILE_FL_TRIGGER_COND = (1 << EVENT_FILE_FL_TRIGGER_COND_BIT),
356+
EVENT_FILE_FL_PID_FILTER = (1 << EVENT_FILE_FL_PID_FILTER_BIT),
354357
};
355358

356359
struct trace_event_file {
@@ -429,6 +432,8 @@ extern enum event_trigger_type event_triggers_call(struct trace_event_file *file
429432
extern void event_triggers_post_call(struct trace_event_file *file,
430433
enum event_trigger_type tt);
431434

435+
bool trace_event_ignore_this_pid(struct trace_event_file *trace_file);
436+
432437
/**
433438
* trace_trigger_soft_disabled - do triggers and test if soft disabled
434439
* @file: The file pointer of the event to test
@@ -448,6 +453,8 @@ trace_trigger_soft_disabled(struct trace_event_file *file)
448453
event_triggers_call(file, NULL);
449454
if (eflags & EVENT_FILE_FL_SOFT_DISABLED)
450455
return true;
456+
if (eflags & EVENT_FILE_FL_PID_FILTER)
457+
return trace_event_ignore_this_pid(file);
451458
}
452459
return false;
453460
}

‎kernel/trace/trace.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,8 @@ struct trace_array_cpu {
156156
pid_t pid;
157157
kuid_t uid;
158158
char comm[TASK_COMM_LEN];
159+
160+
bool ignore_pid;
159161
};
160162

161163
struct tracer;

‎kernel/trace/trace_events.c‎

Lines changed: 145 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include <linux/slab.h>
2323
#include <linux/delay.h>
2424

25+
#include <trace/events/sched.h>
26+
2527
#include <asm/setup.h>
2628

2729
#include "trace_output.h"
@@ -212,12 +214,32 @@ int trace_event_raw_init(struct trace_event_call *call)
212214
}
213215
EXPORT_SYMBOL_GPL(trace_event_raw_init);
214216

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+
215233
void *trace_event_buffer_reserve(struct trace_event_buffer *fbuffer,
216234
struct trace_event_file *trace_file,
217235
unsigned long len)
218236
{
219237
struct trace_event_call *event_call = trace_file->event_call;
220238

239+
if ((trace_file->flags & EVENT_FILE_FL_PID_FILTER) &&
240+
trace_event_ignore_this_pid(trace_file))
241+
return NULL;
242+
221243
local_save_flags(fbuffer->flags);
222244
fbuffer->pc = preempt_count();
223245
fbuffer->trace_file = trace_file;
@@ -459,15 +481,114 @@ static int cmp_pid(const void *key, const void *elt)
459481
return 1;
460482
}
461483

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+
462568
static void __ftrace_clear_event_pids(struct trace_array *tr)
463569
{
464570
struct trace_pid_list *pid_list;
571+
struct trace_event_file *file;
572+
int cpu;
465573

466574
pid_list = rcu_dereference_protected(tr->filtered_pids,
467575
lockdep_is_held(&event_mutex));
468576
if (!pid_list)
469577
return;
470578

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+
471592
rcu_assign_pointer(tr->filtered_pids, NULL);
472593

473594
/* Wait till all users are no longer using pid filtering */
@@ -1429,13 +1550,14 @@ static int max_pids(struct trace_pid_list *pid_list)
14291550
}
14301551

14311552
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,
14331554
size_t cnt, loff_t *ppos)
14341555
{
1435-
struct seq_file *m = file->private_data;
1556+
struct seq_file *m = filp->private_data;
14361557
struct trace_array *tr = m->private;
14371558
struct trace_pid_list *filtered_pids = NULL;
14381559
struct trace_pid_list *pid_list = NULL;
1560+
struct trace_event_file *file;
14391561
struct trace_parser parser;
14401562
unsigned long val;
14411563
loff_t this_pos;
@@ -1564,15 +1686,35 @@ ftrace_event_pid_write(struct file *file, const char __user *ubuf,
15641686

15651687
rcu_assign_pointer(tr->filtered_pids, pid_list);
15661688

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+
}
15681692

15691693
if (filtered_pids) {
15701694
synchronize_sched();
15711695

15721696
free_pages((unsigned long)filtered_pids->pids, filtered_pids->order);
15731697
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);
15741714
}
15751715

1716+
mutex_unlock(&event_mutex);
1717+
15761718
ret = read;
15771719
*ppos += read;
15781720

0 commit comments

Comments
 (0)