Skip to content

Commit 76c813e

Browse files
committed
tracing: Move pid_list write processing into its own function
The addition of PIDs into a pid_list via the write operation of set_event_pid is a bit complex. The same operation will be needed for function tracing pids. Move the code into its own generic function in trace.c, so that we can avoid duplication of this code. Signed-off-by: Steven Rostedt <rostedt@goodmis.org>
1 parent 5cc8976 commit 76c813e

3 files changed

Lines changed: 124 additions & 102 deletions

File tree

‎kernel/trace/trace.c‎

Lines changed: 108 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include <linux/hardirq.h>
2626
#include <linux/linkage.h>
2727
#include <linux/uaccess.h>
28-
#include <linux/kprobes.h>
28+
#include <linux/vmalloc.h>
2929
#include <linux/ftrace.h>
3030
#include <linux/module.h>
3131
#include <linux/percpu.h>
@@ -319,6 +319,12 @@ int call_filter_check_discard(struct trace_event_call *call, void *rec,
319319
return 0;
320320
}
321321

322+
void trace_free_pid_list(struct trace_pid_list *pid_list)
323+
{
324+
vfree(pid_list->pids);
325+
kfree(pid_list);
326+
}
327+
322328
/**
323329
* trace_find_filtered_pid - check if a pid exists in a filtered_pid list
324330
* @filtered_pids: The list of pids to check
@@ -468,6 +474,107 @@ int trace_pid_show(struct seq_file *m, void *v)
468474
return 0;
469475
}
470476

477+
/* 128 should be much more than enough */
478+
#define PID_BUF_SIZE 127
479+
480+
int trace_pid_write(struct trace_pid_list *filtered_pids,
481+
struct trace_pid_list **new_pid_list,
482+
const char __user *ubuf, size_t cnt)
483+
{
484+
struct trace_pid_list *pid_list;
485+
struct trace_parser parser;
486+
unsigned long val;
487+
int nr_pids = 0;
488+
ssize_t read = 0;
489+
ssize_t ret = 0;
490+
loff_t pos;
491+
pid_t pid;
492+
493+
if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
494+
return -ENOMEM;
495+
496+
/*
497+
* Always recreate a new array. The write is an all or nothing
498+
* operation. Always create a new array when adding new pids by
499+
* the user. If the operation fails, then the current list is
500+
* not modified.
501+
*/
502+
pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
503+
if (!pid_list)
504+
return -ENOMEM;
505+
506+
pid_list->pid_max = READ_ONCE(pid_max);
507+
508+
/* Only truncating will shrink pid_max */
509+
if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
510+
pid_list->pid_max = filtered_pids->pid_max;
511+
512+
pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
513+
if (!pid_list->pids) {
514+
kfree(pid_list);
515+
return -ENOMEM;
516+
}
517+
518+
if (filtered_pids) {
519+
/* copy the current bits to the new max */
520+
pid = find_first_bit(filtered_pids->pids,
521+
filtered_pids->pid_max);
522+
while (pid < filtered_pids->pid_max) {
523+
set_bit(pid, pid_list->pids);
524+
pid = find_next_bit(filtered_pids->pids,
525+
filtered_pids->pid_max,
526+
pid + 1);
527+
nr_pids++;
528+
}
529+
}
530+
531+
while (cnt > 0) {
532+
533+
pos = 0;
534+
535+
ret = trace_get_user(&parser, ubuf, cnt, &pos);
536+
if (ret < 0 || !trace_parser_loaded(&parser))
537+
break;
538+
539+
read += ret;
540+
ubuf += ret;
541+
cnt -= ret;
542+
543+
parser.buffer[parser.idx] = 0;
544+
545+
ret = -EINVAL;
546+
if (kstrtoul(parser.buffer, 0, &val))
547+
break;
548+
if (val >= pid_list->pid_max)
549+
break;
550+
551+
pid = (pid_t)val;
552+
553+
set_bit(pid, pid_list->pids);
554+
nr_pids++;
555+
556+
trace_parser_clear(&parser);
557+
ret = 0;
558+
}
559+
trace_parser_put(&parser);
560+
561+
if (ret < 0) {
562+
trace_free_pid_list(pid_list);
563+
return ret;
564+
}
565+
566+
if (!nr_pids) {
567+
/* Cleared the list of pids */
568+
trace_free_pid_list(pid_list);
569+
read = ret;
570+
pid_list = NULL;
571+
}
572+
573+
*new_pid_list = pid_list;
574+
575+
return read;
576+
}
577+
471578
static cycle_t buffer_ftrace_now(struct trace_buffer *buf, int cpu)
472579
{
473580
u64 ts;

‎kernel/trace/trace.h‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,9 @@ extern unsigned long nsecs_to_usecs(unsigned long nsecs);
629629
extern unsigned long tracing_thresh;
630630

631631
/* PID filtering */
632+
633+
extern int pid_max;
634+
632635
bool trace_find_filtered_pid(struct trace_pid_list *filtered_pids,
633636
pid_t search_pid);
634637
bool trace_ignore_this_task(struct trace_pid_list *filtered_pids,
@@ -639,6 +642,10 @@ void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
639642
void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos);
640643
void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos);
641644
int trace_pid_show(struct seq_file *m, void *v);
645+
void trace_free_pid_list(struct trace_pid_list *pid_list);
646+
int trace_pid_write(struct trace_pid_list *filtered_pids,
647+
struct trace_pid_list **new_pid_list,
648+
const char __user *ubuf, size_t cnt);
642649

643650
#ifdef CONFIG_TRACER_MAX_TRACE
644651
void update_max_tr(struct trace_array *tr, struct task_struct *tsk, int cpu);

‎kernel/trace/trace_events.c‎

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
#include <linux/kthread.h>
1616
#include <linux/tracefs.h>
1717
#include <linux/uaccess.h>
18-
#include <linux/vmalloc.h>
1918
#include <linux/module.h>
2019
#include <linux/ctype.h>
2120
#include <linux/sort.h>
@@ -499,9 +498,6 @@ static void ftrace_clear_events(struct trace_array *tr)
499498
mutex_unlock(&event_mutex);
500499
}
501500

502-
/* Shouldn't this be in a header? */
503-
extern int pid_max;
504-
505501
static void
506502
event_filter_pid_sched_process_exit(void *data, struct task_struct *task)
507503
{
@@ -634,8 +630,7 @@ static void __ftrace_clear_event_pids(struct trace_array *tr)
634630
/* Wait till all users are no longer using pid filtering */
635631
synchronize_sched();
636632

637-
vfree(pid_list->pids);
638-
kfree(pid_list);
633+
trace_free_pid_list(pid_list);
639634
}
640635

641636
static void ftrace_clear_event_pids(struct trace_array *tr)
@@ -1587,13 +1582,7 @@ ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
15871582
struct trace_pid_list *filtered_pids = NULL;
15881583
struct trace_pid_list *pid_list;
15891584
struct trace_event_file *file;
1590-
struct trace_parser parser;
1591-
unsigned long val;
1592-
loff_t this_pos;
1593-
ssize_t read = 0;
1594-
ssize_t ret = 0;
1595-
pid_t pid;
1596-
int nr_pids = 0;
1585+
ssize_t ret;
15971586

15981587
if (!cnt)
15991588
return 0;
@@ -1602,93 +1591,15 @@ ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
16021591
if (ret < 0)
16031592
return ret;
16041593

1605-
if (trace_parser_get_init(&parser, EVENT_BUF_SIZE + 1))
1606-
return -ENOMEM;
1607-
16081594
mutex_lock(&event_mutex);
1595+
16091596
filtered_pids = rcu_dereference_protected(tr->filtered_pids,
16101597
lockdep_is_held(&event_mutex));
16111598

1612-
/*
1613-
* Always recreate a new array. The write is an all or nothing
1614-
* operation. Always create a new array when adding new pids by
1615-
* the user. If the operation fails, then the current list is
1616-
* not modified.
1617-
*/
1618-
pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
1619-
if (!pid_list) {
1620-
read = -ENOMEM;
1621-
goto out;
1622-
}
1623-
pid_list->pid_max = READ_ONCE(pid_max);
1624-
/* Only truncating will shrink pid_max */
1625-
if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
1626-
pid_list->pid_max = filtered_pids->pid_max;
1627-
pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
1628-
if (!pid_list->pids) {
1629-
kfree(pid_list);
1630-
read = -ENOMEM;
1631-
goto out;
1632-
}
1633-
if (filtered_pids) {
1634-
/* copy the current bits to the new max */
1635-
pid = find_first_bit(filtered_pids->pids,
1636-
filtered_pids->pid_max);
1637-
while (pid < filtered_pids->pid_max) {
1638-
set_bit(pid, pid_list->pids);
1639-
pid = find_next_bit(filtered_pids->pids,
1640-
filtered_pids->pid_max,
1641-
pid + 1);
1642-
nr_pids++;
1643-
}
1644-
}
1645-
1646-
while (cnt > 0) {
1647-
1648-
this_pos = 0;
1649-
1650-
ret = trace_get_user(&parser, ubuf, cnt, &this_pos);
1651-
if (ret < 0 || !trace_parser_loaded(&parser))
1652-
break;
1653-
1654-
read += ret;
1655-
ubuf += ret;
1656-
cnt -= ret;
1657-
1658-
parser.buffer[parser.idx] = 0;
1659-
1660-
ret = -EINVAL;
1661-
if (kstrtoul(parser.buffer, 0, &val))
1662-
break;
1663-
if (val >= pid_list->pid_max)
1664-
break;
1665-
1666-
pid = (pid_t)val;
1667-
1668-
set_bit(pid, pid_list->pids);
1669-
nr_pids++;
1670-
1671-
trace_parser_clear(&parser);
1672-
ret = 0;
1673-
}
1674-
trace_parser_put(&parser);
1675-
1676-
if (ret < 0) {
1677-
vfree(pid_list->pids);
1678-
kfree(pid_list);
1679-
read = ret;
1599+
ret = trace_pid_write(filtered_pids, &pid_list, ubuf, cnt);
1600+
if (ret < 0)
16801601
goto out;
1681-
}
16821602

1683-
if (!nr_pids) {
1684-
/* Cleared the list of pids */
1685-
vfree(pid_list->pids);
1686-
kfree(pid_list);
1687-
read = ret;
1688-
if (!filtered_pids)
1689-
goto out;
1690-
pid_list = NULL;
1691-
}
16921603
rcu_assign_pointer(tr->filtered_pids, pid_list);
16931604

16941605
list_for_each_entry(file, &tr->events, list) {
@@ -1697,10 +1608,8 @@ ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
16971608

16981609
if (filtered_pids) {
16991610
synchronize_sched();
1700-
1701-
vfree(filtered_pids->pids);
1702-
kfree(filtered_pids);
1703-
} else {
1611+
trace_free_pid_list(filtered_pids);
1612+
} else if (pid_list) {
17041613
/*
17051614
* Register a probe that is called before all other probes
17061615
* to set ignore_pid if next or prev do not match.
@@ -1738,9 +1647,8 @@ ftrace_event_pid_write(struct file *filp, const char __user *ubuf,
17381647
out:
17391648
mutex_unlock(&event_mutex);
17401649

1741-
ret = read;
1742-
if (read > 0)
1743-
*ppos += read;
1650+
if (ret > 0)
1651+
*ppos += ret;
17441652

17451653
return ret;
17461654
}

0 commit comments

Comments
 (0)