Skip to content

Commit 98021e3

Browse files
committed
tracing: Move pid filtering into trace_pid.c
The trace.c file was a dumping ground for most tracing code. Start organizing it better by moving various functions out into their own files. Move the PID filtering functions from trace.c into its own trace_pid.c file. Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Mathieu Desnoyers <mathieu.desnoyers@efficios.com> Cc: Andrew Morton <akpm@linux-foundation.org> Link: https://patch.msgid.link/20260208032450.998330662@kernel.org Signed-off-by: Steven Rostedt (Google) <rostedt@goodmis.org>
1 parent 27931ee commit 98021e3

3 files changed

Lines changed: 247 additions & 242 deletions

File tree

kernel/trace/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ obj-$(CONFIG_TRACING) += trace_output.o
6868
obj-$(CONFIG_TRACING) += trace_seq.o
6969
obj-$(CONFIG_TRACING) += trace_stat.o
7070
obj-$(CONFIG_TRACING) += trace_printk.o
71+
obj-$(CONFIG_TRACING) += trace_pid.o
7172
obj-$(CONFIG_TRACING) += pid_list.o
7273
obj-$(CONFIG_TRACING_MAP) += tracing_map.o
7374
obj-$(CONFIG_PREEMPTIRQ_DELAY_TEST) += preemptirq_delay_test.o

kernel/trace/trace.c

Lines changed: 0 additions & 242 deletions
Original file line numberDiff line numberDiff line change
@@ -637,248 +637,6 @@ int tracing_check_open_get_tr(struct trace_array *tr)
637637
return 0;
638638
}
639639

640-
/**
641-
* trace_find_filtered_pid - check if a pid exists in a filtered_pid list
642-
* @filtered_pids: The list of pids to check
643-
* @search_pid: The PID to find in @filtered_pids
644-
*
645-
* Returns true if @search_pid is found in @filtered_pids, and false otherwise.
646-
*/
647-
bool
648-
trace_find_filtered_pid(struct trace_pid_list *filtered_pids, pid_t search_pid)
649-
{
650-
return trace_pid_list_is_set(filtered_pids, search_pid);
651-
}
652-
653-
/**
654-
* trace_ignore_this_task - should a task be ignored for tracing
655-
* @filtered_pids: The list of pids to check
656-
* @filtered_no_pids: The list of pids not to be traced
657-
* @task: The task that should be ignored if not filtered
658-
*
659-
* Checks if @task should be traced or not from @filtered_pids.
660-
* Returns true if @task should *NOT* be traced.
661-
* Returns false if @task should be traced.
662-
*/
663-
bool
664-
trace_ignore_this_task(struct trace_pid_list *filtered_pids,
665-
struct trace_pid_list *filtered_no_pids,
666-
struct task_struct *task)
667-
{
668-
/*
669-
* If filtered_no_pids is not empty, and the task's pid is listed
670-
* in filtered_no_pids, then return true.
671-
* Otherwise, if filtered_pids is empty, that means we can
672-
* trace all tasks. If it has content, then only trace pids
673-
* within filtered_pids.
674-
*/
675-
676-
return (filtered_pids &&
677-
!trace_find_filtered_pid(filtered_pids, task->pid)) ||
678-
(filtered_no_pids &&
679-
trace_find_filtered_pid(filtered_no_pids, task->pid));
680-
}
681-
682-
/**
683-
* trace_filter_add_remove_task - Add or remove a task from a pid_list
684-
* @pid_list: The list to modify
685-
* @self: The current task for fork or NULL for exit
686-
* @task: The task to add or remove
687-
*
688-
* If adding a task, if @self is defined, the task is only added if @self
689-
* is also included in @pid_list. This happens on fork and tasks should
690-
* only be added when the parent is listed. If @self is NULL, then the
691-
* @task pid will be removed from the list, which would happen on exit
692-
* of a task.
693-
*/
694-
void trace_filter_add_remove_task(struct trace_pid_list *pid_list,
695-
struct task_struct *self,
696-
struct task_struct *task)
697-
{
698-
if (!pid_list)
699-
return;
700-
701-
/* For forks, we only add if the forking task is listed */
702-
if (self) {
703-
if (!trace_find_filtered_pid(pid_list, self->pid))
704-
return;
705-
}
706-
707-
/* "self" is set for forks, and NULL for exits */
708-
if (self)
709-
trace_pid_list_set(pid_list, task->pid);
710-
else
711-
trace_pid_list_clear(pid_list, task->pid);
712-
}
713-
714-
/**
715-
* trace_pid_next - Used for seq_file to get to the next pid of a pid_list
716-
* @pid_list: The pid list to show
717-
* @v: The last pid that was shown (+1 the actual pid to let zero be displayed)
718-
* @pos: The position of the file
719-
*
720-
* This is used by the seq_file "next" operation to iterate the pids
721-
* listed in a trace_pid_list structure.
722-
*
723-
* Returns the pid+1 as we want to display pid of zero, but NULL would
724-
* stop the iteration.
725-
*/
726-
void *trace_pid_next(struct trace_pid_list *pid_list, void *v, loff_t *pos)
727-
{
728-
long pid = (unsigned long)v;
729-
unsigned int next;
730-
731-
(*pos)++;
732-
733-
/* pid already is +1 of the actual previous bit */
734-
if (trace_pid_list_next(pid_list, pid, &next) < 0)
735-
return NULL;
736-
737-
pid = next;
738-
739-
/* Return pid + 1 to allow zero to be represented */
740-
return (void *)(pid + 1);
741-
}
742-
743-
/**
744-
* trace_pid_start - Used for seq_file to start reading pid lists
745-
* @pid_list: The pid list to show
746-
* @pos: The position of the file
747-
*
748-
* This is used by seq_file "start" operation to start the iteration
749-
* of listing pids.
750-
*
751-
* Returns the pid+1 as we want to display pid of zero, but NULL would
752-
* stop the iteration.
753-
*/
754-
void *trace_pid_start(struct trace_pid_list *pid_list, loff_t *pos)
755-
{
756-
unsigned long pid;
757-
unsigned int first;
758-
loff_t l = 0;
759-
760-
if (trace_pid_list_first(pid_list, &first) < 0)
761-
return NULL;
762-
763-
pid = first;
764-
765-
/* Return pid + 1 so that zero can be the exit value */
766-
for (pid++; pid && l < *pos;
767-
pid = (unsigned long)trace_pid_next(pid_list, (void *)pid, &l))
768-
;
769-
return (void *)pid;
770-
}
771-
772-
/**
773-
* trace_pid_show - show the current pid in seq_file processing
774-
* @m: The seq_file structure to write into
775-
* @v: A void pointer of the pid (+1) value to display
776-
*
777-
* Can be directly used by seq_file operations to display the current
778-
* pid value.
779-
*/
780-
int trace_pid_show(struct seq_file *m, void *v)
781-
{
782-
unsigned long pid = (unsigned long)v - 1;
783-
784-
seq_printf(m, "%lu\n", pid);
785-
return 0;
786-
}
787-
788-
/* 128 should be much more than enough */
789-
#define PID_BUF_SIZE 127
790-
791-
int trace_pid_write(struct trace_pid_list *filtered_pids,
792-
struct trace_pid_list **new_pid_list,
793-
const char __user *ubuf, size_t cnt)
794-
{
795-
struct trace_pid_list *pid_list;
796-
struct trace_parser parser;
797-
unsigned long val;
798-
int nr_pids = 0;
799-
ssize_t read = 0;
800-
ssize_t ret;
801-
loff_t pos;
802-
pid_t pid;
803-
804-
if (trace_parser_get_init(&parser, PID_BUF_SIZE + 1))
805-
return -ENOMEM;
806-
807-
/*
808-
* Always recreate a new array. The write is an all or nothing
809-
* operation. Always create a new array when adding new pids by
810-
* the user. If the operation fails, then the current list is
811-
* not modified.
812-
*/
813-
pid_list = trace_pid_list_alloc();
814-
if (!pid_list) {
815-
trace_parser_put(&parser);
816-
return -ENOMEM;
817-
}
818-
819-
if (filtered_pids) {
820-
/* copy the current bits to the new max */
821-
ret = trace_pid_list_first(filtered_pids, &pid);
822-
while (!ret) {
823-
ret = trace_pid_list_set(pid_list, pid);
824-
if (ret < 0)
825-
goto out;
826-
827-
ret = trace_pid_list_next(filtered_pids, pid + 1, &pid);
828-
nr_pids++;
829-
}
830-
}
831-
832-
ret = 0;
833-
while (cnt > 0) {
834-
835-
pos = 0;
836-
837-
ret = trace_get_user(&parser, ubuf, cnt, &pos);
838-
if (ret < 0)
839-
break;
840-
841-
read += ret;
842-
ubuf += ret;
843-
cnt -= ret;
844-
845-
if (!trace_parser_loaded(&parser))
846-
break;
847-
848-
ret = -EINVAL;
849-
if (kstrtoul(parser.buffer, 0, &val))
850-
break;
851-
852-
pid = (pid_t)val;
853-
854-
if (trace_pid_list_set(pid_list, pid) < 0) {
855-
ret = -1;
856-
break;
857-
}
858-
nr_pids++;
859-
860-
trace_parser_clear(&parser);
861-
ret = 0;
862-
}
863-
out:
864-
trace_parser_put(&parser);
865-
866-
if (ret < 0) {
867-
trace_pid_list_free(pid_list);
868-
return ret;
869-
}
870-
871-
if (!nr_pids) {
872-
/* Cleared the list of pids */
873-
trace_pid_list_free(pid_list);
874-
pid_list = NULL;
875-
}
876-
877-
*new_pid_list = pid_list;
878-
879-
return read;
880-
}
881-
882640
static u64 buffer_ftrace_now(struct array_buffer *buf, int cpu)
883641
{
884642
u64 ts;

0 commit comments

Comments
 (0)