Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

perf tools: Use scandir() to iterate threads when synthesizing PERF_RECORD_ events

Like in __event__synthesize_thread(), I think it's better to use
scandir() instead of the readdir() loop. In case some malicious task
continues to create new threads, the readdir() loop will run over and
over to collect tids. The scandir() also has the problem but the window
is much smaller since it doesn't do much work during the iteration.

Also add filter_task() function as we only care the tasks.

Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Acked-by: Jiri Olsa <jolsa@redhat.com>
Cc: Alexander Shishkin <alexander.shishkin@linux.intel.com>
Cc: Andi Kleen <ak@linux.intel.com>
Cc: Ian Rogers <irogers@google.com>
Cc: Ingo Molnar <mingo@kernel.org>
Cc: Mark Rutland <mark.rutland@arm.com>
Cc: Peter Zijlstra <peterz@infradead.org>
Cc: Stephane Eranian <eranian@google.com>
Link: https://lore.kernel.org/r/20210202090118.2008551-4-namhyung@kernel.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>

authored by

Namhyung Kim and committed by
Arnaldo Carvalho de Melo
473f742e c1b90795

+17 -11
+17 -11
tools/perf/util/synthetic-events.c
··· 704 704 return rc; 705 705 } 706 706 707 + static int filter_task(const struct dirent *dirent) 708 + { 709 + return isdigit(dirent->d_name[0]); 710 + } 711 + 707 712 static int __event__synthesize_thread(union perf_event *comm_event, 708 713 union perf_event *mmap_event, 709 714 union perf_event *fork_event, ··· 717 712 struct perf_tool *tool, struct machine *machine, bool mmap_data) 718 713 { 719 714 char filename[PATH_MAX]; 720 - DIR *tasks; 721 - struct dirent *dirent; 715 + struct dirent **dirent; 722 716 pid_t tgid, ppid; 723 717 int rc = 0; 718 + int i, n; 724 719 725 720 /* special case: only send one comm event using passed in pid */ 726 721 if (!full) { ··· 752 747 snprintf(filename, sizeof(filename), "%s/proc/%d/task", 753 748 machine->root_dir, pid); 754 749 755 - tasks = opendir(filename); 756 - if (tasks == NULL) { 757 - pr_debug("couldn't open %s\n", filename); 758 - return 0; 759 - } 750 + n = scandir(filename, &dirent, filter_task, alphasort); 751 + if (n < 0) 752 + return n; 760 753 761 - while ((dirent = readdir(tasks)) != NULL) { 754 + for (i = 0; i < n; i++) { 762 755 char *end; 763 756 pid_t _pid; 764 757 bool kernel_thread; 765 758 766 - _pid = strtol(dirent->d_name, &end, 10); 759 + _pid = strtol(dirent[i]->d_name, &end, 10); 767 760 if (*end) 768 761 continue; 769 762 ··· 794 791 } 795 792 } 796 793 797 - closedir(tasks); 794 + for (i = 0; i < n; i++) 795 + zfree(&dirent[i]); 796 + free(dirent); 797 + 798 798 return rc; 799 799 } 800 800 ··· 982 976 return 0; 983 977 984 978 snprintf(proc_path, sizeof(proc_path), "%s/proc", machine->root_dir); 985 - n = scandir(proc_path, &dirent, 0, alphasort); 979 + n = scandir(proc_path, &dirent, filter_task, alphasort); 986 980 if (n < 0) 987 981 return err; 988 982