]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
perf stat: Close cork_fd when create_perf_stat_counter() failed
authorLevi Yun <yeoreum.yun@arm.com>
Wed, 25 Sep 2024 13:20:21 +0000 (14:20 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Thu, 5 Dec 2024 12:53:46 +0000 (13:53 +0100)
[ Upstream commit e880a70f8046df0dd9089fa60dcb866a2cc69194 ]

When create_perf_stat_counter() failed, it doesn't close workload.cork_fd
open in evlist__prepare_workload(). This could make too many open file
error while __run_perf_stat() repeats.

Introduce evlist__cancel_workload to close workload.cork_fd and
wait workload.child_pid until exit to clear child process
when create_perf_stat_counter() is failed.

Signed-off-by: Levi Yun <yeoreum.yun@arm.com>
Reviewed-by: James Clark <james.clark@linaro.org>
Reviewed-by: Andi Kleen <ak@linux.intel.com>
Cc: nd@arm.com
Cc: howardchu95@gmail.com
Link: https://lore.kernel.org/r/20240925132022.2650180-2-yeoreum.yun@arm.com
Signed-off-by: Namhyung Kim <namhyung@kernel.org>
Stable-dep-of: 7f6ccb70e465 ("perf stat: Fix affinity memory leaks on error path")
Signed-off-by: Sasha Levin <sashal@kernel.org>
tools/perf/builtin-stat.c
tools/perf/util/evlist.c
tools/perf/util/evlist.h

index 661832756a248f5c0c0a394d20e6ccd24469258f..d0cbd1477dfcc76632985fd1ba4a04699dc484ab 100644 (file)
@@ -712,15 +712,19 @@ static int __run_perf_stat(int argc, const char **argv, int run_idx)
        }
 
        if (!cpu_map__is_dummy(evsel_list->core.user_requested_cpus)) {
-               if (affinity__setup(&saved_affinity) < 0)
-                       return -1;
+               if (affinity__setup(&saved_affinity) < 0) {
+                       err = -1;
+                       goto err_out;
+               }
                affinity = &saved_affinity;
        }
 
        evlist__for_each_entry(evsel_list, counter) {
                counter->reset_group = false;
-               if (bpf_counter__load(counter, &target))
-                       return -1;
+               if (bpf_counter__load(counter, &target)) {
+                       err = -1;
+                       goto err_out;
+               }
                if (!(evsel__is_bperf(counter)))
                        all_counters_use_bpf = false;
        }
@@ -763,7 +767,8 @@ try_again:
 
                        switch (stat_handle_error(counter)) {
                        case COUNTER_FATAL:
-                               return -1;
+                               err = -1;
+                               goto err_out;
                        case COUNTER_RETRY:
                                goto try_again;
                        case COUNTER_SKIP:
@@ -804,7 +809,8 @@ try_again_reset:
 
                                switch (stat_handle_error(counter)) {
                                case COUNTER_FATAL:
-                                       return -1;
+                                       err = -1;
+                                       goto err_out;
                                case COUNTER_RETRY:
                                        goto try_again_reset;
                                case COUNTER_SKIP:
@@ -829,8 +835,10 @@ try_again_reset:
                        stat_config.unit_width = l;
 
                if (evsel__should_store_id(counter) &&
-                   evsel__store_ids(counter, evsel_list))
-                       return -1;
+                   evsel__store_ids(counter, evsel_list)) {
+                       err = -1;
+                       goto err_out;
+               }
        }
 
        if (evlist__apply_filters(evsel_list, &counter)) {
@@ -851,20 +859,23 @@ try_again_reset:
                }
 
                if (err < 0)
-                       return err;
+                       goto err_out;
 
                err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list,
                                                         process_synthesized_event, is_pipe);
                if (err < 0)
-                       return err;
+                       goto err_out;
+
        }
 
        if (target.initial_delay) {
                pr_info(EVLIST_DISABLED_MSG);
        } else {
                err = enable_counters();
-               if (err)
-                       return -1;
+               if (err) {
+                       err = -1;
+                       goto err_out;
+               }
        }
 
        /* Exec the command, if any */
@@ -874,8 +885,10 @@ try_again_reset:
        if (target.initial_delay > 0) {
                usleep(target.initial_delay * USEC_PER_MSEC);
                err = enable_counters();
-               if (err)
-                       return -1;
+               if (err) {
+                       err = -1;
+                       goto err_out;
+               }
 
                pr_info(EVLIST_ENABLED_MSG);
        }
@@ -895,7 +908,8 @@ try_again_reset:
                if (workload_exec_errno) {
                        const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg));
                        pr_err("Workload failed: %s\n", emsg);
-                       return -1;
+                       err = -1;
+                       goto err_out;
                }
 
                if (WIFSIGNALED(status))
@@ -942,6 +956,12 @@ try_again_reset:
                evlist__close(evsel_list);
 
        return WEXITSTATUS(status);
+
+err_out:
+       if (forks)
+               evlist__cancel_workload(evsel_list);
+
+       return err;
 }
 
 static int run_perf_stat(int argc, const char **argv, int run_idx)
index 3a719edafc7ad263f0d5226184f47050de29a945..6c4227e9832facbb64d35b3cf0c9c4d02dbfbb0b 100644 (file)
@@ -46,6 +46,7 @@
 #include <sys/mman.h>
 #include <sys/prctl.h>
 #include <sys/timerfd.h>
+#include <sys/wait.h>
 
 #include <linux/bitops.h>
 #include <linux/hash.h>
@@ -1413,6 +1414,8 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
        int child_ready_pipe[2], go_pipe[2];
        char bf;
 
+       evlist->workload.cork_fd = -1;
+
        if (pipe(child_ready_pipe) < 0) {
                perror("failed to create 'ready' pipe");
                return -1;
@@ -1465,7 +1468,7 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target, const
                 * For cancelling the workload without actually running it,
                 * the parent will just close workload.cork_fd, without writing
                 * anything, i.e. read will return zero and we just exit()
-                * here.
+                * here (See evlist__cancel_workload()).
                 */
                if (ret != 1) {
                        if (ret == -1)
@@ -1529,7 +1532,7 @@ out_close_ready_pipe:
 
 int evlist__start_workload(struct evlist *evlist)
 {
-       if (evlist->workload.cork_fd > 0) {
+       if (evlist->workload.cork_fd >= 0) {
                char bf = 0;
                int ret;
                /*
@@ -1540,12 +1543,24 @@ int evlist__start_workload(struct evlist *evlist)
                        perror("unable to write to pipe");
 
                close(evlist->workload.cork_fd);
+               evlist->workload.cork_fd = -1;
                return ret;
        }
 
        return 0;
 }
 
+void evlist__cancel_workload(struct evlist *evlist)
+{
+       int status;
+
+       if (evlist->workload.cork_fd >= 0) {
+               close(evlist->workload.cork_fd);
+               evlist->workload.cork_fd = -1;
+               waitpid(evlist->workload.pid, &status, WNOHANG);
+       }
+}
+
 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
 {
        struct evsel *evsel = evlist__event2evsel(evlist, event);
index cb91dc9117a2726b34b9dce5265186c89eee96cd..12f929ffdf920d429ffbdc8ba785e9743c1777b3 100644 (file)
@@ -184,6 +184,7 @@ int evlist__prepare_workload(struct evlist *evlist, struct target *target,
                             const char *argv[], bool pipe_output,
                             void (*exec_error)(int signo, siginfo_t *info, void *ucontext));
 int evlist__start_workload(struct evlist *evlist);
+void evlist__cancel_workload(struct evlist *evlist);
 
 struct option;