• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2021 VMware Inc, Steven Rostedt <rostedt@goodmis.org>
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  */
trace_pid_list_is_set(struct trace_pid_list * pid_list,unsigned int pid)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  */
trace_pid_list_set(struct trace_pid_list * pid_list,unsigned int pid)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  */
trace_pid_list_clear(struct trace_pid_list * pid_list,unsigned int pid)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  */
trace_pid_list_next(struct trace_pid_list * pid_list,unsigned int pid,unsigned int * next)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  */
trace_pid_list_first(struct trace_pid_list * pid_list,unsigned int * pid)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  */
trace_pid_list_alloc(void)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  */
trace_pid_list_free(struct trace_pid_list * pid_list)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 }
161