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

perf stat: Close cork_fd when create_perf_stat_counter() failed

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>

authored by

Levi Yun and committed by
Namhyung Kim
e880a70f f115506d

+53 -17
+35 -15
tools/perf/builtin-stat.c
··· 716 716 } 717 717 718 718 if (!cpu_map__is_dummy(evsel_list->core.user_requested_cpus)) { 719 - if (affinity__setup(&saved_affinity) < 0) 720 - return -1; 719 + if (affinity__setup(&saved_affinity) < 0) { 720 + err = -1; 721 + goto err_out; 722 + } 721 723 affinity = &saved_affinity; 722 724 } 723 725 724 726 evlist__for_each_entry(evsel_list, counter) { 725 727 counter->reset_group = false; 726 - if (bpf_counter__load(counter, &target)) 727 - return -1; 728 + if (bpf_counter__load(counter, &target)) { 729 + err = -1; 730 + goto err_out; 731 + } 728 732 if (!(evsel__is_bperf(counter))) 729 733 all_counters_use_bpf = false; 730 734 } ··· 771 767 772 768 switch (stat_handle_error(counter)) { 773 769 case COUNTER_FATAL: 774 - return -1; 770 + err = -1; 771 + goto err_out; 775 772 case COUNTER_RETRY: 776 773 goto try_again; 777 774 case COUNTER_SKIP: ··· 813 808 814 809 switch (stat_handle_error(counter)) { 815 810 case COUNTER_FATAL: 816 - return -1; 811 + err = -1; 812 + goto err_out; 817 813 case COUNTER_RETRY: 818 814 goto try_again_reset; 819 815 case COUNTER_SKIP: ··· 839 833 stat_config.unit_width = l; 840 834 841 835 if (evsel__should_store_id(counter) && 842 - evsel__store_ids(counter, evsel_list)) 843 - return -1; 836 + evsel__store_ids(counter, evsel_list)) { 837 + err = -1; 838 + goto err_out; 839 + } 844 840 } 845 841 846 842 if (evlist__apply_filters(evsel_list, &counter, &target)) { ··· 863 855 } 864 856 865 857 if (err < 0) 866 - return err; 858 + goto err_out; 867 859 868 860 err = perf_event__synthesize_stat_events(&stat_config, NULL, evsel_list, 869 861 process_synthesized_event, is_pipe); 870 862 if (err < 0) 871 - return err; 863 + goto err_out; 864 + 872 865 } 873 866 874 867 if (target.initial_delay) { 875 868 pr_info(EVLIST_DISABLED_MSG); 876 869 } else { 877 870 err = enable_counters(); 878 - if (err) 879 - return -1; 871 + if (err) { 872 + err = -1; 873 + goto err_out; 874 + } 880 875 } 881 876 882 877 /* Exec the command, if any */ ··· 889 878 if (target.initial_delay > 0) { 890 879 usleep(target.initial_delay * USEC_PER_MSEC); 891 880 err = enable_counters(); 892 - if (err) 893 - return -1; 881 + if (err) { 882 + err = -1; 883 + goto err_out; 884 + } 894 885 895 886 pr_info(EVLIST_ENABLED_MSG); 896 887 } ··· 912 899 if (workload_exec_errno) { 913 900 const char *emsg = str_error_r(workload_exec_errno, msg, sizeof(msg)); 914 901 pr_err("Workload failed: %s\n", emsg); 915 - return -1; 902 + err = -1; 903 + goto err_out; 916 904 } 917 905 918 906 if (WIFSIGNALED(status)) ··· 960 946 evlist__close(evsel_list); 961 947 962 948 return WEXITSTATUS(status); 949 + 950 + err_out: 951 + if (forks) 952 + evlist__cancel_workload(evsel_list); 953 + 954 + return err; 963 955 } 964 956 965 957 static int run_perf_stat(int argc, const char **argv, int run_idx)
+17 -2
tools/perf/util/evlist.c
··· 48 48 #include <sys/mman.h> 49 49 #include <sys/prctl.h> 50 50 #include <sys/timerfd.h> 51 + #include <sys/wait.h> 51 52 52 53 #include <linux/bitops.h> 53 54 #include <linux/hash.h> ··· 1485 1484 int child_ready_pipe[2], go_pipe[2]; 1486 1485 char bf; 1487 1486 1487 + evlist->workload.cork_fd = -1; 1488 + 1488 1489 if (pipe(child_ready_pipe) < 0) { 1489 1490 perror("failed to create 'ready' pipe"); 1490 1491 return -1; ··· 1539 1536 * For cancelling the workload without actually running it, 1540 1537 * the parent will just close workload.cork_fd, without writing 1541 1538 * anything, i.e. read will return zero and we just exit() 1542 - * here. 1539 + * here (See evlist__cancel_workload()). 1543 1540 */ 1544 1541 if (ret != 1) { 1545 1542 if (ret == -1) ··· 1603 1600 1604 1601 int evlist__start_workload(struct evlist *evlist) 1605 1602 { 1606 - if (evlist->workload.cork_fd > 0) { 1603 + if (evlist->workload.cork_fd >= 0) { 1607 1604 char bf = 0; 1608 1605 int ret; 1609 1606 /* ··· 1614 1611 perror("unable to write to pipe"); 1615 1612 1616 1613 close(evlist->workload.cork_fd); 1614 + evlist->workload.cork_fd = -1; 1617 1615 return ret; 1618 1616 } 1619 1617 1620 1618 return 0; 1619 + } 1620 + 1621 + void evlist__cancel_workload(struct evlist *evlist) 1622 + { 1623 + int status; 1624 + 1625 + if (evlist->workload.cork_fd >= 0) { 1626 + close(evlist->workload.cork_fd); 1627 + evlist->workload.cork_fd = -1; 1628 + waitpid(evlist->workload.pid, &status, WNOHANG); 1629 + } 1621 1630 } 1622 1631 1623 1632 int evlist__parse_sample(struct evlist *evlist, union perf_event *event, struct perf_sample *sample)
+1
tools/perf/util/evlist.h
··· 186 186 const char *argv[], bool pipe_output, 187 187 void (*exec_error)(int signo, siginfo_t *info, void *ucontext)); 188 188 int evlist__start_workload(struct evlist *evlist); 189 + void evlist__cancel_workload(struct evlist *evlist); 189 190 190 191 struct option; 191 192