Skip to content

Commit 6954e41

Browse files
committed
tracing: Place trace_pid_list logic into abstract functions
Instead of having the logic that does trace_pid_list open coded, wrap it in abstract functions. This will allow a rewrite of the logic that implements the trace_pid_list without affecting the users. Note, this causes a change in behavior. Every time a pid is written into the set_*_pid file, it creates a new list and uses RCU to update it. If pid_max is lowered, but there was a pid currently in the list that was higher than pid_max, those pids will now be removed on updating the list. The old behavior kept that from happening. The rewrite of the pid_list logic will no longer depend on pid_max, and will return the old behavior. Signed-off-by: Steven Rostedt (VMware) <[email protected]>
1 parent bf094cf commit 6954e41

File tree

7 files changed

+217
-61
lines changed

7 files changed

+217
-61
lines changed

kernel/trace/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ obj-$(CONFIG_TRACING) += trace_output.o
4747
obj-$(CONFIG_TRACING) += trace_seq.o
4848
obj-$(CONFIG_TRACING) += trace_stat.o
4949
obj-$(CONFIG_TRACING) += trace_printk.o
50+
obj-$(CONFIG_TRACING) += pid_list.o
5051
obj-$(CONFIG_TRACING_MAP) += tracing_map.o
5152
obj-$(CONFIG_PREEMPTIRQ_DELAY_TEST) += preemptirq_delay_test.o
5253
obj-$(CONFIG_SYNTH_EVENT_GEN_TEST) += synth_event_gen_test.o

kernel/trace/ftrace.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7184,10 +7184,10 @@ static void clear_ftrace_pids(struct trace_array *tr, int type)
71847184
synchronize_rcu();
71857185

71867186
if ((type & TRACE_PIDS) && pid_list)
7187-
trace_free_pid_list(pid_list);
7187+
trace_pid_list_free(pid_list);
71887188

71897189
if ((type & TRACE_NO_PIDS) && no_pid_list)
7190-
trace_free_pid_list(no_pid_list);
7190+
trace_pid_list_free(no_pid_list);
71917191
}
71927192

71937193
void ftrace_clear_pids(struct trace_array *tr)
@@ -7428,7 +7428,7 @@ pid_write(struct file *filp, const char __user *ubuf,
74287428

74297429
if (filtered_pids) {
74307430
synchronize_rcu();
7431-
trace_free_pid_list(filtered_pids);
7431+
trace_pid_list_free(filtered_pids);
74327432
} else if (pid_list && !other_pids) {
74337433
/* Register a probe to set whether to ignore the tracing of a task */
74347434
register_trace_sched_switch(ftrace_filter_pid_sched_switch_probe, tr);

kernel/trace/pid_list.c

Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
/*
3+
* Copyright (C) 2021 VMware Inc, Steven Rostedt <[email protected]>
4+
*/
5+
#include <linux/vmalloc.h>
6+
#include <linux/slab.h>
7+
#include "trace.h"
8+
9+
/**
10+
* trace_pid_list_is_set - test if the pid is set in the list
11+
* @pid_list: The pid list to test
12+
* @pid: The pid to to see if set in the list.
13+
*
14+
* Tests if @pid is is set in the @pid_list. This is usually called
15+
* from the scheduler when a task is scheduled. Its pid is checked
16+
* if it should be traced or not.
17+
*
18+
* Return true if the pid is in the list, false otherwise.
19+
*/
20+
bool trace_pid_list_is_set(struct trace_pid_list *pid_list, unsigned int pid)
21+
{
22+
/*
23+
* If pid_max changed after filtered_pids was created, we
24+
* by default ignore all pids greater than the previous pid_max.
25+
*/
26+
if (pid >= pid_list->pid_max)
27+
return false;
28+
29+
return test_bit(pid, pid_list->pids);
30+
}
31+
32+
/**
33+
* trace_pid_list_set - add a pid to the list
34+
* @pid_list: The pid list to add the @pid to.
35+
* @pid: The pid to add.
36+
*
37+
* Adds @pid to @pid_list. This is usually done explicitly by a user
38+
* adding a task to be traced, or indirectly by the fork function
39+
* when children should be traced and a task's pid is in the list.
40+
*
41+
* Return 0 on success, negative otherwise.
42+
*/
43+
int trace_pid_list_set(struct trace_pid_list *pid_list, unsigned int pid)
44+
{
45+
/* Sorry, but we don't support pid_max changing after setting */
46+
if (pid >= pid_list->pid_max)
47+
return -EINVAL;
48+
49+
set_bit(pid, pid_list->pids);
50+
51+
return 0;
52+
}
53+
54+
/**
55+
* trace_pid_list_clear - remove a pid from the list
56+
* @pid_list: The pid list to remove the @pid from.
57+
* @pid: The pid to remove.
58+
*
59+
* Removes @pid from @pid_list. This is usually done explicitly by a user
60+
* removing tasks from tracing, or indirectly by the exit function
61+
* when a task that is set to be traced exits.
62+
*
63+
* Return 0 on success, negative otherwise.
64+
*/
65+
int trace_pid_list_clear(struct trace_pid_list *pid_list, unsigned int pid)
66+
{
67+
/* Sorry, but we don't support pid_max changing after setting */
68+
if (pid >= pid_list->pid_max)
69+
return -EINVAL;
70+
71+
clear_bit(pid, pid_list->pids);
72+
73+
return 0;
74+
}
75+
76+
/**
77+
* trace_pid_list_next - return the next pid in the list
78+
* @pid_list: The pid list to examine.
79+
* @pid: The pid to start from
80+
* @next: The pointer to place the pid that is set starting from @pid.
81+
*
82+
* Looks for the next consecutive pid that is in @pid_list starting
83+
* at the pid specified by @pid. If one is set (including @pid), then
84+
* that pid is placed into @next.
85+
*
86+
* Return 0 when a pid is found, -1 if there are no more pids included.
87+
*/
88+
int trace_pid_list_next(struct trace_pid_list *pid_list, unsigned int pid,
89+
unsigned int *next)
90+
{
91+
pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
92+
93+
if (pid < pid_list->pid_max) {
94+
*next = pid;
95+
return 0;
96+
}
97+
return -1;
98+
}
99+
100+
/**
101+
* trace_pid_list_first - return the first pid in the list
102+
* @pid_list: The pid list to examine.
103+
* @pid: The pointer to place the pid first found pid that is set.
104+
*
105+
* Looks for the first pid that is set in @pid_list, and places it
106+
* into @pid if found.
107+
*
108+
* Return 0 when a pid is found, -1 if there are no pids set.
109+
*/
110+
int trace_pid_list_first(struct trace_pid_list *pid_list, unsigned int *pid)
111+
{
112+
unsigned int first;
113+
114+
first = find_first_bit(pid_list->pids, pid_list->pid_max);
115+
116+
if (first < pid_list->pid_max) {
117+
*pid = first;
118+
return 0;
119+
}
120+
return -1;
121+
}
122+
123+
/**
124+
* trace_pid_list_alloc - create a new pid_list
125+
*
126+
* Allocates a new pid_list to store pids into.
127+
*
128+
* Returns the pid_list on success, NULL otherwise.
129+
*/
130+
struct trace_pid_list *trace_pid_list_alloc(void)
131+
{
132+
struct trace_pid_list *pid_list;
133+
134+
pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
135+
if (!pid_list)
136+
return NULL;
137+
138+
pid_list->pid_max = READ_ONCE(pid_max);
139+
140+
pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
141+
if (!pid_list->pids) {
142+
kfree(pid_list);
143+
return NULL;
144+
}
145+
return pid_list;
146+
}
147+
148+
/**
149+
* trace_pid_list_free - Frees an allocated pid_list.
150+
*
151+
* Frees the memory for a pid_list that was allocated.
152+
*/
153+
void trace_pid_list_free(struct trace_pid_list *pid_list)
154+
{
155+
if (!pid_list)
156+
return;
157+
158+
vfree(pid_list->pids);
159+
kfree(pid_list);
160+
}

kernel/trace/pid_list.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
3+
/* Do not include this file directly. */
4+
5+
#ifndef _TRACE_INTERNAL_PID_LIST_H
6+
#define _TRACE_INTERNAL_PID_LIST_H
7+
8+
struct trace_pid_list {
9+
int pid_max;
10+
unsigned long *pids;
11+
};
12+
13+
#endif /* _TRACE_INTERNAL_PID_LIST_H */

kernel/trace/trace.c

Lines changed: 27 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -512,12 +512,6 @@ int call_filter_check_discard(struct trace_event_call *call, void *rec,
512512
return 0;
513513
}
514514

515-
void trace_free_pid_list(struct trace_pid_list *pid_list)
516-
{
517-
vfree(pid_list->pids);
518-
kfree(pid_list);
519-
}
520-
521515
/**
522516
* trace_find_filtered_pid - check if a pid exists in a filtered_pid list
523517
* @filtered_pids: The list of pids to check
@@ -528,14 +522,7 @@ void trace_free_pid_list(struct trace_pid_list *pid_list)
528522
bool
529523
trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
530524
{
531-
/*
532-
* If pid_max changed after filtered_pids was created, we
533-
* by default ignore all pids greater than the previous pid_max.
534-
*/
535-
if (search_pid >= filtered_pids->pid_max)
536-
return false;
537-
538-
return test_bit(search_pid, filtered_pids->pids);
525+
return trace_pid_list_is_set(filtered_pids, search_pid);
539526
}
540527

541528
/**
@@ -592,15 +579,11 @@ void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
592579
return;
593580
}
594581

595-
/* Sorry, but we don't support pid_max changing after setting */
596-
if (task->pid >= pid_list->pid_max)
597-
return;
598-
599582
/* "self" is set for forks, and NULL for exits */
600583
if (self)
601-
set_bit(task->pid, pid_list->pids);
584+
trace_pid_list_set(pid_list, task->pid);
602585
else
603-
clear_bit(task->pid, pid_list->pids);
586+
trace_pid_list_clear(pid_list, task->pid);
604587
}
605588

606589
/**
@@ -617,18 +600,19 @@ void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
617600
*/
618601
void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
619602
{
620-
unsigned long pid = (unsigned long)v;
603+
long pid = (unsigned long)v;
604+
unsigned int next;
621605

622606
(*pos)++;
623607

624608
/* pid already is +1 of the actual previous bit */
625-
pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
609+
if (trace_pid_list_next(pid_list, pid, &next) < 0)
610+
return NULL;
626611

627-
/* Return pid + 1 to allow zero to be represented */
628-
if (pid < pid_list->pid_max)
629-
return (void *)(pid + 1);
612+
pid = next;
630613

631-
return NULL;
614+
/* Return pid + 1 to allow zero to be represented */
615+
return (void *)(pid + 1);
632616
}
633617

634618
/**
@@ -645,12 +629,14 @@ void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
645629
void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
646630
{
647631
unsigned long pid;
632+
unsigned int first;
648633
loff_t l = 0;
649634

650-
pid = find_first_bit(pid_list->pids, pid_list->pid_max);
651-
if (pid >= pid_list->pid_max)
635+
if (trace_pid_list_first(pid_list, &first) < 0)
652636
return NULL;
653637

638+
pid = first;
639+
654640
/* Return pid + 1 so that zero can be the exit value */
655641
for (pid++; pid && l < *pos;
656642
pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
@@ -686,7 +672,7 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,
686672
unsigned long val;
687673
int nr_pids = 0;
688674
ssize_t read = 0;
689-
ssize_t ret = 0;
675+
ssize_t ret;
690676
loff_t pos;
691677
pid_t pid;
692678

@@ -699,34 +685,23 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,
699685
* the user. If the operation fails, then the current list is
700686
* not modified.
701687
*/
702-
pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
688+
pid_list = trace_pid_list_alloc();
703689
if (!pid_list) {
704690
trace_parser_put(&parser);
705691
return -ENOMEM;
706692
}
707693

708-
pid_list->pid_max = READ_ONCE(pid_max);
709-
710-
/* Only truncating will shrink pid_max */
711-
if (filtered_pids && filtered_pids->pid_max > pid_list->pid_max)
712-
pid_list->pid_max = filtered_pids->pid_max;
713-
714-
pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
715-
if (!pid_list->pids) {
716-
trace_parser_put(&parser);
717-
kfree(pid_list);
718-
return -ENOMEM;
719-
}
720-
721694
if (filtered_pids) {
722695
/* copy the current bits to the new max */
723-
for_each_set_bit(pid, filtered_pids->pids,
724-
filtered_pids->pid_max) {
725-
set_bit(pid, pid_list->pids);
696+
ret = trace_pid_list_first(filtered_pids, &pid);
697+
while (!ret) {
698+
trace_pid_list_set(pid_list, pid);
699+
ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
726700
nr_pids++;
727701
}
728702
}
729703

704+
ret = 0;
730705
while (cnt > 0) {
731706

732707
pos = 0;
@@ -742,12 +717,13 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,
742717
ret = -EINVAL;
743718
if (kstrtoul(parser.buffer, 0, &val))
744719
break;
745-
if (val >= pid_list->pid_max)
746-
break;
747720

748721
pid = (pid_t)val;
749722

750-
set_bit(pid, pid_list->pids);
723+
if (trace_pid_list_set(pid_list, pid) < 0) {
724+
ret = -1;
725+
break;
726+
}
751727
nr_pids++;
752728

753729
trace_parser_clear(&parser);
@@ -756,13 +732,13 @@ int trace_pid_write(struct trace_pid_list *filtered_pids,
756732
trace_parser_put(&parser);
757733

758734
if (ret < 0) {
759-
trace_free_pid_list(pid_list);
735+
trace_pid_list_free(pid_list);
760736
return ret;
761737
}
762738

763739
if (!nr_pids) {
764740
/* Cleared the list of pids */
765-
trace_free_pid_list(pid_list);
741+
trace_pid_list_free(pid_list);
766742
read = ret;
767743
pid_list = NULL;
768744
}

0 commit comments

Comments
 (0)