1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2021 VMware Inc, Steven Rostedt <rostedt@goodmis.org>
5 #include <linux/vmalloc.h>
6 #include <linux/slab.h>
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.
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.
18 * Return true if the pid is in the list, false otherwise.
20 bool trace_pid_list_is_set(struct trace_pid_list *pid_list, unsigned int pid)
23 * If pid_max changed after filtered_pids was created, we
24 * by default ignore all pids greater than the previous pid_max.
26 if (pid >= pid_list->pid_max)
29 return test_bit(pid, pid_list->pids);
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.
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.
41 * Return 0 on success, negative otherwise.
43 int trace_pid_list_set(struct trace_pid_list *pid_list, unsigned int pid)
45 /* Sorry, but we don't support pid_max changing after setting */
46 if (pid >= pid_list->pid_max)
49 set_bit(pid, pid_list->pids);
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.
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.
63 * Return 0 on success, negative otherwise.
65 int trace_pid_list_clear(struct trace_pid_list *pid_list, unsigned int pid)
67 /* Sorry, but we don't support pid_max changing after setting */
68 if (pid >= pid_list->pid_max)
71 clear_bit(pid, pid_list->pids);
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.
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.
86 * Return 0 when a pid is found, -1 if there are no more pids included.
88 int trace_pid_list_next(struct trace_pid_list *pid_list, unsigned int pid,
91 pid = find_next_bit(pid_list->pids, pid_list->pid_max, pid);
93 if (pid < pid_list->pid_max) {
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.
105 * Looks for the first pid that is set in @pid_list, and places it
106 * into @pid if found.
108 * Return 0 when a pid is found, -1 if there are no pids set.
110 int trace_pid_list_first(struct trace_pid_list *pid_list, unsigned int *pid)
114 first = find_first_bit(pid_list->pids, pid_list->pid_max);
116 if (first < pid_list->pid_max) {
124 * trace_pid_list_alloc - create a new pid_list
126 * Allocates a new pid_list to store pids into.
128 * Returns the pid_list on success, NULL otherwise.
130 struct trace_pid_list *trace_pid_list_alloc(void)
132 struct trace_pid_list *pid_list;
134 pid_list = kmalloc(sizeof(*pid_list), GFP_KERNEL);
138 pid_list->pid_max = READ_ONCE(pid_max);
140 pid_list->pids = vzalloc((pid_list->pid_max + 7) >> 3);
141 if (!pid_list->pids) {
149 * trace_pid_list_free - Frees an allocated pid_list.
151 * Frees the memory for a pid_list that was allocated.
153 void trace_pid_list_free(struct trace_pid_list *pid_list)
158 vfree(pid_list->pids);