perf thread_map: Skip exited threads when scanning /proc
authorIan Rogers <irogers@google.com>
Wed, 21 Feb 2024 03:41:48 +0000 (19:41 -0800)
committerNamhyung Kim <namhyung@kernel.org>
Thu, 22 Feb 2024 17:11:03 +0000 (09:11 -0800)
Scanning /proc is inherently racy. Scanning /proc/pid/task within that
is also racy as the pid can terminate. Rather than failing in
__thread_map__new_all_cpus, skip pids for such failures.

Signed-off-by: Ian Rogers <irogers@google.com>
Cc: James Clark <james.clark@arm.com>
Cc: Justin Stitt <justinstitt@google.com>
Cc: Bill Wendling <morbo@google.com>
Cc: Nick Desaulniers <ndesaulniers@google.com>
Cc: Yang Jihong <yangjihong1@huawei.com>
Cc: Nathan Chancellor <nathan@kernel.org>
Cc: Kan Liang <kan.liang@linux.intel.com>
Cc: Athira Jajeev <atrajeev@linux.vnet.ibm.com>
Cc: llvm@lists.linux.dev
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Link: https://lore.kernel.org/r/20240221034155.1500118-2-irogers@google.com
tools/perf/util/thread_map.c

index ea3b431b978301edcbc35c867c05ad8ce6ff697e..b5f12390c3558016499b9dded1bf7d409c1738cc 100644 (file)
@@ -109,9 +109,10 @@ static struct perf_thread_map *__thread_map__new_all_cpus(uid_t uid)
 
                snprintf(path, sizeof(path), "/proc/%d/task", pid);
                items = scandir(path, &namelist, filter, NULL);
-               if (items <= 0)
-                       goto out_free_closedir;
-
+               if (items <= 0) {
+                       pr_debug("scandir for %d returned empty, skipping\n", pid);
+                       continue;
+               }
                while (threads->nr + items >= max_threads) {
                        max_threads *= 2;
                        grow = true;
@@ -152,8 +153,6 @@ out_free_namelist:
        for (i = 0; i < items; i++)
                zfree(&namelist[i]);
        free(namelist);
-
-out_free_closedir:
        zfree(&threads);
        goto out_closedir;
 }